ust comm: more resilient handle table, fix size of reply
[lttng-ust.git] / libust / lttng-ust-comm.c
CommitLineData
2691221a
MD
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>
b35d179d
MD
26#include <ust/lttng-ust-abi.h>
27#include <lttng-ust-comm.h>
2691221a 28#include <ust/usterr-signal-safe.h>
d9e99d10 29#include <pthread.h>
1ea11eab
MD
30#include <assert.h>
31
32/*
33 * communication thread mutex. Held when handling a command, also held
34 * by fork() to deal with removal of threads, and by exit path.
35 */
36static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38/* Should the ust comm thread quit ? */
39static int lttng_ust_comm_should_quit;
40
41/*
42 * Info about socket and associated listener thread.
43 */
44struct sock_info {
45 char sock_path[PATH_MAX];
46 int socket;
47 pthread_t ust_listener; /* listener thread */
48};
2691221a
MD
49
50/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab
MD
51struct sock_info global_apps = {
52 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
53 .socket = -1,
54};
2691221a
MD
55
56/* TODO: allow global_apps_sock_path override */
57
1ea11eab
MD
58struct sock_info local_apps = {
59 .socket = -1,
60};
2691221a
MD
61
62static
9eb62b9c 63int setup_local_apps_socket(void)
2691221a
MD
64{
65 const char *home_dir;
2691221a
MD
66
67 home_dir = (const char *) getenv("HOME");
68 if (!home_dir)
69 return -ENOENT;
1ea11eab 70 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 71 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
2691221a
MD
72 return 0;
73}
74
75static
76int register_app_to_sessiond(int socket)
77{
78 ssize_t ret;
79 struct {
e44418f3
MD
80 uint32_t major;
81 uint32_t minor;
2691221a
MD
82 pid_t pid;
83 uid_t uid;
84 } reg_msg;
85
e44418f3
MD
86 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
87 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a
MD
88 reg_msg.pid = getpid();
89 reg_msg.uid = getuid();
90
91 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
92 if (ret >= 0 && ret != sizeof(reg_msg))
93 return -EIO;
94 return ret;
95}
96
d9e99d10 97static
d3a492d1 98int send_reply(int sock, struct lttcomm_ust_reply *lur)
d9e99d10 99{
9eb62b9c 100 ssize_t len;
d3a492d1 101
a4be8962 102 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 103 switch (len) {
a4be8962 104 case sizeof(*lur):
d3a492d1
MD
105 DBG("message successfully sent");
106 return 0;
107 case -1:
108 if (errno == ECONNRESET) {
109 printf("remote end closed connection\n");
110 return 0;
111 }
112 return -1;
113 default:
114 printf("incorrect message size: %zd\n", len);
115 return -1;
116 }
117}
118
119static
120int handle_message(int sock, struct lttcomm_ust_msg *lum)
121{
1ea11eab
MD
122 int ret = 0;
123
124 pthread_mutex_lock(&lttng_ust_comm_mutex);
125
126 if (lttng_ust_comm_should_quit) {
127 ret = 0;
128 goto end;
129 }
9eb62b9c 130
e7723462 131 switch (lum->cmd_type) {
b35d179d 132 case UST_CREATE_SESSION:
9eb62b9c 133 {
e7723462 134 struct lttcomm_ust_reply lur;
d9e99d10 135
9eb62b9c
MD
136 DBG("Handling create session message");
137 memset(&lur, 0, sizeof(lur));
b35d179d 138 lur.cmd_type = UST_CREATE_SESSION;
1ea11eab
MD
139 ret = lttng_abi_create_session();
140 if (ret >= 0) {
141 lur.ret_val = ret;
9eb62b9c 142 lur.ret_code = LTTCOMM_OK;
1ea11eab 143 } else {
9eb62b9c 144 lur.ret_code = LTTCOMM_SESSION_FAIL;
1ea11eab 145 }
d3a492d1 146 ret = send_reply(sock, &lur);
1ea11eab
MD
147 break;
148 }
149 case UST_RELEASE:
150 {
151 struct lttcomm_ust_reply lur;
152
153 DBG("Handling release message, handle: %d",
154 lum->handle);
155 memset(&lur, 0, sizeof(lur));
156 lur.cmd_type = UST_RELEASE;
1ea11eab
MD
157 ret = objd_unref(lum->handle);
158 if (!ret) {
159 lur.ret_code = LTTCOMM_OK;
160 } else {
161 lur.ret_code = LTTCOMM_ERR;
162 }
d3a492d1 163 ret = send_reply(sock, &lur);
d9e99d10 164 break;
9eb62b9c 165 }
d9e99d10 166 default:
e7723462 167 ERR("Unimplemented command %d", (int) lum->cmd_type);
1ea11eab
MD
168 ret = -1;
169 goto end;
d9e99d10 170 }
1ea11eab
MD
171end:
172 pthread_mutex_unlock(&lttng_ust_comm_mutex);
173 return ret;
d9e99d10
MD
174}
175
1ea11eab
MD
176/*
177 * This thread does not allocate any resource, except within
178 * handle_message, within mutex protection. This mutex protects against
179 * fork and exit.
180 * The other moment it allocates resources is at socket connexion, which
181 * is also protected by the mutex.
182 */
d9e99d10
MD
183static
184void *ust_listener_thread(void *arg)
185{
1ea11eab
MD
186 struct sock_info *sock_info = arg;
187 int sock, ret;
d9e99d10 188
9eb62b9c
MD
189 /* Restart trying to connect to the session daemon */
190restart:
1ea11eab
MD
191 pthread_mutex_lock(&lttng_ust_comm_mutex);
192
193 if (lttng_ust_comm_should_quit) {
194 pthread_mutex_unlock(&lttng_ust_comm_mutex);
195 goto quit;
196 }
9eb62b9c 197
1ea11eab
MD
198 if (sock_info->socket != -1) {
199 ret = close(sock_info->socket);
200 if (ret) {
201 ERR("Error closing local apps socket");
202 }
203 sock_info->socket = -1;
204 }
9eb62b9c
MD
205 /* Check for sessiond availability with pipe TODO */
206
207 /* Register */
1ea11eab 208 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c
MD
209 if (ret < 0) {
210 ERR("Error connecting to global apps socket");
1ea11eab 211 pthread_mutex_unlock(&lttng_ust_comm_mutex);
4eef2998 212 sleep(5);
1ea11eab
MD
213 goto restart;
214 } else {
215 sock_info->socket = sock = ret;
216 pthread_mutex_unlock(&lttng_ust_comm_mutex);
9eb62b9c 217 }
1ea11eab 218
9eb62b9c
MD
219 ret = register_app_to_sessiond(sock);
220 if (ret < 0) {
221 ERR("Error registering app to local apps socket");
222 sleep(5);
223 goto restart;
224 }
d9e99d10
MD
225 for (;;) {
226 ssize_t len;
e7723462 227 struct lttcomm_ust_msg lum;
d9e99d10
MD
228
229 /* Receive session handle */
e7723462 230 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
231 switch (len) {
232 case 0: /* orderly shutdown */
233 DBG("ltt-sessiond has performed an orderly shutdown\n");
234 goto end;
e7723462 235 case sizeof(lum):
d9e99d10 236 DBG("message received\n");
e7723462 237 ret = handle_message(sock, &lum);
2a80c9d8 238 if (ret < 0) {
9eb62b9c 239 ERR("Error handling message\n");
d9e99d10
MD
240 }
241 continue;
242 case -1:
243 if (errno == ECONNRESET) {
244 ERR("remote end closed connection\n");
245 goto end;
246 }
247 goto end;
248 default:
249 ERR("incorrect message size: %zd\n", len);
250 continue;
251 }
252
253 }
254end:
9eb62b9c 255 goto restart; /* try to reconnect */
1ea11eab 256quit:
d9e99d10
MD
257 return NULL;
258}
259
260
2691221a
MD
261/*
262 * sessiond monitoring thread: monitor presence of global and per-user
263 * sessiond by polling the application common named pipe.
264 */
265/* TODO */
266
267void __attribute__((constructor)) lttng_ust_comm_init(void)
268{
269 int ret;
270
271 init_usterr();
272
9eb62b9c 273 ret = setup_local_apps_socket();
2691221a 274 if (ret) {
9eb62b9c 275 ERR("Error setting up to local apps socket");
2691221a 276 }
9eb62b9c 277#if 0
1ea11eab
MD
278 ret = pthread_create(&global_apps.ust_listener, NULL,
279 ust_listener_thread, &global_apps);
9eb62b9c 280#endif //0
1ea11eab
MD
281 ret = pthread_create(&local_apps.ust_listener, NULL,
282 ust_listener_thread, &local_apps);
2691221a
MD
283}
284
285void __attribute__((destructor)) lttng_ust_comm_exit(void)
286{
287 int ret;
288
9eb62b9c
MD
289 /*
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.
293 */
1ea11eab
MD
294
295 /*
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.
299 */
300 pthread_mutex_lock(&lttng_ust_comm_mutex);
301 lttng_ust_comm_should_quit = 1;
302 pthread_mutex_unlock(&lttng_ust_comm_mutex);
303
304#if 0
305 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
306 if (ret) {
307 ERR("Error cancelling global ust listener thread");
2691221a 308 }
1ea11eab
MD
309#endif //0
310 if (global_apps.socket != -1) {
311 ret = close(global_apps.socket);
312 assert(!ret);
313 }
314
315 ret = pthread_cancel(local_apps.ust_listener);
9eb62b9c
MD
316 if (ret) {
317 ERR("Error cancelling local ust listener thread");
2691221a 318 }
1ea11eab
MD
319
320 if (local_apps.socket != -1) {
321 ret = close(local_apps.socket);
322 assert(!ret);
323 }
324
b35d179d 325 lttng_ust_abi_exit();
1ea11eab 326 ltt_events_exit();
2691221a 327}
This page took 0.037309 seconds and 4 git commands to generate.