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 */
51 /* Socket from app (connect) to session daemon (listen) for communication */
52 struct sock_info global_apps
= {
53 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
58 /* TODO: allow global_apps_sock_path override */
60 struct sock_info local_apps
= {
66 int setup_local_apps_socket(void)
70 home_dir
= (const char *) getenv("HOME");
73 snprintf(local_apps
.sock_path
, PATH_MAX
,
74 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
79 int register_app_to_sessiond(int socket
)
89 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
90 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
91 reg_msg
.pid
= getpid();
92 reg_msg
.uid
= getuid();
94 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
95 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
101 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
105 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
108 DBG("message successfully sent");
111 if (errno
== ECONNRESET
) {
112 printf("remote end closed connection\n");
117 printf("incorrect message size: %zd\n", len
);
123 int handle_message(int sock
, struct lttcomm_ust_msg
*lum
)
126 const struct objd_ops
*ops
;
127 struct lttcomm_ust_reply lur
;
129 pthread_mutex_lock(<tng_ust_comm_mutex
);
131 memset(&lur
, 0, sizeof(lur
));
133 if (lttng_ust_comm_should_quit
) {
138 ops
= objd_ops(lum
->handle
);
145 case LTTNG_UST_RELEASE
:
146 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
149 ret
= objd_unref(lum
->handle
);
153 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
154 (unsigned long) &lum
->u
);
161 lur
.handle
= lum
->handle
;
165 lur
.ret_code
= LTTCOMM_OK
;
167 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
169 ret
= send_reply(sock
, &lur
);
171 pthread_mutex_unlock(<tng_ust_comm_mutex
);
176 void cleanup_sock_info(struct sock_info
*sock_info
)
180 if (sock_info
->socket
!= -1) {
181 ret
= close(sock_info
->socket
);
183 ERR("Error closing local apps socket");
185 sock_info
->socket
= -1;
187 if (sock_info
->root_handle
!= -1) {
188 ret
= objd_unref(sock_info
->root_handle
);
190 ERR("Error unref root handle");
192 sock_info
->root_handle
= -1;
197 * This thread does not allocate any resource, except within
198 * handle_message, within mutex protection. This mutex protects against
200 * The other moment it allocates resources is at socket connexion, which
201 * is also protected by the mutex.
204 void *ust_listener_thread(void *arg
)
206 struct sock_info
*sock_info
= arg
;
209 /* Restart trying to connect to the session daemon */
211 pthread_mutex_lock(<tng_ust_comm_mutex
);
213 if (lttng_ust_comm_should_quit
) {
214 pthread_mutex_unlock(<tng_ust_comm_mutex
);
218 if (sock_info
->socket
!= -1) {
219 ret
= close(sock_info
->socket
);
221 ERR("Error closing local apps socket");
223 sock_info
->socket
= -1;
226 /* Check for sessiond availability with pipe TODO */
229 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
231 ERR("Error connecting to global apps socket");
232 pthread_mutex_unlock(<tng_ust_comm_mutex
);
237 sock_info
->socket
= sock
= ret
;
240 * Create only one root handle per listener thread for the whole
243 if (sock_info
->root_handle
== -1) {
244 ret
= lttng_abi_create_root_handle();
246 ERR("Error creating root handle");
247 pthread_mutex_unlock(<tng_ust_comm_mutex
);
250 sock_info
->root_handle
= ret
;
253 ret
= register_app_to_sessiond(sock
);
255 ERR("Error registering app to local apps socket");
256 pthread_mutex_unlock(<tng_ust_comm_mutex
);
260 pthread_mutex_unlock(<tng_ust_comm_mutex
);
264 struct lttcomm_ust_msg lum
;
266 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
268 case 0: /* orderly shutdown */
269 DBG("ltt-sessiond has performed an orderly shutdown\n");
272 DBG("message received\n");
273 ret
= handle_message(sock
, &lum
);
275 ERR("Error handling message\n");
279 if (errno
== ECONNRESET
) {
280 ERR("remote end closed connection\n");
285 ERR("incorrect message size: %zd\n", len
);
291 goto restart
; /* try to reconnect */
298 * sessiond monitoring thread: monitor presence of global and per-user
299 * sessiond by polling the application common named pipe.
303 void __attribute__((constructor
)) lttng_ust_comm_init(void)
309 ret
= setup_local_apps_socket();
311 ERR("Error setting up to local apps socket");
314 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
315 ust_listener_thread
, &global_apps
);
317 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
318 ust_listener_thread
, &local_apps
);
321 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
326 * Using pthread_cancel here because:
327 * A) we don't want to hang application teardown.
328 * B) the thread is not allocating any resource.
332 * Require the communication thread to quit. Synchronize with
333 * mutexes to ensure it is not in a mutex critical section when
334 * pthread_cancel is later called.
336 pthread_mutex_lock(<tng_ust_comm_mutex
);
337 lttng_ust_comm_should_quit
= 1;
338 pthread_mutex_unlock(<tng_ust_comm_mutex
);
341 ret
= pthread_cancel(global_apps
.ust_listener
);
343 ERR("Error cancelling global ust listener thread");
347 cleanup_sock_info(&global_apps
);
349 ret
= pthread_cancel(local_apps
.ust_listener
);
351 ERR("Error cancelling local ust listener thread");
354 cleanup_sock_info(&local_apps
);
356 lttng_ust_abi_exit();