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> | |
12 | ||
13 | #include "utils.h" | |
14 | ||
15 | #define MAX_LEN 16 | |
5024c2ac JR |
16 | |
17 | static | |
18 | int open_read_close(const char *path) | |
19 | { | |
20 | int fd, ret; | |
21 | char buf[MAX_LEN]; | |
22 | /* | |
23 | * Start generating syscalls. We use syscall(2) to prevent libc to change | |
24 | * the underlying syscall. e.g. calling openat(2) instead of open(2). | |
25 | */ | |
26 | fd = syscall(SYS_openat, AT_FDCWD, path, O_RDONLY); | |
27 | if (fd < 0) { | |
28 | perror("open"); | |
29 | ret = -1; | |
30 | goto error; | |
31 | } | |
32 | ||
33 | ret = syscall(SYS_read, fd, buf, MAX_LEN); | |
34 | if (ret < 0) { | |
35 | perror("read"); | |
36 | ret = -1; | |
37 | goto error; | |
38 | } | |
39 | ||
40 | ret = syscall(SYS_close, fd); | |
41 | if (ret == -1) { | |
42 | perror("close"); | |
43 | ret = -1; | |
44 | goto error; | |
45 | } | |
46 | ||
47 | error: | |
48 | return ret; | |
49 | } | |
50 | ||
030312cf FD |
51 | /* |
52 | * The process waits for the creation of a file passed as argument from an | |
53 | * external processes to execute a syscall and exiting. This is useful for tests | |
54 | * in combinaison with LTTng's PID tracker feature where we can trace the kernel | |
55 | * events generated by our test process only. | |
56 | */ | |
57 | int main(int argc, char **argv) | |
58 | { | |
5024c2ac JR |
59 | int ret; |
60 | const char *start_file, *path1, *path2; | |
030312cf | 61 | |
5024c2ac | 62 | if (argc != 4) { |
030312cf | 63 | fprintf(stderr, "Error: Missing argument\n"); |
5024c2ac | 64 | fprintf(stderr, "USAGE: %s PATH_WAIT_FILE PATH1_TO_OPEN PATH2_TO_OPEN\n", argv[0]); |
030312cf FD |
65 | fprintf(stderr, "USAGE: %s PATH_WAIT_FILE\n", argv[0]); |
66 | ret = -1; | |
67 | goto error; | |
68 | } | |
69 | ||
70 | start_file = argv[1]; | |
5024c2ac JR |
71 | path1 = argv[2]; |
72 | path2 = argv[3]; | |
030312cf FD |
73 | |
74 | /* | |
75 | * Wait for the start_file to be created by an external process | |
76 | * (typically the test script) before executing the syscalls. | |
77 | */ | |
78 | ret = wait_on_file(start_file); | |
79 | if (ret != 0) { | |
80 | goto error; | |
81 | } | |
82 | ||
83 | /* | |
84 | * Start generating syscalls. We use syscall(2) to prevent libc to change | |
85 | * the underlying syscall. e.g. calling openat(2) instead of open(2). | |
86 | */ | |
5024c2ac JR |
87 | ret = open_read_close(path1); |
88 | if (ret == -1) { | |
030312cf FD |
89 | ret = -1; |
90 | goto error; | |
91 | } | |
92 | ||
5024c2ac | 93 | ret = open_read_close(path2); |
030312cf | 94 | if (ret == -1) { |
030312cf FD |
95 | ret = -1; |
96 | goto error; | |
97 | } | |
98 | ||
99 | error: | |
100 | return ret; | |
101 | } |