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
;
40 static int check_tracing_group(const char *grp_name
);
41 static int ask_sessiond(enum lttcomm_command_type lct
, void **buf
);
42 static int recv_data_sessiond(void *buf
, size_t len
);
43 static int send_data_sessiond(void);
44 static int set_session_daemon_path(void);
47 static char *tracing_group
;
53 * Send lttcomm_session_msg to the session daemon.
55 * On success, return 0
56 * On error, return error code
58 static int send_data_sessiond(void)
67 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
76 * Receive data from the sessiond socket.
78 * On success, return 0
79 * On error, return recv() error code
81 static int recv_data_sessiond(void *buf
, size_t len
)
90 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
102 * Ask the session daemon a specific command
103 * and put the data into buf.
105 * Return size of data (only payload, not header).
107 static int ask_sessiond(enum lttcomm_command_type lct
, void **buf
)
112 struct lttcomm_lttng_msg llm
;
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_list_apps
173 * Ask the session daemon for all UST traceable
176 * Return the number of pids.
177 * On error, return negative value.
179 int lttng_ust_list_apps(pid_t
**pids
)
183 ret
= ask_sessiond(UST_LIST_APPS
, (void**) pids
);
188 return ret
/ sizeof(pid_t
);
192 * lttng_list_sessions
194 * Ask the session daemon for all available sessions.
196 * Return number of session.
197 * On error, return negative value.
199 int lttng_list_sessions(struct lttng_session
**sessions
)
203 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
208 return ret
/ sizeof(struct lttng_session
);
212 * lttng_connect_sessiond
214 * Connect to the LTTng session daemon.
215 * On success, return 0
216 * On error, return a negative value
218 int lttng_connect_sessiond(void)
222 ret
= set_session_daemon_path();
227 /* Connect to the sesssion daemon */
228 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
233 sessiond_socket
= ret
;
240 * lttng_set_tracing_group
242 * Set tracing group variable with name. This function
243 * allocate memory pointed by tracing_group.
245 int lttng_set_tracing_group(const char *name
)
247 if (asprintf(&tracing_group
, "%s", name
) < 0) {
255 * lttng_check_session_daemon
257 * Return 0 if a sesssion daemon is available
260 int lttng_check_session_daemon(void)
264 ret
= set_session_daemon_path();
269 /* If socket exist, we consider the daemon started */
270 ret
= access(sessiond_sock_path
, F_OK
);
279 * set_session_daemon_path
281 * Set sessiond socket path by putting it in
282 * the global sessiond_sock_path variable.
284 static int set_session_daemon_path(void)
288 /* Are we in the tracing group ? */
289 ret
= check_tracing_group(tracing_group
);
291 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
292 getenv("HOME")) < 0) {
296 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
297 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
304 * check_tracing_group
306 * Check if the specified group name exist.
309 static int check_tracing_group(const char *grp_name
)
311 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
313 int grp_list_size
, grp_id
, i
;
316 /* Get GID of group 'tracing' */
317 grp_tracing
= getgrnam(grp_name
);
318 if (grp_tracing
== NULL
) {
319 /* NULL means not found also. getgrnam(3) */
326 /* Get number of supplementary group IDs */
327 grp_list_size
= getgroups(0, NULL
);
328 if (grp_list_size
< 0) {
333 /* Alloc group list of the right size */
334 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
335 grp_id
= getgroups(grp_list_size
, grp_list
);
341 for (i
= 0; i
< grp_list_size
; i
++) {
342 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
358 static void __attribute__((constructor
)) init()
360 /* Set default session group */
361 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);