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