| 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/liblttngctl.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_lttng_msg llm; |
| 38 | static struct lttcomm_session_msg lsm; |
| 39 | |
| 40 | /* Prototypes */ |
| 41 | static int check_tracing_group(const char *grp_name); |
| 42 | static int ask_sessiond(void); |
| 43 | static int recvfrom_sessiond(void); |
| 44 | static int set_session_daemon_path(void); |
| 45 | static void reset_data_struct(void); |
| 46 | |
| 47 | int lttng_connect_sessiond(void); |
| 48 | int lttng_create_session(const char *name, char *session_id); |
| 49 | int lttng_check_session_daemon(void); |
| 50 | |
| 51 | /* Variables */ |
| 52 | static char *tracing_group; |
| 53 | static int connected; |
| 54 | |
| 55 | /* |
| 56 | * ask_sessiond |
| 57 | * |
| 58 | * Send lttcomm_session_msg to the session daemon. |
| 59 | * |
| 60 | * On success, return 0 |
| 61 | * On error, return error code |
| 62 | */ |
| 63 | static int ask_sessiond(void) |
| 64 | { |
| 65 | int ret; |
| 66 | |
| 67 | if (!connected) { |
| 68 | ret = -ENOTCONN; |
| 69 | goto end; |
| 70 | } |
| 71 | |
| 72 | ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm)); |
| 73 | |
| 74 | end: |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * recvfrom_sessiond |
| 80 | * |
| 81 | * Receive data from the sessiond socket. |
| 82 | * |
| 83 | * On success, return 0 |
| 84 | * On error, return recv() error code |
| 85 | */ |
| 86 | static int recvfrom_sessiond(void) |
| 87 | { |
| 88 | int ret; |
| 89 | |
| 90 | if (!connected) { |
| 91 | ret = -ENOTCONN; |
| 92 | goto end; |
| 93 | } |
| 94 | |
| 95 | ret = lttcomm_recv_unix_sock(sessiond_socket, &llm, sizeof(llm)); |
| 96 | if (ret < 0) { |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | /* Check return code */ |
| 101 | if (llm.ret_code != LTTCOMM_OK) { |
| 102 | ret = -llm.ret_code; |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | end: |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * lttng_get_readable_code |
| 112 | * |
| 113 | * Return a human readable string of code |
| 114 | */ |
| 115 | const char *lttng_get_readable_code(int code) |
| 116 | { |
| 117 | if (code > -LTTCOMM_OK) { |
| 118 | return "Ended with errors"; |
| 119 | } |
| 120 | |
| 121 | return lttcomm_get_readable_code(code); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * lttng_create_session |
| 126 | * |
| 127 | * Create a tracing session using "name" to the session daemon. |
| 128 | * If no name is given, the auto session creation is set and |
| 129 | * the daemon will take care of finding a name. |
| 130 | * |
| 131 | * On success, return 0 and session_id point to the uuid str. |
| 132 | * On error, negative value is returned. |
| 133 | */ |
| 134 | int lttng_create_session(const char *name, char *session_id) |
| 135 | { |
| 136 | int ret; |
| 137 | |
| 138 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
| 139 | if (name == NULL) { |
| 140 | lsm.u.create_session.auto_session = 1; |
| 141 | } else { |
| 142 | strncpy(lsm.session_name, name, strlen(name)); |
| 143 | lsm.u.create_session.auto_session = 0; |
| 144 | } |
| 145 | |
| 146 | /* Ask the session daemon */ |
| 147 | ret = ask_sessiond(); |
| 148 | if (ret < 0) { |
| 149 | goto end; |
| 150 | } |
| 151 | |
| 152 | /* Unparse session ID */ |
| 153 | uuid_unparse(llm.session_id, session_id); |
| 154 | |
| 155 | end: |
| 156 | reset_data_struct(); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * lttng_ust_list_apps |
| 163 | * |
| 164 | * Ask the session daemon for all UST traceable |
| 165 | * applications. |
| 166 | * |
| 167 | * Return the size of pids. |
| 168 | */ |
| 169 | size_t lttng_ust_list_apps(pid_t **pids) |
| 170 | { |
| 171 | int ret, first = 0; |
| 172 | size_t size = 0; |
| 173 | pid_t *p = NULL; |
| 174 | |
| 175 | lsm.cmd_type = UST_LIST_APPS; |
| 176 | |
| 177 | ret = ask_sessiond(); |
| 178 | if (ret < 0) { |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | do { |
| 183 | ret = recvfrom_sessiond(); |
| 184 | if (ret < 0) { |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | if (first == 0) { |
| 189 | first = 1; |
| 190 | size = llm.num_pckt; |
| 191 | p = malloc(sizeof(pid_t) * size); |
| 192 | } |
| 193 | p[size - llm.num_pckt] = llm.u.list_apps.pid; |
| 194 | } while ((llm.num_pckt-1) != 0); |
| 195 | |
| 196 | *pids = p; |
| 197 | |
| 198 | return size; |
| 199 | |
| 200 | error: |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * lttng_list_sessions |
| 206 | * |
| 207 | * Ask the session daemon for all available sessions. |
| 208 | * |
| 209 | * Return number of sessions |
| 210 | */ |
| 211 | size_t lttng_list_sessions(struct lttng_session **sessions) |
| 212 | { |
| 213 | int ret, first = 0; |
| 214 | size_t size = 0; |
| 215 | struct lttng_session *ls = NULL; |
| 216 | |
| 217 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
| 218 | |
| 219 | ret = ask_sessiond(); |
| 220 | if (ret < 0) { |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | do { |
| 225 | ret = recvfrom_sessiond(); |
| 226 | if (ret < 0) { |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | if (first == 0) { |
| 231 | first = 1; |
| 232 | size = llm.num_pckt; |
| 233 | ls = malloc(sizeof(struct lttng_session) * size); |
| 234 | } |
| 235 | strncpy(ls[size - llm.num_pckt].name, llm.u.list_sessions.name, |
| 236 | sizeof(ls[size - llm.num_pckt].name)); |
| 237 | strncpy(ls[size - llm.num_pckt].uuid, llm.u.list_sessions.uuid, |
| 238 | sizeof(ls[size - llm.num_pckt].uuid)); |
| 239 | } while ((llm.num_pckt - 1) != 0); |
| 240 | |
| 241 | *sessions = ls; |
| 242 | |
| 243 | return size; |
| 244 | |
| 245 | error: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * lttng_connect_sessiond |
| 251 | * |
| 252 | * Connect to the LTTng session daemon. |
| 253 | * On success, return 0 |
| 254 | * On error, return a negative value |
| 255 | */ |
| 256 | int lttng_connect_sessiond(void) |
| 257 | { |
| 258 | int ret; |
| 259 | |
| 260 | ret = set_session_daemon_path(); |
| 261 | if (ret < 0) { |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* Connect to the sesssion daemon */ |
| 266 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
| 267 | if (ret < 0) { |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | sessiond_socket = ret; |
| 272 | connected = 1; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * lttng_set_tracing_group |
| 279 | * |
| 280 | * Set tracing group variable with name. This function |
| 281 | * allocate memory pointed by tracing_group. |
| 282 | */ |
| 283 | int lttng_set_tracing_group(const char *name) |
| 284 | { |
| 285 | if (asprintf(&tracing_group, "%s", name) < 0) { |
| 286 | return -ENOMEM; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * lttng_check_session_daemon |
| 294 | * |
| 295 | * Return 0 if a sesssion daemon is available |
| 296 | * else return -1 |
| 297 | */ |
| 298 | int lttng_check_session_daemon(void) |
| 299 | { |
| 300 | int ret; |
| 301 | |
| 302 | ret = set_session_daemon_path(); |
| 303 | if (ret < 0) { |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | /* If socket exist, we consider the daemon started */ |
| 308 | ret = access(sessiond_sock_path, F_OK); |
| 309 | if (ret < 0) { |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * reset_data_struct |
| 318 | * |
| 319 | * Reset session daemon structures. |
| 320 | */ |
| 321 | static void reset_data_struct(void) |
| 322 | { |
| 323 | memset(&lsm, 0, sizeof(lsm)); |
| 324 | memset(&llm, 0, sizeof(llm)); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * set_session_daemon_path |
| 329 | * |
| 330 | * Set sessiond socket path by putting it in |
| 331 | * the global sessiond_sock_path variable. |
| 332 | */ |
| 333 | static int set_session_daemon_path(void) |
| 334 | { |
| 335 | int ret; |
| 336 | |
| 337 | /* Are we in the tracing group ? */ |
| 338 | ret = check_tracing_group(tracing_group); |
| 339 | if (ret < 0) { |
| 340 | if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK, |
| 341 | getenv("HOME")) < 0) { |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | } else { |
| 345 | strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, |
| 346 | sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * check_tracing_group |
| 354 | * |
| 355 | * Check if the specified group name exist. |
| 356 | * If yes, 0, else -1 |
| 357 | */ |
| 358 | static int check_tracing_group(const char *grp_name) |
| 359 | { |
| 360 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ |
| 361 | gid_t *grp_list; |
| 362 | int grp_list_size, grp_id, i; |
| 363 | int ret = -1; |
| 364 | |
| 365 | /* Get GID of group 'tracing' */ |
| 366 | grp_tracing = getgrnam(grp_name); |
| 367 | if (grp_tracing == NULL) { |
| 368 | /* NULL means not found also. getgrnam(3) */ |
| 369 | if (errno != 0) { |
| 370 | perror("getgrnam"); |
| 371 | } |
| 372 | goto end; |
| 373 | } |
| 374 | |
| 375 | /* Get number of supplementary group IDs */ |
| 376 | grp_list_size = getgroups(0, NULL); |
| 377 | if (grp_list_size < 0) { |
| 378 | perror("getgroups"); |
| 379 | goto end; |
| 380 | } |
| 381 | |
| 382 | /* Alloc group list of the right size */ |
| 383 | grp_list = malloc(grp_list_size * sizeof(gid_t)); |
| 384 | grp_id = getgroups(grp_list_size, grp_list); |
| 385 | if (grp_id < -1) { |
| 386 | perror("getgroups"); |
| 387 | goto free_list; |
| 388 | } |
| 389 | |
| 390 | for (i = 0; i < grp_list_size; i++) { |
| 391 | if (grp_list[i] == grp_tracing->gr_gid) { |
| 392 | ret = 0; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | free_list: |
| 398 | free(grp_list); |
| 399 | |
| 400 | end: |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * lib constructor |
| 406 | */ |
| 407 | static void __attribute__((constructor)) init() |
| 408 | { |
| 409 | /* Set default session group */ |
| 410 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
| 411 | } |