Commit | Line | Data |
---|---|---|
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 | */ | |
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 */ | |
46050b1a | 48 | int root_handle; |
1ea11eab | 49 | }; |
2691221a MD |
50 | |
51 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab MD |
52 | struct sock_info global_apps = { |
53 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, | |
54 | .socket = -1, | |
46050b1a | 55 | .root_handle = -1, |
1ea11eab | 56 | }; |
2691221a MD |
57 | |
58 | /* TODO: allow global_apps_sock_path override */ | |
59 | ||
1ea11eab MD |
60 | struct sock_info local_apps = { |
61 | .socket = -1, | |
46050b1a | 62 | .root_handle = -1, |
1ea11eab | 63 | }; |
2691221a MD |
64 | |
65 | static | |
9eb62b9c | 66 | int setup_local_apps_socket(void) |
2691221a MD |
67 | { |
68 | const char *home_dir; | |
2691221a MD |
69 | |
70 | home_dir = (const char *) getenv("HOME"); | |
71 | if (!home_dir) | |
72 | return -ENOENT; | |
1ea11eab | 73 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 74 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
2691221a MD |
75 | return 0; |
76 | } | |
77 | ||
78 | static | |
79 | int register_app_to_sessiond(int socket) | |
80 | { | |
81 | ssize_t ret; | |
82 | struct { | |
e44418f3 MD |
83 | uint32_t major; |
84 | uint32_t minor; | |
2691221a MD |
85 | pid_t pid; |
86 | uid_t uid; | |
87 | } reg_msg; | |
88 | ||
e44418f3 MD |
89 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
90 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a MD |
91 | reg_msg.pid = getpid(); |
92 | reg_msg.uid = getuid(); | |
93 | ||
94 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); | |
95 | if (ret >= 0 && ret != sizeof(reg_msg)) | |
96 | return -EIO; | |
97 | return ret; | |
98 | } | |
99 | ||
d9e99d10 | 100 | static |
d3a492d1 | 101 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
d9e99d10 | 102 | { |
9eb62b9c | 103 | ssize_t len; |
d3a492d1 | 104 | |
a4be8962 | 105 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 106 | switch (len) { |
a4be8962 | 107 | case sizeof(*lur): |
d3a492d1 MD |
108 | DBG("message successfully sent"); |
109 | return 0; | |
110 | case -1: | |
111 | if (errno == ECONNRESET) { | |
112 | printf("remote end closed connection\n"); | |
113 | return 0; | |
114 | } | |
115 | return -1; | |
116 | default: | |
117 | printf("incorrect message size: %zd\n", len); | |
118 | return -1; | |
119 | } | |
120 | } | |
121 | ||
122 | static | |
123 | int handle_message(int sock, struct lttcomm_ust_msg *lum) | |
124 | { | |
1ea11eab | 125 | int ret = 0; |
46050b1a MD |
126 | const struct objd_ops *ops; |
127 | struct lttcomm_ust_reply lur; | |
1ea11eab MD |
128 | |
129 | pthread_mutex_lock(<tng_ust_comm_mutex); | |
130 | ||
46050b1a MD |
131 | memset(&lur, 0, sizeof(lur)); |
132 | ||
1ea11eab | 133 | if (lttng_ust_comm_should_quit) { |
46050b1a | 134 | ret = -EPERM; |
1ea11eab MD |
135 | goto end; |
136 | } | |
9eb62b9c | 137 | |
46050b1a MD |
138 | ops = objd_ops(lum->handle); |
139 | if (!ops) { | |
140 | ret = -ENOENT; | |
141 | goto end; | |
1ea11eab | 142 | } |
46050b1a MD |
143 | |
144 | switch (lum->cmd) { | |
145 | case LTTNG_UST_RELEASE: | |
146 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
147 | ret = -EPERM; | |
148 | else | |
149 | ret = objd_unref(lum->handle); | |
d9e99d10 MD |
150 | break; |
151 | default: | |
46050b1a MD |
152 | if (ops->cmd) |
153 | ret = ops->cmd(lum->handle, lum->cmd, | |
154 | (unsigned long) &lum->u); | |
155 | else | |
156 | ret = -ENOSYS; | |
157 | break; | |
d9e99d10 | 158 | } |
46050b1a | 159 | |
1ea11eab | 160 | end: |
46050b1a MD |
161 | lur.handle = lum->handle; |
162 | lur.cmd = lum->cmd; | |
163 | lur.ret_val = ret; | |
164 | if (ret >= 0) { | |
165 | lur.ret_code = LTTCOMM_OK; | |
166 | } else { | |
167 | lur.ret_code = LTTCOMM_SESSION_FAIL; | |
168 | } | |
169 | ret = send_reply(sock, &lur); | |
170 | ||
1ea11eab MD |
171 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
172 | return ret; | |
d9e99d10 MD |
173 | } |
174 | ||
46050b1a MD |
175 | static |
176 | void cleanup_sock_info(struct sock_info *sock_info) | |
177 | { | |
178 | int ret; | |
179 | ||
180 | if (sock_info->socket != -1) { | |
181 | ret = close(sock_info->socket); | |
182 | if (ret) { | |
183 | ERR("Error closing local apps socket"); | |
184 | } | |
185 | sock_info->socket = -1; | |
186 | } | |
187 | if (sock_info->root_handle != -1) { | |
188 | ret = objd_unref(sock_info->root_handle); | |
189 | if (ret) { | |
190 | ERR("Error unref root handle"); | |
191 | } | |
192 | sock_info->root_handle = -1; | |
193 | } | |
194 | } | |
195 | ||
1ea11eab MD |
196 | /* |
197 | * This thread does not allocate any resource, except within | |
198 | * handle_message, within mutex protection. This mutex protects against | |
199 | * fork and exit. | |
200 | * The other moment it allocates resources is at socket connexion, which | |
201 | * is also protected by the mutex. | |
202 | */ | |
d9e99d10 MD |
203 | static |
204 | void *ust_listener_thread(void *arg) | |
205 | { | |
1ea11eab MD |
206 | struct sock_info *sock_info = arg; |
207 | int sock, ret; | |
d9e99d10 | 208 | |
9eb62b9c MD |
209 | /* Restart trying to connect to the session daemon */ |
210 | restart: | |
1ea11eab MD |
211 | pthread_mutex_lock(<tng_ust_comm_mutex); |
212 | ||
213 | if (lttng_ust_comm_should_quit) { | |
214 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
215 | goto quit; | |
216 | } | |
9eb62b9c | 217 | |
1ea11eab MD |
218 | if (sock_info->socket != -1) { |
219 | ret = close(sock_info->socket); | |
220 | if (ret) { | |
221 | ERR("Error closing local apps socket"); | |
222 | } | |
223 | sock_info->socket = -1; | |
224 | } | |
46050b1a | 225 | |
9eb62b9c MD |
226 | /* Check for sessiond availability with pipe TODO */ |
227 | ||
228 | /* Register */ | |
1ea11eab | 229 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c MD |
230 | if (ret < 0) { |
231 | ERR("Error connecting to global apps socket"); | |
1ea11eab | 232 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
4eef2998 | 233 | sleep(5); |
1ea11eab | 234 | goto restart; |
46050b1a MD |
235 | } |
236 | ||
237 | sock_info->socket = sock = ret; | |
238 | ||
239 | /* | |
240 | * Create only one root handle per listener thread for the whole | |
241 | * process lifetime. | |
242 | */ | |
243 | if (sock_info->root_handle == -1) { | |
244 | ret = lttng_abi_create_root_handle(); | |
245 | if (ret) { | |
246 | ERR("Error creating root handle"); | |
247 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
248 | goto quit; | |
249 | } | |
250 | sock_info->root_handle = ret; | |
9eb62b9c | 251 | } |
1ea11eab | 252 | |
9eb62b9c MD |
253 | ret = register_app_to_sessiond(sock); |
254 | if (ret < 0) { | |
255 | ERR("Error registering app to local apps socket"); | |
46050b1a | 256 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
9eb62b9c MD |
257 | sleep(5); |
258 | goto restart; | |
259 | } | |
46050b1a MD |
260 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
261 | ||
d9e99d10 MD |
262 | for (;;) { |
263 | ssize_t len; | |
e7723462 | 264 | struct lttcomm_ust_msg lum; |
d9e99d10 | 265 | |
e7723462 | 266 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
267 | switch (len) { |
268 | case 0: /* orderly shutdown */ | |
269 | DBG("ltt-sessiond has performed an orderly shutdown\n"); | |
270 | goto end; | |
e7723462 | 271 | case sizeof(lum): |
d9e99d10 | 272 | DBG("message received\n"); |
e7723462 | 273 | ret = handle_message(sock, &lum); |
2a80c9d8 | 274 | if (ret < 0) { |
9eb62b9c | 275 | ERR("Error handling message\n"); |
d9e99d10 MD |
276 | } |
277 | continue; | |
278 | case -1: | |
279 | if (errno == ECONNRESET) { | |
280 | ERR("remote end closed connection\n"); | |
281 | goto end; | |
282 | } | |
283 | goto end; | |
284 | default: | |
285 | ERR("incorrect message size: %zd\n", len); | |
286 | continue; | |
287 | } | |
288 | ||
289 | } | |
290 | end: | |
9eb62b9c | 291 | goto restart; /* try to reconnect */ |
1ea11eab | 292 | quit: |
d9e99d10 MD |
293 | return NULL; |
294 | } | |
295 | ||
296 | ||
2691221a MD |
297 | /* |
298 | * sessiond monitoring thread: monitor presence of global and per-user | |
299 | * sessiond by polling the application common named pipe. | |
300 | */ | |
301 | /* TODO */ | |
302 | ||
303 | void __attribute__((constructor)) lttng_ust_comm_init(void) | |
304 | { | |
305 | int ret; | |
306 | ||
307 | init_usterr(); | |
308 | ||
9eb62b9c | 309 | ret = setup_local_apps_socket(); |
2691221a | 310 | if (ret) { |
9eb62b9c | 311 | ERR("Error setting up to local apps socket"); |
2691221a | 312 | } |
9eb62b9c | 313 | #if 0 |
1ea11eab MD |
314 | ret = pthread_create(&global_apps.ust_listener, NULL, |
315 | ust_listener_thread, &global_apps); | |
9eb62b9c | 316 | #endif //0 |
1ea11eab MD |
317 | ret = pthread_create(&local_apps.ust_listener, NULL, |
318 | ust_listener_thread, &local_apps); | |
2691221a MD |
319 | } |
320 | ||
321 | void __attribute__((destructor)) lttng_ust_comm_exit(void) | |
322 | { | |
323 | int ret; | |
324 | ||
9eb62b9c MD |
325 | /* |
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. | |
329 | */ | |
1ea11eab MD |
330 | |
331 | /* | |
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. | |
335 | */ | |
336 | pthread_mutex_lock(<tng_ust_comm_mutex); | |
337 | lttng_ust_comm_should_quit = 1; | |
338 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
339 | ||
340 | #if 0 | |
341 | ret = pthread_cancel(global_apps.ust_listener); | |
9eb62b9c MD |
342 | if (ret) { |
343 | ERR("Error cancelling global ust listener thread"); | |
2691221a | 344 | } |
1ea11eab | 345 | #endif //0 |
46050b1a MD |
346 | |
347 | cleanup_sock_info(&global_apps); | |
1ea11eab MD |
348 | |
349 | ret = pthread_cancel(local_apps.ust_listener); | |
9eb62b9c MD |
350 | if (ret) { |
351 | ERR("Error cancelling local ust listener thread"); | |
2691221a | 352 | } |
1ea11eab | 353 | |
46050b1a | 354 | cleanup_sock_info(&local_apps); |
1ea11eab | 355 | |
b35d179d | 356 | lttng_ust_abi_exit(); |
1ea11eab | 357 | ltt_events_exit(); |
2691221a | 358 | } |