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 send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
102 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
105 DBG("message successfully sent");
108 if (errno
== ECONNRESET
) {
109 printf("remote end closed connection\n");
114 printf("incorrect message size: %zd\n", len
);
120 int handle_message(int sock
, struct lttcomm_ust_msg
*lum
)
124 pthread_mutex_lock(<tng_ust_comm_mutex
);
126 if (lttng_ust_comm_should_quit
) {
131 switch (lum
->cmd_type
) {
132 case UST_CREATE_SESSION
:
134 struct lttcomm_ust_reply lur
;
136 DBG("Handling create session message");
137 memset(&lur
, 0, sizeof(lur
));
138 lur
.cmd_type
= UST_CREATE_SESSION
;
139 ret
= lttng_abi_create_session();
142 lur
.ret_code
= LTTCOMM_OK
;
144 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
146 ret
= send_reply(sock
, &lur
);
151 struct lttcomm_ust_reply lur
;
153 DBG("Handling release message, handle: %d",
155 memset(&lur
, 0, sizeof(lur
));
156 lur
.cmd_type
= UST_RELEASE
;
157 ret
= objd_unref(lum
->handle
);
159 lur
.ret_code
= LTTCOMM_OK
;
161 lur
.ret_code
= LTTCOMM_ERR
;
163 ret
= send_reply(sock
, &lur
);
167 ERR("Unimplemented command %d", (int) lum
->cmd_type
);
172 pthread_mutex_unlock(<tng_ust_comm_mutex
);
177 * This thread does not allocate any resource, except within
178 * handle_message, within mutex protection. This mutex protects against
180 * The other moment it allocates resources is at socket connexion, which
181 * is also protected by the mutex.
184 void *ust_listener_thread(void *arg
)
186 struct sock_info
*sock_info
= arg
;
189 /* Restart trying to connect to the session daemon */
191 pthread_mutex_lock(<tng_ust_comm_mutex
);
193 if (lttng_ust_comm_should_quit
) {
194 pthread_mutex_unlock(<tng_ust_comm_mutex
);
198 if (sock_info
->socket
!= -1) {
199 ret
= close(sock_info
->socket
);
201 ERR("Error closing local apps socket");
203 sock_info
->socket
= -1;
205 /* Check for sessiond availability with pipe TODO */
208 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
210 ERR("Error connecting to global apps socket");
211 pthread_mutex_unlock(<tng_ust_comm_mutex
);
215 sock_info
->socket
= sock
= ret
;
216 pthread_mutex_unlock(<tng_ust_comm_mutex
);
219 ret
= register_app_to_sessiond(sock
);
221 ERR("Error registering app to local apps socket");
227 struct lttcomm_ust_msg lum
;
229 /* Receive session handle */
230 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
232 case 0: /* orderly shutdown */
233 DBG("ltt-sessiond has performed an orderly shutdown\n");
236 DBG("message received\n");
237 ret
= handle_message(sock
, &lum
);
239 ERR("Error handling message\n");
243 if (errno
== ECONNRESET
) {
244 ERR("remote end closed connection\n");
249 ERR("incorrect message size: %zd\n", len
);
255 goto restart
; /* try to reconnect */
262 * sessiond monitoring thread: monitor presence of global and per-user
263 * sessiond by polling the application common named pipe.
267 void __attribute__((constructor
)) lttng_ust_comm_init(void)
273 ret
= setup_local_apps_socket();
275 ERR("Error setting up to local apps socket");
278 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
279 ust_listener_thread
, &global_apps
);
281 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
282 ust_listener_thread
, &local_apps
);
285 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
290 * Using pthread_cancel here because:
291 * A) we don't want to hang application teardown.
292 * B) the thread is not allocating any resource.
296 * Require the communication thread to quit. Synchronize with
297 * mutexes to ensure it is not in a mutex critical section when
298 * pthread_cancel is later called.
300 pthread_mutex_lock(<tng_ust_comm_mutex
);
301 lttng_ust_comm_should_quit
= 1;
302 pthread_mutex_unlock(<tng_ust_comm_mutex
);
305 ret
= pthread_cancel(global_apps
.ust_listener
);
307 ERR("Error cancelling global ust listener thread");
310 if (global_apps
.socket
!= -1) {
311 ret
= close(global_apps
.socket
);
315 ret
= pthread_cancel(local_apps
.ust_listener
);
317 ERR("Error cancelling local ust listener thread");
320 if (local_apps
.socket
!= -1) {
321 ret
= close(local_apps
.socket
);
325 lttng_ust_abi_exit();