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