factorize out send_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
MD
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
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
MD
211 pthread_mutex_unlock(&lttng_ust_comm_mutex);
212 goto restart;
213 } else {
214 sock_info->socket = sock = ret;
215 pthread_mutex_unlock(&lttng_ust_comm_mutex);
9eb62b9c 216 }
1ea11eab 217
9eb62b9c
MD
218 ret = register_app_to_sessiond(sock);
219 if (ret < 0) {
220 ERR("Error registering app to local apps socket");
221 sleep(5);
222 goto restart;
223 }
d9e99d10
MD
224 for (;;) {
225 ssize_t len;
e7723462 226 struct lttcomm_ust_msg lum;
d9e99d10
MD
227
228 /* Receive session handle */
e7723462 229 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
230 switch (len) {
231 case 0: /* orderly shutdown */
232 DBG("ltt-sessiond has performed an orderly shutdown\n");
233 goto end;
e7723462 234 case sizeof(lum):
d9e99d10 235 DBG("message received\n");
e7723462 236 ret = handle_message(sock, &lum);
2a80c9d8 237 if (ret < 0) {
9eb62b9c 238 ERR("Error handling message\n");
d9e99d10
MD
239 }
240 continue;
241 case -1:
242 if (errno == ECONNRESET) {
243 ERR("remote end closed connection\n");
244 goto end;
245 }
246 goto end;
247 default:
248 ERR("incorrect message size: %zd\n", len);
249 continue;
250 }
251
252 }
253end:
9eb62b9c 254 goto restart; /* try to reconnect */
1ea11eab 255quit:
d9e99d10
MD
256 return NULL;
257}
258
259
2691221a
MD
260/*
261 * sessiond monitoring thread: monitor presence of global and per-user
262 * sessiond by polling the application common named pipe.
263 */
264/* TODO */
265
266void __attribute__((constructor)) lttng_ust_comm_init(void)
267{
268 int ret;
269
270 init_usterr();
271
9eb62b9c 272 ret = setup_local_apps_socket();
2691221a 273 if (ret) {
9eb62b9c 274 ERR("Error setting up to local apps socket");
2691221a 275 }
9eb62b9c 276#if 0
1ea11eab
MD
277 ret = pthread_create(&global_apps.ust_listener, NULL,
278 ust_listener_thread, &global_apps);
9eb62b9c 279#endif //0
1ea11eab
MD
280 ret = pthread_create(&local_apps.ust_listener, NULL,
281 ust_listener_thread, &local_apps);
2691221a
MD
282}
283
284void __attribute__((destructor)) lttng_ust_comm_exit(void)
285{
286 int ret;
287
9eb62b9c
MD
288 /*
289 * Using pthread_cancel here because:
290 * A) we don't want to hang application teardown.
291 * B) the thread is not allocating any resource.
292 */
1ea11eab
MD
293
294 /*
295 * Require the communication thread to quit. Synchronize with
296 * mutexes to ensure it is not in a mutex critical section when
297 * pthread_cancel is later called.
298 */
299 pthread_mutex_lock(&lttng_ust_comm_mutex);
300 lttng_ust_comm_should_quit = 1;
301 pthread_mutex_unlock(&lttng_ust_comm_mutex);
302
303#if 0
304 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
305 if (ret) {
306 ERR("Error cancelling global ust listener thread");
2691221a 307 }
1ea11eab
MD
308#endif //0
309 if (global_apps.socket != -1) {
310 ret = close(global_apps.socket);
311 assert(!ret);
312 }
313
314 ret = pthread_cancel(local_apps.ust_listener);
9eb62b9c
MD
315 if (ret) {
316 ERR("Error cancelling local ust listener thread");
2691221a 317 }
1ea11eab
MD
318
319 if (local_apps.socket != -1) {
320 ret = close(local_apps.socket);
321 assert(!ret);
322 }
323
b35d179d 324 lttng_ust_abi_exit();
1ea11eab 325 ltt_events_exit();
2691221a 326}
This page took 0.053073 seconds and 4 git commands to generate.