1 /* Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * 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 set_session_daemon_path(void);
44 static void reset_data_struct(void);
46 int lttng_connect_sessiond(void);
47 int lttng_create_session(const char *name
, char *session_id
);
48 int lttng_check_session_daemon(void);
51 static char *tracing_group
;
57 * Send lttcomm_session_msg to the daemon and wait
58 * for the reply. Data replied will be put in llm
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
));
77 ret
= lttcomm_recv_unix_sock(sessiond_socket
, &llm
, sizeof(llm
));
82 /* Check return code */
83 if (llm
.ret_code
!= LTTCOMM_OK
) {
95 * lttng_get_readable_code
97 * Return a human readable string of code
99 const char *lttng_get_readable_code(int code
)
101 if (code
> -LTTCOMM_OK
) {
102 return "Ended with errors";
105 return lttcomm_get_readable_code(code
);
109 * lttng_create_session
111 * Create a tracing session using "name" to the session daemon.
112 * If no name is given, the auto session creation is set and
113 * the daemon will take care of finding a name.
115 * On success, return 0 and session_id point to the uuid str.
116 * On error, negative value is returned.
118 int lttng_create_session(const char *name
, char *session_id
)
122 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
124 lsm
.u
.create_session
.auto_session
= 1;
126 strncpy(lsm
.session_name
, name
, strlen(name
));
127 lsm
.u
.create_session
.auto_session
= 0;
130 /* Ask the session daemon */
131 ret
= ask_sessiond();
136 /* Unparse session ID */
137 uuid_unparse(llm
.session_id
, session_id
);
146 * lttng_ust_list_apps
148 * Ask the session daemon for all UST traceable
151 * Return the size of pids.
153 size_t lttng_ust_list_apps(pid_t
**pids
)
157 lsm
.cmd_type
= UST_LIST_APPS
;
159 ret
= ask_sessiond();
164 *pids
= llm
.u
.list_apps
.pids
;
166 return llm
.u
.list_apps
.size
;
173 * lttng_connect_sessiond
175 * Connect to the LTTng session daemon.
176 * On success, return 0
177 * On error, return a negative value
179 int lttng_connect_sessiond(void)
183 ret
= set_session_daemon_path();
188 /* Connect to the sesssion daemon */
189 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
194 sessiond_socket
= ret
;
201 * lttng_set_tracing_group
203 * Set tracing group variable with name. This function
204 * allocate memory pointed by tracing_group.
206 int lttng_set_tracing_group(const char *name
)
208 if (asprintf(&tracing_group
, "%s", name
) < 0) {
216 * lttng_check_session_daemon
218 * Return 0 if a sesssion daemon is available
221 int lttng_check_session_daemon(void)
225 ret
= set_session_daemon_path();
230 /* If socket exist, we consider the daemon started */
231 ret
= access(sessiond_sock_path
, F_OK
);
242 * Reset session daemon structures.
244 static void reset_data_struct(void)
246 memset(&lsm
, 0, sizeof(lsm
));
247 memset(&llm
, 0, sizeof(llm
));
251 * set_session_daemon_path
253 * Set sessiond socket path by putting it in
254 * the global sessiond_sock_path variable.
256 static int set_session_daemon_path(void)
260 /* Are we in the tracing group ? */
261 ret
= check_tracing_group(tracing_group
);
263 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
264 getenv("HOME")) < 0) {
268 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
269 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
276 * check_tracing_group
278 * Check if the specified group name exist.
281 static int check_tracing_group(const char *grp_name
)
283 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
285 int grp_list_size
, grp_id
, i
;
288 /* Get GID of group 'tracing' */
289 grp_tracing
= getgrnam(grp_name
);
290 if (grp_tracing
== NULL
) {
291 /* NULL means not found also. getgrnam(3) */
298 /* Get number of supplementary group IDs */
299 grp_list_size
= getgroups(0, NULL
);
300 if (grp_list_size
< 0) {
305 /* Alloc group list of the right size */
306 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
307 grp_id
= getgroups(grp_list_size
, grp_list
);
313 for (i
= 0; i
< grp_list_size
; i
++) {
314 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
330 static void __attribute__((constructor
)) init()
332 /* Set default session group */
333 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);