factorize out send_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 goto restart;
213 } else {
214 sock_info->socket = sock = ret;
215 pthread_mutex_unlock(&lttng_ust_comm_mutex);
216 }
217
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 }
224 for (;;) {
225 ssize_t len;
226 struct lttcomm_ust_msg lum;
227
228 /* Receive session handle */
229 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
230 switch (len) {
231 case 0: /* orderly shutdown */
232 DBG("ltt-sessiond has performed an orderly shutdown\n");
233 goto end;
234 case sizeof(lum):
235 DBG("message received\n");
236 ret = handle_message(sock, &lum);
237 if (ret < 0) {
238 ERR("Error handling message\n");
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 }
253 end:
254 goto restart; /* try to reconnect */
255 quit:
256 return NULL;
257 }
258
259
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
266 void __attribute__((constructor)) lttng_ust_comm_init(void)
267 {
268 int ret;
269
270 init_usterr();
271
272 ret = setup_local_apps_socket();
273 if (ret) {
274 ERR("Error setting up to local apps socket");
275 }
276 #if 0
277 ret = pthread_create(&global_apps.ust_listener, NULL,
278 ust_listener_thread, &global_apps);
279 #endif //0
280 ret = pthread_create(&local_apps.ust_listener, NULL,
281 ust_listener_thread, &local_apps);
282 }
283
284 void __attribute__((destructor)) lttng_ust_comm_exit(void)
285 {
286 int ret;
287
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 */
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);
305 if (ret) {
306 ERR("Error cancelling global ust listener thread");
307 }
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);
315 if (ret) {
316 ERR("Error cancelling local ust listener thread");
317 }
318
319 if (local_apps.socket != -1) {
320 ret = close(local_apps.socket);
321 assert(!ret);
322 }
323
324 lttng_ust_abi_exit();
325 ltt_events_exit();
326 }
This page took 0.035258 seconds and 4 git commands to generate.