Commit | Line | Data |
---|---|---|
030312cf | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com> |
030312cf | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
030312cf | 5 | * |
030312cf FD |
6 | */ |
7 | ||
030312cf FD |
8 | #include <fcntl.h> |
9 | #include <stdio.h> | |
10 | #include <sys/syscall.h> | |
11 | #include <unistd.h> | |
d23253ae FD |
12 | #include <common/error.h> |
13 | #include <common/align.h> | |
030312cf FD |
14 | |
15 | #include "utils.h" | |
16 | ||
17 | #define MAX_LEN 16 | |
d23253ae FD |
18 | |
19 | /* | |
20 | * The LTTng system call tracing facilities can't handle page faults at the | |
21 | * moment. If a fault would occur while reading a syscall argument, the | |
22 | * tracer will report an empty string (""). Since the proper execution of the | |
23 | * tests which use this generator depends on some syscall string arguments being | |
24 | * present, this util allows us to mitigate the page-fault risk. | |
25 | * | |
26 | * This isn't a proper fix; it is simply the best we can do for now. | |
27 | * See bug #1261 for more context. | |
28 | */ | |
29 | static | |
30 | void prefault_string(const char *p) | |
31 | { | |
32 | const char * const end = p + strlen(p) + 1; | |
33 | ||
34 | while (p < end) { | |
35 | /* | |
36 | * Trigger a read attempt on *p, faulting-in the pages | |
37 | * for reading. | |
38 | */ | |
39 | asm volatile("" : : "m"(*p)); | |
40 | p += PAGE_SIZE; | |
41 | } | |
42 | } | |
43 | ||
44 | static | |
45 | int open_read_close(const char *path) | |
46 | { | |
47 | int fd, ret; | |
48 | char buf[MAX_LEN]; | |
49 | ||
50 | /* | |
51 | * Start generating syscalls. We use syscall(2) to prevent libc from | |
52 | * changing the underlying syscall (e.g. calling openat(2) instead of | |
53 | * open(2)). | |
54 | */ | |
55 | prefault_string(path); | |
56 | fd = syscall(SYS_openat, AT_FDCWD, path, O_RDONLY); | |
57 | if (fd < 0) { | |
58 | PERROR_NO_LOGGER("Failed to open file with openat(): path = '%s'", path); | |
59 | ret = -1; | |
60 | goto error; | |
61 | } | |
62 | ||
63 | ret = syscall(SYS_read, fd, buf, MAX_LEN); | |
64 | if (ret < 0) { | |
65 | PERROR_NO_LOGGER("Failed to read file: path = '%s', fd = %d, length = %d", | |
66 | path, fd, MAX_LEN); | |
67 | ret = -1; | |
68 | goto error; | |
69 | } | |
70 | ||
71 | ret = syscall(SYS_close, fd); | |
72 | if (ret == -1) { | |
73 | PERROR_NO_LOGGER("Failed to close file: path = '%s', fd = %d", path, fd); | |
74 | ret = -1; | |
75 | goto error; | |
76 | } | |
77 | ||
78 | error: | |
79 | return ret; | |
80 | } | |
81 | ||
030312cf FD |
82 | /* |
83 | * The process waits for the creation of a file passed as argument from an | |
84 | * external processes to execute a syscall and exiting. This is useful for tests | |
85 | * in combinaison with LTTng's PID tracker feature where we can trace the kernel | |
86 | * events generated by our test process only. | |
87 | */ | |
88 | int main(int argc, char **argv) | |
89 | { | |
d23253ae | 90 | int ret; |
ca342eaf | 91 | const char *start_file, *path1, *path2; |
030312cf | 92 | |
ca342eaf | 93 | if (argc != 4) { |
030312cf | 94 | fprintf(stderr, "Error: Missing argument\n"); |
ca342eaf | 95 | fprintf(stderr, "USAGE: %s PATH_WAIT_FILE PATH1_TO_OPEN PATH2_TO_OPEN\n", argv[0]); |
030312cf FD |
96 | ret = -1; |
97 | goto error; | |
98 | } | |
99 | ||
100 | start_file = argv[1]; | |
ca342eaf JR |
101 | path1 = argv[2]; |
102 | path2 = argv[3]; | |
030312cf FD |
103 | |
104 | /* | |
105 | * Wait for the start_file to be created by an external process | |
106 | * (typically the test script) before executing the syscalls. | |
107 | */ | |
108 | ret = wait_on_file(start_file); | |
109 | if (ret != 0) { | |
110 | goto error; | |
111 | } | |
112 | ||
113 | /* | |
114 | * Start generating syscalls. We use syscall(2) to prevent libc to change | |
115 | * the underlying syscall. e.g. calling openat(2) instead of open(2). | |
116 | */ | |
ca342eaf | 117 | ret = open_read_close(path1); |
d23253ae | 118 | if (ret == -1) { |
030312cf FD |
119 | ret = -1; |
120 | goto error; | |
121 | } | |
122 | ||
ca342eaf | 123 | ret = open_read_close(path2); |
030312cf | 124 | if (ret == -1) { |
030312cf FD |
125 | ret = -1; |
126 | goto error; | |
127 | } | |
128 | ||
129 | error: | |
130 | return ret; | |
131 | } |