Error message should only appear when return value is negative
[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
e7723462 98int handle_message(int sock, struct lttcomm_ust_msg *lum)
d9e99d10 99{
9eb62b9c 100 ssize_t len;
1ea11eab
MD
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 }
9eb62b9c 109
e7723462 110 switch (lum->cmd_type) {
b35d179d 111 case UST_CREATE_SESSION:
9eb62b9c 112 {
e7723462 113 struct lttcomm_ust_reply lur;
d9e99d10 114
9eb62b9c
MD
115 DBG("Handling create session message");
116 memset(&lur, 0, sizeof(lur));
b35d179d 117 lur.cmd_type = UST_CREATE_SESSION;
9eb62b9c 118
1ea11eab
MD
119 ret = lttng_abi_create_session();
120 if (ret >= 0) {
121 lur.ret_val = ret;
9eb62b9c 122 lur.ret_code = LTTCOMM_OK;
1ea11eab 123 } else {
9eb62b9c 124 lur.ret_code = LTTCOMM_SESSION_FAIL;
1ea11eab 125 }
9eb62b9c
MD
126 len = lttcomm_send_unix_sock(sock, &lur, sizeof(lur));
127 switch (len) {
128 case sizeof(lur):
1ea11eab 129 DBG("message successfully sent");
9eb62b9c
MD
130 break;
131 case -1:
132 if (errno == ECONNRESET) {
133 printf("remote end closed connection\n");
1ea11eab
MD
134 ret = 0;
135 goto end;
9eb62b9c 136 }
1ea11eab
MD
137 ret = -1;
138 goto end;
9eb62b9c
MD
139 default:
140 printf("incorrect message size: %zd\n", len);
1ea11eab
MD
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;
9eb62b9c 178 }
d9e99d10 179 break;
9eb62b9c 180 }
d9e99d10 181 default:
e7723462 182 ERR("Unimplemented command %d", (int) lum->cmd_type);
1ea11eab
MD
183 ret = -1;
184 goto end;
d9e99d10 185 }
1ea11eab
MD
186end:
187 pthread_mutex_unlock(&lttng_ust_comm_mutex);
188 return ret;
d9e99d10
MD
189}
190
1ea11eab
MD
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 */
d9e99d10
MD
198static
199void *ust_listener_thread(void *arg)
200{
1ea11eab
MD
201 struct sock_info *sock_info = arg;
202 int sock, ret;
d9e99d10 203
9eb62b9c
MD
204 /* Restart trying to connect to the session daemon */
205restart:
1ea11eab
MD
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 }
9eb62b9c 212
1ea11eab
MD
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 }
9eb62b9c
MD
220 /* Check for sessiond availability with pipe TODO */
221
222 /* Register */
1ea11eab 223 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c
MD
224 if (ret < 0) {
225 ERR("Error connecting to global apps socket");
1ea11eab
MD
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);
9eb62b9c 231 }
1ea11eab 232
9eb62b9c
MD
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 }
d9e99d10
MD
239 for (;;) {
240 ssize_t len;
e7723462 241 struct lttcomm_ust_msg lum;
d9e99d10
MD
242
243 /* Receive session handle */
e7723462 244 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
245 switch (len) {
246 case 0: /* orderly shutdown */
247 DBG("ltt-sessiond has performed an orderly shutdown\n");
248 goto end;
e7723462 249 case sizeof(lum):
d9e99d10 250 DBG("message received\n");
e7723462 251 ret = handle_message(sock, &lum);
2a80c9d8 252 if (ret < 0) {
9eb62b9c 253 ERR("Error handling message\n");
d9e99d10
MD
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 }
268end:
9eb62b9c 269 goto restart; /* try to reconnect */
1ea11eab 270quit:
d9e99d10
MD
271 return NULL;
272}
273
274
2691221a
MD
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
281void __attribute__((constructor)) lttng_ust_comm_init(void)
282{
283 int ret;
284
285 init_usterr();
286
9eb62b9c 287 ret = setup_local_apps_socket();
2691221a 288 if (ret) {
9eb62b9c 289 ERR("Error setting up to local apps socket");
2691221a 290 }
9eb62b9c 291#if 0
1ea11eab
MD
292 ret = pthread_create(&global_apps.ust_listener, NULL,
293 ust_listener_thread, &global_apps);
9eb62b9c 294#endif //0
1ea11eab
MD
295 ret = pthread_create(&local_apps.ust_listener, NULL,
296 ust_listener_thread, &local_apps);
2691221a
MD
297}
298
299void __attribute__((destructor)) lttng_ust_comm_exit(void)
300{
301 int ret;
302
9eb62b9c
MD
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 */
1ea11eab
MD
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);
9eb62b9c
MD
320 if (ret) {
321 ERR("Error cancelling global ust listener thread");
2691221a 322 }
1ea11eab
MD
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);
9eb62b9c
MD
330 if (ret) {
331 ERR("Error cancelling local ust listener thread");
2691221a 332 }
1ea11eab
MD
333
334 if (local_apps.socket != -1) {
335 ret = close(local_apps.socket);
336 assert(!ret);
337 }
338
b35d179d 339 lttng_ust_abi_exit();
1ea11eab 340 ltt_events_exit();
2691221a 341}
This page took 0.03782 seconds and 4 git commands to generate.