ust comm: more resilient handle table, fix size of reply
[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 <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 */
36 static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 /* Should the ust comm thread quit ? */
39 static int lttng_ust_comm_should_quit;
40
41 /*
42 * Info about socket and associated listener thread.
43 */
44 struct sock_info {
45 char sock_path[PATH_MAX];
46 int socket;
47 pthread_t ust_listener; /* listener thread */
48 };
49
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,
53 .socket = -1,
54 };
55
56 /* TODO: allow global_apps_sock_path override */
57
58 struct sock_info local_apps = {
59 .socket = -1,
60 };
61
62 static
63 int setup_local_apps_socket(void)
64 {
65 const char *home_dir;
66
67 home_dir = (const char *) getenv("HOME");
68 if (!home_dir)
69 return -ENOENT;
70 snprintf(local_apps.sock_path, PATH_MAX,
71 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
72 return 0;
73 }
74
75 static
76 int register_app_to_sessiond(int socket)
77 {
78 ssize_t ret;
79 struct {
80 uint32_t major;
81 uint32_t minor;
82 pid_t pid;
83 uid_t uid;
84 } reg_msg;
85
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();
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
97 static
98 int send_reply(int sock, struct lttcomm_ust_reply *lur)
99 {
100 ssize_t len;
101
102 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
103 switch (len) {
104 case sizeof(*lur):
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
119 static
120 int handle_message(int sock, struct lttcomm_ust_msg *lum)
121 {
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 }
130
131 switch (lum->cmd_type) {
132 case UST_CREATE_SESSION:
133 {
134 struct lttcomm_ust_reply lur;
135
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();
140 if (ret >= 0) {
141 lur.ret_val = ret;
142 lur.ret_code = LTTCOMM_OK;
143 } else {
144 lur.ret_code = LTTCOMM_SESSION_FAIL;
145 }
146 ret = send_reply(sock, &lur);
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;
157 ret = objd_unref(lum->handle);
158 if (!ret) {
159 lur.ret_code = LTTCOMM_OK;
160 } else {
161 lur.ret_code = LTTCOMM_ERR;
162 }
163 ret = send_reply(sock, &lur);
164 break;
165 }
166 default:
167 ERR("Unimplemented command %d", (int) lum->cmd_type);
168 ret = -1;
169 goto end;
170 }
171 end:
172 pthread_mutex_unlock(&lttng_ust_comm_mutex);
173 return ret;
174 }
175
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 */
183 static
184 void *ust_listener_thread(void *arg)
185 {
186 struct sock_info *sock_info = arg;
187 int sock, ret;
188
189 /* Restart trying to connect to the session daemon */
190 restart:
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 }
197
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 }
205 /* Check for sessiond availability with pipe TODO */
206
207 /* Register */
208 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
209 if (ret < 0) {
210 ERR("Error connecting to global apps socket");
211 pthread_mutex_unlock(&lttng_ust_comm_mutex);
212 sleep(5);
213 goto restart;
214 } else {
215 sock_info->socket = sock = ret;
216 pthread_mutex_unlock(&lttng_ust_comm_mutex);
217 }
218
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 }
225 for (;;) {
226 ssize_t len;
227 struct lttcomm_ust_msg lum;
228
229 /* Receive session handle */
230 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
231 switch (len) {
232 case 0: /* orderly shutdown */
233 DBG("ltt-sessiond has performed an orderly shutdown\n");
234 goto end;
235 case sizeof(lum):
236 DBG("message received\n");
237 ret = handle_message(sock, &lum);
238 if (ret < 0) {
239 ERR("Error handling message\n");
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 }
254 end:
255 goto restart; /* try to reconnect */
256 quit:
257 return NULL;
258 }
259
260
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
267 void __attribute__((constructor)) lttng_ust_comm_init(void)
268 {
269 int ret;
270
271 init_usterr();
272
273 ret = setup_local_apps_socket();
274 if (ret) {
275 ERR("Error setting up to local apps socket");
276 }
277 #if 0
278 ret = pthread_create(&global_apps.ust_listener, NULL,
279 ust_listener_thread, &global_apps);
280 #endif //0
281 ret = pthread_create(&local_apps.ust_listener, NULL,
282 ust_listener_thread, &local_apps);
283 }
284
285 void __attribute__((destructor)) lttng_ust_comm_exit(void)
286 {
287 int ret;
288
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 */
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);
306 if (ret) {
307 ERR("Error cancelling global ust listener thread");
308 }
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);
316 if (ret) {
317 ERR("Error cancelling local ust listener thread");
318 }
319
320 if (local_apps.socket != -1) {
321 ret = close(local_apps.socket);
322 assert(!ret);
323 }
324
325 lttng_ust_abi_exit();
326 ltt_events_exit();
327 }
This page took 0.036057 seconds and 4 git commands to generate.