a9f5797df671cc8af4ff5e2f01f4408ab0143360
[lttng-ust.git] / libust / lttng-ust-comm.c
1 /*
2 * lttng-ust-comm.c
3 *
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <ust/lttng-ust-abi.h>
27 #include <lttng-ust-comm.h>
28 #include <ust/usterr-signal-safe.h>
29 #include <pthread.h>
30 #include <semaphore.h>
31 #include <time.h>
32 #include <assert.h>
33
34 /*
35 * communication thread mutex. Held when handling a command, also held
36 * by fork() to deal with removal of threads, and by exit path.
37 */
38 static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER;
39
40 /* Should the ust comm thread quit ? */
41 static int lttng_ust_comm_should_quit;
42
43 /*
44 * Wait for either of these before continuing to the main
45 * program:
46 * - the register_done message from sessiond daemon
47 * (will let the sessiond daemon enable sessions before main
48 * starts.)
49 * - sessiond daemon is not reachable.
50 * - timeout (ensuring applications are resilient to session
51 * daemon problems).
52 */
53 static sem_t constructor_wait;
54
55 /*
56 * Info about socket and associated listener thread.
57 */
58 struct sock_info {
59 const char *name;
60 char sock_path[PATH_MAX];
61 int socket;
62 pthread_t ust_listener; /* listener thread */
63 int root_handle;
64 };
65
66 /* Socket from app (connect) to session daemon (listen) for communication */
67 struct sock_info global_apps = {
68 .name = "global",
69 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
70 .socket = -1,
71 .root_handle = -1,
72 };
73
74 /* TODO: allow global_apps_sock_path override */
75
76 struct sock_info local_apps = {
77 .name = "local",
78 .socket = -1,
79 .root_handle = -1,
80 };
81
82 static
83 int setup_local_apps_socket(void)
84 {
85 const char *home_dir;
86
87 home_dir = (const char *) getenv("HOME");
88 if (!home_dir)
89 return -ENOENT;
90 snprintf(local_apps.sock_path, PATH_MAX,
91 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
92 return 0;
93 }
94
95 static
96 int register_app_to_sessiond(int socket)
97 {
98 ssize_t ret;
99 struct {
100 uint32_t major;
101 uint32_t minor;
102 pid_t pid;
103 uid_t uid;
104 } reg_msg;
105
106 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
107 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
108 reg_msg.pid = getpid();
109 reg_msg.uid = getuid();
110
111 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
112 if (ret >= 0 && ret != sizeof(reg_msg))
113 return -EIO;
114 return ret;
115 }
116
117 static
118 int send_reply(int sock, struct lttcomm_ust_reply *lur)
119 {
120 ssize_t len;
121
122 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
123 switch (len) {
124 case sizeof(*lur):
125 DBG("message successfully sent");
126 return 0;
127 case -1:
128 if (errno == ECONNRESET) {
129 printf("remote end closed connection\n");
130 return 0;
131 }
132 return -1;
133 default:
134 printf("incorrect message size: %zd\n", len);
135 return -1;
136 }
137 }
138
139 static
140 int handle_register_done(void)
141 {
142 int ret;
143
144 ret = sem_post(&constructor_wait);
145 assert(!ret);
146 return 0;
147 }
148
149 static
150 int handle_message(struct sock_info *sock_info,
151 int sock, struct lttcomm_ust_msg *lum)
152 {
153 int ret = 0;
154 const struct objd_ops *ops;
155 struct lttcomm_ust_reply lur;
156
157 pthread_mutex_lock(&lttng_ust_comm_mutex);
158
159 memset(&lur, 0, sizeof(lur));
160
161 if (lttng_ust_comm_should_quit) {
162 ret = -EPERM;
163 goto end;
164 }
165
166 ops = objd_ops(lum->handle);
167 if (!ops) {
168 ret = -ENOENT;
169 goto end;
170 }
171
172 switch (lum->cmd) {
173 case LTTNG_UST_REGISTER_DONE:
174 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
175 ret = handle_register_done();
176 else
177 ret = -EINVAL;
178 break;
179 case LTTNG_UST_RELEASE:
180 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
181 ret = -EPERM;
182 else
183 ret = objd_unref(lum->handle);
184 break;
185 default:
186 if (ops->cmd)
187 ret = ops->cmd(lum->handle, lum->cmd,
188 (unsigned long) &lum->u);
189 else
190 ret = -ENOSYS;
191 break;
192 }
193
194 end:
195 lur.handle = lum->handle;
196 lur.cmd = lum->cmd;
197 lur.ret_val = ret;
198 if (ret >= 0) {
199 lur.ret_code = LTTCOMM_OK;
200 } else {
201 lur.ret_code = LTTCOMM_SESSION_FAIL;
202 }
203 ret = send_reply(sock, &lur);
204
205 pthread_mutex_unlock(&lttng_ust_comm_mutex);
206 return ret;
207 }
208
209 static
210 void cleanup_sock_info(struct sock_info *sock_info)
211 {
212 int ret;
213
214 if (sock_info->socket != -1) {
215 ret = close(sock_info->socket);
216 if (ret) {
217 ERR("Error closing local apps socket");
218 }
219 sock_info->socket = -1;
220 }
221 if (sock_info->root_handle != -1) {
222 ret = objd_unref(sock_info->root_handle);
223 if (ret) {
224 ERR("Error unref root handle");
225 }
226 sock_info->root_handle = -1;
227 }
228 }
229
230 /*
231 * This thread does not allocate any resource, except within
232 * handle_message, within mutex protection. This mutex protects against
233 * fork and exit.
234 * The other moment it allocates resources is at socket connexion, which
235 * is also protected by the mutex.
236 */
237 static
238 void *ust_listener_thread(void *arg)
239 {
240 struct sock_info *sock_info = arg;
241 int sock, ret;
242
243 /* Restart trying to connect to the session daemon */
244 restart:
245 pthread_mutex_lock(&lttng_ust_comm_mutex);
246
247 if (lttng_ust_comm_should_quit) {
248 pthread_mutex_unlock(&lttng_ust_comm_mutex);
249 goto quit;
250 }
251
252 if (sock_info->socket != -1) {
253 ret = close(sock_info->socket);
254 if (ret) {
255 ERR("Error closing %s apps socket", sock_info->name);
256 }
257 sock_info->socket = -1;
258 }
259
260 /* Check for sessiond availability with pipe TODO */
261
262 /* Register */
263 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
264 if (ret < 0) {
265 ERR("Error connecting to %s apps socket", sock_info->name);
266 /*
267 * If we cannot find the sessiond daemon, don't delay
268 * constructor execution.
269 */
270 ret = handle_register_done();
271 assert(!ret);
272 pthread_mutex_unlock(&lttng_ust_comm_mutex);
273 sleep(5);
274 goto restart;
275 }
276
277 sock_info->socket = sock = ret;
278
279 /*
280 * Create only one root handle per listener thread for the whole
281 * process lifetime.
282 */
283 if (sock_info->root_handle == -1) {
284 ret = lttng_abi_create_root_handle();
285 if (ret) {
286 ERR("Error creating root handle");
287 pthread_mutex_unlock(&lttng_ust_comm_mutex);
288 goto quit;
289 }
290 sock_info->root_handle = ret;
291 }
292
293 ret = register_app_to_sessiond(sock);
294 if (ret < 0) {
295 ERR("Error registering to %s apps socket", sock_info->name);
296 /*
297 * If we cannot register to the sessiond daemon, don't
298 * delay constructor execution.
299 */
300 ret = handle_register_done();
301 assert(!ret);
302 pthread_mutex_unlock(&lttng_ust_comm_mutex);
303 sleep(5);
304 goto restart;
305 }
306 pthread_mutex_unlock(&lttng_ust_comm_mutex);
307
308 for (;;) {
309 ssize_t len;
310 struct lttcomm_ust_msg lum;
311
312 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
313 switch (len) {
314 case 0: /* orderly shutdown */
315 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
316 goto end;
317 case sizeof(lum):
318 DBG("message received\n");
319 ret = handle_message(sock_info, sock, &lum);
320 if (ret < 0) {
321 ERR("Error handling message for %s socket", sock_info->name);
322 }
323 continue;
324 case -1:
325 if (errno == ECONNRESET) {
326 ERR("%s remote end closed connection\n", sock_info->name);
327 goto end;
328 }
329 goto end;
330 default:
331 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
332 continue;
333 }
334
335 }
336 end:
337 goto restart; /* try to reconnect */
338 quit:
339 return NULL;
340 }
341
342 static
343 int get_timeout(struct timespec *constructor_timeout)
344 {
345 struct timespec constructor_delay =
346 {
347 .tv_sec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_S,
348 .tv_nsec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_NS,
349 };
350 struct timespec realtime;
351 int ret;
352
353 ret = clock_gettime(CLOCK_REALTIME, &realtime);
354 if (ret)
355 return ret;
356
357 constructor_timeout->tv_sec =
358 realtime.tv_sec + constructor_delay.tv_sec;
359 constructor_timeout->tv_nsec =
360 constructor_delay.tv_nsec + realtime.tv_nsec;
361 if (constructor_timeout->tv_nsec >= 1000000000UL) {
362 constructor_timeout->tv_sec++;
363 constructor_timeout->tv_nsec -= 1000000000UL;
364 }
365 return 0;
366 }
367
368 /*
369 * sessiond monitoring thread: monitor presence of global and per-user
370 * sessiond by polling the application common named pipe.
371 */
372 /* TODO */
373
374 void __attribute__((constructor)) lttng_ust_comm_init(void)
375 {
376 struct timespec constructor_timeout;
377 int ret;
378
379 init_usterr();
380
381 ret = get_timeout(&constructor_timeout);
382 assert(!ret);
383
384 ret = sem_init(&constructor_wait, 0, 2);
385 assert(!ret);
386
387 ret = setup_local_apps_socket();
388 if (ret) {
389 ERR("Error setting up to local apps socket");
390 }
391
392 /*
393 * Wait for the pthread cond to let us continue to main program
394 * execution. Hold mutex across thread creation, so we start
395 * waiting for the condition before the threads can signal its
396 * completion.
397 */
398 pthread_mutex_lock(&lttng_ust_comm_mutex);
399 ret = pthread_create(&global_apps.ust_listener, NULL,
400 ust_listener_thread, &global_apps);
401 ret = pthread_create(&local_apps.ust_listener, NULL,
402 ust_listener_thread, &local_apps);
403
404 ret = sem_timedwait(&constructor_wait, &constructor_timeout);
405 if (ret < 0 && errno == ETIMEDOUT) {
406 ERR("Timed out waiting for ltt-sessiond");
407 } else {
408 assert(!ret);
409 }
410 pthread_mutex_unlock(&lttng_ust_comm_mutex);
411
412 }
413
414 void __attribute__((destructor)) lttng_ust_comm_exit(void)
415 {
416 int ret;
417
418 /*
419 * Using pthread_cancel here because:
420 * A) we don't want to hang application teardown.
421 * B) the thread is not allocating any resource.
422 */
423
424 /*
425 * Require the communication thread to quit. Synchronize with
426 * mutexes to ensure it is not in a mutex critical section when
427 * pthread_cancel is later called.
428 */
429 pthread_mutex_lock(&lttng_ust_comm_mutex);
430 lttng_ust_comm_should_quit = 1;
431 pthread_mutex_unlock(&lttng_ust_comm_mutex);
432
433 #if 0
434 ret = pthread_cancel(global_apps.ust_listener);
435 if (ret) {
436 ERR("Error cancelling global ust listener thread");
437 }
438 #endif //0
439
440 cleanup_sock_info(&global_apps);
441
442 ret = pthread_cancel(local_apps.ust_listener);
443 if (ret) {
444 ERR("Error cancelling local ust listener thread");
445 }
446
447 cleanup_sock_info(&local_apps);
448
449 lttng_ust_abi_exit();
450 ltt_events_exit();
451 }
This page took 0.047159 seconds and 4 git commands to generate.