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_lttng_msg llm
;
38 static struct lttcomm_session_msg lsm
;
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);
47 int lttng_connect_sessiond(void);
48 int lttng_create_session(const char *name
, char *session_id
);
49 int lttng_check_session_daemon(void);
52 static char *tracing_group
;
58 * Send lttcomm_session_msg to the session daemon.
60 * On success, return 0
61 * On error, return error code
63 static int ask_sessiond(void)
72 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
81 * Receive data from the sessiond socket.
83 * On success, return 0
84 * On error, return recv() error code
86 static int recvfrom_sessiond(void)
95 ret
= lttcomm_recv_unix_sock(sessiond_socket
, &llm
, sizeof(llm
));
100 /* Check return code */
101 if (llm
.ret_code
!= LTTCOMM_OK
) {
111 * lttng_get_readable_code
113 * Return a human readable string of code
115 const char *lttng_get_readable_code(int code
)
117 if (code
> -LTTCOMM_OK
) {
118 return "Ended with errors";
121 return lttcomm_get_readable_code(code
);
125 * lttng_create_session
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.
131 * On success, return 0 and session_id point to the uuid str.
132 * On error, negative value is returned.
134 int lttng_create_session(const char *name
, char *session_id
)
138 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
140 lsm
.u
.create_session
.auto_session
= 1;
142 strncpy(lsm
.session_name
, name
, strlen(name
));
143 lsm
.u
.create_session
.auto_session
= 0;
146 /* Ask the session daemon */
147 ret
= ask_sessiond();
152 /* Unparse session ID */
153 uuid_unparse(llm
.session_id
, session_id
);
162 * lttng_ust_list_apps
164 * Ask the session daemon for all UST traceable
167 * Return the size of pids.
169 size_t lttng_ust_list_apps(pid_t
**pids
)
175 lsm
.cmd_type
= UST_LIST_APPS
;
177 ret
= ask_sessiond();
183 ret
= recvfrom_sessiond();
191 p
= malloc(sizeof(pid_t
) * size
);
193 p
[size
- llm
.num_pckt
] = llm
.u
.list_apps
.pid
;
194 } while ((llm
.num_pckt
-1) != 0);
205 * lttng_list_sessions
207 * Ask the session daemon for all available sessions.
209 * Return number of sessions
211 size_t lttng_list_sessions(struct lttng_session
**sessions
)
215 struct lttng_session
*ls
= NULL
;
217 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
219 ret
= ask_sessiond();
225 ret
= recvfrom_sessiond();
233 ls
= malloc(sizeof(struct lttng_session
) * size
);
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);
250 * lttng_connect_sessiond
252 * Connect to the LTTng session daemon.
253 * On success, return 0
254 * On error, return a negative value
256 int lttng_connect_sessiond(void)
260 ret
= set_session_daemon_path();
265 /* Connect to the sesssion daemon */
266 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
271 sessiond_socket
= ret
;
278 * lttng_set_tracing_group
280 * Set tracing group variable with name. This function
281 * allocate memory pointed by tracing_group.
283 int lttng_set_tracing_group(const char *name
)
285 if (asprintf(&tracing_group
, "%s", name
) < 0) {
293 * lttng_check_session_daemon
295 * Return 0 if a sesssion daemon is available
298 int lttng_check_session_daemon(void)
302 ret
= set_session_daemon_path();
307 /* If socket exist, we consider the daemon started */
308 ret
= access(sessiond_sock_path
, F_OK
);
319 * Reset session daemon structures.
321 static void reset_data_struct(void)
323 memset(&lsm
, 0, sizeof(lsm
));
324 memset(&llm
, 0, sizeof(llm
));
328 * set_session_daemon_path
330 * Set sessiond socket path by putting it in
331 * the global sessiond_sock_path variable.
333 static int set_session_daemon_path(void)
337 /* Are we in the tracing group ? */
338 ret
= check_tracing_group(tracing_group
);
340 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
341 getenv("HOME")) < 0) {
345 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
346 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
353 * check_tracing_group
355 * Check if the specified group name exist.
358 static int check_tracing_group(const char *grp_name
)
360 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
362 int grp_list_size
, grp_id
, i
;
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) */
375 /* Get number of supplementary group IDs */
376 grp_list_size
= getgroups(0, NULL
);
377 if (grp_list_size
< 0) {
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
);
390 for (i
= 0; i
< grp_list_size
; i
++) {
391 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
407 static void __attribute__((constructor
)) init()
409 /* Set default session group */
410 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);