Tests: cleanly exit from test apps on reception of SIGTERM
[lttng-tools.git] / tests / utils / testapp / gen-ust-tracef / gen-ust-tracef.c
1 /*
2 * Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) - 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by the
7 * Free Software Foundation; version 2.1 of the License.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include <lttng/tracef.h>
32 #include "signal-helper.h"
33
34 const char *str = "test string";
35
36 void create_file(const char *path)
37 {
38 int ret;
39
40 assert(path);
41
42 ret = creat(path, S_IRWXU);
43 if (ret < 0) {
44 fprintf(stderr, "Failed to create file %s\n", path);
45 return;
46 }
47
48 (void) close(ret);
49 }
50
51 int main(int argc, char **argv)
52 {
53 int i;
54 unsigned int nr_iter = 100;
55 useconds_t nr_usec = 0;
56 char *tmp_file_path = NULL;
57
58 if (set_signal_handler()) {
59 return 1;
60 }
61
62 if (argc >= 2) {
63 nr_iter = atoi(argv[1]);
64 }
65
66 if (argc >= 3) {
67 /* By default, don't wait unless user specifies. */
68 nr_usec = atoi(argv[2]);
69 }
70
71 if (argc >= 4) {
72 tmp_file_path = argv[3];
73 }
74
75 for (i = 0; i < nr_iter; i++) {
76 tracef("Test message %d with string \"%s\"", i, str);
77
78 /*
79 * First loop we create the file if asked to indicate
80 * that at least one tracepoint has been hit.
81 */
82 if (i == 0 && tmp_file_path) {
83 create_file(tmp_file_path);
84 }
85 usleep(nr_usec);
86 if (should_quit) {
87 break;
88 }
89 }
90
91 return 0;
92 }
This page took 0.031487 seconds and 5 git commands to generate.