4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <ust/lttng-ust-abi.h>
27 #include <lttng-ust-comm.h>
28 #include <ust/usterr-signal-safe.h>
33 * communication thread mutex. Held when handling a command, also held
34 * by fork() to deal with removal of threads, and by exit path.
36 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
38 /* Should the ust comm thread quit ? */
39 static int lttng_ust_comm_should_quit
;
42 * Info about socket and associated listener thread.
45 char sock_path
[PATH_MAX
];
47 pthread_t ust_listener
; /* listener thread */
50 /* Socket from app (connect) to session daemon (listen) for communication */
51 struct sock_info global_apps
= {
52 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
56 /* TODO: allow global_apps_sock_path override */
58 struct sock_info local_apps
= {
63 int setup_local_apps_socket(void)
67 home_dir
= (const char *) getenv("HOME");
70 snprintf(local_apps
.sock_path
, PATH_MAX
,
71 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
76 int register_app_to_sessiond(int socket
)
86 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
87 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
88 reg_msg
.pid
= getpid();
89 reg_msg
.uid
= getuid();
91 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
92 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
98 int handle_message(int sock
, struct lttcomm_ust_msg
*lum
)
103 pthread_mutex_lock(<tng_ust_comm_mutex
);
105 if (lttng_ust_comm_should_quit
) {
110 switch (lum
->cmd_type
) {
111 case UST_CREATE_SESSION
:
113 struct lttcomm_ust_reply lur
;
115 DBG("Handling create session message");
116 memset(&lur
, 0, sizeof(lur
));
117 lur
.cmd_type
= UST_CREATE_SESSION
;
119 ret
= lttng_abi_create_session();
122 lur
.ret_code
= LTTCOMM_OK
;
124 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
126 len
= lttcomm_send_unix_sock(sock
, &lur
, sizeof(lur
));
129 DBG("message successfully sent");
132 if (errno
== ECONNRESET
) {
133 printf("remote end closed connection\n");
140 printf("incorrect message size: %zd\n", len
);
148 struct lttcomm_ust_reply lur
;
150 DBG("Handling release message, handle: %d",
152 memset(&lur
, 0, sizeof(lur
));
153 lur
.cmd_type
= UST_RELEASE
;
155 ret
= objd_unref(lum
->handle
);
157 lur
.ret_code
= LTTCOMM_OK
;
159 lur
.ret_code
= LTTCOMM_ERR
;
161 len
= lttcomm_send_unix_sock(sock
, &lur
, sizeof(lur
));
164 DBG("message successfully sent\n");
167 if (errno
== ECONNRESET
) {
168 printf("remote end closed connection\n");
175 printf("incorrect message size: %zd\n", len
);
182 ERR("Unimplemented command %d", (int) lum
->cmd_type
);
187 pthread_mutex_unlock(<tng_ust_comm_mutex
);
192 * This thread does not allocate any resource, except within
193 * handle_message, within mutex protection. This mutex protects against
195 * The other moment it allocates resources is at socket connexion, which
196 * is also protected by the mutex.
199 void *ust_listener_thread(void *arg
)
201 struct sock_info
*sock_info
= arg
;
204 /* Restart trying to connect to the session daemon */
206 pthread_mutex_lock(<tng_ust_comm_mutex
);
208 if (lttng_ust_comm_should_quit
) {
209 pthread_mutex_unlock(<tng_ust_comm_mutex
);
213 if (sock_info
->socket
!= -1) {
214 ret
= close(sock_info
->socket
);
216 ERR("Error closing local apps socket");
218 sock_info
->socket
= -1;
220 /* Check for sessiond availability with pipe TODO */
223 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
225 ERR("Error connecting to global apps socket");
226 pthread_mutex_unlock(<tng_ust_comm_mutex
);
229 sock_info
->socket
= sock
= ret
;
230 pthread_mutex_unlock(<tng_ust_comm_mutex
);
233 ret
= register_app_to_sessiond(sock
);
235 ERR("Error registering app to local apps socket");
241 struct lttcomm_ust_msg lum
;
243 /* Receive session handle */
244 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
246 case 0: /* orderly shutdown */
247 DBG("ltt-sessiond has performed an orderly shutdown\n");
250 DBG("message received\n");
251 ret
= handle_message(sock
, &lum
);
253 ERR("Error handling message\n");
257 if (errno
== ECONNRESET
) {
258 ERR("remote end closed connection\n");
263 ERR("incorrect message size: %zd\n", len
);
269 goto restart
; /* try to reconnect */
276 * sessiond monitoring thread: monitor presence of global and per-user
277 * sessiond by polling the application common named pipe.
281 void __attribute__((constructor
)) lttng_ust_comm_init(void)
287 ret
= setup_local_apps_socket();
289 ERR("Error setting up to local apps socket");
292 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
293 ust_listener_thread
, &global_apps
);
295 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
296 ust_listener_thread
, &local_apps
);
299 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
304 * Using pthread_cancel here because:
305 * A) we don't want to hang application teardown.
306 * B) the thread is not allocating any resource.
310 * Require the communication thread to quit. Synchronize with
311 * mutexes to ensure it is not in a mutex critical section when
312 * pthread_cancel is later called.
314 pthread_mutex_lock(<tng_ust_comm_mutex
);
315 lttng_ust_comm_should_quit
= 1;
316 pthread_mutex_unlock(<tng_ust_comm_mutex
);
319 ret
= pthread_cancel(global_apps
.ust_listener
);
321 ERR("Error cancelling global ust listener thread");
324 if (global_apps
.socket
!= -1) {
325 ret
= close(global_apps
.socket
);
329 ret
= pthread_cancel(local_apps
.ust_listener
);
331 ERR("Error cancelling local ust listener thread");
334 if (local_apps
.socket
!= -1) {
335 ret
= close(local_apps
.socket
);
339 lttng_ust_abi_exit();