2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
27 #include <lttng/liblttngctl.h>
29 #include "liblttsessiondcomm.h"
32 /* Socket to session daemon for communication */
33 static int sessiond_socket
;
34 static char sessiond_sock_path
[PATH_MAX
];
36 /* Communication structure to ltt-sessiond */
37 static struct lttcomm_session_msg lsm
;
38 static struct lttcomm_lttng_msg llm
;
41 static int check_tracing_group(const char *grp_name
);
42 static int ask_sessiond(enum lttcomm_command_type 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);
48 static char *tracing_group
;
54 * Send lttcomm_session_msg to the session daemon.
56 * On success, return 0
57 * On error, return error code
59 static int send_data_sessiond(void)
68 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
77 * Receive data from the sessiond socket.
79 * On success, return 0
80 * On error, return recv() error code
82 static int recv_data_sessiond(void *buf
, size_t len
)
91 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
103 * Ask the session daemon a specific command
104 * and put the data into buf.
106 * Return size of data (only payload, not header).
108 static int ask_sessiond(enum lttcomm_command_type lct
, void **buf
)
116 /* Send command to session daemon */
117 ret
= send_data_sessiond();
122 /* Get header from data transmission */
123 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
128 /* Check error code if OK */
129 if (llm
.ret_code
!= LTTCOMM_OK
) {
134 size
= llm
.size_payload
;
139 data
= (void*) malloc(size
);
141 /* Get payload data */
142 ret
= recv_data_sessiond(data
, size
);
151 /* Reset lsm data struct */
152 memset(&lsm
, 0, sizeof(lsm
));
157 * lttng_get_readable_code
159 * Return a human readable string of code
161 const char *lttng_get_readable_code(int code
)
163 if (code
> -LTTCOMM_OK
) {
164 return "Ended with errors";
167 return lttcomm_get_readable_code(code
);
171 * lttng_ust_create_trace
173 * Request a trace creation for pid.
175 int lttng_ust_create_trace(pid_t pid
)
180 ret
= ask_sessiond(UST_CREATE_TRACE
, NULL
);
186 * lttng_ust_list_apps
188 * Ask the session daemon for all UST traceable
191 * Return the number of pids.
192 * On error, return negative value.
194 int lttng_ust_list_apps(pid_t
**pids
)
198 ret
= ask_sessiond(UST_LIST_APPS
, (void**) pids
);
203 return ret
/ sizeof(pid_t
);
207 * lttng_create_session
209 * Create a brand new session using name. Allocate
210 * the session_id param pointing to the UUID.
212 int lttng_create_session(char *name
, uuid_t
*session_id
)
216 strncpy(lsm
.session_name
, name
, sizeof(lsm
.session_name
));
217 lsm
.session_name
[sizeof(lsm
.session_name
) - 1] = '\0';
219 ret
= ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
224 uuid_copy(*session_id
, llm
.session_id
);
231 * lttng_destroy_session
233 * Destroy session using name.
235 int lttng_destroy_session(uuid_t
*uuid
)
239 uuid_copy(lsm
.session_id
, *uuid
);
241 ret
= ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
251 * lttng_list_sessions
253 * Ask the session daemon for all available sessions.
255 * Return number of session.
256 * On error, return negative value.
258 int lttng_list_sessions(struct lttng_session
**sessions
)
262 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
267 return ret
/ sizeof(struct lttng_session
);
271 * lttng_connect_sessiond
273 * Connect to the LTTng session daemon.
274 * On success, return 0
275 * On error, return a negative value
277 int lttng_connect_sessiond(void)
281 ret
= set_session_daemon_path();
286 /* Connect to the sesssion daemon */
287 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
292 sessiond_socket
= ret
;
299 * lttng_disconnect_sessiond
301 * Clean disconnect the session daemon.
303 int lttng_disconnect_sessiond(void)
308 ret
= lttcomm_close_unix_sock(sessiond_socket
);
317 * lttng_set_current_session_uuid
319 * Set the session uuid for current lsm.
321 void lttng_set_current_session_uuid(char *uuid
)
323 uuid_parse(uuid
, lsm
.session_id
);
327 * lttng_set_tracing_group
329 * Set tracing group variable with name. This function
330 * allocate memory pointed by tracing_group.
332 int lttng_set_tracing_group(const char *name
)
334 if (asprintf(&tracing_group
, "%s", name
) < 0) {
342 * lttng_check_session_daemon
344 * Return 0 if a sesssion daemon is available
347 int lttng_check_session_daemon(void)
351 ret
= set_session_daemon_path();
356 /* If socket exist, we consider the daemon started */
357 ret
= access(sessiond_sock_path
, F_OK
);
366 * set_session_daemon_path
368 * Set sessiond socket path by putting it in
369 * the global sessiond_sock_path variable.
371 static int set_session_daemon_path(void)
375 /* Are we in the tracing group ? */
376 ret
= check_tracing_group(tracing_group
);
378 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
379 getenv("HOME")) < 0) {
383 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
384 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
391 * check_tracing_group
393 * Check if the specified group name exist.
396 static int check_tracing_group(const char *grp_name
)
398 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
400 int grp_list_size
, grp_id
, i
;
403 /* Get GID of group 'tracing' */
404 grp_tracing
= getgrnam(grp_name
);
405 if (grp_tracing
== NULL
) {
406 /* NULL means not found also. getgrnam(3) */
413 /* Get number of supplementary group IDs */
414 grp_list_size
= getgroups(0, NULL
);
415 if (grp_list_size
< 0) {
420 /* Alloc group list of the right size */
421 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
422 grp_id
= getgroups(grp_list_size
, grp_list
);
428 for (i
= 0; i
< grp_list_size
; i
++) {
429 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
445 static void __attribute__((constructor
)) init()
447 /* Set default session group */
448 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);