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_mkdir_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_mkdir_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
];
109 RUN_AS_RMDIR_RECURSIVE
,
110 RUN_AS_MKDIR_RECURSIVE
,
111 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
112 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
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
;
131 * The run_as_ret structure holds the returned value and status of the command.
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.
137 * The _errno field is the errno recorded after the execution of the command.
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
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
;
159 struct run_as_worker
{
160 pid_t pid
; /* Worker PID. */
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
;
180 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
185 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
188 * Create recursively directory using the FULL path.
191 int _mkdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
196 path
= data
->u
.mkdir
.path
;
197 mode
= data
->u
.mkdir
.mode
;
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
;
207 int _mkdir(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
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
;
216 int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
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
;
226 int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
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
;
235 int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
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
;
245 int _extract_elf_symbol_offset(struct run_as_data
*data
,
246 struct run_as_ret
*ret_value
)
249 ret_value
->_error
= false;
251 ret
= lttng_elf_get_symbol_offset(data
->fd
,
252 data
->u
.extract_elf_symbol_offset
.function
,
253 &ret_value
->u
.extract_elf_symbol_offset
.offset
);
255 DBG("Failed to extract ELF function offset");
256 ret_value
->_error
= true;
263 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
264 struct run_as_ret
*ret_value
)
267 uint64_t *offsets
= NULL
;
270 ret_value
->_error
= false;
272 /* On success, this call allocates the offsets paramater. */
273 ret
= lttng_elf_get_sdt_probe_offsets(data
->fd
,
274 data
->u
.extract_sdt_probe_offsets
.provider_name
,
275 data
->u
.extract_sdt_probe_offsets
.probe_name
,
276 &offsets
, &num_offset
);
279 DBG("Failed to extract SDT probe offsets");
280 ret_value
->_error
= true;
284 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_MAX_UPROBE_NUM
) {
285 DBG("Wrong number of probes.");
287 ret_value
->_error
= true;
291 /* Copy the content of the offsets array to the ret struct. */
292 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
293 offsets
, num_offset
* sizeof(uint64_t));
295 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
304 int _extract_elf_symbol_offset(struct run_as_data
*data
,
305 struct run_as_ret
*ret_value
)
307 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
312 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
313 struct run_as_ret
*ret_value
)
315 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
321 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
330 case RUN_AS_RMDIR_RECURSIVE
:
331 return _rmdir_recursive
;
332 case RUN_AS_MKDIR_RECURSIVE
:
333 return _mkdir_recursive
;
334 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
335 return _extract_elf_symbol_offset
;
336 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
337 return _extract_sdt_probe_offsets
;
339 ERR("Unknown command %d", (int) cmd
);
345 int do_send_fd(int sock
, int fd
)
350 ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd
);
351 /* Return 0 as this is not a fatal error. */
355 len
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
357 PERROR("lttcomm_send_fds_unix_sock");
364 int do_recv_fd(int sock
, int *fd
)
368 len
= lttcomm_recv_fds_unix_sock(sock
, fd
, 1);
372 } else if (len
< 0) {
373 PERROR("lttcomm_recv_fds_unix_sock");
377 ERR("Invalid file descriptor received from worker (fd = %i)", *fd
);
378 /* Return 0 as this is not a fatal error. */
386 int send_fd_to_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
391 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
392 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
399 ERR("Refusing to send invalid fd to worker (fd = %i)", fd
);
403 ret
= do_send_fd(worker
->sockpair
[0], fd
);
405 PERROR("do_send_fd");
413 int send_fd_to_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int fd
)
415 int ret
= 0, ret_close
= 0;
425 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd
);
428 ret
= do_send_fd(worker
->sockpair
[1], fd
);
430 PERROR("do_send_fd error");
434 ret_close
= close(fd
);
443 int recv_fd_from_worker(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
454 ret
= do_recv_fd(worker
->sockpair
[0], fd
);
456 PERROR("do_recv_fd error");
464 int recv_fd_from_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
, int *fd
)
469 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
470 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
476 ret
= do_recv_fd(worker
->sockpair
[1], fd
);
478 PERROR("do_recv_fd error");
486 int cleanup_received_fd(enum run_as_cmd cmd
, int fd
)
491 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
492 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
503 PERROR("close error");
511 * Return < 0 on error, 0 if OK, 1 on hangup.
514 int handle_one_cmd(struct run_as_worker
*worker
)
517 struct run_as_data data
;
518 ssize_t readlen
, writelen
;
519 struct run_as_ret sendret
;
523 memset(&sendret
, 0, sizeof(sendret
));
527 * Stage 1: Receive run_as_data struct from the master.
528 * The structure contains the command type and all the parameters needed for
531 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
538 if (readlen
< sizeof(data
)) {
539 PERROR("lttcomm_recv_unix_sock error");
544 cmd
= run_as_enum_to_fct(data
.cmd
);
551 * Stage 2: Receive file descriptor from master.
552 * Some commands need a file descriptor as input so if it's needed we
553 * receive the fd using the Unix socket.
555 ret
= recv_fd_from_master(worker
, data
.cmd
, &data
.fd
);
557 PERROR("recv_fd_from_master error");
562 prev_euid
= getuid();
563 if (data
.gid
!= getegid()) {
564 ret
= setegid(data
.gid
);
566 sendret
._error
= true;
567 sendret
._errno
= errno
;
572 if (data
.uid
!= prev_euid
) {
573 ret
= seteuid(data
.uid
);
575 sendret
._error
= true;
576 sendret
._errno
= errno
;
583 * Also set umask to 0 for mkdir executable bit.
588 * Stage 3: Execute the command
590 ret
= (*cmd
)(&data
, &sendret
);
592 DBG("Execution of command returned an error");
596 ret
= cleanup_received_fd(data
.cmd
, data
.fd
);
598 ERR("Error cleaning up FD");
603 * Stage 4: Send run_as_ret structure to the master.
604 * This structure contain the return value of the command and the errno.
606 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
608 if (writelen
< sizeof(sendret
)) {
609 PERROR("lttcomm_send_unix_sock error");
615 * Stage 5: Send file descriptor to the master
616 * Some commands return a file descriptor so if it's needed we pass it back
617 * to the master using the Unix socket.
619 ret
= send_fd_to_master(worker
, data
.cmd
, sendret
.fd
);
621 DBG("Sending FD to master returned an error");
625 if (seteuid(prev_euid
) < 0) {
636 int run_as_worker(struct run_as_worker
*worker
)
640 struct run_as_ret sendret
;
641 size_t proc_orig_len
;
644 * Initialize worker. Set a different process cmdline.
646 proc_orig_len
= strlen(worker
->procname
);
647 memset(worker
->procname
, 0, proc_orig_len
);
648 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
650 ret
= lttng_prctl(PR_SET_NAME
,
651 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
652 if (ret
&& ret
!= -ENOSYS
) {
653 /* Don't fail as this is not essential. */
654 PERROR("prctl PR_SET_NAME");
657 memset(&sendret
, 0, sizeof(sendret
));
659 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
661 if (writelen
< sizeof(sendret
)) {
662 PERROR("lttcomm_send_unix_sock error");
668 ret
= handle_one_cmd(worker
);
672 } else if (ret
> 0) {
675 continue; /* Next command. */
684 int run_as_cmd(struct run_as_worker
*worker
,
686 struct run_as_data
*data
,
687 struct run_as_ret
*ret_value
,
688 uid_t uid
, gid_t gid
)
691 ssize_t readlen
, writelen
;
694 * If we are non-root, we can only deal with our own uid.
696 if (geteuid() != 0) {
697 if (uid
!= geteuid()) {
699 ret_value
->_errno
= EPERM
;
700 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
701 (int) uid
, (int) geteuid());
711 * Stage 1: Send the run_as_data struct to the worker process
713 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
715 if (writelen
< sizeof(*data
)) {
716 PERROR("Error writing message to run_as");
718 ret_value
->_errno
= EIO
;
723 * Stage 2: Send file descriptor to the worker process if needed
725 ret
= send_fd_to_worker(worker
, data
->cmd
, data
->fd
);
727 PERROR("do_send_fd error");
729 ret_value
->_errno
= EIO
;
734 * Stage 3: Wait for the execution of the command
738 * Stage 4: Receive the run_as_ret struct containing the return value and
741 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
744 ERR("Run-as worker has hung-up during run_as_cmd");
746 ret_value
->_errno
= EIO
;
748 } else if (readlen
< sizeof(*ret_value
)) {
749 PERROR("Error reading response from run_as");
751 ret_value
->_errno
= errno
;
755 if (ret_value
->_error
) {
756 /* Skip stage 5 on error as there will be no fd to receive. */
761 * Stage 5: Receive file descriptor if needed
763 ret
= recv_fd_from_worker(worker
, data
->cmd
, &ret_value
->fd
);
765 ERR("Error receiving fd");
767 ret_value
->_errno
= EIO
;
775 * This is for debugging ONLY and should not be considered secure.
778 int run_as_noworker(enum run_as_cmd cmd
,
779 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
780 uid_t uid
, gid_t gid
)
782 int ret
, saved_errno
;
786 fct
= run_as_enum_to_fct(cmd
);
793 ret
= fct(data
, ret_value
);
794 saved_errno
= ret_value
->_errno
;
802 int reset_sighandler(void)
806 DBG("Resetting run_as worker signal handlers to default");
807 for (sig
= 1; sig
<= 31; sig
++) {
808 (void) signal(sig
, SIG_DFL
);
814 void worker_sighandler(int sig
)
819 * The worker will inherit its parent's signals since they are part of
820 * the same process group. However, in the case of SIGINT and SIGTERM,
821 * we want to give the worker a chance to teardown gracefully when its
822 * parent closes the command socket.
836 DBG("run_as worker received signal %s", signame
);
838 DBG("run_as_worker received signal %d", sig
);
843 int set_worker_sighandlers(void)
849 if ((ret
= sigemptyset(&sigset
)) < 0) {
850 PERROR("sigemptyset");
854 sa
.sa_handler
= worker_sighandler
;
857 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
858 PERROR("sigaction SIGINT");
862 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
863 PERROR("sigaction SIGTERM");
867 DBG("run_as signal handler set for SIGTERM and SIGINT");
873 int run_as_create_worker_no_lock(const char *procname
,
874 post_fork_cleanup_cb clean_up_func
,
875 void *clean_up_user_data
)
880 struct run_as_ret recvret
;
881 struct run_as_worker
*worker
;
883 assert(!global_worker
);
886 * Don't initialize a worker, all run_as tasks will be performed
887 * in the current process.
892 worker
= zmalloc(sizeof(*worker
));
897 worker
->procname
= strdup(procname
);
898 if (!worker
->procname
) {
900 goto error_procname_alloc
;
902 /* Create unix socket. */
903 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
914 } else if (pid
== 0) {
919 set_worker_sighandlers();
921 if (clean_up_func(clean_up_user_data
) < 0) {
922 ERR("Run-as post-fork clean-up failed, exiting.");
927 /* Just close, no shutdown. */
928 if (close(worker
->sockpair
[0])) {
934 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
935 * Sockpair[1] is used as a control channel with the master
937 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
938 if (i
!= worker
->sockpair
[1]) {
943 worker
->sockpair
[0] = -1;
944 ret
= run_as_worker(worker
);
945 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
949 worker
->sockpair
[1] = -1;
950 free(worker
->procname
);
952 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
953 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
957 /* Just close, no shutdown. */
958 if (close(worker
->sockpair
[1])) {
963 worker
->sockpair
[1] = -1;
965 /* Wait for worker to become ready. */
966 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
967 &recvret
, sizeof(recvret
));
968 if (readlen
< sizeof(recvret
)) {
969 ERR("readlen: %zd", readlen
);
970 PERROR("Error reading response from run_as at creation");
974 global_worker
= worker
;
979 /* Error handling. */
981 for (i
= 0; i
< 2; i
++) {
982 if (worker
->sockpair
[i
] < 0) {
985 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
988 worker
->sockpair
[i
] = -1;
991 free(worker
->procname
);
992 error_procname_alloc
:
998 void run_as_destroy_worker_no_lock(void)
1000 struct run_as_worker
*worker
= global_worker
;
1002 DBG("Destroying run_as worker");
1006 /* Close unix socket */
1007 DBG("Closing run_as worker socket");
1008 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1011 worker
->sockpair
[0] = -1;
1012 /* Wait for worker. */
1017 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1019 if (errno
== EINTR
) {
1026 if (WIFEXITED(status
)) {
1027 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1028 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1029 WEXITSTATUS(status
));
1031 } else if (WIFSIGNALED(status
)) {
1032 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1037 free(worker
->procname
);
1039 global_worker
= NULL
;
1043 int run_as_restart_worker(struct run_as_worker
*worker
)
1046 char *procname
= NULL
;
1048 procname
= worker
->procname
;
1050 /* Close socket to run_as worker process and clean up the zombie process */
1051 run_as_destroy_worker_no_lock();
1053 /* Create a new run_as worker process*/
1054 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1056 ERR("Restarting the worker process failed");
1065 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1066 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1068 int ret
, saved_errno
;
1070 pthread_mutex_lock(&worker_lock
);
1072 DBG("Using run_as worker");
1074 assert(global_worker
);
1076 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1077 saved_errno
= ret_value
->_errno
;
1080 * If the worker thread crashed the errno is set to EIO. we log
1081 * the error and start a new worker process.
1083 if (ret
== -1 && saved_errno
== EIO
) {
1084 DBG("Socket closed unexpectedly... "
1085 "Restarting the worker process");
1086 ret
= run_as_restart_worker(global_worker
);
1088 ERR("Failed to restart worker process.");
1093 DBG("Using run_as without worker");
1094 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1097 pthread_mutex_unlock(&worker_lock
);
1102 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1104 struct run_as_data data
;
1105 struct run_as_ret ret
;
1107 memset(&data
, 0, sizeof(data
));
1108 memset(&ret
, 0, sizeof(ret
));
1109 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
1110 path
, (int) mode
, (int) uid
, (int) gid
);
1111 strncpy(data
.u
.mkdir
.path
, path
, PATH_MAX
- 1);
1112 data
.u
.mkdir
.path
[PATH_MAX
- 1] = '\0';
1113 data
.u
.mkdir
.mode
= mode
;
1115 run_as(RUN_AS_MKDIR_RECURSIVE
, &data
, &ret
, uid
, gid
);
1117 return ret
.u
.mkdir
.ret
;
1121 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1123 struct run_as_data data
;
1124 struct run_as_ret ret
;
1126 memset(&data
, 0, sizeof(data
));
1127 memset(&ret
, 0, sizeof(ret
));
1129 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
1130 path
, (int) mode
, (int) uid
, (int) gid
);
1131 strncpy(data
.u
.mkdir
.path
, path
, PATH_MAX
- 1);
1132 data
.u
.mkdir
.path
[PATH_MAX
- 1] = '\0';
1133 data
.u
.mkdir
.mode
= mode
;
1134 run_as(RUN_AS_MKDIR
, &data
, &ret
, uid
, gid
);
1136 return ret
.u
.mkdir
.ret
;
1140 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
1142 struct run_as_data data
;
1143 struct run_as_ret ret
;
1145 memset(&data
, 0, sizeof(data
));
1146 memset(&ret
, 0, sizeof(ret
));
1148 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
1149 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1150 strncpy(data
.u
.open
.path
, path
, PATH_MAX
- 1);
1151 data
.u
.open
.path
[PATH_MAX
- 1] = '\0';
1152 data
.u
.open
.flags
= flags
;
1153 data
.u
.open
.mode
= mode
;
1154 run_as(RUN_AS_OPEN
, &data
, &ret
, uid
, gid
);
1156 ret
.u
.open
.ret
= ret
.fd
;
1157 return ret
.u
.open
.ret
;
1161 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1163 struct run_as_data data
;
1164 struct run_as_ret ret
;
1166 memset(&data
, 0, sizeof(data
));
1167 memset(&ret
, 0, sizeof(ret
));
1169 DBG3("unlink() %s with for uid %d and gid %d",
1170 path
, (int) uid
, (int) gid
);
1171 strncpy(data
.u
.unlink
.path
, path
, PATH_MAX
- 1);
1172 data
.u
.unlink
.path
[PATH_MAX
- 1] = '\0';
1173 run_as(RUN_AS_UNLINK
, &data
, &ret
, uid
, gid
);
1175 return ret
.u
.unlink
.ret
;
1179 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
)
1181 struct run_as_data data
;
1182 struct run_as_ret ret
;
1184 memset(&data
, 0, sizeof(data
));
1185 memset(&ret
, 0, sizeof(ret
));
1187 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1188 path
, (int) uid
, (int) gid
);
1189 strncpy(data
.u
.rmdir_recursive
.path
, path
, PATH_MAX
- 1);
1190 data
.u
.rmdir_recursive
.path
[PATH_MAX
- 1] = '\0';
1191 run_as(RUN_AS_RMDIR_RECURSIVE
, &data
, &ret
, uid
, gid
);
1193 return ret
.u
.rmdir_recursive
.ret
;
1197 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1198 uid_t uid
, gid_t gid
, uint64_t *offset
)
1200 struct run_as_data data
;
1201 struct run_as_ret ret
;
1203 memset(&data
, 0, sizeof(data
));
1204 memset(&ret
, 0, sizeof(ret
));
1206 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1207 "with for uid %d and gid %d", fd
, function
, (int) uid
, (int) gid
);
1211 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1213 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1215 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &ret
, uid
, gid
);
1223 *offset
= ret
.u
.extract_elf_symbol_offset
.offset
;
1228 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1229 const char* probe_name
, uid_t uid
, gid_t gid
,
1230 uint64_t **offsets
, uint32_t *num_offset
)
1232 struct run_as_data data
;
1233 struct run_as_ret ret
;
1235 memset(&data
, 0, sizeof(data
));
1236 memset(&ret
, 0, sizeof(ret
));
1238 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1239 "provider_name=%s with for uid %d and gid %d", fd
, probe_name
,
1240 provider_name
, (int) uid
, (int) gid
);
1244 strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1245 strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
, provider_name
, LTTNG_SYMBOL_NAME_LEN
- 1);
1247 data
.u
.extract_sdt_probe_offsets
.probe_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1248 data
.u
.extract_sdt_probe_offsets
.provider_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1250 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &ret
, uid
, gid
);
1258 *num_offset
= ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1260 *offsets
= zmalloc(*num_offset
* sizeof(uint64_t));
1265 memcpy(*offsets
, ret
.u
.extract_sdt_probe_offsets
.offsets
, *num_offset
* sizeof(uint64_t));
1270 int run_as_create_worker(const char *procname
,
1271 post_fork_cleanup_cb clean_up_func
,
1272 void *clean_up_user_data
)
1276 pthread_mutex_lock(&worker_lock
);
1277 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
1278 clean_up_user_data
);
1279 pthread_mutex_unlock(&worker_lock
);
1284 void run_as_destroy_worker(void)
1286 pthread_mutex_lock(&worker_lock
);
1287 run_as_destroy_worker_no_lock();
1288 pthread_mutex_unlock(&worker_lock
);