Make root see all available pids v2
[ust.git] / libustctl / libustctl.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
2 * Copyright (C) 2011 Ericsson AB
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <dirent.h>
27
28 #include "ustcomm.h"
29 #include "ust/ustctl.h"
30 #include "usterr.h"
31
32 static int do_cmd(int sock,
33 const struct ustcomm_header *req_header,
34 const char *req_data,
35 struct ustcomm_header *res_header,
36 char **res_data)
37 {
38 int result, saved_errno = 0;
39 char *recv_buf;
40
41 recv_buf = zmalloc(USTCOMM_BUFFER_SIZE);
42 if (!recv_buf) {
43 saved_errno = ENOMEM;
44 goto out;
45 }
46
47 result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf);
48 if (result > 0) {
49 saved_errno = -res_header->result;
50 if (res_header->size == 0 || saved_errno > 0) {
51 free(recv_buf);
52 } else {
53 if (res_data) {
54 *res_data = recv_buf;
55 } else {
56 free(recv_buf);
57 }
58 }
59 } else {
60 ERR("ustcomm req failed");
61 if (result == 0) {
62 saved_errno = ENOTCONN;
63 } else {
64 saved_errno = -result;
65 }
66 free(recv_buf);
67 }
68
69 out:
70 errno = saved_errno;
71 if (errno) {
72 return -1;
73 }
74
75 return 0;
76 }
77
78 int ustctl_connect_pid(pid_t pid)
79 {
80 int sock;
81
82 if (ustcomm_connect_app(pid, &sock)) {
83 ERR("could not connect to PID %u", (unsigned int) pid);
84 errno = ENOTCONN;
85 return -1;
86 }
87
88 return sock;
89 }
90
91 static void get_pids_in_dir(DIR *dir, pid_t **pid_list,
92 unsigned int *pid_list_size)
93 {
94 struct dirent *dirent;
95 unsigned int read_pid;
96
97 while ((dirent = readdir(dir))) {
98 if (!strcmp(dirent->d_name, ".") ||
99 !strcmp(dirent->d_name, "..") ||
100 !strcmp(dirent->d_name, "ust-consumer") ||
101 dirent->d_type == DT_DIR) {
102
103 continue;
104 }
105
106 sscanf(dirent->d_name, "%u", &read_pid);
107
108 (*pid_list)[*pid_list_size - 1] = read_pid;
109 /* FIXME: Here we previously called pid_is_online, which
110 * always returned 1, now I replaced it with just 1.
111 * We need to figure out an intelligent way of solving
112 * this, maybe connect-disconnect.
113 */
114 if (1) {
115 (*pid_list_size)++;
116 *pid_list = realloc(*pid_list,
117 *pid_list_size * sizeof(pid_t));
118 }
119 }
120
121 (*pid_list)[*pid_list_size - 1] = 0; /* Array end */
122 }
123
124 static pid_t *get_pids_non_root(void)
125 {
126 char *dir_name;
127 DIR *dir;
128 unsigned int pid_list_size = 1;
129 pid_t *pid_list = NULL;
130
131 dir_name = ustcomm_user_sock_dir();
132 if (!dir_name) {
133 return NULL;
134 }
135
136 dir = opendir(dir_name);
137 if (!dir) {
138 goto free_dir_name;
139 }
140
141 pid_list = malloc(pid_list_size * sizeof(pid_t));
142 if (!pid_list) {
143 goto close_dir;
144 }
145
146 get_pids_in_dir(dir, &pid_list, &pid_list_size);
147
148 if (pid_list[0] == 0) {
149 /* No PID at all */
150 free(pid_list);
151 pid_list = NULL;
152 goto close_dir;
153 }
154
155 close_dir:
156 closedir(dir);
157
158 free_dir_name:
159 free(dir_name);
160
161 return pid_list;
162 }
163
164 static pid_t *get_pids_root(void)
165 {
166 char *dir_name;
167 DIR *tmp_dir, *dir;
168 unsigned int pid_list_size = 1;
169 pid_t *pid_list = NULL;
170 struct dirent *dirent;
171
172 tmp_dir = opendir(USER_TMP_DIR);
173 if (!tmp_dir) {
174 return NULL;
175 }
176
177 pid_list = malloc(pid_list_size * sizeof(pid_t));
178 if (!pid_list) {
179 goto close_tmp_dir;
180 }
181
182 while ((dirent = readdir(tmp_dir))) {
183 /* Compare the dir to check for the USER_SOCK_DIR_BASE prefix */
184 if (!strncmp(dirent->d_name, USER_SOCK_DIR_BASE,
185 strlen(USER_SOCK_DIR_BASE))) {
186
187 if (asprintf(&dir_name, USER_TMP_DIR "/%s", dirent->d_name) < 0) {
188 goto close_tmp_dir;
189 }
190
191 dir = opendir(dir_name);
192
193 free(dir_name);
194
195 if (!dir) {
196 continue;
197 }
198
199 get_pids_in_dir(dir, &pid_list, &pid_list_size);
200
201 closedir(dir);
202 }
203 }
204
205 close_tmp_dir:
206 closedir(tmp_dir);
207
208 return pid_list;
209 }
210
211 pid_t *ustctl_get_online_pids(void)
212 {
213 if (geteuid()) {
214 return get_pids_non_root();
215 } else {
216 return get_pids_root();
217 }
218 }
219
220 /**
221 * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
222 *
223 * @param mn Marker name
224 * @param state Marker's new state
225 * @param pid Traced process ID
226 * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG}
227 */
228 int ustctl_set_marker_state(int sock, const char *trace, const char *channel,
229 const char *marker, int state)
230 {
231 struct ustcomm_header req_header, res_header;
232 struct ustcomm_marker_info marker_inf;
233 int result;
234
235 result = ustcomm_pack_marker_info(&req_header,
236 &marker_inf,
237 trace,
238 channel,
239 marker);
240 if (result < 0) {
241 errno = -result;
242 return -1;
243 }
244
245 req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER;
246
247 return do_cmd(sock, &req_header, (char *)&marker_inf,
248 &res_header, NULL);
249 }
250
251 /**
252 * Set subbuffer size.
253 *
254 * @param channel_size Channel name and size
255 * @param pid Traced process ID
256 * @return 0 if successful, or error
257 */
258 int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel,
259 unsigned int subbuf_size)
260 {
261 struct ustcomm_header req_header, res_header;
262 struct ustcomm_channel_info ch_inf;
263 int result;
264
265 result = ustcomm_pack_channel_info(&req_header,
266 &ch_inf,
267 trace,
268 channel);
269 if (result < 0) {
270 errno = -result;
271 return -1;
272 }
273
274 req_header.command = SET_SUBBUF_SIZE;
275 ch_inf.subbuf_size = subbuf_size;
276
277 return do_cmd(sock, &req_header, (char *)&ch_inf,
278 &res_header, NULL);
279 }
280
281 /**
282 * Set subbuffer num.
283 *
284 * @param channel_num Channel name and num
285 * @param pid Traced process ID
286 * @return 0 if successful, or error
287 */
288 int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel,
289 unsigned int num)
290 {
291 struct ustcomm_header req_header, res_header;
292 struct ustcomm_channel_info ch_inf;
293 int result;
294
295 result = ustcomm_pack_channel_info(&req_header,
296 &ch_inf,
297 trace,
298 channel);
299 if (result < 0) {
300 errno = -result;
301 return -1;
302 }
303
304 req_header.command = SET_SUBBUF_NUM;
305 ch_inf.subbuf_num = num;
306
307 return do_cmd(sock, &req_header, (char *)&ch_inf,
308 &res_header, NULL);
309
310 }
311
312
313 static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel,
314 int *num, int *size)
315 {
316 struct ustcomm_header req_header, res_header;
317 struct ustcomm_channel_info ch_inf, *ch_inf_res;
318 int result;
319
320
321 result = ustcomm_pack_channel_info(&req_header,
322 &ch_inf,
323 trace,
324 channel);
325 if (result < 0) {
326 errno = -result;
327 return -1;
328 }
329
330 req_header.command = GET_SUBBUF_NUM_SIZE;
331
332 result = do_cmd(sock, &req_header, (char *)&ch_inf,
333 &res_header, (char **)&ch_inf_res);
334 if (result < 0) {
335 return -1;
336 }
337
338 *num = ch_inf_res->subbuf_num;
339 *size = ch_inf_res->subbuf_size;
340
341 free(ch_inf_res);
342
343 return 0;
344 }
345
346 /**
347 * Get subbuffer num.
348 *
349 * @param channel Channel name
350 * @param pid Traced process ID
351 * @return subbuf cnf if successful, or error
352 */
353 int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel)
354 {
355 int num, size, result;
356
357 result = ustctl_get_subbuf_num_size(sock, trace, channel,
358 &num, &size);
359 if (result < 0) {
360 errno = -result;
361 return -1;
362 }
363
364 return num;
365 }
366
367 /**
368 * Get subbuffer size.
369 *
370 * @param channel Channel name
371 * @param pid Traced process ID
372 * @return subbuf size if successful, or error
373 */
374 int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel)
375 {
376 int num, size, result;
377
378 result = ustctl_get_subbuf_num_size(sock, trace, channel,
379 &num, &size);
380 if (result < 0) {
381 errno = -result;
382 return -1;
383 }
384
385 return size;
386 }
387
388 static int do_trace_cmd(int sock, const char *trace, int command)
389 {
390 struct ustcomm_header req_header, res_header;
391 struct ustcomm_single_field trace_inf;
392 int result;
393
394 result = ustcomm_pack_single_field(&req_header,
395 &trace_inf,
396 trace);
397 if (result < 0) {
398 errno = -result;
399 return -1;
400 }
401
402 req_header.command = command;
403
404 return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL);
405 }
406
407 /**
408 * Destroys an UST trace according to a PID.
409 *
410 * @param pid Traced process ID
411 * @return 0 if successful, or error USTCTL_ERR_GEN
412 */
413 int ustctl_destroy_trace(int sock, const char *trace)
414 {
415 return do_trace_cmd(sock, trace, DESTROY_TRACE);
416 }
417
418 /**
419 * Starts an UST trace (and setups it) according to a PID.
420 *
421 * @param pid Traced process ID
422 * @return 0 if successful, or error USTCTL_ERR_GEN
423 */
424 int ustctl_setup_and_start(int sock, const char *trace)
425 {
426 return do_trace_cmd(sock, trace, START);
427 }
428
429 /**
430 * Creates an UST trace according to a PID.
431 *
432 * @param pid Traced process ID
433 * @return 0 if successful, or error USTCTL_ERR_GEN
434 */
435 int ustctl_create_trace(int sock, const char *trace)
436 {
437 return do_trace_cmd(sock, trace, CREATE_TRACE);
438 }
439
440 /**
441 * Starts an UST trace according to a PID.
442 *
443 * @param pid Traced process ID
444 * @return 0 if successful, or error USTCTL_ERR_GEN
445 */
446 int ustctl_start_trace(int sock, const char *trace)
447 {
448 return do_trace_cmd(sock, trace, START_TRACE);
449 }
450
451 /**
452 * Alloc an UST trace according to a PID.
453 *
454 * @param pid Traced process ID
455 * @return 0 if successful, or error USTCTL_ERR_GEN
456 */
457 int ustctl_alloc_trace(int sock, const char *trace)
458 {
459 return do_trace_cmd(sock, trace, ALLOC_TRACE);
460 }
461
462
463 int ustctl_force_switch(int sock, const char *trace)
464 {
465 return do_trace_cmd(sock, trace, FORCE_SUBBUF_SWITCH);
466 }
467
468 /**
469 * Stops an UST trace according to a PID.
470 *
471 * @param pid Traced process ID
472 * @return 0 if successful, or error USTCTL_ERR_GEN
473 */
474 int ustctl_stop_trace(int sock, const char *trace)
475 {
476 return do_trace_cmd(sock, trace, STOP_TRACE);
477 }
478
479 /**
480 * Counts newlines ('\n') in a string.
481 *
482 * @param str String to search in
483 * @return Total newlines count
484 */
485 unsigned int ustctl_count_nl(const char *str)
486 {
487 unsigned int i = 0, tot = 0;
488
489 while (str[i] != '\0') {
490 if (str[i] == '\n') {
491 ++tot;
492 }
493 ++i;
494 }
495
496 return tot;
497 }
498
499 /**
500 * Frees a CMSF array.
501 *
502 * @param cmsf CMSF array to free
503 * @return 0 if successful, or error USTCTL_ERR_ARG
504 */
505 int ustctl_free_cmsf(struct marker_status *cmsf)
506 {
507 if (cmsf == NULL) {
508 return USTCTL_ERR_ARG;
509 }
510
511 unsigned int i = 0;
512 while (cmsf[i].channel != NULL) {
513 free(cmsf[i].channel);
514 free(cmsf[i].marker);
515 free(cmsf[i].fs);
516 ++i;
517 }
518 free(cmsf);
519
520 return 0;
521 }
522
523 /**
524 * Gets channel/marker/state/format string for a given PID.
525 *
526 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
527 * frees with `ustctl_free_cmsf')
528 * @param pid Targeted PID
529 * @return 0 if successful, or -1 on error
530 */
531 int ustctl_get_cmsf(int sock, struct marker_status **cmsf)
532 {
533 struct ustcomm_header req_header, res_header;
534 char *big_str = NULL;
535 int result;
536 struct marker_status *tmp_cmsf = NULL;
537 unsigned int i = 0, cmsf_ind = 0;
538
539 if (cmsf == NULL) {
540 return -1;
541 }
542
543 req_header.command = LIST_MARKERS;
544 req_header.size = 0;
545
546 result = ustcomm_send(sock, &req_header, NULL);
547 if (result <= 0) {
548 PERROR("error while requesting markers list");
549 return -1;
550 }
551
552 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
553 if (result <= 0) {
554 ERR("error while receiving markers list");
555 return -1;
556 }
557
558 tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) *
559 (ustctl_count_nl(big_str) + 1));
560 if (tmp_cmsf == NULL) {
561 ERR("Failed to allocate CMSF array");
562 return -1;
563 }
564
565 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
566 while (big_str[i] != '\0') {
567 char state;
568
569 sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
570 &tmp_cmsf[cmsf_ind].channel,
571 &tmp_cmsf[cmsf_ind].marker,
572 &state,
573 &tmp_cmsf[cmsf_ind].fs);
574 tmp_cmsf[cmsf_ind].state = (state == USTCTL_MS_CHR_ON ?
575 USTCTL_MS_ON : USTCTL_MS_OFF); /* Marker state */
576
577 while (big_str[i] != '\n') {
578 ++i; /* Go to next '\n' */
579 }
580 ++i; /* Skip current pointed '\n' */
581 ++cmsf_ind;
582 }
583 tmp_cmsf[cmsf_ind].channel = NULL;
584 tmp_cmsf[cmsf_ind].marker = NULL;
585 tmp_cmsf[cmsf_ind].fs = NULL;
586
587 *cmsf = tmp_cmsf;
588
589 free(big_str);
590 return 0;
591 }
592
593 /**
594 * Frees a TES array.
595 *
596 * @param tes TES array to free
597 * @return 0 if successful, or error USTCTL_ERR_ARG
598 */
599 int ustctl_free_tes(struct trace_event_status *tes)
600 {
601 if (tes == NULL) {
602 return USTCTL_ERR_ARG;
603 }
604
605 unsigned int i = 0;
606 while (tes[i].name != NULL) {
607 free(tes[i].name);
608 ++i;
609 }
610 free(tes);
611
612 return 0;
613 }
614
615 /**
616 * Gets trace_events string for a given PID.
617 *
618 * @param tes Pointer to TES array to be filled (callee allocates, caller
619 * frees with `ustctl_free_tes')
620 * @param pid Targeted PID
621 * @return 0 if successful, or -1 on error
622 */
623 int ustctl_get_tes(int sock, struct trace_event_status **tes)
624 {
625 struct ustcomm_header req_header, res_header;
626 char *big_str = NULL;
627 int result;
628 struct trace_event_status *tmp_tes = NULL;
629 unsigned int i = 0, tes_ind = 0;
630
631 if (tes == NULL) {
632 return -1;
633 }
634
635 req_header.command = LIST_TRACE_EVENTS;
636 req_header.size = 0;
637
638 result = ustcomm_send(sock, &req_header, NULL);
639 if (result != 1) {
640 ERR("error while requesting trace_event list");
641 return -1;
642 }
643
644 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
645 if (result != 1) {
646 ERR("error while receiving markers list");
647 return -1;
648 }
649
650 tmp_tes = (struct trace_event_status *)
651 zmalloc(sizeof(struct trace_event_status) *
652 (ustctl_count_nl(big_str) + 1));
653 if (tmp_tes == NULL) {
654 ERR("Failed to allocate TES array");
655 return -1;
656 }
657
658 /* Parse received reply string (format: "[name]"): */
659 while (big_str[i] != '\0') {
660 sscanf(big_str + i, "trace_event: %a[^\n]",
661 &tmp_tes[tes_ind].name);
662 while (big_str[i] != '\n') {
663 ++i; /* Go to next '\n' */
664 }
665 ++i; /* Skip current pointed '\n' */
666 ++tes_ind;
667 }
668 tmp_tes[tes_ind].name = NULL;
669
670 *tes = tmp_tes;
671
672 free(big_str);
673 return 0;
674 }
675
676 /**
677 * Set sock path
678 *
679 * @param sock_path Sock path
680 * @param pid Traced process ID
681 * @return 0 if successful, or error
682 */
683 int ustctl_set_sock_path(int sock, const char *sock_path)
684 {
685 int result;
686 struct ustcomm_header req_header, res_header;
687 struct ustcomm_single_field sock_path_msg;
688
689 result = ustcomm_pack_single_field(&req_header,
690 &sock_path_msg,
691 sock_path);
692 if (result < 0) {
693 errno = -result;
694 return -1;
695 }
696
697 req_header.command = SET_SOCK_PATH;
698
699 return do_cmd(sock, &req_header, (char *)&sock_path_msg,
700 &res_header, NULL);
701 }
702
703 /**
704 * Get sock path
705 *
706 * @param sock_path Pointer to where the sock path will be returned
707 * @param pid Traced process ID
708 * @return 0 if successful, or error
709 */
710 int ustctl_get_sock_path(int sock, char **sock_path)
711 {
712 int result;
713 struct ustcomm_header req_header, res_header;
714 struct ustcomm_single_field *sock_path_msg;
715
716 req_header.command = GET_SOCK_PATH;
717 req_header.size = 0;
718
719 result = do_cmd(sock, &req_header, NULL, &res_header,
720 (char **)&sock_path_msg);
721 if (result < 0) {
722 return -1;
723 }
724
725 result = ustcomm_unpack_single_field(sock_path_msg);
726 if (result < 0) {
727 return result;
728 }
729
730 *sock_path = strdup(sock_path_msg->field);
731
732 free(sock_path_msg);
733
734 return 0;
735 }
This page took 0.044295 seconds and 4 git commands to generate.