Session create/release tested.
[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 handle_message(int sock, struct lttcomm_ust_msg *lum)
99 {
100 ssize_t len;
101 int ret = 0;
102
103 pthread_mutex_lock(&lttng_ust_comm_mutex);
104
105 if (lttng_ust_comm_should_quit) {
106 ret = 0;
107 goto end;
108 }
109
110 switch (lum->cmd_type) {
111 case UST_CREATE_SESSION:
112 {
113 struct lttcomm_ust_reply lur;
114
115 DBG("Handling create session message");
116 memset(&lur, 0, sizeof(lur));
117 lur.cmd_type = UST_CREATE_SESSION;
118
119 ret = lttng_abi_create_session();
120 if (ret >= 0) {
121 lur.ret_val = ret;
122 lur.ret_code = LTTCOMM_OK;
123 } else {
124 lur.ret_code = LTTCOMM_SESSION_FAIL;
125 }
126 len = lttcomm_send_unix_sock(sock, &lur, sizeof(lur));
127 switch (len) {
128 case sizeof(lur):
129 DBG("message successfully sent");
130 break;
131 case -1:
132 if (errno == ECONNRESET) {
133 printf("remote end closed connection\n");
134 ret = 0;
135 goto end;
136 }
137 ret = -1;
138 goto end;
139 default:
140 printf("incorrect message size: %zd\n", len);
141 ret = -1;
142 goto end;
143 }
144 break;
145 }
146 case UST_RELEASE:
147 {
148 struct lttcomm_ust_reply lur;
149
150 DBG("Handling release message, handle: %d",
151 lum->handle);
152 memset(&lur, 0, sizeof(lur));
153 lur.cmd_type = UST_RELEASE;
154
155 ret = objd_unref(lum->handle);
156 if (!ret) {
157 lur.ret_code = LTTCOMM_OK;
158 } else {
159 lur.ret_code = LTTCOMM_ERR;
160 }
161 len = lttcomm_send_unix_sock(sock, &lur, sizeof(lur));
162 switch (len) {
163 case sizeof(lur):
164 DBG("message successfully sent\n");
165 break;
166 case -1:
167 if (errno == ECONNRESET) {
168 printf("remote end closed connection\n");
169 ret = 0;
170 goto end;
171 }
172 ret = -1;
173 goto end;
174 default:
175 printf("incorrect message size: %zd\n", len);
176 ret = -1;
177 goto end;
178 }
179 break;
180 }
181 default:
182 ERR("Unimplemented command %d", (int) lum->cmd_type);
183 ret = -1;
184 goto end;
185 }
186 end:
187 pthread_mutex_unlock(&lttng_ust_comm_mutex);
188 return ret;
189 }
190
191 /*
192 * This thread does not allocate any resource, except within
193 * handle_message, within mutex protection. This mutex protects against
194 * fork and exit.
195 * The other moment it allocates resources is at socket connexion, which
196 * is also protected by the mutex.
197 */
198 static
199 void *ust_listener_thread(void *arg)
200 {
201 struct sock_info *sock_info = arg;
202 int sock, ret;
203
204 /* Restart trying to connect to the session daemon */
205 restart:
206 pthread_mutex_lock(&lttng_ust_comm_mutex);
207
208 if (lttng_ust_comm_should_quit) {
209 pthread_mutex_unlock(&lttng_ust_comm_mutex);
210 goto quit;
211 }
212
213 if (sock_info->socket != -1) {
214 ret = close(sock_info->socket);
215 if (ret) {
216 ERR("Error closing local apps socket");
217 }
218 sock_info->socket = -1;
219 }
220 /* Check for sessiond availability with pipe TODO */
221
222 /* Register */
223 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
224 if (ret < 0) {
225 ERR("Error connecting to global apps socket");
226 pthread_mutex_unlock(&lttng_ust_comm_mutex);
227 goto restart;
228 } else {
229 sock_info->socket = sock = ret;
230 pthread_mutex_unlock(&lttng_ust_comm_mutex);
231 }
232
233 ret = register_app_to_sessiond(sock);
234 if (ret < 0) {
235 ERR("Error registering app to local apps socket");
236 sleep(5);
237 goto restart;
238 }
239 for (;;) {
240 ssize_t len;
241 struct lttcomm_ust_msg lum;
242
243 /* Receive session handle */
244 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
245 switch (len) {
246 case 0: /* orderly shutdown */
247 DBG("ltt-sessiond has performed an orderly shutdown\n");
248 goto end;
249 case sizeof(lum):
250 DBG("message received\n");
251 ret = handle_message(sock, &lum);
252 if (ret) {
253 ERR("Error handling message\n");
254 }
255 continue;
256 case -1:
257 if (errno == ECONNRESET) {
258 ERR("remote end closed connection\n");
259 goto end;
260 }
261 goto end;
262 default:
263 ERR("incorrect message size: %zd\n", len);
264 continue;
265 }
266
267 }
268 end:
269 goto restart; /* try to reconnect */
270 quit:
271 return NULL;
272 }
273
274
275 /*
276 * sessiond monitoring thread: monitor presence of global and per-user
277 * sessiond by polling the application common named pipe.
278 */
279 /* TODO */
280
281 void __attribute__((constructor)) lttng_ust_comm_init(void)
282 {
283 int ret;
284
285 init_usterr();
286
287 ret = setup_local_apps_socket();
288 if (ret) {
289 ERR("Error setting up to local apps socket");
290 }
291 #if 0
292 ret = pthread_create(&global_apps.ust_listener, NULL,
293 ust_listener_thread, &global_apps);
294 #endif //0
295 ret = pthread_create(&local_apps.ust_listener, NULL,
296 ust_listener_thread, &local_apps);
297 }
298
299 void __attribute__((destructor)) lttng_ust_comm_exit(void)
300 {
301 int ret;
302
303 /*
304 * Using pthread_cancel here because:
305 * A) we don't want to hang application teardown.
306 * B) the thread is not allocating any resource.
307 */
308
309 /*
310 * Require the communication thread to quit. Synchronize with
311 * mutexes to ensure it is not in a mutex critical section when
312 * pthread_cancel is later called.
313 */
314 pthread_mutex_lock(&lttng_ust_comm_mutex);
315 lttng_ust_comm_should_quit = 1;
316 pthread_mutex_unlock(&lttng_ust_comm_mutex);
317
318 #if 0
319 ret = pthread_cancel(global_apps.ust_listener);
320 if (ret) {
321 ERR("Error cancelling global ust listener thread");
322 }
323 #endif //0
324 if (global_apps.socket != -1) {
325 ret = close(global_apps.socket);
326 assert(!ret);
327 }
328
329 ret = pthread_cancel(local_apps.ust_listener);
330 if (ret) {
331 ERR("Error cancelling local ust listener thread");
332 }
333
334 if (local_apps.socket != -1) {
335 ret = close(local_apps.socket);
336 assert(!ret);
337 }
338
339 lttng_ust_abi_exit();
340 ltt_events_exit();
341 }
This page took 0.035708 seconds and 4 git commands to generate.