1 /*
2 * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
3 * Copyright (c) 2016-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include "scno.h"
11
12 #ifdef __NR_open
13
14 # include <asm/fcntl.h>
15 # include <stdio.h>
16 # include <unistd.h>
17
18 # include "secontext.h"
19
20 int
21 main(void)
22 {
23 /*
24 * Make sure the current workdir of the tracee
25 * is different from the current workdir of the tracer.
26 */
27 create_and_enter_subdir("open_subdir");
28
29 static const char sample[] = "open.sample";
30 char *my_secontext = SECONTEXT_PID_MY();
31
32 long fd = syscall(__NR_open, sample, O_RDONLY|O_CREAT, 0400);
33 printf("%s%s(\"%s\", O_RDONLY|O_CREAT, 0400) = %s%s\n",
34 my_secontext, "open",
35 sample, sprintrc(fd), SECONTEXT_FILE(sample));
36
37 if (fd != -1) {
38 close(fd);
39 if (unlink(sample))
40 perror_msg_and_fail("unlink");
41
42 fd = syscall(__NR_open, sample, O_RDONLY);
43 printf("%s%s(\"%s\", O_RDONLY) = %s\n",
44 my_secontext, "open", sample, sprintrc(fd));
45
46 fd = syscall(__NR_open, sample, O_WRONLY|O_NONBLOCK|0x80000000);
47 printf("%s%s(\"%s\", O_WRONLY|O_NONBLOCK|0x80000000) = %s\n",
48 my_secontext, "open", sample, sprintrc(fd));
49 }
50
51 # ifdef O_TMPFILE
52 fd = syscall(__NR_open, sample, O_WRONLY|O_TMPFILE, 0600);
53 printf("%s%s(\"%s\", O_WRONLY|O_TMPFILE, 0600) = %s\n",
54 my_secontext, "open",
55 sample, sprintrc(fd));
56 # endif /* O_TMPFILE */
57
58 leave_and_remove_subdir();
59
60 puts("+++ exited with 0 +++");
61 return 0;
62 }
63
64 #else
65
66 SKIP_MAIN_UNDEFINED("__NR_open")
67
68 #endif