2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
26 #include <sys/types.h>
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>
44 #include <lttng/constant.h>
50 typedef int (*run_as_fct
)(struct run_as_data
*data
, struct run_as_ret
*ret_value
);
52 struct run_as_mkdirat_data
{
57 struct run_as_open_data
{
63 struct run_as_unlink_data
{
67 struct run_as_rmdir_recursive_data
{
71 struct run_as_extract_elf_symbol_offset_data
{
72 char function
[LTTNG_SYMBOL_NAME_LEN
];
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
];
80 struct run_as_mkdirat_ret
{
84 struct run_as_open_ret
{
88 struct run_as_unlink_ret
{
92 struct run_as_rmdir_recursive_ret
{
96 struct run_as_extract_elf_symbol_offset_ret
{
100 struct run_as_extract_sdt_probe_offsets_ret
{
102 uint64_t offsets
[LTTNG_KERNEL_MAX_UPROBE_NUM
];
108 RUN_AS_MKDIR_RECURSIVE
,
109 RUN_AS_MKDIRAT_RECURSIVE
,
112 RUN_AS_RMDIR_RECURSIVE
,
113 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
114 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
121 struct run_as_mkdirat_data mkdirat
;
122 struct run_as_open_data open
;
123 struct run_as_unlink_data unlink
;
124 struct run_as_rmdir_recursive_data rmdir_recursive
;
125 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset
;
126 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets
;
133 * The run_as_ret structure holds the returned value and status of the command.
135 * The `u` union field holds the return value of the command; in most cases it
136 * represents the success or the failure of the command. In more complex
137 * commands, it holds a computed value.
139 * The _errno field is the errno recorded after the execution of the command.
141 * The _error fields is used the signify that return status of the command. For
142 * simple commands returning `int` the _error field will be the same as the
143 * ret_int field. In complex commands, it signify the success or failure of the
150 struct run_as_mkdirat_ret mkdirat
;
151 struct run_as_open_ret open
;
152 struct run_as_unlink_ret unlink
;
153 struct run_as_rmdir_recursive_ret rmdir_recursive
;
154 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset
;
155 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets
;
161 struct run_as_worker
{
162 pid_t pid
; /* Worker PID. */
167 /* Single global worker per process (for now). */
168 static struct run_as_worker
*global_worker
;
169 /* Lock protecting the worker. */
170 static pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
182 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
187 * Create recursively directory using the FULL path.
190 int _mkdirat_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
194 struct lttng_directory_handle handle
;
196 path
= data
->u
.mkdirat
.path
;
197 mode
= data
->u
.mkdirat
.mode
;
199 (void) lttng_directory_handle_init_from_dirfd(&handle
, data
->fd
);
200 /* Safe to call as we have transitioned to the requested uid/gid. */
201 ret_value
->u
.mkdirat
.ret
=
202 lttng_directory_handle_create_subdirectory_recursive(
203 &handle
, path
, mode
);
204 ret_value
->_errno
= errno
;
205 ret_value
->_error
= (ret_value
->u
.mkdirat
.ret
) ? true : false;
206 lttng_directory_handle_fini(&handle
);
207 return ret_value
->u
.mkdirat
.ret
;
211 int _mkdirat(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
215 struct lttng_directory_handle handle
;
217 path
= data
->u
.mkdirat
.path
;
218 mode
= data
->u
.mkdirat
.mode
;
220 (void) lttng_directory_handle_init_from_dirfd(&handle
, data
->fd
);
221 /* Safe to call as we have transitioned to the requested uid/gid. */
222 ret_value
->u
.mkdirat
.ret
=
223 lttng_directory_handle_create_subdirectory(
224 &handle
, path
, mode
);
225 ret_value
->_errno
= errno
;
226 ret_value
->_error
= (ret_value
->u
.mkdirat
.ret
) ? true : false;
227 lttng_directory_handle_fini(&handle
);
228 return ret_value
->u
.mkdirat
.ret
;
232 int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
234 ret_value
->u
.open
.ret
= open(data
->u
.open
.path
, data
->u
.open
.flags
, data
->u
.open
.mode
);
235 ret_value
->fd
= ret_value
->u
.open
.ret
;
236 ret_value
->_errno
= errno
;
237 ret_value
->_error
= ret_value
->u
.open
.ret
< 0;
238 return ret_value
->u
.open
.ret
;
242 int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
244 ret_value
->u
.unlink
.ret
= unlink(data
->u
.unlink
.path
);
245 ret_value
->_errno
= errno
;
246 ret_value
->_error
= (ret_value
->u
.unlink
.ret
) ? true : false;
247 return ret_value
->u
.unlink
.ret
;
251 int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
253 ret_value
->u
.rmdir_recursive
.ret
= utils_recursive_rmdir(data
->u
.rmdir_recursive
.path
);
254 ret_value
->_errno
= errno
;
255 ret_value
->_error
= (ret_value
->u
.rmdir_recursive
.ret
) ? true : false;
256 return ret_value
->u
.rmdir_recursive
.ret
;
261 int _extract_elf_symbol_offset(struct run_as_data
*data
,
262 struct run_as_ret
*ret_value
)
265 ret_value
->_error
= false;
267 ret
= lttng_elf_get_symbol_offset(data
->fd
,
268 data
->u
.extract_elf_symbol_offset
.function
,
269 &ret_value
->u
.extract_elf_symbol_offset
.offset
);
271 DBG("Failed to extract ELF function offset");
272 ret_value
->_error
= true;
279 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
280 struct run_as_ret
*ret_value
)
283 uint64_t *offsets
= NULL
;
286 ret_value
->_error
= false;
288 /* On success, this call allocates the offsets paramater. */
289 ret
= lttng_elf_get_sdt_probe_offsets(data
->fd
,
290 data
->u
.extract_sdt_probe_offsets
.provider_name
,
291 data
->u
.extract_sdt_probe_offsets
.probe_name
,
292 &offsets
, &num_offset
);
295 DBG("Failed to extract SDT probe offsets");
296 ret_value
->_error
= true;
300 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_MAX_UPROBE_NUM
) {
301 DBG("Wrong number of probes.");
303 ret_value
->_error
= true;
307 /* Copy the content of the offsets array to the ret struct. */
308 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
309 offsets
, num_offset
* sizeof(uint64_t));
311 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
320 int _extract_elf_symbol_offset(struct run_as_data
*data
,
321 struct run_as_ret
*ret_value
)
323 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
328 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
329 struct run_as_ret
*ret_value
)
331 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
337 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
343 case RUN_AS_MKDIR_RECURSIVE
:
344 case RUN_AS_MKDIRAT_RECURSIVE
:
345 return _mkdirat_recursive
;
350 case RUN_AS_RMDIR_RECURSIVE
:
351 return _rmdir_recursive
;
352 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
353 return _extract_elf_symbol_offset
;
354 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
355 return _extract_sdt_probe_offsets
;
357 ERR("Unknown command %d", (int) cmd
);
363 int do_send_fd(int sock
, int fd
)
368 ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd
);
369 /* Return 0 as this is not a fatal error. */
373 len
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
375 PERROR("lttcomm_send_fds_unix_sock");
382 int do_recv_fd(int sock
, int *fd
)
386 len
= lttcomm_recv_fds_unix_sock(sock
, fd
, 1);
390 } else if (len
< 0) {
391 PERROR("lttcomm_recv_fds_unix_sock");
395 ERR("Invalid file descriptor received from worker (fd = %i)", *fd
);
396 /* Return 0 as this is not a fatal error. */
404 int send_fd_to_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
409 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
410 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
412 case RUN_AS_MKDIRAT_RECURSIVE
:
419 ERR("Refusing to send invalid fd to worker (fd = %i)", fd
);
423 ret
= do_send_fd(worker
->sockpair
[0], fd
);
425 PERROR("do_send_fd");
433 int send_fd_to_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
435 int ret
= 0, ret_close
= 0;
445 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd
);
448 ret
= do_send_fd(worker
->sockpair
[1], fd
);
450 PERROR("do_send_fd error");
454 ret_close
= close(fd
);
463 int recv_fd_from_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
474 ret
= do_recv_fd(worker
->sockpair
[0], fd
);
476 PERROR("do_recv_fd error");
484 int recv_fd_from_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
489 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
490 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
492 case RUN_AS_MKDIRAT_RECURSIVE
:
495 case RUN_AS_MKDIR_RECURSIVE
:
502 ret
= do_recv_fd(worker
->sockpair
[1], fd
);
504 PERROR("do_recv_fd error");
512 int cleanup_received_fd(enum run_as_cmd cmd
, int fd
)
517 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
518 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
520 case RUN_AS_MKDIRAT_RECURSIVE
:
531 PERROR("close error");
539 * Return < 0 on error, 0 if OK, 1 on hangup.
542 int handle_one_cmd(struct run_as_worker
*worker
)
545 struct run_as_data data
;
546 ssize_t readlen
, writelen
;
547 struct run_as_ret sendret
;
551 memset(&sendret
, 0, sizeof(sendret
));
555 * Stage 1: Receive run_as_data struct from the master.
556 * The structure contains the command type and all the parameters needed for
559 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
566 if (readlen
< sizeof(data
)) {
567 PERROR("lttcomm_recv_unix_sock error");
572 cmd
= run_as_enum_to_fct(data
.cmd
);
579 * Stage 2: Receive file descriptor from master.
580 * Some commands need a file descriptor as input so if it's needed we
581 * receive the fd using the Unix socket.
583 ret
= recv_fd_from_master(worker
, data
.cmd
, &data
.fd
);
585 PERROR("recv_fd_from_master error");
590 prev_euid
= getuid();
591 if (data
.gid
!= getegid()) {
592 ret
= setegid(data
.gid
);
594 sendret
._error
= true;
595 sendret
._errno
= errno
;
600 if (data
.uid
!= prev_euid
) {
601 ret
= seteuid(data
.uid
);
603 sendret
._error
= true;
604 sendret
._errno
= errno
;
611 * Also set umask to 0 for mkdir executable bit.
616 * Stage 3: Execute the command
618 ret
= (*cmd
)(&data
, &sendret
);
620 DBG("Execution of command returned an error");
624 ret
= cleanup_received_fd(data
.cmd
, data
.fd
);
626 ERR("Error cleaning up FD");
631 * Stage 4: Send run_as_ret structure to the master.
632 * This structure contain the return value of the command and the errno.
634 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
636 if (writelen
< sizeof(sendret
)) {
637 PERROR("lttcomm_send_unix_sock error");
643 * Stage 5: Send file descriptor to the master
644 * Some commands return a file descriptor so if it's needed we pass it back
645 * to the master using the Unix socket.
647 ret
= send_fd_to_master(worker
, data
.cmd
, sendret
.fd
);
649 DBG("Sending FD to master returned an error");
653 if (seteuid(prev_euid
) < 0) {
664 int run_as_worker(struct run_as_worker
*worker
)
668 struct run_as_ret sendret
;
669 size_t proc_orig_len
;
672 * Initialize worker. Set a different process cmdline.
674 proc_orig_len
= strlen(worker
->procname
);
675 memset(worker
->procname
, 0, proc_orig_len
);
676 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
678 ret
= lttng_prctl(PR_SET_NAME
,
679 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
680 if (ret
&& ret
!= -ENOSYS
) {
681 /* Don't fail as this is not essential. */
682 PERROR("prctl PR_SET_NAME");
685 memset(&sendret
, 0, sizeof(sendret
));
687 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
689 if (writelen
< sizeof(sendret
)) {
690 PERROR("lttcomm_send_unix_sock error");
696 ret
= handle_one_cmd(worker
);
700 } else if (ret
> 0) {
703 continue; /* Next command. */
712 int run_as_cmd(struct run_as_worker
*worker
,
714 struct run_as_data
*data
,
715 struct run_as_ret
*ret_value
,
716 uid_t uid
, gid_t gid
)
719 ssize_t readlen
, writelen
;
722 * If we are non-root, we can only deal with our own uid.
724 if (geteuid() != 0) {
725 if (uid
!= geteuid()) {
727 ret_value
->_errno
= EPERM
;
728 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
729 (int) uid
, (int) geteuid());
739 * Stage 1: Send the run_as_data struct to the worker process
741 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
743 if (writelen
< sizeof(*data
)) {
744 PERROR("Error writing message to run_as");
746 ret_value
->_errno
= EIO
;
751 * Stage 2: Send file descriptor to the worker process if needed
753 ret
= send_fd_to_worker(worker
, data
->cmd
, data
->fd
);
755 PERROR("do_send_fd error");
757 ret_value
->_errno
= EIO
;
762 * Stage 3: Wait for the execution of the command
766 * Stage 4: Receive the run_as_ret struct containing the return value and
769 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
772 ERR("Run-as worker has hung-up during run_as_cmd");
774 ret_value
->_errno
= EIO
;
776 } else if (readlen
< sizeof(*ret_value
)) {
777 PERROR("Error reading response from run_as");
779 ret_value
->_errno
= errno
;
783 if (ret_value
->_error
) {
784 /* Skip stage 5 on error as there will be no fd to receive. */
789 * Stage 5: Receive file descriptor if needed
791 ret
= recv_fd_from_worker(worker
, data
->cmd
, &ret_value
->fd
);
793 ERR("Error receiving fd");
795 ret_value
->_errno
= EIO
;
803 * This is for debugging ONLY and should not be considered secure.
806 int run_as_noworker(enum run_as_cmd cmd
,
807 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
808 uid_t uid
, gid_t gid
)
810 int ret
, saved_errno
;
814 fct
= run_as_enum_to_fct(cmd
);
821 ret
= fct(data
, ret_value
);
822 saved_errno
= ret_value
->_errno
;
830 int reset_sighandler(void)
834 DBG("Resetting run_as worker signal handlers to default");
835 for (sig
= 1; sig
<= 31; sig
++) {
836 (void) signal(sig
, SIG_DFL
);
842 void worker_sighandler(int sig
)
847 * The worker will inherit its parent's signals since they are part of
848 * the same process group. However, in the case of SIGINT and SIGTERM,
849 * we want to give the worker a chance to teardown gracefully when its
850 * parent closes the command socket.
864 DBG("run_as worker received signal %s", signame
);
866 DBG("run_as_worker received signal %d", sig
);
871 int set_worker_sighandlers(void)
877 if ((ret
= sigemptyset(&sigset
)) < 0) {
878 PERROR("sigemptyset");
882 sa
.sa_handler
= worker_sighandler
;
885 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
886 PERROR("sigaction SIGINT");
890 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
891 PERROR("sigaction SIGTERM");
895 DBG("run_as signal handler set for SIGTERM and SIGINT");
901 int run_as_create_worker_no_lock(const char *procname
,
902 post_fork_cleanup_cb clean_up_func
,
903 void *clean_up_user_data
)
908 struct run_as_ret recvret
;
909 struct run_as_worker
*worker
;
911 assert(!global_worker
);
914 * Don't initialize a worker, all run_as tasks will be performed
915 * in the current process.
920 worker
= zmalloc(sizeof(*worker
));
925 worker
->procname
= strdup(procname
);
926 if (!worker
->procname
) {
928 goto error_procname_alloc
;
930 /* Create unix socket. */
931 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
942 } else if (pid
== 0) {
947 set_worker_sighandlers();
949 if (clean_up_func(clean_up_user_data
) < 0) {
950 ERR("Run-as post-fork clean-up failed, exiting.");
955 /* Just close, no shutdown. */
956 if (close(worker
->sockpair
[0])) {
962 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
963 * Sockpair[1] is used as a control channel with the master
965 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
966 if (i
!= worker
->sockpair
[1]) {
971 worker
->sockpair
[0] = -1;
972 ret
= run_as_worker(worker
);
973 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
977 worker
->sockpair
[1] = -1;
978 free(worker
->procname
);
980 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
981 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
985 /* Just close, no shutdown. */
986 if (close(worker
->sockpair
[1])) {
991 worker
->sockpair
[1] = -1;
993 /* Wait for worker to become ready. */
994 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
995 &recvret
, sizeof(recvret
));
996 if (readlen
< sizeof(recvret
)) {
997 ERR("readlen: %zd", readlen
);
998 PERROR("Error reading response from run_as at creation");
1002 global_worker
= worker
;
1007 /* Error handling. */
1009 for (i
= 0; i
< 2; i
++) {
1010 if (worker
->sockpair
[i
] < 0) {
1013 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1016 worker
->sockpair
[i
] = -1;
1019 free(worker
->procname
);
1020 error_procname_alloc
:
1026 void run_as_destroy_worker_no_lock(void)
1028 struct run_as_worker
*worker
= global_worker
;
1030 DBG("Destroying run_as worker");
1034 /* Close unix socket */
1035 DBG("Closing run_as worker socket");
1036 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1039 worker
->sockpair
[0] = -1;
1040 /* Wait for worker. */
1045 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1047 if (errno
== EINTR
) {
1054 if (WIFEXITED(status
)) {
1055 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1056 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1057 WEXITSTATUS(status
));
1059 } else if (WIFSIGNALED(status
)) {
1060 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1065 free(worker
->procname
);
1067 global_worker
= NULL
;
1071 int run_as_restart_worker(struct run_as_worker
*worker
)
1074 char *procname
= NULL
;
1076 procname
= worker
->procname
;
1078 /* Close socket to run_as worker process and clean up the zombie process */
1079 run_as_destroy_worker_no_lock();
1081 /* Create a new run_as worker process*/
1082 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1084 ERR("Restarting the worker process failed");
1093 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1094 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1096 int ret
, saved_errno
;
1098 pthread_mutex_lock(&worker_lock
);
1100 DBG("Using run_as worker");
1102 assert(global_worker
);
1104 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1105 saved_errno
= ret_value
->_errno
;
1108 * If the worker thread crashed the errno is set to EIO. we log
1109 * the error and start a new worker process.
1111 if (ret
== -1 && saved_errno
== EIO
) {
1112 DBG("Socket closed unexpectedly... "
1113 "Restarting the worker process");
1114 ret
= run_as_restart_worker(global_worker
);
1116 ERR("Failed to restart worker process.");
1121 DBG("Using run_as without worker");
1122 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1125 pthread_mutex_unlock(&worker_lock
);
1130 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1132 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1136 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
,
1137 uid_t uid
, gid_t gid
)
1140 struct run_as_data data
;
1141 struct run_as_ret run_as_ret
;
1143 memset(&data
, 0, sizeof(data
));
1144 memset(&run_as_ret
, 0, sizeof(run_as_ret
));
1145 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1146 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1147 path
, (int) mode
, (int) uid
, (int) gid
);
1148 ret
= lttng_strncpy(data
.u
.mkdirat
.path
, path
,
1149 sizeof(data
.u
.mkdirat
.path
));
1151 ERR("Failed to copy path argument of mkdirat recursive command");
1154 data
.u
.mkdirat
.path
[PATH_MAX
- 1] = '\0';
1155 data
.u
.mkdirat
.mode
= mode
;
1157 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1158 &data
, &run_as_ret
, uid
, gid
);
1159 errno
= run_as_ret
._errno
;
1160 ret
= run_as_ret
.u
.mkdirat
.ret
;
1166 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1168 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1172 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
,
1173 uid_t uid
, gid_t gid
)
1176 struct run_as_data data
;
1177 struct run_as_ret run_as_ret
;
1179 memset(&data
, 0, sizeof(data
));
1180 memset(&run_as_ret
, 0, sizeof(run_as_ret
));
1182 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1183 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1184 path
, (int) mode
, (int) uid
, (int) gid
);
1185 ret
= lttng_strncpy(data
.u
.mkdirat
.path
, path
,
1186 sizeof(data
.u
.mkdirat
.path
));
1188 ERR("Failed to copy path argument of mkdirat command");
1191 data
.u
.mkdirat
.path
[PATH_MAX
- 1] = '\0';
1192 data
.u
.mkdirat
.mode
= mode
;
1194 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR
: RUN_AS_MKDIRAT
,
1195 &data
, &run_as_ret
, uid
, gid
);
1196 errno
= run_as_ret
._errno
;
1197 ret
= run_as_ret
._errno
;
1203 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
1205 struct run_as_data data
;
1206 struct run_as_ret ret
;
1208 memset(&data
, 0, sizeof(data
));
1209 memset(&ret
, 0, sizeof(ret
));
1211 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
1212 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1213 strncpy(data
.u
.open
.path
, path
, PATH_MAX
- 1);
1214 data
.u
.open
.path
[PATH_MAX
- 1] = '\0';
1215 data
.u
.open
.flags
= flags
;
1216 data
.u
.open
.mode
= mode
;
1217 run_as(RUN_AS_OPEN
, &data
, &ret
, uid
, gid
);
1219 ret
.u
.open
.ret
= ret
.fd
;
1220 return ret
.u
.open
.ret
;
1224 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1226 struct run_as_data data
;
1227 struct run_as_ret ret
;
1229 memset(&data
, 0, sizeof(data
));
1230 memset(&ret
, 0, sizeof(ret
));
1232 DBG3("unlink() %s with for uid %d and gid %d",
1233 path
, (int) uid
, (int) gid
);
1234 strncpy(data
.u
.unlink
.path
, path
, PATH_MAX
- 1);
1235 data
.u
.unlink
.path
[PATH_MAX
- 1] = '\0';
1236 run_as(RUN_AS_UNLINK
, &data
, &ret
, uid
, gid
);
1238 return ret
.u
.unlink
.ret
;
1242 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
)
1244 struct run_as_data data
;
1245 struct run_as_ret ret
;
1247 memset(&data
, 0, sizeof(data
));
1248 memset(&ret
, 0, sizeof(ret
));
1250 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1251 path
, (int) uid
, (int) gid
);
1252 strncpy(data
.u
.rmdir_recursive
.path
, path
, PATH_MAX
- 1);
1253 data
.u
.rmdir_recursive
.path
[PATH_MAX
- 1] = '\0';
1254 run_as(RUN_AS_RMDIR_RECURSIVE
, &data
, &ret
, uid
, gid
);
1256 return ret
.u
.rmdir_recursive
.ret
;
1260 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1261 uid_t uid
, gid_t gid
, uint64_t *offset
)
1263 struct run_as_data data
;
1264 struct run_as_ret ret
;
1266 memset(&data
, 0, sizeof(data
));
1267 memset(&ret
, 0, sizeof(ret
));
1269 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1270 "with for uid %d and gid %d", fd
, function
, (int) uid
, (int) gid
);
1274 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1276 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1278 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &ret
, uid
, gid
);
1286 *offset
= ret
.u
.extract_elf_symbol_offset
.offset
;
1291 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1292 const char* probe_name
, uid_t uid
, gid_t gid
,
1293 uint64_t **offsets
, uint32_t *num_offset
)
1295 struct run_as_data data
;
1296 struct run_as_ret ret
;
1298 memset(&data
, 0, sizeof(data
));
1299 memset(&ret
, 0, sizeof(ret
));
1301 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1302 "provider_name=%s with for uid %d and gid %d", fd
, probe_name
,
1303 provider_name
, (int) uid
, (int) gid
);
1307 strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1308 strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
, provider_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1310 data
.u
.extract_sdt_probe_offsets
.probe_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1311 data
.u
.extract_sdt_probe_offsets
.provider_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1313 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &ret
, uid
, gid
);
1321 *num_offset
= ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1323 *offsets
= zmalloc(*num_offset
* sizeof(uint64_t));
1328 memcpy(*offsets
, ret
.u
.extract_sdt_probe_offsets
.offsets
, *num_offset
* sizeof(uint64_t));
1333 int run_as_create_worker(const char *procname
,
1334 post_fork_cleanup_cb clean_up_func
,
1335 void *clean_up_user_data
)
1339 pthread_mutex_lock(&worker_lock
);
1340 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
1341 clean_up_user_data
);
1342 pthread_mutex_unlock(&worker_lock
);
1347 void run_as_destroy_worker(void)
1349 pthread_mutex_lock(&worker_lock
);
1350 run_as_destroy_worker_no_lock();
1351 pthread_mutex_unlock(&worker_lock
);