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_connect_sessiond
207 * Connect to the LTTng session daemon.
208 * On success, return 0
209 * On error, return a negative value
211 int lttng_connect_sessiond(void)
215 ret
= set_session_daemon_path();
220 /* Connect to the sesssion daemon */
221 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
226 sessiond_socket
= ret
;
233 * lttng_set_tracing_group
235 * Set tracing group variable with name. This function
236 * allocate memory pointed by tracing_group.
238 int lttng_set_tracing_group(const char *name
)
240 if (asprintf(&tracing_group
, "%s", name
) < 0) {
248 * lttng_check_session_daemon
250 * Return 0 if a sesssion daemon is available
253 int lttng_check_session_daemon(void)
257 ret
= set_session_daemon_path();
262 /* If socket exist, we consider the daemon started */
263 ret
= access(sessiond_sock_path
, F_OK
);
274 * Reset session daemon structures.
276 static void reset_data_struct(void)
278 memset(&lsm
, 0, sizeof(lsm
));
279 memset(&llm
, 0, sizeof(llm
));
283 * set_session_daemon_path
285 * Set sessiond socket path by putting it in
286 * the global sessiond_sock_path variable.
288 static int set_session_daemon_path(void)
292 /* Are we in the tracing group ? */
293 ret
= check_tracing_group(tracing_group
);
295 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
296 getenv("HOME")) < 0) {
300 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
301 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
308 * check_tracing_group
310 * Check if the specified group name exist.
313 static int check_tracing_group(const char *grp_name
)
315 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
317 int grp_list_size
, grp_id
, i
;
320 /* Get GID of group 'tracing' */
321 grp_tracing
= getgrnam(grp_name
);
322 if (grp_tracing
== NULL
) {
323 /* NULL means not found also. getgrnam(3) */
330 /* Get number of supplementary group IDs */
331 grp_list_size
= getgroups(0, NULL
);
332 if (grp_list_size
< 0) {
337 /* Alloc group list of the right size */
338 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
339 grp_id
= getgroups(grp_list_size
, grp_list
);
345 for (i
= 0; i
< grp_list_size
; i
++) {
346 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
362 static void __attribute__((constructor
)) init()
364 /* Set default session group */
365 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);