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