Fix: global run_as worker lock released during restart
[lttng-tools.git] / src / common / runas.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
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.
17 */
18
19 #define _LGPL_SOURCE
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>
30 #include <sched.h>
31 #include <signal.h>
32 #include <assert.h>
33 #include <signal.h>
34
35 #include <common/lttng-kernel.h>
36 #include <common/common.h>
37 #include <common/utils.h>
38 #include <common/compat/getenv.h>
39 #include <common/compat/prctl.h>
40 #include <common/unix.h>
41 #include <common/defaults.h>
42 #include <common/lttng-elf.h>
43
44 #include <lttng/constant.h>
45
46 #include "runas.h"
47
48 struct run_as_data;
49 struct run_as_ret;
50 typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
51
52 struct run_as_mkdir_data {
53 char path[PATH_MAX];
54 mode_t mode;
55 };
56
57 struct run_as_open_data {
58 char path[PATH_MAX];
59 int flags;
60 mode_t mode;
61 };
62
63 struct run_as_unlink_data {
64 char path[PATH_MAX];
65 };
66
67 struct run_as_rmdir_recursive_data {
68 char path[PATH_MAX];
69 };
70
71 struct run_as_extract_elf_symbol_offset_data {
72 char function[LTTNG_SYMBOL_NAME_LEN];
73 };
74
75 struct run_as_extract_sdt_probe_offsets_data {
76 char probe_name[LTTNG_SYMBOL_NAME_LEN];
77 char provider_name[LTTNG_SYMBOL_NAME_LEN];
78 };
79
80 struct run_as_mkdir_ret {
81 int ret;
82 };
83
84 struct run_as_open_ret {
85 int ret;
86 };
87
88 struct run_as_unlink_ret {
89 int ret;
90 };
91
92 struct run_as_rmdir_recursive_ret {
93 int ret;
94 };
95
96 struct run_as_extract_elf_symbol_offset_ret {
97 uint64_t offset;
98 };
99
100 struct run_as_extract_sdt_probe_offsets_ret {
101 uint32_t num_offset;
102 uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM];
103 };
104
105 enum run_as_cmd {
106 RUN_AS_MKDIR,
107 RUN_AS_OPEN,
108 RUN_AS_UNLINK,
109 RUN_AS_RMDIR_RECURSIVE,
110 RUN_AS_MKDIR_RECURSIVE,
111 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
112 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
113 };
114
115 struct run_as_data {
116 enum run_as_cmd cmd;
117 int fd;
118 union {
119 struct run_as_mkdir_data mkdir;
120 struct run_as_open_data open;
121 struct run_as_unlink_data unlink;
122 struct run_as_rmdir_recursive_data rmdir_recursive;
123 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
124 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
125 } u;
126 uid_t uid;
127 gid_t gid;
128 };
129
130 /*
131 * The run_as_ret structure holds the returned value and status of the command.
132 *
133 * The `u` union field holds the return value of the command; in most cases it
134 * represents the success or the failure of the command. In more complex
135 * commands, it holds a computed value.
136 *
137 * The _errno field is the errno recorded after the execution of the command.
138 *
139 * The _error fields is used the signify that return status of the command. For
140 * simple commands returning `int` the _error field will be the same as the
141 * ret_int field. In complex commands, it signify the success or failure of the
142 * command.
143 *
144 */
145 struct run_as_ret {
146 int fd;
147 union {
148 struct run_as_mkdir_ret mkdir;
149 struct run_as_open_ret open;
150 struct run_as_unlink_ret unlink;
151 struct run_as_rmdir_recursive_ret rmdir_recursive;
152 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset;
153 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets;
154 } u;
155 int _errno;
156 bool _error;
157 };
158
159 struct run_as_worker {
160 pid_t pid; /* Worker PID. */
161 int sockpair[2];
162 char *procname;
163 };
164
165 /* Single global worker per process (for now). */
166 static struct run_as_worker *global_worker;
167 /* Lock protecting the worker. */
168 static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
169
170 #ifdef VALGRIND
171 static
172 int use_clone(void)
173 {
174 return 0;
175 }
176 #else
177 static
178 int use_clone(void)
179 {
180 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
181 }
182 #endif
183
184 LTTNG_HIDDEN
185 int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
186
187 /*
188 * Create recursively directory using the FULL path.
189 */
190 static
191 int _mkdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
192 {
193 const char *path;
194 mode_t mode;
195
196 path = data->u.mkdir.path;
197 mode = data->u.mkdir.mode;
198
199 /* Safe to call as we have transitioned to the requested uid/gid. */
200 ret_value->u.mkdir.ret = _utils_mkdir_recursive_unsafe(path, mode);
201 ret_value->_errno = errno;
202 ret_value->_error = (ret_value->u.mkdir.ret) ? true : false;
203 return ret_value->u.mkdir.ret;
204 }
205
206 static
207 int _mkdir(struct run_as_data *data, struct run_as_ret *ret_value)
208 {
209 ret_value->u.mkdir.ret = mkdir(data->u.mkdir.path, data->u.mkdir.mode);
210 ret_value->_errno = errno;
211 ret_value->_error = (ret_value->u.mkdir.ret) ? true : false;
212 return ret_value->u.mkdir.ret;
213 }
214
215 static
216 int _open(struct run_as_data *data, struct run_as_ret *ret_value)
217 {
218 ret_value->u.open.ret = open(data->u.open.path, data->u.open.flags, data->u.open.mode);
219 ret_value->fd = ret_value->u.open.ret;
220 ret_value->_errno = errno;
221 ret_value->_error = (ret_value->u.open.ret) ? true : false;
222 return ret_value->u.open.ret;
223 }
224
225 static
226 int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
227 {
228 ret_value->u.unlink.ret = unlink(data->u.unlink.path);
229 ret_value->_errno = errno;
230 ret_value->_error = (ret_value->u.unlink.ret) ? true : false;
231 return ret_value->u.unlink.ret;
232 }
233
234 static
235 int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
236 {
237 ret_value->u.rmdir_recursive.ret = utils_recursive_rmdir(data->u.rmdir_recursive.path);
238 ret_value->_errno = errno;
239 ret_value->_error = (ret_value->u.rmdir_recursive.ret) ? true : false;
240 return ret_value->u.rmdir_recursive.ret;
241 }
242
243 static
244 int _extract_elf_symbol_offset(struct run_as_data *data,
245 struct run_as_ret *ret_value)
246 {
247 int ret = 0;
248 ret_value->_error = false;
249
250 ret = lttng_elf_get_symbol_offset(data->fd,
251 data->u.extract_elf_symbol_offset.function,
252 &ret_value->u.extract_elf_symbol_offset.offset);
253 if (ret) {
254 DBG("Failed to extract ELF function offset");
255 ret_value->_error = true;
256 }
257
258 return ret;
259 }
260
261 static
262 int _extract_sdt_probe_offsets(struct run_as_data *data,
263 struct run_as_ret *ret_value)
264 {
265 int ret = 0;
266 uint64_t *offsets = NULL;
267 uint32_t num_offset;
268
269 ret_value->_error = false;
270
271 /* On success, this call allocates the offsets paramater. */
272 ret = lttng_elf_get_sdt_probe_offsets(data->fd,
273 data->u.extract_sdt_probe_offsets.provider_name,
274 data->u.extract_sdt_probe_offsets.probe_name,
275 &offsets, &num_offset);
276
277 if (ret) {
278 DBG("Failed to extract SDT probe offsets");
279 ret_value->_error = true;
280 goto end;
281 }
282
283 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) {
284 DBG("Wrong number of probes.");
285 ret = -1;
286 ret_value->_error = true;
287 goto free_offset;
288 }
289
290 /* Copy the content of the offsets array to the ret struct. */
291 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
292 offsets, num_offset * sizeof(uint64_t));
293
294 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
295
296 free_offset:
297 free(offsets);
298 end:
299 return ret;
300 }
301
302 static
303 run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
304 {
305 switch (cmd) {
306 case RUN_AS_MKDIR:
307 return _mkdir;
308 case RUN_AS_OPEN:
309 return _open;
310 case RUN_AS_UNLINK:
311 return _unlink;
312 case RUN_AS_RMDIR_RECURSIVE:
313 return _rmdir_recursive;
314 case RUN_AS_MKDIR_RECURSIVE:
315 return _mkdir_recursive;
316 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
317 return _extract_elf_symbol_offset;
318 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
319 return _extract_sdt_probe_offsets;
320 default:
321 ERR("Unknown command %d", (int) cmd);
322 return NULL;
323 }
324 }
325
326 static
327 int do_send_fd(int sock, int fd)
328 {
329 ssize_t len;
330
331 if (fd < 0) {
332 ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd);
333 /* Return 0 as this is not a fatal error. */
334 return 0;
335 }
336
337 len = lttcomm_send_fds_unix_sock(sock, &fd, 1);
338 if (len < 0) {
339 PERROR("lttcomm_send_fds_unix_sock");
340 return -1;
341 }
342 return 0;
343 }
344
345 static
346 int do_recv_fd(int sock, int *fd)
347 {
348 ssize_t len;
349
350 len = lttcomm_recv_fds_unix_sock(sock, fd, 1);
351
352 if (!len) {
353 return -1;
354 } else if (len < 0) {
355 PERROR("lttcomm_recv_fds_unix_sock");
356 return -1;
357 }
358 if (*fd < 0) {
359 ERR("Invalid file descriptor received from worker (fd = %i)", *fd);
360 /* Return 0 as this is not a fatal error. */
361 return 0;
362 }
363
364 return 0;
365 }
366
367 static
368 int send_fd_to_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
369 {
370 int ret = 0;
371
372 switch (cmd) {
373 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
374 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
375 break;
376 default:
377 return 0;
378 }
379
380 if (fd < 0) {
381 ERR("Refusing to send invalid fd to worker (fd = %i)", fd);
382 return -1;
383 }
384
385 ret = do_send_fd(worker->sockpair[0], fd);
386 if (ret < 0) {
387 PERROR("do_send_fd");
388 ret = -1;
389 }
390
391 return ret;
392 }
393
394 static
395 int send_fd_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
396 {
397 int ret = 0, ret_close = 0;
398
399 switch (cmd) {
400 case RUN_AS_OPEN:
401 break;
402 default:
403 return 0;
404 }
405
406 if (fd < 0) {
407 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd);
408 return 0;
409 }
410 ret = do_send_fd(worker->sockpair[1], fd);
411 if (ret < 0) {
412 PERROR("do_send_fd error");
413 ret = -1;
414 }
415
416 ret_close = close(fd);
417 if (ret_close < 0) {
418 PERROR("close");
419 }
420
421 return ret;
422 }
423
424 static
425 int recv_fd_from_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
426 {
427 int ret = 0;
428
429 switch (cmd) {
430 case RUN_AS_OPEN:
431 break;
432 default:
433 return 0;
434 }
435
436 ret = do_recv_fd(worker->sockpair[0], fd);
437 if (ret < 0) {
438 PERROR("do_recv_fd error");
439 ret = -1;
440 }
441
442 return ret;
443 }
444
445 static
446 int recv_fd_from_master(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
447 {
448 int ret = 0;
449
450 switch (cmd) {
451 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
452 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
453 break;
454 default:
455 return 0;
456 }
457
458 ret = do_recv_fd(worker->sockpair[1], fd);
459 if (ret < 0) {
460 PERROR("do_recv_fd error");
461 ret = -1;
462 }
463
464 return ret;
465 }
466
467 static
468 int cleanup_received_fd(enum run_as_cmd cmd, int fd)
469 {
470 int ret = 0;
471
472 switch (cmd) {
473 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
474 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
475 break;
476 default:
477 return 0;
478 }
479
480 if (fd < 0) {
481 return 0;
482 }
483 ret = close(fd);
484 if (ret < 0) {
485 PERROR("close error");
486 ret = -1;
487 }
488
489 return ret;
490 }
491
492 /*
493 * Return < 0 on error, 0 if OK, 1 on hangup.
494 */
495 static
496 int handle_one_cmd(struct run_as_worker *worker)
497 {
498 int ret = 0;
499 struct run_as_data data;
500 ssize_t readlen, writelen;
501 struct run_as_ret sendret;
502 run_as_fct cmd;
503 uid_t prev_euid;
504
505 memset(&sendret, 0, sizeof(sendret));
506 sendret.fd = -1;
507
508 /*
509 * Stage 1: Receive run_as_data struct from the master.
510 * The structure contains the command type and all the parameters needed for
511 * its execution
512 */
513 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
514 sizeof(data));
515 if (readlen == 0) {
516 /* hang up */
517 ret = 1;
518 goto end;
519 }
520 if (readlen < sizeof(data)) {
521 PERROR("lttcomm_recv_unix_sock error");
522 ret = -1;
523 goto end;
524 }
525
526 cmd = run_as_enum_to_fct(data.cmd);
527 if (!cmd) {
528 ret = -1;
529 goto end;
530 }
531
532 /*
533 * Stage 2: Receive file descriptor from master.
534 * Some commands need a file descriptor as input so if it's needed we
535 * receive the fd using the Unix socket.
536 */
537 ret = recv_fd_from_master(worker, data.cmd, &data.fd);
538 if (ret < 0) {
539 PERROR("recv_fd_from_master error");
540 ret = -1;
541 goto end;
542 }
543
544 prev_euid = getuid();
545 if (data.gid != getegid()) {
546 ret = setegid(data.gid);
547 if (ret < 0) {
548 sendret._error = true;
549 sendret._errno = errno;
550 PERROR("setegid");
551 goto write_return;
552 }
553 }
554 if (data.uid != prev_euid) {
555 ret = seteuid(data.uid);
556 if (ret < 0) {
557 sendret._error = true;
558 sendret._errno = errno;
559 PERROR("seteuid");
560 goto write_return;
561 }
562 }
563
564 /*
565 * Also set umask to 0 for mkdir executable bit.
566 */
567 umask(0);
568
569 /*
570 * Stage 3: Execute the command
571 */
572 ret = (*cmd)(&data, &sendret);
573 if (ret < 0) {
574 DBG("Execution of command returned an error");
575 }
576
577 write_return:
578 ret = cleanup_received_fd(data.cmd, data.fd);
579 if (ret < 0) {
580 ERR("Error cleaning up FD");
581 goto end;
582 }
583
584 /*
585 * Stage 4: Send run_as_ret structure to the master.
586 * This structure contain the return value of the command and the errno.
587 */
588 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
589 sizeof(sendret));
590 if (writelen < sizeof(sendret)) {
591 PERROR("lttcomm_send_unix_sock error");
592 ret = -1;
593 goto end;
594 }
595
596 /*
597 * Stage 5: Send file descriptor to the master
598 * Some commands return a file descriptor so if it's needed we pass it back
599 * to the master using the Unix socket.
600 */
601 ret = send_fd_to_master(worker, data.cmd, sendret.fd);
602 if (ret < 0) {
603 DBG("Sending FD to master returned an error");
604 goto end;
605 }
606
607 if (seteuid(prev_euid) < 0) {
608 PERROR("seteuid");
609 ret = -1;
610 goto end;
611 }
612 ret = 0;
613 end:
614 return ret;
615 }
616
617 static
618 int run_as_worker(struct run_as_worker *worker)
619 {
620 int ret;
621 ssize_t writelen;
622 struct run_as_ret sendret;
623 size_t proc_orig_len;
624
625 /*
626 * Initialize worker. Set a different process cmdline.
627 */
628 proc_orig_len = strlen(worker->procname);
629 memset(worker->procname, 0, proc_orig_len);
630 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
631
632 ret = lttng_prctl(PR_SET_NAME,
633 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
634 if (ret && ret != -ENOSYS) {
635 /* Don't fail as this is not essential. */
636 PERROR("prctl PR_SET_NAME");
637 }
638
639 memset(&sendret, 0, sizeof(sendret));
640
641 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
642 sizeof(sendret));
643 if (writelen < sizeof(sendret)) {
644 PERROR("lttcomm_send_unix_sock error");
645 ret = EXIT_FAILURE;
646 goto end;
647 }
648
649 for (;;) {
650 ret = handle_one_cmd(worker);
651 if (ret < 0) {
652 ret = EXIT_FAILURE;
653 goto end;
654 } else if (ret > 0) {
655 break;
656 } else {
657 continue; /* Next command. */
658 }
659 }
660 ret = EXIT_SUCCESS;
661 end:
662 return ret;
663 }
664
665 static
666 int run_as_cmd(struct run_as_worker *worker,
667 enum run_as_cmd cmd,
668 struct run_as_data *data,
669 struct run_as_ret *ret_value,
670 uid_t uid, gid_t gid)
671 {
672 int ret = 0;
673 ssize_t readlen, writelen;
674
675 /*
676 * If we are non-root, we can only deal with our own uid.
677 */
678 if (geteuid() != 0) {
679 if (uid != geteuid()) {
680 ret = -1;
681 ret_value->_errno = EPERM;
682 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
683 (int) uid, (int) geteuid());
684 goto end;
685 }
686 }
687
688 data->cmd = cmd;
689 data->uid = uid;
690 data->gid = gid;
691
692 /*
693 * Stage 1: Send the run_as_data struct to the worker process
694 */
695 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
696 sizeof(*data));
697 if (writelen < sizeof(*data)) {
698 PERROR("Error writing message to run_as");
699 ret = -1;
700 ret_value->_errno = EIO;
701 goto end;
702 }
703
704 /*
705 * Stage 2: Send file descriptor to the worker process if needed
706 */
707 ret = send_fd_to_worker(worker, data->cmd, data->fd);
708 if (ret) {
709 PERROR("do_send_fd error");
710 ret = -1;
711 ret_value->_errno = EIO;
712 goto end;
713 }
714
715 /*
716 * Stage 3: Wait for the execution of the command
717 */
718
719 /*
720 * Stage 4: Receive the run_as_ret struct containing the return value and
721 * errno
722 */
723 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
724 sizeof(*ret_value));
725 if (!readlen) {
726 ERR("Run-as worker has hung-up during run_as_cmd");
727 ret = -1;
728 ret_value->_errno = EIO;
729 goto end;
730 } else if (readlen < sizeof(*ret_value)) {
731 PERROR("Error reading response from run_as");
732 ret = -1;
733 ret_value->_errno = errno;
734 goto end;
735 }
736
737 if (ret_value->_error) {
738 /* Skip stage 5 on error as there will be no fd to receive. */
739 goto end;
740 }
741
742 /*
743 * Stage 5: Receive file descriptor if needed
744 */
745 ret = recv_fd_from_worker(worker, data->cmd, &ret_value->fd);
746 if (ret < 0) {
747 ERR("Error receiving fd");
748 ret = -1;
749 ret_value->_errno = EIO;
750 }
751
752 end:
753 return ret;
754 }
755
756 /*
757 * This is for debugging ONLY and should not be considered secure.
758 */
759 static
760 int run_as_noworker(enum run_as_cmd cmd,
761 struct run_as_data *data, struct run_as_ret *ret_value,
762 uid_t uid, gid_t gid)
763 {
764 int ret, saved_errno;
765 mode_t old_mask;
766 run_as_fct fct;
767
768 fct = run_as_enum_to_fct(cmd);
769 if (!fct) {
770 errno = -ENOSYS;
771 ret = -1;
772 goto end;
773 }
774 old_mask = umask(0);
775 ret = fct(data, ret_value);
776 saved_errno = ret_value->_errno;
777 umask(old_mask);
778 errno = saved_errno;
779 end:
780 return ret;
781 }
782
783 static
784 int reset_sighandler(void)
785 {
786 int sig;
787
788 DBG("Resetting run_as worker signal handlers to default");
789 for (sig = 1; sig <= 31; sig++) {
790 (void) signal(sig, SIG_DFL);
791 }
792 return 0;
793 }
794
795 static
796 void worker_sighandler(int sig)
797 {
798 const char *signame;
799
800 /*
801 * The worker will inherit its parent's signals since they are part of
802 * the same process group. However, in the case of SIGINT and SIGTERM,
803 * we want to give the worker a chance to teardown gracefully when its
804 * parent closes the command socket.
805 */
806 switch (sig) {
807 case SIGINT:
808 signame = "SIGINT";
809 break;
810 case SIGTERM:
811 signame = "SIGTERM";
812 break;
813 default:
814 signame = NULL;
815 }
816
817 if (signame) {
818 DBG("run_as worker received signal %s", signame);
819 } else {
820 DBG("run_as_worker received signal %d", sig);
821 }
822 }
823
824 static
825 int set_worker_sighandlers(void)
826 {
827 int ret = 0;
828 sigset_t sigset;
829 struct sigaction sa;
830
831 if ((ret = sigemptyset(&sigset)) < 0) {
832 PERROR("sigemptyset");
833 goto end;
834 }
835
836 sa.sa_handler = worker_sighandler;
837 sa.sa_mask = sigset;
838 sa.sa_flags = 0;
839 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
840 PERROR("sigaction SIGINT");
841 goto end;
842 }
843
844 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
845 PERROR("sigaction SIGTERM");
846 goto end;
847 }
848
849 DBG("run_as signal handler set for SIGTERM and SIGINT");
850 end:
851 return ret;
852 }
853
854 static
855 int run_as_create_worker_no_lock(const char *procname)
856 {
857 pid_t pid;
858 int i, ret = 0;
859 ssize_t readlen;
860 struct run_as_ret recvret;
861 struct run_as_worker *worker;
862
863 assert(!global_worker);
864 if (!use_clone()) {
865 /*
866 * Don't initialize a worker, all run_as tasks will be performed
867 * in the current process.
868 */
869 ret = 0;
870 goto end;
871 }
872 worker = zmalloc(sizeof(*worker));
873 if (!worker) {
874 ret = -ENOMEM;
875 goto end;
876 }
877 worker->procname = strdup(procname);
878 if (!worker->procname) {
879 ret = -ENOMEM;
880 goto end;
881 }
882 /* Create unix socket. */
883 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
884 ret = -1;
885 goto error_sock;
886 }
887
888 /* Fork worker. */
889 pid = fork();
890 if (pid < 0) {
891 PERROR("fork");
892 ret = -1;
893 goto error_fork;
894 } else if (pid == 0) {
895 /* Child */
896
897 reset_sighandler();
898
899 set_worker_sighandlers();
900
901 /* Just close, no shutdown. */
902 if (close(worker->sockpair[0])) {
903 PERROR("close");
904 exit(EXIT_FAILURE);
905 }
906
907 /*
908 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
909 * Sockpair[1] is used as a control channel with the master
910 */
911 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
912 if (i != worker->sockpair[1]) {
913 (void) close(i);
914 }
915 }
916
917 worker->sockpair[0] = -1;
918 ret = run_as_worker(worker);
919 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
920 PERROR("close");
921 ret = -1;
922 }
923 worker->sockpair[1] = -1;
924 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
925 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
926 } else {
927 /* Parent */
928
929 /* Just close, no shutdown. */
930 if (close(worker->sockpair[1])) {
931 PERROR("close");
932 ret = -1;
933 goto error_fork;
934 }
935 worker->sockpair[1] = -1;
936 worker->pid = pid;
937 /* Wait for worker to become ready. */
938 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
939 &recvret, sizeof(recvret));
940 if (readlen < sizeof(recvret)) {
941 ERR("readlen: %zd", readlen);
942 PERROR("Error reading response from run_as at creation");
943 ret = -1;
944 goto error_fork;
945 }
946 global_worker = worker;
947 }
948 end:
949 return ret;
950
951 /* Error handling. */
952 error_fork:
953 for (i = 0; i < 2; i++) {
954 if (worker->sockpair[i] < 0) {
955 continue;
956 }
957 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
958 PERROR("close");
959 }
960 worker->sockpair[i] = -1;
961 }
962 error_sock:
963 free(worker);
964 return ret;
965 }
966
967 static
968 int run_as_restart_worker(struct run_as_worker *worker)
969 {
970 int ret = 0;
971 char *procname = NULL;
972
973 procname = worker->procname;
974
975 /* Close socket to run_as worker process and clean up the zombie process */
976 run_as_destroy_worker();
977
978 /* Create a new run_as worker process*/
979 ret = run_as_create_worker_no_lock(procname);
980 if (ret < 0 ) {
981 ERR("Restarting the worker process failed");
982 ret = -1;
983 goto err;
984 }
985 err:
986 return ret;
987 }
988
989 static
990 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
991 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
992 {
993 int ret, saved_errno;
994
995 pthread_mutex_lock(&worker_lock);
996 if (use_clone()) {
997 DBG("Using run_as worker");
998
999 assert(global_worker);
1000
1001 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1002 saved_errno = ret_value->_errno;
1003
1004 /*
1005 * If the worker thread crashed the errno is set to EIO. we log
1006 * the error and start a new worker process.
1007 */
1008 if (ret == -1 && saved_errno == EIO) {
1009 DBG("Socket closed unexpectedly... "
1010 "Restarting the worker process");
1011 ret = run_as_restart_worker(global_worker);
1012 if (ret == -1) {
1013 ERR("Failed to restart worker process.");
1014 goto err;
1015 }
1016 }
1017 } else {
1018 DBG("Using run_as without worker");
1019 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1020 }
1021 err:
1022 pthread_mutex_unlock(&worker_lock);
1023 return ret;
1024 }
1025
1026 LTTNG_HIDDEN
1027 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1028 {
1029 struct run_as_data data;
1030 struct run_as_ret ret;
1031
1032 memset(&data, 0, sizeof(data));
1033 memset(&ret, 0, sizeof(ret));
1034 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
1035 path, (int) mode, (int) uid, (int) gid);
1036 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
1037 data.u.mkdir.path[PATH_MAX - 1] = '\0';
1038 data.u.mkdir.mode = mode;
1039
1040 run_as(RUN_AS_MKDIR_RECURSIVE, &data, &ret, uid, gid);
1041 errno = ret._errno;
1042 return ret.u.mkdir.ret;
1043 }
1044
1045 LTTNG_HIDDEN
1046 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1047 {
1048 struct run_as_data data;
1049 struct run_as_ret ret;
1050
1051 memset(&data, 0, sizeof(data));
1052 memset(&ret, 0, sizeof(ret));
1053
1054 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
1055 path, (int) mode, (int) uid, (int) gid);
1056 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
1057 data.u.mkdir.path[PATH_MAX - 1] = '\0';
1058 data.u.mkdir.mode = mode;
1059 run_as(RUN_AS_MKDIR, &data, &ret, uid, gid);
1060 errno = ret._errno;
1061 return ret.u.mkdir.ret;
1062 }
1063
1064 LTTNG_HIDDEN
1065 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
1066 {
1067 struct run_as_data data;
1068 struct run_as_ret ret;
1069
1070 memset(&data, 0, sizeof(data));
1071 memset(&ret, 0, sizeof(ret));
1072
1073 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
1074 path, flags, (int) mode, (int) uid, (int) gid);
1075 strncpy(data.u.open.path, path, PATH_MAX - 1);
1076 data.u.open.path[PATH_MAX - 1] = '\0';
1077 data.u.open.flags = flags;
1078 data.u.open.mode = mode;
1079 run_as(RUN_AS_OPEN, &data, &ret, uid, gid);
1080 errno = ret._errno;
1081 ret.u.open.ret = ret.fd;
1082 return ret.u.open.ret;
1083 }
1084
1085 LTTNG_HIDDEN
1086 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1087 {
1088 struct run_as_data data;
1089 struct run_as_ret ret;
1090
1091 memset(&data, 0, sizeof(data));
1092 memset(&ret, 0, sizeof(ret));
1093
1094 DBG3("unlink() %s with for uid %d and gid %d",
1095 path, (int) uid, (int) gid);
1096 strncpy(data.u.unlink.path, path, PATH_MAX - 1);
1097 data.u.unlink.path[PATH_MAX - 1] = '\0';
1098 run_as(RUN_AS_UNLINK, &data, &ret, uid, gid);
1099 errno = ret._errno;
1100 return ret.u.unlink.ret;
1101 }
1102
1103 LTTNG_HIDDEN
1104 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
1105 {
1106 struct run_as_data data;
1107 struct run_as_ret ret;
1108
1109 memset(&data, 0, sizeof(data));
1110 memset(&ret, 0, sizeof(ret));
1111
1112 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1113 path, (int) uid, (int) gid);
1114 strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
1115 data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
1116 run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid);
1117 errno = ret._errno;
1118 return ret.u.rmdir_recursive.ret;
1119 }
1120
1121 LTTNG_HIDDEN
1122 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1123 uid_t uid, gid_t gid, uint64_t *offset)
1124 {
1125 struct run_as_data data;
1126 struct run_as_ret ret;
1127
1128 memset(&data, 0, sizeof(data));
1129 memset(&ret, 0, sizeof(ret));
1130
1131 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1132 "with for uid %d and gid %d", fd, function, (int) uid, (int) gid);
1133
1134 data.fd = fd;
1135
1136 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1137
1138 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1139
1140 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid);
1141
1142 errno = ret._errno;
1143
1144 if (ret._error) {
1145 return -1;
1146 }
1147
1148 *offset = ret.u.extract_elf_symbol_offset.offset;
1149 return 0;
1150 }
1151
1152 LTTNG_HIDDEN
1153 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1154 const char* probe_name, uid_t uid, gid_t gid,
1155 uint64_t **offsets, uint32_t *num_offset)
1156 {
1157 struct run_as_data data;
1158 struct run_as_ret ret;
1159
1160 memset(&data, 0, sizeof(data));
1161 memset(&ret, 0, sizeof(ret));
1162
1163 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1164 "provider_name=%s with for uid %d and gid %d", fd, probe_name,
1165 provider_name, (int) uid, (int) gid);
1166
1167 data.fd = fd;
1168
1169 strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1);
1170 strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1);
1171
1172 data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1173 data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1174
1175 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid);
1176
1177 errno = ret._errno;
1178
1179 if (ret._error) {
1180 return -1;
1181 }
1182
1183 *num_offset = ret.u.extract_sdt_probe_offsets.num_offset;
1184
1185 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1186 if (!*offsets) {
1187 return -ENOMEM;
1188 }
1189
1190 memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t));
1191 return 0;
1192 }
1193
1194 LTTNG_HIDDEN
1195 int run_as_create_worker(const char *procname)
1196 {
1197 int ret;
1198
1199 pthread_mutex_lock(&worker_lock);
1200 ret = run_as_create_worker_no_lock(procname);
1201 pthread_mutex_unlock(&worker_lock);
1202 return ret;
1203 }
1204
1205 LTTNG_HIDDEN
1206 void run_as_destroy_worker(void)
1207 {
1208 struct run_as_worker *worker = global_worker;
1209
1210 DBG("Destroying run_as worker");
1211 pthread_mutex_lock(&worker_lock);
1212 if (!worker) {
1213 goto end;
1214 }
1215 /* Close unix socket */
1216 DBG("Closing run_as worker socket");
1217 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1218 PERROR("close");
1219 }
1220 worker->sockpair[0] = -1;
1221 /* Wait for worker. */
1222 for (;;) {
1223 int status;
1224 pid_t wait_ret;
1225
1226 wait_ret = waitpid(worker->pid, &status, 0);
1227 if (wait_ret < 0) {
1228 if (errno == EINTR) {
1229 continue;
1230 }
1231 PERROR("waitpid");
1232 break;
1233 }
1234
1235 if (WIFEXITED(status)) {
1236 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1237 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1238 WEXITSTATUS(status));
1239 break;
1240 } else if (WIFSIGNALED(status)) {
1241 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1242 WTERMSIG(status));
1243 break;
1244 }
1245 }
1246 free(worker->procname);
1247 free(worker);
1248 global_worker = NULL;
1249 end:
1250 pthread_mutex_unlock(&worker_lock);
1251 }
This page took 0.105281 seconds and 4 git commands to generate.