SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / tests / utils / testapp / gen-syscall-events / gen-syscall-events.c
1 /*
2 * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
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
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
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 {
59 int ret;
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 */
84 ret = open_read_close("/proc/cpuinfo");
85 if (ret == -1) {
86 ret = -1;
87 goto error;
88 }
89
90 ret = open_read_close("/proc/cmdline");
91 if (ret == -1) {
92 ret = -1;
93 goto error;
94 }
95
96 error:
97 return ret;
98 }
This page took 0.031408 seconds and 5 git commands to generate.