Commit | Line | Data |
---|---|---|
f0966704 MD |
1 | /* |
2 | * ust-multi-test.c - single-proces, multi-session, multi-channel, multi-event UST tracing | |
3 | * | |
4 | * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
6 | * | |
e92f3e28 MD |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; version 2 of the License only. | |
f0966704 | 10 | * |
e92f3e28 MD |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
f0966704 | 15 | * |
e92f3e28 MD |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
f0966704 MD |
19 | */ |
20 | ||
21 | #define _LARGEFILE64_SOURCE | |
9d335227 | 22 | #define _GNU_SOURCE |
f0966704 MD |
23 | #include <errno.h> |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <signal.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/wait.h> | |
30 | #include <sys/types.h> | |
07dd0d60 MD |
31 | #include <sys/time.h> |
32 | #include <sys/resource.h> | |
f0966704 MD |
33 | #include <sys/stat.h> |
34 | #include <fcntl.h> | |
35 | #include <sys/mman.h> | |
36 | #include <limits.h> | |
37 | #include <urcu/futex.h> | |
38 | #include <urcu/uatomic.h> | |
39 | #include <assert.h> | |
40 | #include <sys/socket.h> | |
41 | ||
a3bb4b27 | 42 | #include <ust-comm.h> |
1a34b35b | 43 | #include <lttng/ust-error.h> |
f0966704 MD |
44 | #include <../../libringbuffer/backend.h> |
45 | #include <../../libringbuffer/frontend.h> | |
bdcf8d82 | 46 | #include "../../liblttng-ust/compat.h" /* For ENODATA */ |
f0966704 MD |
47 | |
48 | #define NR_SESSIONS 4 | |
49 | #define NR_CHANNELS 1 | |
50 | #define MAX_NR_STREAMS 64 | |
51 | #define NR_EVENTS 3 | |
52 | ||
53 | const char *evname[] = { | |
54 | "ust_tests_hello_tptest", | |
55 | "ust_tests_hello_tptest_sighandler", | |
56 | "ust_tests_hello_dontexist", | |
57 | }; | |
58 | ||
f0966704 | 59 | static int session_handle[NR_SESSIONS]; |
61f02aea MD |
60 | static struct lttng_ust_object_data metadata_stream_data[NR_SESSIONS]; |
61 | static struct lttng_ust_object_data metadata_data[NR_SESSIONS]; | |
62 | static struct lttng_ust_object_data channel_data[NR_SESSIONS][NR_CHANNELS]; | |
63 | static struct lttng_ust_object_data stream_data[NR_SESSIONS][NR_CHANNELS][MAX_NR_STREAMS]; | |
f0966704 MD |
64 | static int event_handle[NR_SESSIONS][NR_CHANNELS][NR_EVENTS]; |
65 | ||
66 | static int apps_socket = -1; | |
67 | static char apps_sock_path[PATH_MAX]; | |
68 | static char local_apps_wait_shm_path[PATH_MAX]; | |
69 | ||
70 | static volatile int quit_program; | |
71 | ||
72 | static void handle_signals(int signo) | |
73 | { | |
74 | quit_program = 1; | |
75 | } | |
76 | ||
f0966704 | 77 | static |
61f02aea | 78 | int open_streams(int sock, int channel_handle, struct lttng_ust_object_data *stream_datas, |
f0966704 MD |
79 | int nr_check) |
80 | { | |
81 | int ret, k = 0; | |
82 | ||
83 | for (;;) { | |
57773204 MD |
84 | struct ustcomm_ust_msg lum; |
85 | struct ustcomm_ust_reply lur; | |
f0966704 MD |
86 | |
87 | memset(&lum, 0, sizeof(lum)); | |
88 | lum.handle = channel_handle; | |
89 | lum.cmd = LTTNG_UST_STREAM; | |
57773204 | 90 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
91 | if (!ret) { |
92 | assert(k < nr_check); | |
93 | stream_datas[k].handle = lur.ret_val; | |
94 | printf("received stream handle %u\n", | |
95 | stream_datas[k].handle); | |
7bc53e94 | 96 | if (lur.ret_code == LTTNG_UST_OK) { |
f0966704 MD |
97 | ssize_t len; |
98 | ||
99 | stream_datas[k].memory_map_size = lur.u.stream.memory_map_size; | |
100 | /* get shm fd */ | |
57773204 | 101 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
102 | if (len < 0) |
103 | return -EINVAL; | |
104 | stream_datas[k].shm_fd = len; | |
105 | /* get wait fd */ | |
57773204 | 106 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
107 | if (len < 0) |
108 | return -EINVAL; | |
109 | stream_datas[k].wait_fd = len; | |
110 | } | |
111 | k++; | |
112 | } | |
1a34b35b | 113 | if (ret == -LTTNG_UST_ERR_NOENT) |
f0966704 MD |
114 | break; |
115 | if (ret) | |
116 | return ret; | |
117 | } | |
118 | return 0; | |
119 | } | |
120 | ||
121 | static | |
61f02aea | 122 | int close_streams(int sock, struct lttng_ust_object_data *stream_datas, int nr_check) |
f0966704 MD |
123 | { |
124 | int ret, k; | |
125 | ||
126 | for (k = 0; k < nr_check; k++) { | |
57773204 MD |
127 | struct ustcomm_ust_msg lum; |
128 | struct ustcomm_ust_reply lur; | |
f0966704 MD |
129 | |
130 | if (!stream_datas[k].handle) | |
131 | continue; | |
132 | memset(&lum, 0, sizeof(lum)); | |
133 | lum.handle = stream_datas[k].handle; | |
134 | lum.cmd = LTTNG_UST_RELEASE; | |
57773204 | 135 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
136 | if (ret) { |
137 | printf("Error closing stream\n"); | |
138 | return ret; | |
139 | } | |
140 | if (stream_datas[k].shm_fd >= 0) { | |
141 | ret = close(stream_datas[k].shm_fd); | |
142 | if (ret) { | |
143 | printf("Error closing stream shm_fd\n"); | |
144 | return ret; | |
145 | } | |
146 | } | |
147 | if (stream_datas[k].wait_fd >= 0) { | |
148 | ret = close(stream_datas[k].wait_fd); | |
149 | if (ret) { | |
150 | printf("Error closing stream wait_fd\n"); | |
151 | return ret; | |
152 | } | |
153 | } | |
154 | } | |
155 | return 0; | |
156 | } | |
157 | ||
158 | static | |
38fae1d3 | 159 | struct lttng_ust_shm_handle *map_channel(struct lttng_ust_object_data *chan_data, |
61f02aea | 160 | struct lttng_ust_object_data *stream_datas, int nr_check) |
f0966704 | 161 | { |
38fae1d3 | 162 | struct lttng_ust_shm_handle *handle; |
f0966704 MD |
163 | struct channel *chan; |
164 | int k, ret; | |
165 | ||
166 | /* map metadata channel */ | |
167 | handle = channel_handle_create(chan_data->shm_fd, | |
168 | chan_data->wait_fd, | |
169 | chan_data->memory_map_size); | |
170 | if (!handle) { | |
171 | printf("create handle error\n"); | |
172 | return NULL; | |
173 | } | |
174 | chan_data->shm_fd = -1; | |
175 | chan_data->wait_fd = -1; | |
176 | chan = shmp(handle, handle->chan); | |
177 | ||
178 | for (k = 0; k < nr_check; k++) { | |
61f02aea | 179 | struct lttng_ust_object_data *stream_data = &stream_datas[k]; |
f0966704 MD |
180 | |
181 | if (!stream_data->handle) | |
182 | break; | |
183 | /* map stream */ | |
184 | ret = channel_handle_add_stream(handle, | |
185 | stream_data->shm_fd, | |
186 | stream_data->wait_fd, | |
187 | stream_data->memory_map_size); | |
188 | if (ret) { | |
189 | printf("add stream error\n"); | |
190 | goto error_destroy; | |
191 | } | |
192 | stream_data->shm_fd = -1; | |
193 | stream_data->wait_fd = -1; | |
194 | } | |
195 | return handle; | |
196 | ||
197 | error_destroy: | |
198 | channel_destroy(chan, handle, 1); | |
199 | return NULL; | |
200 | } | |
201 | ||
202 | static | |
38fae1d3 | 203 | void unmap_channel(struct lttng_ust_shm_handle *handle) |
f0966704 MD |
204 | { |
205 | struct channel *chan; | |
206 | ||
207 | chan = shmp(handle, handle->chan); | |
208 | /* unmap channel */ | |
209 | channel_destroy(chan, handle, 1); | |
210 | } | |
211 | ||
212 | static | |
38fae1d3 | 213 | int consume_stream(struct lttng_ust_shm_handle *handle, int cpu, char *outfile) |
f0966704 MD |
214 | { |
215 | struct channel *chan; | |
4cfec15c | 216 | struct lttng_ust_lib_ring_buffer *buf; |
f0966704 | 217 | int outfd, ret; |
ef9ff354 MD |
218 | int *shm_fd, *wait_fd; |
219 | uint64_t *memory_map_size; | |
f0966704 MD |
220 | |
221 | chan = shmp(handle, handle->chan); | |
222 | ||
223 | /* open stream */ | |
224 | buf = channel_get_ring_buffer(&chan->backend.config, | |
225 | chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size); | |
226 | if (!buf) | |
227 | return -ENOENT; | |
228 | ret = lib_ring_buffer_open_read(buf, handle, 1); | |
229 | if (ret) { | |
230 | return -1; | |
231 | } | |
232 | ||
233 | /* copy */ | |
5cd19802 | 234 | outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, |
f0966704 MD |
235 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
236 | if (outfd < 0) { | |
237 | perror("open output"); | |
238 | return -1; | |
239 | } | |
240 | ||
241 | printf("Waiting for buffer data for %s\n", outfile); | |
242 | for (;;) { | |
243 | unsigned long read_size; | |
244 | unsigned long copy_size; | |
245 | char *ptr; | |
246 | ||
247 | ret = lib_ring_buffer_get_next_subbuf(buf, handle); | |
248 | printf("get next ret %d\n", ret); | |
249 | if (ret == -ENODATA) | |
250 | break; | |
251 | if (ret == -EAGAIN) { | |
252 | sleep(1); | |
253 | continue; | |
254 | } | |
255 | if (ret) { | |
256 | printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret); | |
257 | return -1; | |
258 | } | |
259 | read_size = lib_ring_buffer_get_read_data_size( | |
260 | &chan->backend.config, buf, handle); | |
261 | read_size = PAGE_ALIGN(read_size); | |
262 | ptr = lib_ring_buffer_read_offset_address( | |
263 | &buf->backend, 0, handle); | |
264 | printf("WRITE: copy %lu bytes\n", read_size); | |
265 | copy_size = write(outfd, ptr, read_size); | |
266 | if (copy_size < read_size) { | |
361dedfb | 267 | printf("write issue: copied %lu, expected %lu\n", copy_size, read_size); |
f0966704 MD |
268 | } |
269 | lib_ring_buffer_put_next_subbuf(buf, handle); | |
270 | } | |
271 | ||
272 | ret = close(outfd); | |
273 | if (ret) { | |
274 | perror("close"); | |
275 | return -1; | |
276 | } | |
277 | ||
278 | /* close stream */ | |
279 | lib_ring_buffer_release_read(buf, handle, 1); | |
280 | return 0; | |
281 | } | |
282 | ||
283 | static | |
284 | int consume_buffers(void) | |
285 | { | |
286 | int i, j, k, ret; | |
287 | mode_t old_umask; | |
288 | ||
289 | for (i = 0; i < NR_SESSIONS; i++) { | |
290 | char pathname[PATH_MAX]; | |
38fae1d3 | 291 | struct lttng_ust_shm_handle *handle; |
f0966704 MD |
292 | |
293 | snprintf(pathname, PATH_MAX - 1, "/tmp/testtrace%u", i); | |
294 | old_umask = umask(0); | |
295 | ret = mkdir(pathname, S_IRWXU | S_IRWXG); | |
296 | if (ret && errno != EEXIST) { | |
297 | perror("mkdir"); | |
298 | umask(old_umask); | |
299 | return -1; | |
300 | } | |
301 | umask(old_umask); | |
302 | ||
303 | /* copy metadata */ | |
304 | handle = map_channel(&metadata_data[i], | |
305 | &metadata_stream_data[i], 1); | |
306 | if (!handle) | |
307 | return -1; | |
308 | snprintf(pathname, PATH_MAX - 1, | |
309 | "/tmp/testtrace%u/metadata", i); | |
310 | ret = consume_stream(handle, -1, pathname); | |
311 | if (ret && ret != -ENOENT) { | |
312 | printf("Error in consume_stream\n"); | |
313 | return ret; | |
314 | } | |
315 | unmap_channel(handle); | |
316 | ||
317 | /* copy binary data */ | |
318 | for (j = 0; j < NR_CHANNELS; j++) { | |
319 | handle = map_channel(&channel_data[i][j], | |
320 | stream_data[i][j], MAX_NR_STREAMS); | |
321 | if (!handle) | |
322 | return -1; | |
323 | for (k = 0; k < MAX_NR_STREAMS; k++) { | |
324 | snprintf(pathname, PATH_MAX - 1, | |
325 | "/tmp/testtrace%u/data_%u", i, k); | |
326 | ret = consume_stream(handle, k, pathname); | |
327 | if (ret && ret != -ENOENT) { | |
328 | printf("Error in consume_stream\n"); | |
329 | return ret; | |
330 | } | |
331 | } | |
332 | unmap_channel(handle); | |
333 | } | |
334 | } | |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
339 | int send_app_msgs(int sock) | |
340 | { | |
57773204 MD |
341 | struct ustcomm_ust_msg lum; |
342 | struct ustcomm_ust_reply lur; | |
f0966704 MD |
343 | int ret, i, j, k; |
344 | ||
345 | for (i = 0; i < NR_SESSIONS; i++) { | |
346 | /* Create session */ | |
347 | memset(&lum, 0, sizeof(lum)); | |
348 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
349 | lum.cmd = LTTNG_UST_SESSION; | |
57773204 | 350 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
351 | if (ret) |
352 | return ret; | |
353 | session_handle[i] = lur.ret_val; | |
354 | printf("received session handle %u\n", session_handle[i]); | |
355 | ||
356 | /* Create metadata channel */ | |
357 | memset(&lum, 0, sizeof(lum)); | |
358 | lum.handle = session_handle[i]; | |
359 | lum.cmd = LTTNG_UST_METADATA; | |
360 | lum.u.channel.overwrite = 0; | |
361 | lum.u.channel.subbuf_size = 32768; | |
362 | lum.u.channel.num_subbuf = 4; | |
363 | lum.u.channel.switch_timer_interval = 0; | |
364 | lum.u.channel.read_timer_interval = 0; | |
365 | lum.u.channel.output = LTTNG_UST_MMAP; | |
57773204 | 366 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
367 | if (ret) |
368 | return ret; | |
369 | metadata_data[i].handle = lur.ret_val; | |
370 | printf("received metadata handle %u\n", metadata_data[i].handle); | |
7bc53e94 | 371 | if (lur.ret_code == LTTNG_UST_OK) { |
f0966704 MD |
372 | ssize_t len; |
373 | ||
374 | metadata_data[i].memory_map_size = lur.u.channel.memory_map_size; | |
375 | /* get shm fd */ | |
57773204 | 376 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
377 | if (len < 0) |
378 | return -EINVAL; | |
379 | metadata_data[i].shm_fd = len; | |
380 | /* get wait fd */ | |
57773204 | 381 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
382 | if (len < 0) |
383 | return -EINVAL; | |
384 | metadata_data[i].wait_fd = len; | |
385 | } | |
386 | ||
387 | ret = open_streams(sock, metadata_data[i].handle, | |
388 | &metadata_stream_data[i], 1); | |
389 | if (ret) { | |
390 | printf("Error in open_streams\n"); | |
391 | return ret; | |
392 | } | |
393 | ||
394 | /* Create channels */ | |
395 | for (j = 0; j < NR_CHANNELS; j++) { | |
396 | memset(&lum, 0, sizeof(lum)); | |
397 | lum.handle = session_handle[i]; | |
398 | lum.cmd = LTTNG_UST_CHANNEL; | |
399 | //lum.u.channel.overwrite = 0; | |
400 | lum.u.channel.overwrite = 1; | |
401 | lum.u.channel.subbuf_size = 32768; | |
402 | lum.u.channel.num_subbuf = 8; | |
403 | //lum.u.channel.num_subbuf = 4; | |
404 | //lum.u.channel.num_subbuf = 2; | |
405 | lum.u.channel.switch_timer_interval = 0; | |
406 | lum.u.channel.read_timer_interval = 0; | |
407 | lum.u.channel.output = LTTNG_UST_MMAP; | |
57773204 | 408 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
409 | if (ret) |
410 | return ret; | |
411 | channel_data[i][j].handle = lur.ret_val; | |
412 | printf("received channel handle %u\n", channel_data[i][j].handle); | |
7bc53e94 | 413 | if (lur.ret_code == LTTNG_UST_OK) { |
f0966704 MD |
414 | ssize_t len; |
415 | ||
416 | channel_data[i][j].memory_map_size = lur.u.channel.memory_map_size; | |
417 | /* get shm fd */ | |
57773204 | 418 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
419 | if (len < 0) |
420 | return -EINVAL; | |
421 | channel_data[i][j].shm_fd = len; | |
422 | /* get wait fd */ | |
57773204 | 423 | len = ustcomm_recv_fd(sock); |
f0966704 MD |
424 | if (len < 0) |
425 | return -EINVAL; | |
426 | channel_data[i][j].wait_fd = len; | |
427 | } | |
428 | ||
429 | /* Create events */ | |
430 | for (k = 0; k < NR_EVENTS; k++) { | |
431 | memset(&lum, 0, sizeof(lum)); | |
432 | lum.handle = channel_data[i][j].handle; | |
433 | lum.cmd = LTTNG_UST_EVENT; | |
434 | strncpy(lum.u.event.name, evname[k], | |
435 | LTTNG_UST_SYM_NAME_LEN); | |
436 | lum.u.event.instrumentation = LTTNG_UST_TRACEPOINT; | |
57773204 | 437 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
438 | if (ret) |
439 | return ret; | |
440 | event_handle[i][j][k] = lur.ret_val; | |
441 | printf("received event handle %u\n", event_handle[i][j][k]); | |
442 | } | |
443 | ||
444 | /* Get references to channel streams */ | |
445 | ret = open_streams(sock, channel_data[i][j].handle, | |
446 | stream_data[i][j], MAX_NR_STREAMS); | |
447 | if (ret) { | |
448 | printf("Error in open_streams\n"); | |
449 | return ret; | |
450 | } | |
451 | } | |
452 | ||
453 | memset(&lum, 0, sizeof(lum)); | |
454 | lum.handle = session_handle[i]; | |
455 | lum.cmd = LTTNG_UST_SESSION_START; | |
57773204 | 456 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
457 | if (ret) |
458 | return ret; | |
459 | printf("Session handle %u started.\n", session_handle[i]); | |
460 | } | |
461 | ||
462 | /* Tell application registration is done */ | |
463 | memset(&lum, 0, sizeof(lum)); | |
464 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
465 | lum.cmd = LTTNG_UST_REGISTER_DONE; | |
57773204 | 466 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
467 | if (ret) |
468 | return ret; | |
469 | printf("Registration done acknowledged.\n"); | |
470 | ||
471 | sleep(4); | |
472 | ||
473 | ret = consume_buffers(); | |
474 | if (ret) { | |
475 | printf("Error in consume_buffers\n"); | |
476 | return ret; | |
477 | } | |
478 | ||
479 | for (i = 0; i < NR_SESSIONS; i++) { | |
480 | /* Release channels */ | |
481 | for (j = 0; j < NR_CHANNELS; j++) { | |
482 | /* Release streams */ | |
483 | ret = close_streams(sock, stream_data[i][j], | |
484 | MAX_NR_STREAMS); | |
485 | if (ret) | |
486 | return ret; | |
487 | ||
488 | /* Release events */ | |
489 | for (k = 0; k < NR_EVENTS; k++) { | |
490 | memset(&lum, 0, sizeof(lum)); | |
491 | lum.handle = event_handle[i][j][k]; | |
492 | lum.cmd = LTTNG_UST_RELEASE; | |
57773204 | 493 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
494 | if (ret) |
495 | return ret; | |
496 | } | |
497 | memset(&lum, 0, sizeof(lum)); | |
498 | lum.handle = channel_data[i][j].handle; | |
499 | lum.cmd = LTTNG_UST_RELEASE; | |
57773204 | 500 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
501 | if (ret) |
502 | return ret; | |
503 | if (channel_data[i][j].shm_fd >= 0) { | |
504 | ret = close(channel_data[i][j].shm_fd); | |
505 | if (ret) | |
506 | return ret; | |
507 | } | |
508 | if (channel_data[i][j].wait_fd >= 0) { | |
509 | ret = close(channel_data[i][j].wait_fd); | |
510 | if (ret) | |
511 | return ret; | |
512 | } | |
513 | } | |
514 | ||
515 | /* Release metadata channel */ | |
516 | ret = close_streams(sock, &metadata_stream_data[i], 1); | |
517 | if (ret) | |
518 | return ret; | |
519 | ||
520 | memset(&lum, 0, sizeof(lum)); | |
521 | lum.handle = metadata_data[i].handle; | |
522 | lum.cmd = LTTNG_UST_RELEASE; | |
57773204 | 523 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
524 | if (ret) |
525 | return ret; | |
526 | if (metadata_data[i].shm_fd >= 0) { | |
527 | ret = close(metadata_data[i].shm_fd); | |
528 | if (ret) | |
529 | return ret; | |
530 | } | |
531 | if (metadata_data[i].wait_fd >= 0) { | |
532 | ret = close(metadata_data[i].wait_fd); | |
533 | if (ret) | |
534 | return ret; | |
535 | } | |
536 | ||
537 | /* Release session */ | |
538 | memset(&lum, 0, sizeof(lum)); | |
539 | lum.handle = session_handle[i]; | |
540 | lum.cmd = LTTNG_UST_RELEASE; | |
57773204 | 541 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
f0966704 MD |
542 | if (ret) |
543 | return ret; | |
544 | } | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
549 | /* | |
550 | * Using fork to set umask in the child process (not multi-thread safe). We | |
551 | * deal with the shm_open vs ftruncate race (happening when the sessiond owns | |
552 | * the shm and does not let everybody modify it, to ensure safety against | |
553 | * shm_unlink) by simply letting the mmap fail and retrying after a few | |
554 | * seconds. For global shm, everybody has rw access to it until the sessiond | |
555 | * starts. | |
556 | */ | |
557 | static int get_wait_shm(char *shm_path, size_t mmap_size, int global) | |
558 | { | |
559 | int wait_shm_fd, ret; | |
560 | mode_t mode; | |
561 | ||
562 | /* Default permissions */ | |
563 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
564 | ||
565 | /* Change owner of the shm path */ | |
566 | if (global) { | |
567 | ret = chown(shm_path, 0, 0); | |
568 | if (ret < 0) { | |
569 | if (errno != ENOENT) { | |
570 | perror("chown wait shm"); | |
571 | goto error; | |
572 | } | |
573 | } | |
574 | ||
575 | /* | |
576 | * If global session daemon, any application can register so the shm | |
577 | * needs to be set in read-only mode for others. | |
578 | */ | |
579 | mode |= S_IROTH; | |
580 | } else { | |
581 | ret = chown(shm_path, getuid(), getgid()); | |
582 | if (ret < 0) { | |
583 | if (errno != ENOENT) { | |
584 | perror("chown wait shm"); | |
585 | goto error; | |
586 | } | |
587 | } | |
588 | } | |
589 | ||
590 | /* | |
591 | * Set permissions to the shm even if we did not create the shm. | |
592 | */ | |
593 | ret = chmod(shm_path, mode); | |
594 | if (ret < 0) { | |
595 | if (errno != ENOENT) { | |
596 | perror("chmod wait shm"); | |
597 | goto error; | |
598 | } | |
599 | } | |
600 | ||
601 | /* | |
602 | * We're alone in a child process, so we can modify the process-wide | |
603 | * umask. | |
604 | */ | |
605 | umask(~mode); | |
606 | ||
607 | /* | |
608 | * Try creating shm (or get rw access). We don't do an exclusive open, | |
609 | * because we allow other processes to create+ftruncate it concurrently. | |
610 | */ | |
611 | wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode); | |
612 | if (wait_shm_fd < 0) { | |
613 | perror("shm_open wait shm"); | |
614 | goto error; | |
615 | } | |
616 | ||
617 | ret = ftruncate(wait_shm_fd, mmap_size); | |
618 | if (ret < 0) { | |
619 | perror("ftruncate wait shm"); | |
620 | exit(EXIT_FAILURE); | |
621 | } | |
622 | ||
623 | ret = fchmod(wait_shm_fd, mode); | |
624 | if (ret < 0) { | |
625 | perror("fchmod"); | |
626 | exit(EXIT_FAILURE); | |
627 | } | |
628 | ||
629 | printf("Got the wait shm fd %d\n", wait_shm_fd); | |
630 | ||
631 | return wait_shm_fd; | |
632 | ||
633 | error: | |
634 | printf("Failing to get the wait shm fd\n"); | |
635 | ||
636 | return -1; | |
637 | } | |
638 | ||
639 | int update_futex(int fd, int active) | |
640 | { | |
172d6b68 | 641 | long page_size; |
f0966704 MD |
642 | char *wait_shm_mmap; |
643 | int ret; | |
644 | ||
172d6b68 MD |
645 | page_size = sysconf(_SC_PAGE_SIZE); |
646 | if (page_size < 0) { | |
647 | goto error; | |
648 | } | |
649 | wait_shm_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, | |
f0966704 MD |
650 | MAP_SHARED, fd, 0); |
651 | if (wait_shm_mmap == MAP_FAILED) { | |
652 | perror("mmap"); | |
653 | goto error; | |
654 | } | |
655 | ||
656 | if (active) { | |
657 | uatomic_set((int32_t *) wait_shm_mmap, 1); | |
658 | futex_async((int32_t *) wait_shm_mmap, FUTEX_WAKE, | |
659 | INT_MAX, NULL, NULL, 0); | |
660 | } else { | |
661 | uatomic_set((int32_t *) wait_shm_mmap, 0); | |
662 | } | |
172d6b68 | 663 | ret = munmap(wait_shm_mmap, page_size); |
f0966704 MD |
664 | if (ret) { |
665 | perror("Error unmapping wait shm"); | |
666 | goto error; | |
667 | } | |
668 | return 0; | |
669 | error: | |
670 | return -1; | |
671 | } | |
672 | ||
673 | /* | |
674 | * Set open files limit to unlimited. This daemon can open a large number of | |
675 | * file descriptors in order to consumer multiple kernel traces. | |
676 | */ | |
677 | static void set_ulimit(void) | |
678 | { | |
679 | int ret; | |
680 | struct rlimit lim; | |
681 | ||
682 | /* | |
683 | * If not root, we cannot increase our max open files. But our | |
684 | * scope is then limited to processes from a single user. | |
685 | */ | |
686 | if (getuid() != 0) | |
687 | return; | |
688 | /* The kernel does not allowed an infinite limit for open files */ | |
689 | lim.rlim_cur = 65535; | |
690 | lim.rlim_max = 65535; | |
691 | ||
692 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
693 | if (ret < 0) { | |
694 | perror("failed to set open files limit"); | |
695 | } | |
696 | } | |
697 | ||
698 | int main(int argc, char **argv) | |
699 | { | |
700 | const char *home_dir; | |
48137251 CB |
701 | char home_rundir[PATH_MAX]; |
702 | char *cmd = NULL; | |
f0966704 MD |
703 | int ret, wait_shm_fd; |
704 | struct sigaction act; | |
fb50c39d | 705 | mode_t old_umask = 0; |
f0966704 MD |
706 | |
707 | set_ulimit(); | |
708 | ||
709 | /* Ignore sigpipe */ | |
710 | memset(&act, 0, sizeof(act)); | |
711 | ret = sigemptyset(&act.sa_mask); | |
712 | if (ret == -1) { | |
713 | perror("sigemptyset"); | |
714 | return -1; | |
715 | } | |
716 | ||
717 | act.sa_handler = SIG_IGN; | |
718 | ret = sigaction(SIGPIPE, &act, NULL); | |
719 | if (ret == -1) { | |
720 | perror("sigaction"); | |
721 | return -1; | |
722 | } | |
723 | ||
724 | /* Handle SIGTERM */ | |
725 | act.sa_handler = handle_signals; | |
726 | ret = sigaction(SIGTERM, &act, NULL); | |
727 | if (ret == -1) { | |
728 | perror("sigaction"); | |
729 | return -1; | |
730 | } | |
731 | /* Handle SIGINT */ | |
732 | ret = sigaction(SIGINT, &act, NULL); | |
733 | if (ret == -1) { | |
734 | perror("sigaction"); | |
735 | return -1; | |
736 | } | |
737 | ||
738 | if (geteuid() == 0) { | |
739 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); | |
740 | if (ret && errno != EEXIST) { | |
741 | perror("mkdir"); | |
742 | return -1; | |
743 | } | |
744 | wait_shm_fd = get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, | |
745 | sysconf(_SC_PAGE_SIZE), 1); | |
746 | if (wait_shm_fd < 0) { | |
747 | perror("global wait shm error"); | |
748 | return -1; | |
749 | } | |
750 | strcpy(apps_sock_path, DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
751 | old_umask = umask(0); | |
752 | } else { | |
48137251 CB |
753 | home_dir = (const char *) getenv("HOME"); |
754 | if (!home_dir) { | |
755 | perror("getenv error"); | |
756 | return -ENOENT; | |
757 | } | |
758 | ||
759 | snprintf(home_rundir, PATH_MAX, | |
760 | LTTNG_HOME_RUNDIR, home_dir); | |
761 | ||
762 | ret = mkdir(home_rundir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); | |
763 | if (ret && errno != EEXIST) { | |
764 | perror("mkdir"); | |
765 | return -1; | |
766 | } | |
767 | ||
f0966704 MD |
768 | snprintf(local_apps_wait_shm_path, PATH_MAX, |
769 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); | |
770 | wait_shm_fd = get_wait_shm(local_apps_wait_shm_path, | |
771 | sysconf(_SC_PAGE_SIZE), 0); | |
772 | if (wait_shm_fd < 0) { | |
773 | perror("local wait shm error"); | |
774 | return -1; | |
775 | } | |
f0966704 MD |
776 | snprintf(apps_sock_path, PATH_MAX, |
777 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); | |
778 | } | |
779 | ||
57773204 | 780 | ret = ustcomm_create_unix_sock(apps_sock_path); |
f0966704 MD |
781 | if (ret < 0) { |
782 | perror("create error"); | |
783 | return ret; | |
784 | } | |
785 | apps_socket = ret; | |
786 | ||
787 | if (getuid() == 0) { | |
788 | /* File permission MUST be 666 */ | |
789 | ret = chmod(apps_sock_path, | |
790 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
791 | if (ret < 0) { | |
792 | printf("Set file permissions failed: %s\n", apps_sock_path); | |
793 | perror("chmod"); | |
794 | goto end; | |
795 | } | |
796 | umask(old_umask); | |
797 | } | |
57773204 | 798 | ret = ustcomm_listen_unix_sock(apps_socket); |
f0966704 MD |
799 | if (ret < 0) { |
800 | perror("listen error"); | |
801 | return ret; | |
802 | } | |
803 | ||
804 | /* wake up futexes */ | |
805 | ret = update_futex(wait_shm_fd, 1); | |
806 | if (ret) { | |
807 | fprintf(stderr, "Error wakeup futex\n"); | |
808 | return -1; | |
809 | } | |
810 | ||
811 | for (;;) { | |
812 | int sock; | |
813 | ssize_t len; | |
814 | struct { | |
815 | uint32_t major; | |
816 | uint32_t minor; | |
817 | pid_t pid; | |
818 | pid_t ppid; | |
819 | uid_t uid; | |
820 | gid_t gid; | |
1a34b35b | 821 | uint32_t bits_per_long; |
f0966704 MD |
822 | char name[16]; /* Process name */ |
823 | } reg_msg; | |
824 | char bufname[17]; | |
825 | ||
826 | if (quit_program) | |
827 | break; | |
828 | ||
829 | printf("Accepting application registration\n"); | |
57773204 | 830 | sock = ustcomm_accept_unix_sock(apps_socket); |
f0966704 MD |
831 | if (sock < 0) { |
832 | perror("accept error"); | |
833 | goto end; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Basic recv here to handle the very simple data | |
838 | * that the libust send to register (reg_msg). | |
839 | */ | |
57773204 | 840 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
f0966704 | 841 | if (len < 0 || len != sizeof(reg_msg)) { |
57773204 | 842 | perror("ustcomm_recv_unix_sock"); |
f0966704 MD |
843 | continue; |
844 | } | |
845 | memcpy(bufname, reg_msg.name, 16); | |
846 | bufname[16] = '\0'; | |
847 | printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n", | |
848 | bufname, reg_msg.pid, reg_msg.ppid, reg_msg.uid, | |
849 | reg_msg.gid, reg_msg.major, reg_msg.minor); | |
850 | ret = send_app_msgs(sock); | |
851 | if (ret) { | |
852 | printf("Error in send_app_msgs.\n"); | |
853 | sleep(1); | |
854 | } | |
855 | close(sock); | |
856 | } | |
857 | ||
858 | end: | |
859 | printf("quitting.\n"); | |
860 | /* Let applications know we are not responding anymore */ | |
861 | ret = update_futex(wait_shm_fd, 0); | |
862 | if (ret) { | |
863 | fprintf(stderr, "Error wakeup futex\n"); | |
864 | return -1; | |
865 | } | |
866 | ||
48137251 CB |
867 | if (geteuid()) { |
868 | printf("Removing %s directory\n", home_rundir); | |
869 | ret = asprintf(&cmd, "rm -rf %s", home_rundir); | |
870 | if (ret < 0) { | |
871 | printf("asprintf failed. Something is really wrong!\n"); | |
872 | return -1; | |
873 | } | |
874 | ||
875 | /* Remove lttng run directory */ | |
876 | ret = system(cmd); | |
877 | if (ret < 0) { | |
878 | printf("Unable to clean %s\n", home_rundir); | |
94ec8e33 | 879 | return -1; |
48137251 CB |
880 | } |
881 | } | |
882 | ||
f0966704 MD |
883 | return 0; |
884 | } |