| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program 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 |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <grp.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <lttng/lttng.h> |
| 28 | |
| 29 | #include "liblttsessiondcomm.h" |
| 30 | #include "lttngerr.h" |
| 31 | |
| 32 | /* Socket to session daemon for communication */ |
| 33 | static int sessiond_socket; |
| 34 | static char sessiond_sock_path[PATH_MAX]; |
| 35 | |
| 36 | /* Communication structure to ltt-sessiond */ |
| 37 | static struct lttcomm_session_msg lsm; |
| 38 | static struct lttcomm_lttng_msg llm; |
| 39 | |
| 40 | /* Prototypes */ |
| 41 | static int check_tracing_group(const char *grp_name); |
| 42 | static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf); |
| 43 | static int recv_data_sessiond(void *buf, size_t len); |
| 44 | static int send_data_sessiond(void); |
| 45 | static int set_session_daemon_path(void); |
| 46 | |
| 47 | /* Variables */ |
| 48 | static char *tracing_group; |
| 49 | static int connected; |
| 50 | |
| 51 | /* |
| 52 | * send_data_sessiond |
| 53 | * |
| 54 | * Send lttcomm_session_msg to the session daemon. |
| 55 | * |
| 56 | * On success, return 0 |
| 57 | * On error, return error code |
| 58 | */ |
| 59 | static int send_data_sessiond(void) |
| 60 | { |
| 61 | int ret; |
| 62 | |
| 63 | if (!connected) { |
| 64 | ret = -ENOTCONN; |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm)); |
| 69 | |
| 70 | end: |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * recv_data_sessiond |
| 76 | * |
| 77 | * Receive data from the sessiond socket. |
| 78 | * |
| 79 | * On success, return 0 |
| 80 | * On error, return recv() error code |
| 81 | */ |
| 82 | static int recv_data_sessiond(void *buf, size_t len) |
| 83 | { |
| 84 | int ret; |
| 85 | |
| 86 | if (!connected) { |
| 87 | ret = -ENOTCONN; |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
| 92 | if (ret < 0) { |
| 93 | goto end; |
| 94 | } |
| 95 | |
| 96 | end: |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * ask_sessiond |
| 102 | * |
| 103 | * Ask the session daemon a specific command |
| 104 | * and put the data into buf. |
| 105 | * |
| 106 | * Return size of data (only payload, not header). |
| 107 | */ |
| 108 | static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf) |
| 109 | { |
| 110 | int ret; |
| 111 | size_t size; |
| 112 | void *data = NULL; |
| 113 | |
| 114 | ret = lttng_connect_sessiond(); |
| 115 | if (ret < 0) { |
| 116 | goto end; |
| 117 | } |
| 118 | |
| 119 | lsm.cmd_type = lct; |
| 120 | |
| 121 | /* Send command to session daemon */ |
| 122 | ret = send_data_sessiond(); |
| 123 | if (ret < 0) { |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | /* Get header from data transmission */ |
| 128 | ret = recv_data_sessiond(&llm, sizeof(llm)); |
| 129 | if (ret < 0) { |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | /* Check error code if OK */ |
| 134 | if (llm.ret_code != LTTCOMM_OK) { |
| 135 | ret = -llm.ret_code; |
| 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | size = llm.trace_name_offset + llm.data_size; |
| 140 | if (size == 0) { |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | data = (void*) malloc(size); |
| 145 | |
| 146 | /* Get payload data */ |
| 147 | ret = recv_data_sessiond(data, size); |
| 148 | if (ret < 0) { |
| 149 | goto end; |
| 150 | } |
| 151 | |
| 152 | *buf = data; |
| 153 | ret = size; |
| 154 | |
| 155 | end: |
| 156 | lttng_disconnect_sessiond(); |
| 157 | memset(&lsm, 0, sizeof(lsm)); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * lttng_get_readable_code |
| 163 | * |
| 164 | * Return a human readable string of code |
| 165 | */ |
| 166 | const char *lttng_get_readable_code(int code) |
| 167 | { |
| 168 | if (code > -LTTCOMM_OK) { |
| 169 | return "Ended with errors"; |
| 170 | } |
| 171 | |
| 172 | return lttcomm_get_readable_code(code); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * lttng_ust_start_trace |
| 177 | * |
| 178 | * Request a trace start for pid. |
| 179 | */ |
| 180 | int lttng_ust_start_trace(pid_t pid) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | lsm.pid = pid; |
| 185 | ret = ask_sessiond(UST_START_TRACE, NULL); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * lttng_ust_stop_trace |
| 192 | * |
| 193 | * Request a trace stop for pid. |
| 194 | */ |
| 195 | int lttng_ust_stop_trace(pid_t pid) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | lsm.pid = pid; |
| 200 | ret = ask_sessiond(UST_STOP_TRACE, NULL); |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * lttng_ust_create_trace |
| 207 | * |
| 208 | * Request a trace creation for pid. |
| 209 | */ |
| 210 | int lttng_ust_create_trace(pid_t pid) |
| 211 | { |
| 212 | int ret; |
| 213 | |
| 214 | lsm.pid = pid; |
| 215 | ret = ask_sessiond(UST_CREATE_TRACE, NULL); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * lttng_ust_list_apps |
| 222 | * |
| 223 | * Ask the session daemon for all UST traceable |
| 224 | * applications. |
| 225 | * |
| 226 | * Return the number of pids. |
| 227 | * On error, return negative value. |
| 228 | */ |
| 229 | int lttng_ust_list_apps(pid_t **pids) |
| 230 | { |
| 231 | int ret; |
| 232 | |
| 233 | ret = ask_sessiond(UST_LIST_APPS, (void**) pids); |
| 234 | if (ret < 0) { |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | return ret / sizeof(pid_t); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * lttng_list_traces |
| 243 | * |
| 244 | * Ask the session daemon for all traces (kernel and ust) |
| 245 | * for the session identified by uuid. |
| 246 | * |
| 247 | * Return the number of traces. |
| 248 | */ |
| 249 | int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces) |
| 250 | { |
| 251 | int ret; |
| 252 | |
| 253 | uuid_copy(lsm.session_uuid, *uuid); |
| 254 | |
| 255 | ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces); |
| 256 | if (ret < 0) { |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | return ret / sizeof(struct lttng_trace); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * lttng_create_session |
| 265 | * |
| 266 | * Create a brand new session using name. Allocate |
| 267 | * the session_id param pointing to the UUID. |
| 268 | */ |
| 269 | int lttng_create_session(char *name, uuid_t *session_id) |
| 270 | { |
| 271 | int ret; |
| 272 | |
| 273 | strncpy(lsm.session_name, name, sizeof(lsm.session_name)); |
| 274 | lsm.session_name[sizeof(lsm.session_name) - 1] = '\0'; |
| 275 | |
| 276 | ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL); |
| 277 | if (ret < 0) { |
| 278 | goto end; |
| 279 | } |
| 280 | |
| 281 | uuid_copy(*session_id, llm.session_uuid); |
| 282 | |
| 283 | end: |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * lttng_destroy_session |
| 289 | * |
| 290 | * Destroy session using name. |
| 291 | */ |
| 292 | int lttng_destroy_session(uuid_t *uuid) |
| 293 | { |
| 294 | int ret; |
| 295 | |
| 296 | uuid_copy(lsm.session_uuid, *uuid); |
| 297 | |
| 298 | ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL); |
| 299 | if (ret < 0) { |
| 300 | goto end; |
| 301 | } |
| 302 | |
| 303 | end: |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * lttng_list_sessions |
| 309 | * |
| 310 | * Ask the session daemon for all available sessions. |
| 311 | * |
| 312 | * Return number of session. |
| 313 | * On error, return negative value. |
| 314 | */ |
| 315 | int lttng_list_sessions(struct lttng_session **sessions) |
| 316 | { |
| 317 | int ret; |
| 318 | |
| 319 | ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions); |
| 320 | if (ret < 0) { |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | return ret / sizeof(struct lttng_session); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * lttng_connect_sessiond |
| 329 | * |
| 330 | * Connect to the LTTng session daemon. |
| 331 | * On success, return 0 |
| 332 | * On error, return a negative value |
| 333 | */ |
| 334 | int lttng_connect_sessiond(void) |
| 335 | { |
| 336 | int ret; |
| 337 | |
| 338 | ret = set_session_daemon_path(); |
| 339 | if (ret < 0) { |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /* Connect to the sesssion daemon */ |
| 344 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
| 345 | if (ret < 0) { |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | sessiond_socket = ret; |
| 350 | connected = 1; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * lttng_disconnect_sessiond |
| 357 | * |
| 358 | * Clean disconnect the session daemon. |
| 359 | */ |
| 360 | int lttng_disconnect_sessiond(void) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | |
| 364 | if (connected) { |
| 365 | ret = lttcomm_close_unix_sock(sessiond_socket); |
| 366 | sessiond_socket = 0; |
| 367 | connected = 0; |
| 368 | } |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * lttng_set_current_session_uuid |
| 375 | * |
| 376 | * Set the session uuid for current lsm. |
| 377 | */ |
| 378 | void lttng_set_current_session_uuid(uuid_t *uuid) |
| 379 | { |
| 380 | uuid_copy(lsm.session_uuid, *uuid); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * lttng_set_tracing_group |
| 385 | * |
| 386 | * Set tracing group variable with name. This function |
| 387 | * allocate memory pointed by tracing_group. |
| 388 | */ |
| 389 | int lttng_set_tracing_group(const char *name) |
| 390 | { |
| 391 | if (asprintf(&tracing_group, "%s", name) < 0) { |
| 392 | return -ENOMEM; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * lttng_check_session_daemon |
| 400 | * |
| 401 | * Return 0 if a sesssion daemon is available |
| 402 | * else return -1 |
| 403 | */ |
| 404 | int lttng_check_session_daemon(void) |
| 405 | { |
| 406 | int ret; |
| 407 | |
| 408 | ret = set_session_daemon_path(); |
| 409 | if (ret < 0) { |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | /* If socket exist, we consider the daemon started */ |
| 414 | ret = access(sessiond_sock_path, F_OK); |
| 415 | if (ret < 0) { |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * set_session_daemon_path |
| 424 | * |
| 425 | * Set sessiond socket path by putting it in |
| 426 | * the global sessiond_sock_path variable. |
| 427 | */ |
| 428 | static int set_session_daemon_path(void) |
| 429 | { |
| 430 | int ret; |
| 431 | |
| 432 | /* Are we in the tracing group ? */ |
| 433 | ret = check_tracing_group(tracing_group); |
| 434 | if (ret < 0 && getuid() != 0) { |
| 435 | if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK, |
| 436 | getenv("HOME")) < 0) { |
| 437 | return -ENOMEM; |
| 438 | } |
| 439 | } else { |
| 440 | strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, |
| 441 | sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * check_tracing_group |
| 449 | * |
| 450 | * Check if the specified group name exist. |
| 451 | * If yes, 0, else -1 |
| 452 | */ |
| 453 | static int check_tracing_group(const char *grp_name) |
| 454 | { |
| 455 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ |
| 456 | gid_t *grp_list; |
| 457 | int grp_list_size, grp_id, i; |
| 458 | int ret = -1; |
| 459 | |
| 460 | /* Get GID of group 'tracing' */ |
| 461 | grp_tracing = getgrnam(grp_name); |
| 462 | if (grp_tracing == NULL) { |
| 463 | /* NULL means not found also. getgrnam(3) */ |
| 464 | if (errno != 0) { |
| 465 | perror("getgrnam"); |
| 466 | } |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | /* Get number of supplementary group IDs */ |
| 471 | grp_list_size = getgroups(0, NULL); |
| 472 | if (grp_list_size < 0) { |
| 473 | perror("getgroups"); |
| 474 | goto end; |
| 475 | } |
| 476 | |
| 477 | /* Alloc group list of the right size */ |
| 478 | grp_list = malloc(grp_list_size * sizeof(gid_t)); |
| 479 | grp_id = getgroups(grp_list_size, grp_list); |
| 480 | if (grp_id < -1) { |
| 481 | perror("getgroups"); |
| 482 | goto free_list; |
| 483 | } |
| 484 | |
| 485 | for (i = 0; i < grp_list_size; i++) { |
| 486 | if (grp_list[i] == grp_tracing->gr_gid) { |
| 487 | ret = 0; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | free_list: |
| 493 | free(grp_list); |
| 494 | |
| 495 | end: |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * lib constructor |
| 501 | */ |
| 502 | static void __attribute__((constructor)) init() |
| 503 | { |
| 504 | /* Set default session group */ |
| 505 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
| 506 | } |