Commit | Line | Data |
---|---|---|
60b6c79c MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
60b6c79c MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
d14d33bf | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
60b6c79c MD |
12 | * more details. |
13 | * | |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
60b6c79c MD |
17 | */ |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
60b6c79c MD |
20 | #include <errno.h> |
21 | #include <limits.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/wait.h> | |
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
28 | #include <unistd.h> | |
29 | #include <fcntl.h> | |
c2b75c49 | 30 | #include <sched.h> |
730389d9 | 31 | #include <sys/signal.h> |
749b7a0c | 32 | #include <assert.h> |
e1055edb | 33 | #include <signal.h> |
60b6c79c | 34 | |
90e535ef | 35 | #include <common/common.h> |
3fd15a74 | 36 | #include <common/utils.h> |
e8fa9fb0 | 37 | #include <common/compat/getenv.h> |
e1055edb | 38 | #include <common/compat/prctl.h> |
7567352f | 39 | #include <common/sessiond-comm/unix.h> |
60b6c79c | 40 | |
0857097f DG |
41 | #include "runas.h" |
42 | ||
7567352f MD |
43 | struct run_as_data; |
44 | typedef int (*run_as_fct)(struct run_as_data *data); | |
c2b75c49 | 45 | |
e11d277b | 46 | struct run_as_mkdir_data { |
7567352f | 47 | char path[PATH_MAX]; |
60b6c79c MD |
48 | mode_t mode; |
49 | }; | |
50 | ||
e11d277b | 51 | struct run_as_open_data { |
7567352f | 52 | char path[PATH_MAX]; |
60b6c79c MD |
53 | int flags; |
54 | mode_t mode; | |
55 | }; | |
56 | ||
4628484a | 57 | struct run_as_unlink_data { |
7567352f | 58 | char path[PATH_MAX]; |
4628484a MD |
59 | }; |
60 | ||
7567352f MD |
61 | struct run_as_rmdir_recursive_data { |
62 | char path[PATH_MAX]; | |
63 | }; | |
64 | ||
65 | enum run_as_cmd { | |
66 | RUN_AS_MKDIR, | |
67 | RUN_AS_OPEN, | |
68 | RUN_AS_UNLINK, | |
69 | RUN_AS_RMDIR_RECURSIVE, | |
70 | RUN_AS_MKDIR_RECURSIVE, | |
71 | }; | |
72 | ||
73 | struct run_as_data { | |
74 | enum run_as_cmd cmd; | |
75 | union { | |
76 | struct run_as_mkdir_data mkdir; | |
77 | struct run_as_open_data open; | |
78 | struct run_as_unlink_data unlink; | |
79 | struct run_as_rmdir_recursive_data rmdir_recursive; | |
80 | } u; | |
81 | uid_t uid; | |
82 | gid_t gid; | |
4628484a MD |
83 | }; |
84 | ||
df5b86c8 MD |
85 | struct run_as_ret { |
86 | int ret; | |
87 | int _errno; | |
88 | }; | |
89 | ||
7567352f MD |
90 | struct run_as_worker { |
91 | pid_t pid; /* Worker PID. */ | |
92 | int sockpair[2]; | |
93 | char *procname; | |
94 | }; | |
95 | ||
96 | /* Single global worker per process (for now). */ | |
97 | static struct run_as_worker *global_worker; | |
98 | /* Lock protecting the worker. */ | |
99 | static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER; | |
100 | ||
8f0044bf MD |
101 | #ifdef VALGRIND |
102 | static | |
103 | int use_clone(void) | |
104 | { | |
105 | return 0; | |
106 | } | |
107 | #else | |
108 | static | |
109 | int use_clone(void) | |
110 | { | |
e8fa9fb0 | 111 | return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE"); |
8f0044bf MD |
112 | } |
113 | #endif | |
114 | ||
d77dded2 JG |
115 | LTTNG_HIDDEN |
116 | int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode); | |
117 | ||
60b6c79c MD |
118 | /* |
119 | * Create recursively directory using the FULL path. | |
120 | */ | |
121 | static | |
7567352f | 122 | int _mkdir_recursive(struct run_as_data *data) |
60b6c79c | 123 | { |
60b6c79c | 124 | const char *path; |
60b6c79c | 125 | mode_t mode; |
60b6c79c | 126 | |
7567352f MD |
127 | path = data->u.mkdir.path; |
128 | mode = data->u.mkdir.mode; | |
60b6c79c | 129 | |
d77dded2 JG |
130 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
131 | return _utils_mkdir_recursive_unsafe(path, mode); | |
60b6c79c MD |
132 | } |
133 | ||
134 | static | |
7567352f | 135 | int _mkdir(struct run_as_data *data) |
60b6c79c | 136 | { |
7567352f MD |
137 | return mkdir(data->u.mkdir.path, data->u.mkdir.mode); |
138 | } | |
7ce36756 | 139 | |
7567352f MD |
140 | static |
141 | int _open(struct run_as_data *data) | |
142 | { | |
143 | return open(data->u.open.path, data->u.open.flags, data->u.open.mode); | |
144 | } | |
145 | ||
146 | static | |
147 | int _unlink(struct run_as_data *data) | |
148 | { | |
149 | return unlink(data->u.unlink.path); | |
60b6c79c MD |
150 | } |
151 | ||
152 | static | |
7567352f | 153 | int _rmdir_recursive(struct run_as_data *data) |
60b6c79c | 154 | { |
7567352f MD |
155 | return utils_recursive_rmdir(data->u.rmdir_recursive.path); |
156 | } | |
df5b86c8 | 157 | |
7567352f MD |
158 | static |
159 | run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd) | |
160 | { | |
161 | switch (cmd) { | |
162 | case RUN_AS_MKDIR: | |
163 | return _mkdir; | |
164 | case RUN_AS_OPEN: | |
165 | return _open; | |
166 | case RUN_AS_UNLINK: | |
167 | return _unlink; | |
168 | case RUN_AS_RMDIR_RECURSIVE: | |
169 | return _rmdir_recursive; | |
170 | case RUN_AS_MKDIR_RECURSIVE: | |
171 | return _mkdir_recursive; | |
172 | default: | |
173 | ERR("Unknown command %d", (int) cmd) | |
174 | return NULL; | |
175 | } | |
60b6c79c MD |
176 | } |
177 | ||
4628484a | 178 | static |
7567352f MD |
179 | int do_send_fd(struct run_as_worker *worker, |
180 | enum run_as_cmd cmd, int fd) | |
4628484a | 181 | { |
7567352f | 182 | ssize_t len; |
4628484a | 183 | |
7567352f MD |
184 | switch (cmd) { |
185 | case RUN_AS_OPEN: | |
186 | break; | |
187 | default: | |
188 | return 0; | |
189 | } | |
190 | if (fd < 0) { | |
191 | return 0; | |
192 | } | |
193 | len = lttcomm_send_fds_unix_sock(worker->sockpair[1], &fd, 1); | |
194 | if (len < 0) { | |
195 | PERROR("lttcomm_send_fds_unix_sock"); | |
196 | return -1; | |
197 | } | |
198 | if (close(fd) < 0) { | |
199 | PERROR("close"); | |
200 | return -1; | |
201 | } | |
202 | return 0; | |
4628484a MD |
203 | } |
204 | ||
205 | static | |
7567352f MD |
206 | int do_recv_fd(struct run_as_worker *worker, |
207 | enum run_as_cmd cmd, int *fd) | |
4628484a | 208 | { |
7567352f | 209 | ssize_t len; |
4628484a | 210 | |
7567352f MD |
211 | switch (cmd) { |
212 | case RUN_AS_OPEN: | |
213 | break; | |
214 | default: | |
215 | return 0; | |
216 | } | |
217 | if (*fd < 0) { | |
218 | return 0; | |
219 | } | |
220 | len = lttcomm_recv_fds_unix_sock(worker->sockpair[0], fd, 1); | |
da9ee832 JG |
221 | if (!len) { |
222 | return -1; | |
223 | } else if (len < 0) { | |
7567352f MD |
224 | PERROR("lttcomm_recv_fds_unix_sock"); |
225 | return -1; | |
226 | } | |
227 | return 0; | |
4628484a MD |
228 | } |
229 | ||
7567352f MD |
230 | /* |
231 | * Return < 0 on error, 0 if OK, 1 on hangup. | |
232 | */ | |
c2b75c49 | 233 | static |
7567352f | 234 | int handle_one_cmd(struct run_as_worker *worker) |
c2b75c49 | 235 | { |
7567352f MD |
236 | int ret = 0; |
237 | struct run_as_data data; | |
238 | ssize_t readlen, writelen; | |
df5b86c8 | 239 | struct run_as_ret sendret; |
7567352f MD |
240 | run_as_fct cmd; |
241 | uid_t prev_euid; | |
242 | ||
243 | /* Read data */ | |
244 | readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data, | |
245 | sizeof(data)); | |
246 | if (readlen == 0) { | |
247 | /* hang up */ | |
248 | ret = 1; | |
249 | goto end; | |
250 | } | |
251 | if (readlen < sizeof(data)) { | |
252 | PERROR("lttcomm_recv_unix_sock error"); | |
253 | ret = -1; | |
254 | goto end; | |
255 | } | |
c2b75c49 | 256 | |
7567352f MD |
257 | cmd = run_as_enum_to_fct(data.cmd); |
258 | if (!cmd) { | |
259 | ret = -1; | |
260 | goto end; | |
261 | } | |
262 | ||
263 | prev_euid = getuid(); | |
264 | if (data.gid != getegid()) { | |
265 | ret = setegid(data.gid); | |
1576d582 | 266 | if (ret < 0) { |
4c462e79 | 267 | PERROR("setegid"); |
6d73c4ef | 268 | goto write_return; |
1576d582 | 269 | } |
c2b75c49 | 270 | } |
7567352f MD |
271 | if (data.uid != prev_euid) { |
272 | ret = seteuid(data.uid); | |
1576d582 | 273 | if (ret < 0) { |
4c462e79 | 274 | PERROR("seteuid"); |
6d73c4ef | 275 | goto write_return; |
1576d582 | 276 | } |
c2b75c49 MD |
277 | } |
278 | /* | |
279 | * Also set umask to 0 for mkdir executable bit. | |
280 | */ | |
281 | umask(0); | |
7567352f | 282 | ret = (*cmd)(&data); |
6d73c4ef MD |
283 | |
284 | write_return: | |
df5b86c8 MD |
285 | sendret.ret = ret; |
286 | sendret._errno = errno; | |
c2b75c49 | 287 | /* send back return value */ |
7567352f MD |
288 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
289 | sizeof(sendret)); | |
6cd525e8 | 290 | if (writelen < sizeof(sendret)) { |
7567352f MD |
291 | PERROR("lttcomm_send_unix_sock error"); |
292 | ret = -1; | |
293 | goto end; | |
294 | } | |
295 | ret = do_send_fd(worker, data.cmd, ret); | |
296 | if (ret) { | |
297 | PERROR("do_send_fd error"); | |
298 | ret = -1; | |
299 | goto end; | |
300 | } | |
301 | if (seteuid(prev_euid) < 0) { | |
302 | PERROR("seteuid"); | |
303 | ret = -1; | |
304 | goto end; | |
305 | } | |
306 | ret = 0; | |
307 | end: | |
308 | return ret; | |
309 | } | |
310 | ||
311 | static | |
312 | int run_as_worker(struct run_as_worker *worker) | |
313 | { | |
314 | int ret; | |
315 | ssize_t writelen; | |
316 | struct run_as_ret sendret; | |
317 | size_t proc_orig_len; | |
318 | ||
319 | /* | |
320 | * Initialize worker. Set a different process cmdline. | |
321 | */ | |
322 | proc_orig_len = strlen(worker->procname); | |
323 | memset(worker->procname, 0, proc_orig_len); | |
324 | strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len); | |
325 | ||
e1055edb JG |
326 | ret = lttng_prctl(PR_SET_NAME, |
327 | (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); | |
328 | if (ret && ret != -ENOSYS) { | |
b8090274 JG |
329 | /* Don't fail as this is not essential. */ |
330 | PERROR("prctl PR_SET_NAME"); | |
331 | ret = 0; | |
6cd525e8 | 332 | } |
7567352f MD |
333 | |
334 | sendret.ret = 0; | |
335 | sendret._errno = 0; | |
336 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, | |
337 | sizeof(sendret)); | |
338 | if (writelen < sizeof(sendret)) { | |
339 | PERROR("lttcomm_send_unix_sock error"); | |
340 | ret = EXIT_FAILURE; | |
341 | goto end; | |
342 | } | |
343 | ||
344 | for (;;) { | |
345 | ret = handle_one_cmd(worker); | |
346 | if (ret < 0) { | |
347 | ret = EXIT_FAILURE; | |
348 | goto end; | |
349 | } else if (ret > 0) { | |
350 | break; | |
351 | } else { | |
352 | continue; /* Next command. */ | |
353 | } | |
354 | } | |
355 | ret = EXIT_SUCCESS; | |
356 | end: | |
357 | return ret; | |
c2b75c49 MD |
358 | } |
359 | ||
60b6c79c | 360 | static |
7567352f MD |
361 | int run_as_cmd(struct run_as_worker *worker, |
362 | enum run_as_cmd cmd, | |
363 | struct run_as_data *data, | |
364 | uid_t uid, gid_t gid) | |
60b6c79c | 365 | { |
7567352f | 366 | ssize_t readlen, writelen; |
df5b86c8 | 367 | struct run_as_ret recvret; |
60b6c79c MD |
368 | |
369 | /* | |
370 | * If we are non-root, we can only deal with our own uid. | |
371 | */ | |
372 | if (geteuid() != 0) { | |
373 | if (uid != geteuid()) { | |
df5b86c8 MD |
374 | recvret.ret = -1; |
375 | recvret._errno = EPERM; | |
60b6c79c | 376 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", |
08797918 | 377 | (int) uid, (int) geteuid()); |
df5b86c8 | 378 | goto end; |
60b6c79c | 379 | } |
60b6c79c MD |
380 | } |
381 | ||
7567352f MD |
382 | data->cmd = cmd; |
383 | data->uid = uid; | |
384 | data->gid = gid; | |
385 | ||
386 | writelen = lttcomm_send_unix_sock(worker->sockpair[0], data, | |
387 | sizeof(*data)); | |
388 | if (writelen < sizeof(*data)) { | |
389 | PERROR("Error writing message to run_as"); | |
df5b86c8 MD |
390 | recvret.ret = -1; |
391 | recvret._errno = errno; | |
60b6c79c | 392 | goto end; |
c2b75c49 | 393 | } |
7567352f | 394 | |
c2b75c49 | 395 | /* receive return value */ |
7567352f MD |
396 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], &recvret, |
397 | sizeof(recvret)); | |
da9ee832 JG |
398 | if (!readlen) { |
399 | ERR("Run-as worker has hung-up during run_as_cmd"); | |
400 | recvret.ret = -1; | |
401 | recvret._errno = EIO; | |
402 | goto end; | |
403 | } else if (readlen < sizeof(recvret)) { | |
7567352f | 404 | PERROR("Error reading response from run_as"); |
df5b86c8 MD |
405 | recvret.ret = -1; |
406 | recvret._errno = errno; | |
6cd525e8 | 407 | } |
7567352f | 408 | if (do_recv_fd(worker, cmd, &recvret.ret)) { |
df5b86c8 | 409 | recvret.ret = -1; |
da9ee832 | 410 | recvret._errno = EIO; |
4c462e79 | 411 | } |
7567352f | 412 | |
60b6c79c | 413 | end: |
df5b86c8 MD |
414 | errno = recvret._errno; |
415 | return recvret.ret; | |
60b6c79c MD |
416 | } |
417 | ||
2d85a600 | 418 | /* |
7567352f | 419 | * This is for debugging ONLY and should not be considered secure. |
2d85a600 MD |
420 | */ |
421 | static | |
7567352f MD |
422 | int run_as_noworker(enum run_as_cmd cmd, |
423 | struct run_as_data *data, uid_t uid, gid_t gid) | |
2d85a600 | 424 | { |
df5b86c8 | 425 | int ret, saved_errno; |
5b73926f | 426 | mode_t old_mask; |
7567352f | 427 | run_as_fct fct; |
5b73926f | 428 | |
7567352f MD |
429 | fct = run_as_enum_to_fct(cmd); |
430 | if (!fct) { | |
431 | errno = -ENOSYS; | |
432 | ret = -1; | |
433 | goto end; | |
434 | } | |
5b73926f | 435 | old_mask = umask(0); |
7567352f | 436 | ret = fct(data); |
df5b86c8 | 437 | saved_errno = errno; |
5b73926f | 438 | umask(old_mask); |
df5b86c8 | 439 | errno = saved_errno; |
7567352f | 440 | end: |
5b73926f | 441 | return ret; |
2d85a600 MD |
442 | } |
443 | ||
444 | static | |
749b7a0c | 445 | int run_as(enum run_as_cmd cmd, struct run_as_data *data, uid_t uid, gid_t gid) |
2d85a600 | 446 | { |
7567352f MD |
447 | int ret; |
448 | ||
749b7a0c | 449 | if (use_clone()) { |
7567352f | 450 | DBG("Using run_as worker"); |
749b7a0c JG |
451 | pthread_mutex_lock(&worker_lock); |
452 | assert(global_worker); | |
453 | ret = run_as_cmd(global_worker, cmd, data, uid, gid); | |
454 | pthread_mutex_unlock(&worker_lock); | |
455 | ||
2d85a600 | 456 | } else { |
7567352f MD |
457 | DBG("Using run_as without worker"); |
458 | ret = run_as_noworker(cmd, data, uid, gid); | |
2d85a600 | 459 | } |
7567352f | 460 | return ret; |
2d85a600 MD |
461 | } |
462 | ||
90e535ef | 463 | LTTNG_HIDDEN |
e11d277b | 464 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 465 | { |
7567352f | 466 | struct run_as_data data; |
60b6c79c MD |
467 | |
468 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", | |
08797918 | 469 | path, (int) mode, (int) uid, (int) gid); |
7567352f MD |
470 | strncpy(data.u.mkdir.path, path, PATH_MAX - 1); |
471 | data.u.mkdir.path[PATH_MAX - 1] = '\0'; | |
472 | data.u.mkdir.mode = mode; | |
749b7a0c | 473 | return run_as(RUN_AS_MKDIR_RECURSIVE, &data, uid, gid); |
60b6c79c MD |
474 | } |
475 | ||
90e535ef | 476 | LTTNG_HIDDEN |
e11d277b | 477 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 478 | { |
7567352f | 479 | struct run_as_data data; |
60b6c79c MD |
480 | |
481 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", | |
08797918 | 482 | path, (int) mode, (int) uid, (int) gid); |
7567352f MD |
483 | strncpy(data.u.mkdir.path, path, PATH_MAX - 1); |
484 | data.u.mkdir.path[PATH_MAX - 1] = '\0'; | |
485 | data.u.mkdir.mode = mode; | |
749b7a0c | 486 | return run_as(RUN_AS_MKDIR, &data, uid, gid); |
60b6c79c MD |
487 | } |
488 | ||
489 | /* | |
490 | * Note: open_run_as is currently not working. We'd need to pass the fd | |
491 | * opened in the child to the parent. | |
492 | */ | |
90e535ef | 493 | LTTNG_HIDDEN |
e11d277b | 494 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 495 | { |
7567352f | 496 | struct run_as_data data; |
c2b75c49 | 497 | |
47fb7563 | 498 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
08797918 | 499 | path, flags, (int) mode, (int) uid, (int) gid); |
7567352f MD |
500 | strncpy(data.u.open.path, path, PATH_MAX - 1); |
501 | data.u.open.path[PATH_MAX - 1] = '\0'; | |
502 | data.u.open.flags = flags; | |
503 | data.u.open.mode = mode; | |
749b7a0c | 504 | return run_as(RUN_AS_OPEN, &data, uid, gid); |
60b6c79c | 505 | } |
4628484a MD |
506 | |
507 | LTTNG_HIDDEN | |
508 | int run_as_unlink(const char *path, uid_t uid, gid_t gid) | |
509 | { | |
7567352f | 510 | struct run_as_data data; |
4628484a MD |
511 | |
512 | DBG3("unlink() %s with for uid %d and gid %d", | |
08797918 | 513 | path, (int) uid, (int) gid); |
7567352f MD |
514 | strncpy(data.u.unlink.path, path, PATH_MAX - 1); |
515 | data.u.unlink.path[PATH_MAX - 1] = '\0'; | |
749b7a0c | 516 | return run_as(RUN_AS_UNLINK, &data, uid, gid); |
4628484a MD |
517 | } |
518 | ||
519 | LTTNG_HIDDEN | |
7567352f | 520 | int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid) |
4628484a | 521 | { |
7567352f | 522 | struct run_as_data data; |
4628484a | 523 | |
7567352f | 524 | DBG3("rmdir_recursive() %s with for uid %d and gid %d", |
08797918 | 525 | path, (int) uid, (int) gid); |
7567352f MD |
526 | strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1); |
527 | data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0'; | |
749b7a0c | 528 | return run_as(RUN_AS_RMDIR_RECURSIVE, &data, uid, gid); |
7567352f MD |
529 | } |
530 | ||
f8f66d38 | 531 | static |
a80ed305 | 532 | int reset_sighandler(void) |
f8f66d38 | 533 | { |
5b5bb8c8 | 534 | int sig; |
f8f66d38 | 535 | |
978d5d79 | 536 | DBG("Resetting run_as worker signal handlers to default"); |
5b5bb8c8 JG |
537 | for (sig = 1; sig <= 31; sig++) { |
538 | (void) signal(sig, SIG_DFL); | |
f8f66d38 | 539 | } |
5b5bb8c8 | 540 | return 0; |
a80ed305 JG |
541 | } |
542 | ||
543 | static | |
544 | void worker_sighandler(int sig) | |
545 | { | |
546 | const char *signame; | |
547 | ||
548 | /* | |
549 | * The worker will its parent's signals since they are part of the same | |
550 | * process group. However, in the case of SIGINT and SIGTERM, we want | |
551 | * to give the worker a chance to teardown gracefully when its parent | |
552 | * closes the command socket. | |
553 | */ | |
554 | switch (sig) { | |
555 | case SIGINT: | |
556 | signame = "SIGINT"; | |
557 | break; | |
558 | case SIGTERM: | |
559 | signame = "SIGTERM"; | |
560 | break; | |
561 | default: | |
562 | signame = "Unknown"; | |
563 | } | |
564 | ||
565 | DBG("run_as worker received signal %s", signame); | |
566 | } | |
567 | ||
568 | static | |
569 | int set_worker_sighandlers(void) | |
570 | { | |
571 | int ret = 0; | |
572 | sigset_t sigset; | |
573 | struct sigaction sa; | |
574 | ||
575 | if ((ret = sigemptyset(&sigset)) < 0) { | |
576 | PERROR("sigemptyset"); | |
577 | goto end; | |
578 | } | |
579 | ||
580 | sa.sa_handler = worker_sighandler; | |
581 | sa.sa_mask = sigset; | |
582 | sa.sa_flags = 0; | |
583 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
584 | PERROR("sigaction SIGINT"); | |
585 | goto end; | |
586 | } | |
587 | ||
588 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
589 | PERROR("sigaction SIGTERM"); | |
590 | goto end; | |
591 | } | |
592 | ||
593 | DBG("run_as signal handler set for SIGTERM and SIGINT"); | |
594 | end: | |
595 | return ret; | |
f8f66d38 JG |
596 | } |
597 | ||
7567352f MD |
598 | LTTNG_HIDDEN |
599 | int run_as_create_worker(char *procname) | |
600 | { | |
601 | pid_t pid; | |
602 | int i, ret = 0; | |
603 | ssize_t readlen; | |
604 | struct run_as_ret recvret; | |
605 | struct run_as_worker *worker; | |
606 | ||
749b7a0c JG |
607 | pthread_mutex_lock(&worker_lock); |
608 | assert(!global_worker); | |
7567352f | 609 | if (!use_clone()) { |
749b7a0c JG |
610 | /* |
611 | * Don't initialize a worker, all run_as tasks will be performed | |
612 | * in the current process. | |
613 | */ | |
7567352f MD |
614 | ret = 0; |
615 | goto end; | |
616 | } | |
617 | worker = zmalloc(sizeof(*worker)); | |
618 | if (!worker) { | |
619 | ret = -ENOMEM; | |
620 | goto end; | |
621 | } | |
622 | worker->procname = procname; | |
623 | /* Create unix socket. */ | |
624 | if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) { | |
625 | ret = -1; | |
626 | goto error_sock; | |
627 | } | |
628 | /* Fork worker. */ | |
629 | pid = fork(); | |
630 | if (pid < 0) { | |
631 | PERROR("fork"); | |
632 | ret = -1; | |
633 | goto error_fork; | |
634 | } else if (pid == 0) { | |
635 | /* Child */ | |
636 | ||
f8f66d38 JG |
637 | reset_sighandler(); |
638 | ||
a80ed305 JG |
639 | set_worker_sighandlers(); |
640 | ||
749b7a0c JG |
641 | /* The child has no use for this lock. */ |
642 | pthread_mutex_unlock(&worker_lock); | |
7567352f MD |
643 | /* Just close, no shutdown. */ |
644 | if (close(worker->sockpair[0])) { | |
645 | PERROR("close"); | |
646 | exit(EXIT_FAILURE); | |
647 | } | |
648 | worker->sockpair[0] = -1; | |
649 | ret = run_as_worker(worker); | |
650 | if (lttcomm_close_unix_sock(worker->sockpair[1])) { | |
651 | PERROR("close"); | |
652 | ret = -1; | |
653 | } | |
654 | worker->sockpair[1] = -1; | |
978d5d79 | 655 | LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret); |
7567352f MD |
656 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); |
657 | } else { | |
658 | /* Parent */ | |
659 | ||
660 | /* Just close, no shutdown. */ | |
661 | if (close(worker->sockpair[1])) { | |
662 | PERROR("close"); | |
663 | ret = -1; | |
664 | goto error_fork; | |
665 | } | |
666 | worker->sockpair[1] = -1; | |
667 | worker->pid = pid; | |
668 | /* Wait for worker to become ready. */ | |
669 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], | |
670 | &recvret, sizeof(recvret)); | |
671 | if (readlen < sizeof(recvret)) { | |
672 | ERR("readlen: %zd", readlen); | |
673 | PERROR("Error reading response from run_as at creation"); | |
674 | ret = -1; | |
675 | goto error_fork; | |
676 | } | |
677 | global_worker = worker; | |
678 | } | |
679 | end: | |
749b7a0c | 680 | pthread_mutex_unlock(&worker_lock); |
7567352f MD |
681 | return ret; |
682 | ||
683 | /* Error handling. */ | |
684 | error_fork: | |
685 | for (i = 0; i < 2; i++) { | |
686 | if (worker->sockpair[i] < 0) { | |
687 | continue; | |
688 | } | |
689 | if (lttcomm_close_unix_sock(worker->sockpair[i])) { | |
690 | PERROR("close"); | |
691 | } | |
692 | worker->sockpair[i] = -1; | |
693 | } | |
694 | error_sock: | |
695 | free(worker); | |
749b7a0c | 696 | pthread_mutex_unlock(&worker_lock); |
7567352f MD |
697 | return ret; |
698 | } | |
699 | ||
700 | LTTNG_HIDDEN | |
701 | void run_as_destroy_worker(void) | |
702 | { | |
703 | struct run_as_worker *worker = global_worker; | |
7567352f | 704 | |
978d5d79 | 705 | DBG("Destroying run_as worker"); |
749b7a0c | 706 | pthread_mutex_lock(&worker_lock); |
7567352f | 707 | if (!worker) { |
749b7a0c | 708 | goto end; |
7567352f MD |
709 | } |
710 | /* Close unix socket */ | |
978d5d79 | 711 | DBG("Closing run_as worker socket"); |
7567352f MD |
712 | if (lttcomm_close_unix_sock(worker->sockpair[0])) { |
713 | PERROR("close"); | |
714 | } | |
715 | worker->sockpair[0] = -1; | |
716 | /* Wait for worker. */ | |
f8f66d38 JG |
717 | for (;;) { |
718 | int status; | |
719 | pid_t wait_ret; | |
720 | ||
721 | wait_ret = waitpid(worker->pid, &status, 0); | |
722 | if (wait_ret < 0) { | |
723 | if (errno == EINTR) { | |
724 | continue; | |
725 | } | |
726 | PERROR("waitpid"); | |
727 | break; | |
728 | } | |
729 | ||
730 | if (WIFEXITED(status)) { | |
731 | LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR, | |
732 | DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d", | |
733 | WEXITSTATUS(status)); | |
734 | break; | |
735 | } else if (WIFSIGNALED(status)) { | |
736 | ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d", | |
737 | WTERMSIG(status)); | |
738 | break; | |
739 | } | |
7567352f MD |
740 | } |
741 | free(worker); | |
742 | global_worker = NULL; | |
749b7a0c JG |
743 | end: |
744 | pthread_mutex_unlock(&worker_lock); | |
4628484a | 745 | } |