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 | |
d3a684ee 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 | { | |
d3a684ee | 59 | int ret; |
030312cf FD |
60 | char *start_file; |
61 | ||
62 | if (argc != 2) { | |
63 | fprintf(stderr, "Error: Missing argument\n"); | |
64 | fprintf(stderr, "USAGE: %s PATH_WAIT_FILE\n", argv[0]); | |
65 | ret = -1; | |
66 | goto error; | |
67 | } | |
68 | ||
69 | start_file = argv[1]; | |
70 | ||
71 | /* | |
72 | * Wait for the start_file to be created by an external process | |
73 | * (typically the test script) before executing the syscalls. | |
74 | */ | |
75 | ret = wait_on_file(start_file); | |
76 | if (ret != 0) { | |
77 | goto error; | |
78 | } | |
79 | ||
80 | /* | |
81 | * Start generating syscalls. We use syscall(2) to prevent libc to change | |
82 | * the underlying syscall. e.g. calling openat(2) instead of open(2). | |
83 | */ | |
d3a684ee JR |
84 | ret = open_read_close("/proc/cpuinfo"); |
85 | if (ret == -1) { | |
030312cf FD |
86 | ret = -1; |
87 | goto error; | |
88 | } | |
89 | ||
d3a684ee | 90 | ret = open_read_close("/proc/cmdline"); |
030312cf | 91 | if (ret == -1) { |
030312cf FD |
92 | ret = -1; |
93 | goto error; | |
94 | } | |
95 | ||
96 | error: | |
97 | return ret; | |
98 | } |