• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check printing of character/block device numbers in -yy mode.
3  *
4  * Copyright (c) 2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "tests.h"
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 #include <asm/unistd.h>
36 
37 #include <linux/fcntl.h>
38 
39 #include <sys/sysmacros.h>
40 
41 #if defined __NR_openat && defined O_PATH
42 
43 int
main(void)44 main(void)
45 {
46 	static const struct {
47 		const char *path;
48 		unsigned int major;
49 		unsigned int minor;
50 		bool blk;
51 		bool optional;
52 	} checks[] = {
53 		{ "/dev/zero", 1, 5, false, false },
54 		{ "/dev/full", 1, 7, false, false },
55 		{ "/dev/sda",  8, 0, true,  true  },
56 	};
57 
58 	for (unsigned int i = 0; i < ARRAY_SIZE(checks); i++) {
59 		/*
60 		 * We can't have nice things here and call openat() directly as
61 		 * some libcs (yes, musl, I'm looking at you now) are too
62 		 * frivolous in passing flags to the kernel.
63 		 */
64 		long fd = syscall(__NR_openat, AT_FDCWD, checks[i].path,
65 				  O_RDONLY|O_PATH);
66 
67 		printf("openat(AT_FDCWD, \"%s\", O_RDONLY|O_PATH) = %s",
68 		       checks[i].path, sprintrc(fd));
69 		if (fd >= 0)
70 			printf("<%s<%s %u:%u>>",
71 			       checks[i].path,
72 			       checks[i].blk ? "block" : "char",
73 			       checks[i].major, checks[i].minor);
74 		puts("");
75 
76 		if (fd < 0) {
77 			if (checks[i].optional)
78 				continue;
79 			else
80 				perror_msg_and_fail("openat(\"%s\")",
81 						    checks[i].path);
82 		}
83 
84 		int rc = fsync(fd);
85 
86 		printf("fsync(%ld<%s<%s %u:%u>>) = %s\n",
87 		       fd, checks[i].path, checks[i].blk ? "block" : "char",
88 		       checks[i].major, checks[i].minor, sprintrc(rc));
89 
90 		close(fd);
91 	}
92 
93 	puts("+++ exited with 0 +++");
94 	return 0;
95 }
96 
97 #else
98 
99 SKIP_MAIN_UNDEFINED("__NR_openat && O_PATH")
100 
101 #endif
102