Fix: Memory leak on run_as worker restart error path
[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 < 0;
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 error_procname_alloc;
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->procname);
964 error_procname_alloc:
965 free(worker);
966 return ret;
967 }
968
969 static
970 int run_as_restart_worker(struct run_as_worker *worker)
971 {
972 int ret = 0;
973 char *procname = NULL;
974
975 procname = worker->procname;
976
977 /* Close socket to run_as worker process and clean up the zombie process */
978 run_as_destroy_worker();
979
980 /* Create a new run_as worker process*/
981 ret = run_as_create_worker_no_lock(procname);
982 if (ret < 0 ) {
983 ERR("Restarting the worker process failed");
984 ret = -1;
985 goto err;
986 }
987 err:
988 return ret;
989 }
990
991 static
992 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
993 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
994 {
995 int ret, saved_errno;
996
997 pthread_mutex_lock(&worker_lock);
998 if (use_clone()) {
999 DBG("Using run_as worker");
1000
1001 assert(global_worker);
1002
1003 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1004 saved_errno = ret_value->_errno;
1005
1006 /*
1007 * If the worker thread crashed the errno is set to EIO. we log
1008 * the error and start a new worker process.
1009 */
1010 if (ret == -1 && saved_errno == EIO) {
1011 DBG("Socket closed unexpectedly... "
1012 "Restarting the worker process");
1013 ret = run_as_restart_worker(global_worker);
1014 if (ret == -1) {
1015 ERR("Failed to restart worker process.");
1016 goto err;
1017 }
1018 }
1019 } else {
1020 DBG("Using run_as without worker");
1021 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1022 }
1023 err:
1024 pthread_mutex_unlock(&worker_lock);
1025 return ret;
1026 }
1027
1028 LTTNG_HIDDEN
1029 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1030 {
1031 struct run_as_data data;
1032 struct run_as_ret ret;
1033
1034 memset(&data, 0, sizeof(data));
1035 memset(&ret, 0, sizeof(ret));
1036 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
1037 path, (int) mode, (int) uid, (int) gid);
1038 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
1039 data.u.mkdir.path[PATH_MAX - 1] = '\0';
1040 data.u.mkdir.mode = mode;
1041
1042 run_as(RUN_AS_MKDIR_RECURSIVE, &data, &ret, uid, gid);
1043 errno = ret._errno;
1044 return ret.u.mkdir.ret;
1045 }
1046
1047 LTTNG_HIDDEN
1048 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1049 {
1050 struct run_as_data data;
1051 struct run_as_ret ret;
1052
1053 memset(&data, 0, sizeof(data));
1054 memset(&ret, 0, sizeof(ret));
1055
1056 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
1057 path, (int) mode, (int) uid, (int) gid);
1058 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
1059 data.u.mkdir.path[PATH_MAX - 1] = '\0';
1060 data.u.mkdir.mode = mode;
1061 run_as(RUN_AS_MKDIR, &data, &ret, uid, gid);
1062 errno = ret._errno;
1063 return ret.u.mkdir.ret;
1064 }
1065
1066 LTTNG_HIDDEN
1067 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
1068 {
1069 struct run_as_data data;
1070 struct run_as_ret ret;
1071
1072 memset(&data, 0, sizeof(data));
1073 memset(&ret, 0, sizeof(ret));
1074
1075 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
1076 path, flags, (int) mode, (int) uid, (int) gid);
1077 strncpy(data.u.open.path, path, PATH_MAX - 1);
1078 data.u.open.path[PATH_MAX - 1] = '\0';
1079 data.u.open.flags = flags;
1080 data.u.open.mode = mode;
1081 run_as(RUN_AS_OPEN, &data, &ret, uid, gid);
1082 errno = ret._errno;
1083 ret.u.open.ret = ret.fd;
1084 return ret.u.open.ret;
1085 }
1086
1087 LTTNG_HIDDEN
1088 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1089 {
1090 struct run_as_data data;
1091 struct run_as_ret ret;
1092
1093 memset(&data, 0, sizeof(data));
1094 memset(&ret, 0, sizeof(ret));
1095
1096 DBG3("unlink() %s with for uid %d and gid %d",
1097 path, (int) uid, (int) gid);
1098 strncpy(data.u.unlink.path, path, PATH_MAX - 1);
1099 data.u.unlink.path[PATH_MAX - 1] = '\0';
1100 run_as(RUN_AS_UNLINK, &data, &ret, uid, gid);
1101 errno = ret._errno;
1102 return ret.u.unlink.ret;
1103 }
1104
1105 LTTNG_HIDDEN
1106 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
1107 {
1108 struct run_as_data data;
1109 struct run_as_ret ret;
1110
1111 memset(&data, 0, sizeof(data));
1112 memset(&ret, 0, sizeof(ret));
1113
1114 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1115 path, (int) uid, (int) gid);
1116 strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
1117 data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
1118 run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid);
1119 errno = ret._errno;
1120 return ret.u.rmdir_recursive.ret;
1121 }
1122
1123 LTTNG_HIDDEN
1124 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1125 uid_t uid, gid_t gid, uint64_t *offset)
1126 {
1127 struct run_as_data data;
1128 struct run_as_ret ret;
1129
1130 memset(&data, 0, sizeof(data));
1131 memset(&ret, 0, sizeof(ret));
1132
1133 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1134 "with for uid %d and gid %d", fd, function, (int) uid, (int) gid);
1135
1136 data.fd = fd;
1137
1138 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1139
1140 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1141
1142 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid);
1143
1144 errno = ret._errno;
1145
1146 if (ret._error) {
1147 return -1;
1148 }
1149
1150 *offset = ret.u.extract_elf_symbol_offset.offset;
1151 return 0;
1152 }
1153
1154 LTTNG_HIDDEN
1155 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1156 const char* probe_name, uid_t uid, gid_t gid,
1157 uint64_t **offsets, uint32_t *num_offset)
1158 {
1159 struct run_as_data data;
1160 struct run_as_ret ret;
1161
1162 memset(&data, 0, sizeof(data));
1163 memset(&ret, 0, sizeof(ret));
1164
1165 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1166 "provider_name=%s with for uid %d and gid %d", fd, probe_name,
1167 provider_name, (int) uid, (int) gid);
1168
1169 data.fd = fd;
1170
1171 strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1);
1172 strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1);
1173
1174 data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1175 data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1176
1177 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid);
1178
1179 errno = ret._errno;
1180
1181 if (ret._error) {
1182 return -1;
1183 }
1184
1185 *num_offset = ret.u.extract_sdt_probe_offsets.num_offset;
1186
1187 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1188 if (!*offsets) {
1189 return -ENOMEM;
1190 }
1191
1192 memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t));
1193 return 0;
1194 }
1195
1196 LTTNG_HIDDEN
1197 int run_as_create_worker(const char *procname)
1198 {
1199 int ret;
1200
1201 pthread_mutex_lock(&worker_lock);
1202 ret = run_as_create_worker_no_lock(procname);
1203 pthread_mutex_unlock(&worker_lock);
1204 return ret;
1205 }
1206
1207 LTTNG_HIDDEN
1208 void run_as_destroy_worker(void)
1209 {
1210 struct run_as_worker *worker = global_worker;
1211
1212 DBG("Destroying run_as worker");
1213 pthread_mutex_lock(&worker_lock);
1214 if (!worker) {
1215 goto end;
1216 }
1217 /* Close unix socket */
1218 DBG("Closing run_as worker socket");
1219 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1220 PERROR("close");
1221 }
1222 worker->sockpair[0] = -1;
1223 /* Wait for worker. */
1224 for (;;) {
1225 int status;
1226 pid_t wait_ret;
1227
1228 wait_ret = waitpid(worker->pid, &status, 0);
1229 if (wait_ret < 0) {
1230 if (errno == EINTR) {
1231 continue;
1232 }
1233 PERROR("waitpid");
1234 break;
1235 }
1236
1237 if (WIFEXITED(status)) {
1238 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1239 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1240 WEXITSTATUS(status));
1241 break;
1242 } else if (WIFSIGNALED(status)) {
1243 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1244 WTERMSIG(status));
1245 break;
1246 }
1247 }
1248 free(worker->procname);
1249 free(worker);
1250 global_worker = NULL;
1251 end:
1252 pthread_mutex_unlock(&worker_lock);
1253 }
This page took 0.058587 seconds and 5 git commands to generate.