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
,
114 RUN_AS_RMDIR_RECURSIVE
,
115 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
116 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
123 struct run_as_mkdirat_data mkdirat
;
124 struct run_as_open_data open
;
125 struct run_as_unlink_data unlink
;
126 struct run_as_rmdir_recursive_data rmdir_recursive
;
127 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset
;
128 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets
;
135 * The run_as_ret structure holds the returned value and status of the command.
137 * The `u` union field holds the return value of the command; in most cases it
138 * represents the success or the failure of the command. In more complex
139 * commands, it holds a computed value.
141 * The _errno field is the errno recorded after the execution of the command.
143 * The _error fields is used the signify that return status of the command. For
144 * simple commands returning `int` the _error field will be the same as the
145 * ret_int field. In complex commands, it signify the success or failure of the
152 struct run_as_mkdirat_ret mkdirat
;
153 struct run_as_open_ret open
;
154 struct run_as_unlink_ret unlink
;
155 struct run_as_rmdir_recursive_ret rmdir_recursive
;
156 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset
;
157 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets
;
163 struct run_as_worker
{
164 pid_t pid
; /* Worker PID. */
169 /* Single global worker per process (for now). */
170 static struct run_as_worker
*global_worker
;
171 /* Lock protecting the worker. */
172 static pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
184 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
189 * Create recursively directory using the FULL path.
192 int _mkdirat_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
196 struct lttng_directory_handle handle
;
198 path
= data
->u
.mkdirat
.path
;
199 mode
= data
->u
.mkdirat
.mode
;
201 (void) lttng_directory_handle_init_from_dirfd(&handle
, data
->fd
);
202 /* Safe to call as we have transitioned to the requested uid/gid. */
203 ret_value
->u
.mkdirat
.ret
=
204 lttng_directory_handle_create_subdirectory_recursive(
205 &handle
, path
, mode
);
206 ret_value
->_errno
= errno
;
207 ret_value
->_error
= (ret_value
->u
.mkdirat
.ret
) ? true : false;
208 lttng_directory_handle_fini(&handle
);
209 return ret_value
->u
.mkdirat
.ret
;
213 int _mkdirat(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
217 struct lttng_directory_handle handle
;
219 path
= data
->u
.mkdirat
.path
;
220 mode
= data
->u
.mkdirat
.mode
;
222 (void) lttng_directory_handle_init_from_dirfd(&handle
, data
->fd
);
223 /* Safe to call as we have transitioned to the requested uid/gid. */
224 ret_value
->u
.mkdirat
.ret
=
225 lttng_directory_handle_create_subdirectory(
226 &handle
, path
, mode
);
227 ret_value
->_errno
= errno
;
228 ret_value
->_error
= (ret_value
->u
.mkdirat
.ret
) ? true : false;
229 lttng_directory_handle_fini(&handle
);
230 return ret_value
->u
.mkdirat
.ret
;
234 int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
236 ret_value
->u
.open
.ret
= openat(data
->fd
, data
->u
.open
.path
,
237 data
->u
.open
.flags
, data
->u
.open
.mode
);
238 ret_value
->fd
= ret_value
->u
.open
.ret
;
239 ret_value
->_errno
= errno
;
240 ret_value
->_error
= ret_value
->u
.open
.ret
< 0;
241 return ret_value
->u
.open
.ret
;
245 int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
247 ret_value
->u
.unlink
.ret
= unlinkat(data
->fd
, data
->u
.unlink
.path
, 0);
248 ret_value
->_errno
= errno
;
249 ret_value
->_error
= (ret_value
->u
.unlink
.ret
) ? true : false;
250 return ret_value
->u
.unlink
.ret
;
254 int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
256 ret_value
->u
.rmdir_recursive
.ret
= utils_recursive_rmdir(data
->u
.rmdir_recursive
.path
);
257 ret_value
->_errno
= errno
;
258 ret_value
->_error
= (ret_value
->u
.rmdir_recursive
.ret
) ? true : false;
259 return ret_value
->u
.rmdir_recursive
.ret
;
264 int _extract_elf_symbol_offset(struct run_as_data
*data
,
265 struct run_as_ret
*ret_value
)
268 ret_value
->_error
= false;
270 ret
= lttng_elf_get_symbol_offset(data
->fd
,
271 data
->u
.extract_elf_symbol_offset
.function
,
272 &ret_value
->u
.extract_elf_symbol_offset
.offset
);
274 DBG("Failed to extract ELF function offset");
275 ret_value
->_error
= true;
282 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
283 struct run_as_ret
*ret_value
)
286 uint64_t *offsets
= NULL
;
289 ret_value
->_error
= false;
291 /* On success, this call allocates the offsets paramater. */
292 ret
= lttng_elf_get_sdt_probe_offsets(data
->fd
,
293 data
->u
.extract_sdt_probe_offsets
.provider_name
,
294 data
->u
.extract_sdt_probe_offsets
.probe_name
,
295 &offsets
, &num_offset
);
298 DBG("Failed to extract SDT probe offsets");
299 ret_value
->_error
= true;
303 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_MAX_UPROBE_NUM
) {
304 DBG("Wrong number of probes.");
306 ret_value
->_error
= true;
310 /* Copy the content of the offsets array to the ret struct. */
311 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
312 offsets
, num_offset
* sizeof(uint64_t));
314 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
323 int _extract_elf_symbol_offset(struct run_as_data
*data
,
324 struct run_as_ret
*ret_value
)
326 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
331 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
332 struct run_as_ret
*ret_value
)
334 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
340 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
346 case RUN_AS_MKDIR_RECURSIVE
:
347 case RUN_AS_MKDIRAT_RECURSIVE
:
348 return _mkdirat_recursive
;
353 case RUN_AS_UNLINKAT
:
355 case RUN_AS_RMDIR_RECURSIVE
:
356 return _rmdir_recursive
;
357 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
358 return _extract_elf_symbol_offset
;
359 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
360 return _extract_sdt_probe_offsets
;
362 ERR("Unknown command %d", (int) cmd
);
368 int do_send_fd(int sock
, int fd
)
373 ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd
);
374 /* Return 0 as this is not a fatal error. */
378 len
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
380 PERROR("lttcomm_send_fds_unix_sock");
387 int do_recv_fd(int sock
, int *fd
)
391 len
= lttcomm_recv_fds_unix_sock(sock
, fd
, 1);
395 } else if (len
< 0) {
396 PERROR("lttcomm_recv_fds_unix_sock");
400 ERR("Invalid file descriptor received from worker (fd = %i)", *fd
);
401 /* Return 0 as this is not a fatal error. */
409 int send_fd_to_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
414 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
415 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
417 case RUN_AS_MKDIRAT_RECURSIVE
:
419 case RUN_AS_UNLINKAT
:
426 ERR("Refusing to send invalid fd to worker (fd = %i)", fd
);
430 ret
= do_send_fd(worker
->sockpair
[0], fd
);
432 PERROR("do_send_fd");
440 int send_fd_to_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
442 int ret
= 0, ret_close
= 0;
453 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd
);
456 ret
= do_send_fd(worker
->sockpair
[1], fd
);
458 PERROR("do_send_fd error");
462 ret_close
= close(fd
);
471 int recv_fd_from_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
483 ret
= do_recv_fd(worker
->sockpair
[0], fd
);
485 PERROR("do_recv_fd error");
493 int recv_fd_from_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
498 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
499 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
501 case RUN_AS_MKDIRAT_RECURSIVE
:
503 case RUN_AS_UNLINKAT
:
506 case RUN_AS_MKDIR_RECURSIVE
:
515 ret
= do_recv_fd(worker
->sockpair
[1], fd
);
517 PERROR("do_recv_fd error");
525 int cleanup_received_fd(enum run_as_cmd cmd
, int fd
)
530 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
531 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
533 case RUN_AS_MKDIRAT_RECURSIVE
:
537 case RUN_AS_UNLINKAT
:
548 PERROR("close error");
556 * Return < 0 on error, 0 if OK, 1 on hangup.
559 int handle_one_cmd(struct run_as_worker
*worker
)
562 struct run_as_data data
;
563 ssize_t readlen
, writelen
;
564 struct run_as_ret sendret
;
568 memset(&sendret
, 0, sizeof(sendret
));
572 * Stage 1: Receive run_as_data struct from the master.
573 * The structure contains the command type and all the parameters needed for
576 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
583 if (readlen
< sizeof(data
)) {
584 PERROR("lttcomm_recv_unix_sock error");
589 cmd
= run_as_enum_to_fct(data
.cmd
);
596 * Stage 2: Receive file descriptor from master.
597 * Some commands need a file descriptor as input so if it's needed we
598 * receive the fd using the Unix socket.
600 ret
= recv_fd_from_master(worker
, data
.cmd
, &data
.fd
);
602 PERROR("recv_fd_from_master error");
607 prev_euid
= getuid();
608 if (data
.gid
!= getegid()) {
609 ret
= setegid(data
.gid
);
611 sendret
._error
= true;
612 sendret
._errno
= errno
;
617 if (data
.uid
!= prev_euid
) {
618 ret
= seteuid(data
.uid
);
620 sendret
._error
= true;
621 sendret
._errno
= errno
;
628 * Also set umask to 0 for mkdir executable bit.
633 * Stage 3: Execute the command
635 ret
= (*cmd
)(&data
, &sendret
);
637 DBG("Execution of command returned an error");
641 ret
= cleanup_received_fd(data
.cmd
, data
.fd
);
643 ERR("Error cleaning up FD");
648 * Stage 4: Send run_as_ret structure to the master.
649 * This structure contain the return value of the command and the errno.
651 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
653 if (writelen
< sizeof(sendret
)) {
654 PERROR("lttcomm_send_unix_sock error");
660 * Stage 5: Send file descriptor to the master
661 * Some commands return a file descriptor so if it's needed we pass it back
662 * to the master using the Unix socket.
664 ret
= send_fd_to_master(worker
, data
.cmd
, sendret
.fd
);
666 DBG("Sending FD to master returned an error");
670 if (seteuid(prev_euid
) < 0) {
681 int run_as_worker(struct run_as_worker
*worker
)
685 struct run_as_ret sendret
;
686 size_t proc_orig_len
;
689 * Initialize worker. Set a different process cmdline.
691 proc_orig_len
= strlen(worker
->procname
);
692 memset(worker
->procname
, 0, proc_orig_len
);
693 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
695 ret
= lttng_prctl(PR_SET_NAME
,
696 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
697 if (ret
&& ret
!= -ENOSYS
) {
698 /* Don't fail as this is not essential. */
699 PERROR("prctl PR_SET_NAME");
702 memset(&sendret
, 0, sizeof(sendret
));
704 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
706 if (writelen
< sizeof(sendret
)) {
707 PERROR("lttcomm_send_unix_sock error");
713 ret
= handle_one_cmd(worker
);
717 } else if (ret
> 0) {
720 continue; /* Next command. */
729 int run_as_cmd(struct run_as_worker
*worker
,
731 struct run_as_data
*data
,
732 struct run_as_ret
*ret_value
,
733 uid_t uid
, gid_t gid
)
736 ssize_t readlen
, writelen
;
739 * If we are non-root, we can only deal with our own uid.
741 if (geteuid() != 0) {
742 if (uid
!= geteuid()) {
744 ret_value
->_errno
= EPERM
;
745 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
746 (int) uid
, (int) geteuid());
756 * Stage 1: Send the run_as_data struct to the worker process
758 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
760 if (writelen
< sizeof(*data
)) {
761 PERROR("Error writing message to run_as");
763 ret_value
->_errno
= EIO
;
768 * Stage 2: Send file descriptor to the worker process if needed
770 ret
= send_fd_to_worker(worker
, data
->cmd
, data
->fd
);
772 PERROR("do_send_fd error");
774 ret_value
->_errno
= EIO
;
779 * Stage 3: Wait for the execution of the command
783 * Stage 4: Receive the run_as_ret struct containing the return value and
786 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
789 ERR("Run-as worker has hung-up during run_as_cmd");
791 ret_value
->_errno
= EIO
;
793 } else if (readlen
< sizeof(*ret_value
)) {
794 PERROR("Error reading response from run_as");
796 ret_value
->_errno
= errno
;
800 if (ret_value
->_error
) {
801 /* Skip stage 5 on error as there will be no fd to receive. */
806 * Stage 5: Receive file descriptor if needed
808 ret
= recv_fd_from_worker(worker
, data
->cmd
, &ret_value
->fd
);
810 ERR("Error receiving fd");
812 ret_value
->_errno
= EIO
;
820 * This is for debugging ONLY and should not be considered secure.
823 int run_as_noworker(enum run_as_cmd cmd
,
824 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
825 uid_t uid
, gid_t gid
)
827 int ret
, saved_errno
;
831 fct
= run_as_enum_to_fct(cmd
);
838 ret
= fct(data
, ret_value
);
839 saved_errno
= ret_value
->_errno
;
847 int reset_sighandler(void)
851 DBG("Resetting run_as worker signal handlers to default");
852 for (sig
= 1; sig
<= 31; sig
++) {
853 (void) signal(sig
, SIG_DFL
);
859 void worker_sighandler(int sig
)
864 * The worker will inherit its parent's signals since they are part of
865 * the same process group. However, in the case of SIGINT and SIGTERM,
866 * we want to give the worker a chance to teardown gracefully when its
867 * parent closes the command socket.
881 DBG("run_as worker received signal %s", signame
);
883 DBG("run_as_worker received signal %d", sig
);
888 int set_worker_sighandlers(void)
894 if ((ret
= sigemptyset(&sigset
)) < 0) {
895 PERROR("sigemptyset");
899 sa
.sa_handler
= worker_sighandler
;
902 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
903 PERROR("sigaction SIGINT");
907 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
908 PERROR("sigaction SIGTERM");
912 DBG("run_as signal handler set for SIGTERM and SIGINT");
918 int run_as_create_worker_no_lock(const char *procname
,
919 post_fork_cleanup_cb clean_up_func
,
920 void *clean_up_user_data
)
925 struct run_as_ret recvret
;
926 struct run_as_worker
*worker
;
928 assert(!global_worker
);
931 * Don't initialize a worker, all run_as tasks will be performed
932 * in the current process.
937 worker
= zmalloc(sizeof(*worker
));
942 worker
->procname
= strdup(procname
);
943 if (!worker
->procname
) {
945 goto error_procname_alloc
;
947 /* Create unix socket. */
948 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
959 } else if (pid
== 0) {
964 set_worker_sighandlers();
966 if (clean_up_func(clean_up_user_data
) < 0) {
967 ERR("Run-as post-fork clean-up failed, exiting.");
972 /* Just close, no shutdown. */
973 if (close(worker
->sockpair
[0])) {
979 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
980 * Sockpair[1] is used as a control channel with the master
982 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
983 if (i
!= worker
->sockpair
[1]) {
988 worker
->sockpair
[0] = -1;
989 ret
= run_as_worker(worker
);
990 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
994 worker
->sockpair
[1] = -1;
995 free(worker
->procname
);
997 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
998 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1002 /* Just close, no shutdown. */
1003 if (close(worker
->sockpair
[1])) {
1008 worker
->sockpair
[1] = -1;
1010 /* Wait for worker to become ready. */
1011 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
1012 &recvret
, sizeof(recvret
));
1013 if (readlen
< sizeof(recvret
)) {
1014 ERR("readlen: %zd", readlen
);
1015 PERROR("Error reading response from run_as at creation");
1019 global_worker
= worker
;
1024 /* Error handling. */
1026 for (i
= 0; i
< 2; i
++) {
1027 if (worker
->sockpair
[i
] < 0) {
1030 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1033 worker
->sockpair
[i
] = -1;
1036 free(worker
->procname
);
1037 error_procname_alloc
:
1043 void run_as_destroy_worker_no_lock(void)
1045 struct run_as_worker
*worker
= global_worker
;
1047 DBG("Destroying run_as worker");
1051 /* Close unix socket */
1052 DBG("Closing run_as worker socket");
1053 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1056 worker
->sockpair
[0] = -1;
1057 /* Wait for worker. */
1062 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1064 if (errno
== EINTR
) {
1071 if (WIFEXITED(status
)) {
1072 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1073 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1074 WEXITSTATUS(status
));
1076 } else if (WIFSIGNALED(status
)) {
1077 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1082 free(worker
->procname
);
1084 global_worker
= NULL
;
1088 int run_as_restart_worker(struct run_as_worker
*worker
)
1091 char *procname
= NULL
;
1093 procname
= worker
->procname
;
1095 /* Close socket to run_as worker process and clean up the zombie process */
1096 run_as_destroy_worker_no_lock();
1098 /* Create a new run_as worker process*/
1099 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1101 ERR("Restarting the worker process failed");
1110 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1111 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1113 int ret
, saved_errno
;
1115 pthread_mutex_lock(&worker_lock
);
1117 DBG("Using run_as worker");
1119 assert(global_worker
);
1121 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1122 saved_errno
= ret_value
->_errno
;
1125 * If the worker thread crashed the errno is set to EIO. we log
1126 * the error and start a new worker process.
1128 if (ret
== -1 && saved_errno
== EIO
) {
1129 DBG("Socket closed unexpectedly... "
1130 "Restarting the worker process");
1131 ret
= run_as_restart_worker(global_worker
);
1133 ERR("Failed to restart worker process.");
1138 DBG("Using run_as without worker");
1139 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1142 pthread_mutex_unlock(&worker_lock
);
1147 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1149 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1153 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
,
1154 uid_t uid
, gid_t gid
)
1157 struct run_as_data data
;
1158 struct run_as_ret run_as_ret
;
1160 memset(&data
, 0, sizeof(data
));
1161 memset(&run_as_ret
, 0, sizeof(run_as_ret
));
1162 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1163 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1164 path
, (int) mode
, (int) uid
, (int) gid
);
1165 ret
= lttng_strncpy(data
.u
.mkdirat
.path
, path
,
1166 sizeof(data
.u
.mkdirat
.path
));
1168 ERR("Failed to copy path argument of mkdirat recursive command");
1171 data
.u
.mkdirat
.path
[PATH_MAX
- 1] = '\0';
1172 data
.u
.mkdirat
.mode
= mode
;
1174 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1175 &data
, &run_as_ret
, uid
, gid
);
1176 errno
= run_as_ret
._errno
;
1177 ret
= run_as_ret
.u
.mkdirat
.ret
;
1183 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1185 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1189 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
,
1190 uid_t uid
, gid_t gid
)
1193 struct run_as_data data
;
1194 struct run_as_ret run_as_ret
;
1196 memset(&data
, 0, sizeof(data
));
1197 memset(&run_as_ret
, 0, sizeof(run_as_ret
));
1199 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1200 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1201 path
, (int) mode
, (int) uid
, (int) gid
);
1202 ret
= lttng_strncpy(data
.u
.mkdirat
.path
, path
,
1203 sizeof(data
.u
.mkdirat
.path
));
1205 ERR("Failed to copy path argument of mkdirat command");
1208 data
.u
.mkdirat
.path
[PATH_MAX
- 1] = '\0';
1209 data
.u
.mkdirat
.mode
= mode
;
1211 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR
: RUN_AS_MKDIRAT
,
1212 &data
, &run_as_ret
, uid
, gid
);
1213 errno
= run_as_ret
._errno
;
1214 ret
= run_as_ret
._errno
;
1220 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
,
1223 return run_as_openat(AT_FDCWD
, path
, flags
, mode
, uid
, gid
);
1227 int run_as_openat(int dirfd
, const char *path
, int flags
, mode_t mode
,
1228 uid_t uid
, gid_t gid
)
1230 struct run_as_data data
;
1231 struct run_as_ret ret
;
1233 memset(&data
, 0, sizeof(data
));
1234 memset(&ret
, 0, sizeof(ret
));
1236 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1237 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1238 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1239 strncpy(data
.u
.open
.path
, path
, PATH_MAX
- 1);
1240 data
.u
.open
.path
[PATH_MAX
- 1] = '\0';
1241 data
.u
.open
.flags
= flags
;
1242 data
.u
.open
.mode
= mode
;
1244 run_as(dirfd
== AT_FDCWD
? RUN_AS_OPEN
: RUN_AS_OPENAT
,
1245 &data
, &ret
, uid
, gid
);
1247 ret
.u
.open
.ret
= ret
.fd
;
1248 return ret
.u
.open
.ret
;
1252 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1254 return run_as_unlinkat(AT_FDCWD
, path
, uid
, gid
);
1258 int run_as_unlinkat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1260 struct run_as_data data
;
1261 struct run_as_ret ret
;
1263 memset(&data
, 0, sizeof(data
));
1264 memset(&ret
, 0, sizeof(ret
));
1266 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1267 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1268 path
, (int) uid
, (int) gid
);
1269 strncpy(data
.u
.unlink
.path
, path
, PATH_MAX
- 1);
1270 data
.u
.unlink
.path
[PATH_MAX
- 1] = '\0';
1272 run_as(RUN_AS_UNLINK
, &data
, &ret
, uid
, gid
);
1274 return ret
.u
.unlink
.ret
;
1278 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
)
1280 struct run_as_data data
;
1281 struct run_as_ret ret
;
1283 memset(&data
, 0, sizeof(data
));
1284 memset(&ret
, 0, sizeof(ret
));
1286 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1287 path
, (int) uid
, (int) gid
);
1288 strncpy(data
.u
.rmdir_recursive
.path
, path
, PATH_MAX
- 1);
1289 data
.u
.rmdir_recursive
.path
[PATH_MAX
- 1] = '\0';
1290 run_as(RUN_AS_RMDIR_RECURSIVE
, &data
, &ret
, uid
, gid
);
1292 return ret
.u
.rmdir_recursive
.ret
;
1296 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1297 uid_t uid
, gid_t gid
, uint64_t *offset
)
1299 struct run_as_data data
;
1300 struct run_as_ret ret
;
1302 memset(&data
, 0, sizeof(data
));
1303 memset(&ret
, 0, sizeof(ret
));
1305 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1306 "with for uid %d and gid %d", fd
, function
, (int) uid
, (int) gid
);
1310 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1312 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1314 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &ret
, uid
, gid
);
1322 *offset
= ret
.u
.extract_elf_symbol_offset
.offset
;
1327 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1328 const char* probe_name
, uid_t uid
, gid_t gid
,
1329 uint64_t **offsets
, uint32_t *num_offset
)
1331 struct run_as_data data
;
1332 struct run_as_ret ret
;
1334 memset(&data
, 0, sizeof(data
));
1335 memset(&ret
, 0, sizeof(ret
));
1337 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1338 "provider_name=%s with for uid %d and gid %d", fd
, probe_name
,
1339 provider_name
, (int) uid
, (int) gid
);
1343 strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1344 strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
, provider_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1346 data
.u
.extract_sdt_probe_offsets
.probe_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1347 data
.u
.extract_sdt_probe_offsets
.provider_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1349 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &ret
, uid
, gid
);
1357 *num_offset
= ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1359 *offsets
= zmalloc(*num_offset
* sizeof(uint64_t));
1364 memcpy(*offsets
, ret
.u
.extract_sdt_probe_offsets
.offsets
, *num_offset
* sizeof(uint64_t));
1369 int run_as_create_worker(const char *procname
,
1370 post_fork_cleanup_cb clean_up_func
,
1371 void *clean_up_user_data
)
1375 pthread_mutex_lock(&worker_lock
);
1376 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
1377 clean_up_user_data
);
1378 pthread_mutex_unlock(&worker_lock
);
1383 void run_as_destroy_worker(void)
1385 pthread_mutex_lock(&worker_lock
);
1386 run_as_destroy_worker_no_lock();
1387 pthread_mutex_unlock(&worker_lock
);