1 /*
2 * Copyright (c) 2016 Katerina Koukiou <k.koukiou@gmail.com>
3 * Copyright (c) 2016-2018 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "tests.h"
30 #include <asm/unistd.h>
31
32 #ifdef __NR_openat
33
34 # include <asm/fcntl.h>
35 # include <stdio.h>
36 # include <unistd.h>
37
38 #ifdef O_TMPFILE
39 /* The kernel & C libraries often inline O_DIRECTORY. */
40 # define STRACE_O_TMPFILE (O_TMPFILE & ~O_DIRECTORY)
41 #else
42 # define STRACE_O_TMPFILE 0
43 #endif
44
45 static const char sample[] = "openat.sample";
46
47 static void
test_mode_flag(unsigned int mode_val,const char * mode_str,unsigned int flag_val,const char * flag_str)48 test_mode_flag(unsigned int mode_val, const char *mode_str,
49 unsigned int flag_val, const char *flag_str)
50 {
51 long rc = syscall(__NR_openat, -1, sample, mode_val | flag_val, 0);
52 printf("openat(-1, \"%s\", %s%s%s%s) = %s\n",
53 sample, mode_str,
54 flag_val ? "|" : "", flag_str,
55 flag_val & (O_CREAT | STRACE_O_TMPFILE) ? ", 000" : "",
56 sprintrc(rc));
57 }
58
59 int
main(void)60 main(void)
61 {
62 long fd = syscall(__NR_openat, -100, sample, O_RDONLY|O_CREAT, 0400);
63 printf("openat(AT_FDCWD, \"%s\", O_RDONLY|O_CREAT, 0400) = %s\n",
64 sample, sprintrc(fd));
65
66 if (fd != -1) {
67 close(fd);
68 if (unlink(sample) == -1)
69 perror_msg_and_fail("unlink");
70
71 fd = syscall(__NR_openat, -100, sample, O_RDONLY);
72 printf("openat(AT_FDCWD, \"%s\", O_RDONLY) = %s\n",
73 sample, sprintrc(fd));
74 }
75
76 struct {
77 unsigned int val;
78 const char *str;
79 } modes[] = {
80 { ARG_STR(O_RDONLY) },
81 { ARG_STR(O_WRONLY) },
82 { ARG_STR(O_RDWR) },
83 { ARG_STR(O_ACCMODE) }
84 }, flags[] = {
85 { ARG_STR(O_APPEND) },
86 { ARG_STR(O_DIRECT) },
87 { ARG_STR(O_DIRECTORY) },
88 { ARG_STR(O_EXCL) },
89 { ARG_STR(O_LARGEFILE) },
90 { ARG_STR(O_NOATIME) },
91 { ARG_STR(O_NOCTTY) },
92 { ARG_STR(O_NOFOLLOW) },
93 { ARG_STR(O_NONBLOCK) },
94 { ARG_STR(O_SYNC) },
95 { ARG_STR(O_TRUNC) },
96 { ARG_STR(O_CREAT) },
97 # ifdef O_CLOEXEC
98 { ARG_STR(O_CLOEXEC) },
99 # endif
100 # ifdef O_DSYNC
101 { ARG_STR(O_DSYNC) },
102 # endif
103 # ifdef __O_SYNC
104 { ARG_STR(__O_SYNC) },
105 # endif
106 # ifdef O_PATH
107 { ARG_STR(O_PATH) },
108 # endif
109 # ifdef O_TMPFILE
110 { ARG_STR(O_TMPFILE) },
111 # endif
112 # ifdef __O_TMPFILE
113 { ARG_STR(__O_TMPFILE) },
114 # endif
115 { ARG_STR(0x80000000) },
116 { 0, "" }
117 };
118
119 for (unsigned int m = 0; m < ARRAY_SIZE(modes); ++m)
120 for (unsigned int f = 0; f < ARRAY_SIZE(flags); ++f)
121 test_mode_flag(modes[m].val, modes[m].str,
122 flags[f].val, flags[f].str);
123
124 puts("+++ exited with 0 +++");
125 return 0;
126 }
127
128 #else
129
130 SKIP_MAIN_UNDEFINED("__NR_openat")
131
132 #endif
133