Copyright ownership transfer
[lttng-tools.git] / src / common / runas.c
CommitLineData
60b6c79c 1/*
a4eb26f0 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
60b6c79c 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
60b6c79c 7 *
60b6c79c
MD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
a62bbe8c
FD
11#include <assert.h>
12#include <fcntl.h>
13#include <grp.h>
60b6c79c 14#include <limits.h>
a62bbe8c
FD
15#include <pwd.h>
16#include <sched.h>
17#include <signal.h>
60b6c79c
MD
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
60b6c79c 21#include <sys/stat.h>
a62bbe8c
FD
22#include <sys/types.h>
23#include <sys/wait.h>
60b6c79c 24#include <unistd.h>
60b6c79c 25
0ef03255 26#include <common/lttng-kernel.h>
90e535ef 27#include <common/common.h>
3fd15a74 28#include <common/utils.h>
e8fa9fb0 29#include <common/compat/getenv.h>
e1055edb 30#include <common/compat/prctl.h>
2038dd6c
JG
31#include <common/unix.h>
32#include <common/defaults.h>
241e0a5a
FD
33#include <common/lttng-elf.h>
34
35#include <lttng/constant.h>
60b6c79c 36
0857097f
DG
37#include "runas.h"
38
a62bbe8c
FD
39#define GETPW_BUFFER_FALLBACK_SIZE 4096
40
7567352f 41struct run_as_data;
fe9f7760
FD
42struct run_as_ret;
43typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
c2b75c49 44
93bed9fe
JG
45enum run_as_cmd {
46 RUN_AS_MKDIR,
47 RUN_AS_MKDIRAT,
48 RUN_AS_MKDIR_RECURSIVE,
49 RUN_AS_MKDIRAT_RECURSIVE,
50 RUN_AS_OPEN,
51 RUN_AS_OPENAT,
52 RUN_AS_UNLINK,
53 RUN_AS_UNLINKAT,
54 RUN_AS_RMDIR,
55 RUN_AS_RMDIRAT,
56 RUN_AS_RMDIR_RECURSIVE,
57 RUN_AS_RMDIRAT_RECURSIVE,
58 RUN_AS_RENAME,
59 RUN_AS_RENAMEAT,
60 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
61 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
60b6c79c
MD
62};
63
93bed9fe
JG
64struct run_as_mkdir_data {
65 int dirfd;
66 char path[LTTNG_PATH_MAX];
67 mode_t mode;
68} LTTNG_PACKED;
69
e11d277b 70struct run_as_open_data {
93bed9fe
JG
71 int dirfd;
72 char path[LTTNG_PATH_MAX];
60b6c79c
MD
73 int flags;
74 mode_t mode;
93bed9fe 75} LTTNG_PACKED;
60b6c79c 76
4628484a 77struct run_as_unlink_data {
93bed9fe
JG
78 int dirfd;
79 char path[LTTNG_PATH_MAX];
80} LTTNG_PACKED;
4628484a 81
93bed9fe
JG
82struct run_as_rmdir_data {
83 int dirfd;
84 char path[LTTNG_PATH_MAX];
f75c5439 85 int flags; /* enum lttng_directory_handle_rmdir_recursive_flags */
93bed9fe 86} LTTNG_PACKED;
7567352f 87
241e0a5a 88struct run_as_extract_elf_symbol_offset_data {
93bed9fe 89 int fd;
241e0a5a 90 char function[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 91} LTTNG_PACKED;
241e0a5a 92
0ef03255 93struct run_as_extract_sdt_probe_offsets_data {
93bed9fe 94 int fd;
0ef03255
FD
95 char probe_name[LTTNG_SYMBOL_NAME_LEN];
96 char provider_name[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 97} LTTNG_PACKED;
0ef03255 98
93bed9fe
JG
99struct run_as_rename_data {
100 /*
101 * [0] = old_dirfd
102 * [1] = new_dirfd
103 */
104 int dirfds[2];
105 char old_path[LTTNG_PATH_MAX];
106 char new_path[LTTNG_PATH_MAX];
107} LTTNG_PACKED;
fe9f7760
FD
108
109struct run_as_open_ret {
93bed9fe
JG
110 int fd;
111} LTTNG_PACKED;
fe9f7760 112
241e0a5a
FD
113struct run_as_extract_elf_symbol_offset_ret {
114 uint64_t offset;
93bed9fe 115} LTTNG_PACKED;
241e0a5a 116
0ef03255
FD
117struct run_as_extract_sdt_probe_offsets_ret {
118 uint32_t num_offset;
119 uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM];
93bed9fe 120} LTTNG_PACKED;
7567352f
MD
121
122struct run_as_data {
123 enum run_as_cmd cmd;
124 union {
93bed9fe 125 struct run_as_mkdir_data mkdir;
7567352f
MD
126 struct run_as_open_data open;
127 struct run_as_unlink_data unlink;
93bed9fe
JG
128 struct run_as_rmdir_data rmdir;
129 struct run_as_rename_data rename;
241e0a5a 130 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
0ef03255 131 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
7567352f
MD
132 } u;
133 uid_t uid;
134 gid_t gid;
93bed9fe 135} LTTNG_PACKED;
4628484a 136
fe9f7760
FD
137/*
138 * The run_as_ret structure holds the returned value and status of the command.
139 *
140 * The `u` union field holds the return value of the command; in most cases it
141 * represents the success or the failure of the command. In more complex
142 * commands, it holds a computed value.
143 *
144 * The _errno field is the errno recorded after the execution of the command.
145 *
146 * The _error fields is used the signify that return status of the command. For
147 * simple commands returning `int` the _error field will be the same as the
148 * ret_int field. In complex commands, it signify the success or failure of the
149 * command.
150 *
151 */
df5b86c8 152struct run_as_ret {
fe9f7760 153 union {
93bed9fe 154 int ret;
fe9f7760 155 struct run_as_open_ret open;
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;
93bed9fe
JG
161} LTTNG_PACKED;
162
163#define COMMAND_IN_FDS(data_ptr) ({ \
164 int *fds = NULL; \
165 if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \
166 fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \
167 } \
168 fds; \
169})
170
171#define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
172 int *fds = NULL; \
173 if (command_properties[cmd].out_fds_offset != -1) { \
174 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
175 } \
176 fds; \
177})
178
179#define COMMAND_IN_FD_COUNT(data_ptr) ({ \
180 command_properties[data_ptr->cmd].in_fd_count; \
181})
182
183#define COMMAND_OUT_FD_COUNT(cmd) ({ \
184 command_properties[cmd].out_fd_count; \
185})
186
187#define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
188
189struct run_as_command_properties {
190 /* Set to -1 when not applicable. */
191 ptrdiff_t in_fds_offset, out_fds_offset;
192 unsigned int in_fd_count, out_fd_count;
193 bool use_cwd_fd;
194};
195
196static const struct run_as_command_properties command_properties[] = {
197 [RUN_AS_MKDIR] = {
198 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
199 .in_fd_count = 1,
200 .out_fds_offset = -1,
201 .out_fd_count = 0,
202 .use_cwd_fd = true,
203 },
204 [RUN_AS_MKDIRAT] = {
205 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
206 .in_fd_count = 1,
207 .out_fds_offset = -1,
208 .out_fd_count = 0,
209 .use_cwd_fd = false,
210 },
211 [RUN_AS_MKDIR_RECURSIVE] = {
212 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
213 .in_fd_count = 1,
214 .out_fds_offset = -1,
215 .out_fd_count = 0,
216 .use_cwd_fd = true,
217 },
218 [RUN_AS_MKDIRAT_RECURSIVE] = {
219 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
220 .in_fd_count = 1,
221 .out_fds_offset = -1,
222 .out_fd_count = 0,
223 .use_cwd_fd = false,
224 },
225 [RUN_AS_OPEN] = {
226 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
227 .in_fd_count = 1,
228 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
229 .out_fd_count = 1,
230 .use_cwd_fd = true,
231 },
232 [RUN_AS_OPENAT] = {
233 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
234 .in_fd_count = 1,
235 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
236 .out_fd_count = 1,
237 .use_cwd_fd = false,
238 },
239 [RUN_AS_UNLINK] = {
240 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
241 .in_fd_count = 1,
242 .out_fds_offset = -1,
243 .out_fd_count = 0,
244 .use_cwd_fd = true,
245 },
246 [RUN_AS_UNLINKAT] = {
247 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
248 .in_fd_count = 1,
249 .out_fds_offset = -1,
250 .out_fd_count = 0,
251 .use_cwd_fd = false,
252 },
253 [RUN_AS_RMDIR_RECURSIVE] = {
254 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
255 .in_fd_count = 1,
256 .out_fds_offset = -1,
257 .out_fd_count = 0,
258 .use_cwd_fd = true,
259 },
260 [RUN_AS_RMDIRAT_RECURSIVE] = {
261 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
262 .in_fd_count = 1,
263 .out_fds_offset = -1,
264 .out_fd_count = 0,
265 .use_cwd_fd = false,
266 },
267 [RUN_AS_RMDIR] = {
268 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
269 .in_fd_count = 1,
270 .out_fds_offset = -1,
271 .out_fd_count = 0,
272 .use_cwd_fd = true,
273 },
274 [RUN_AS_RMDIRAT] = {
275 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
276 .in_fd_count = 1,
277 .out_fds_offset = -1,
278 .out_fd_count = 0,
279 .use_cwd_fd = false,
280 },
281 [RUN_AS_RENAME] = {
282 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
283 .in_fd_count = 2,
284 .out_fds_offset = -1,
285 .out_fd_count = 0,
286 .use_cwd_fd = true,
287 },
288 [RUN_AS_RENAMEAT] = {
289 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
290 .in_fd_count = 2,
291 .out_fds_offset = -1,
292 .out_fd_count = 0,
293 .use_cwd_fd = false,
294 },
295 [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET] = {
296 .in_fds_offset = offsetof(struct run_as_data,
297 u.extract_elf_symbol_offset.fd),
298 .in_fd_count = 1,
299 .out_fds_offset = -1,
300 .out_fd_count = 0,
301 .use_cwd_fd = false,
302 },
303 [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS] = {
304 .in_fds_offset = offsetof(struct run_as_data,
305 u.extract_sdt_probe_offsets.fd),
306 .in_fd_count = 1,
307 .out_fds_offset = -1,
308 .out_fd_count = 0,
309 .use_cwd_fd = false,
310 },
df5b86c8
MD
311};
312
7567352f
MD
313struct run_as_worker {
314 pid_t pid; /* Worker PID. */
315 int sockpair[2];
316 char *procname;
317};
318
319/* Single global worker per process (for now). */
320static struct run_as_worker *global_worker;
321/* Lock protecting the worker. */
322static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
323
8f0044bf
MD
324#ifdef VALGRIND
325static
326int use_clone(void)
327{
328 return 0;
329}
330#else
331static
332int use_clone(void)
333{
e8fa9fb0 334 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
8f0044bf
MD
335}
336#endif
337
60b6c79c
MD
338/*
339 * Create recursively directory using the FULL path.
340 */
341static
18710679 342int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 343{
60b6c79c 344 const char *path;
60b6c79c 345 mode_t mode;
cbf53d23 346 struct lttng_directory_handle *handle;
60b6c79c 347
93bed9fe
JG
348 path = data->u.mkdir.path;
349 mode = data->u.mkdir.mode;
60b6c79c 350
cbf53d23
JG
351 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
352 if (!handle) {
353 ret_value->_errno = errno;
354 ret_value->_error = true;
355 ret_value->u.ret = -1;
356 goto end;
357 }
87bbb856 358 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 359 data->u.mkdir.dirfd = -1;
d77dded2 360 /* Safe to call as we have transitioned to the requested uid/gid. */
cbf53d23
JG
361 ret_value->u.ret = lttng_directory_handle_create_subdirectory_recursive(
362 handle, path, mode);
fe9f7760 363 ret_value->_errno = errno;
93bed9fe 364 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
365 lttng_directory_handle_put(handle);
366end:
93bed9fe 367 return ret_value->u.ret;
60b6c79c
MD
368}
369
370static
18710679 371int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 372{
18710679
JG
373 const char *path;
374 mode_t mode;
cbf53d23 375 struct lttng_directory_handle *handle;
18710679 376
93bed9fe
JG
377 path = data->u.mkdir.path;
378 mode = data->u.mkdir.mode;
18710679 379
cbf53d23
JG
380 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
381 if (!handle) {
382 ret_value->u.ret = -1;
383 ret_value->_errno = errno;
384 ret_value->_error = true;
385 goto end;
386 }
87bbb856 387 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 388 data->u.mkdir.dirfd = -1;
18710679 389 /* Safe to call as we have transitioned to the requested uid/gid. */
cbf53d23
JG
390 ret_value->u.ret = lttng_directory_handle_create_subdirectory(
391 handle, path, mode);
fe9f7760 392 ret_value->_errno = errno;
93bed9fe 393 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
394 lttng_directory_handle_put(handle);
395end:
93bed9fe 396 return ret_value->u.ret;
7567352f 397}
7ce36756 398
7567352f 399static
fe9f7760 400int _open(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 401{
93bed9fe 402 int fd;
cbf53d23 403 struct lttng_directory_handle *handle;
93bed9fe 404
cbf53d23
JG
405 handle = lttng_directory_handle_create_from_dirfd(data->u.open.dirfd);
406 if (!handle) {
407 ret_value->_errno = errno;
408 ret_value->_error = true;
409 ret_value->u.ret = -1;
410 goto end;
411 }
93bed9fe
JG
412 /* Ownership of dirfd is transferred to the handle. */
413 data->u.open.dirfd = -1;
414
cbf53d23 415 fd = lttng_directory_handle_open_file(handle,
93bed9fe
JG
416 data->u.open.path, data->u.open.flags,
417 data->u.open.mode);
418 if (fd < 0) {
419 ret_value->u.ret = -1;
420 ret_value->u.open.fd = -1;
421 } else {
422 ret_value->u.ret = 0;
423 ret_value->u.open.fd = fd;
424 }
425
fe9f7760 426 ret_value->_errno = errno;
93bed9fe 427 ret_value->_error = fd < 0;
cbf53d23
JG
428 lttng_directory_handle_put(handle);
429end:
93bed9fe 430 return ret_value->u.ret;
7567352f
MD
431}
432
433static
fe9f7760 434int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 435{
cbf53d23 436 struct lttng_directory_handle *handle;
93bed9fe 437
cbf53d23
JG
438 handle = lttng_directory_handle_create_from_dirfd(data->u.unlink.dirfd);
439 if (!handle) {
440 ret_value->u.ret = -1;
441 ret_value->_errno = errno;
442 ret_value->_error = true;
443 goto end;
444 }
93bed9fe
JG
445
446 /* Ownership of dirfd is transferred to the handle. */
447 data->u.unlink.dirfd = -1;
448
cbf53d23 449 ret_value->u.ret = lttng_directory_handle_unlink_file(handle,
93bed9fe
JG
450 data->u.unlink.path);
451 ret_value->_errno = errno;
452 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
453 lttng_directory_handle_put(handle);
454end:
93bed9fe
JG
455 return ret_value->u.ret;
456}
457
458static
459int _rmdir(struct run_as_data *data, struct run_as_ret *ret_value)
460{
cbf53d23 461 struct lttng_directory_handle *handle;
93bed9fe 462
cbf53d23
JG
463 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
464 if (!handle) {
465 ret_value->u.ret = -1;
466 ret_value->_errno = errno;
467 ret_value->_error = true;
468 goto end;
469 }
93bed9fe
JG
470
471 /* Ownership of dirfd is transferred to the handle. */
472 data->u.rmdir.dirfd = -1;
473
474 ret_value->u.ret = lttng_directory_handle_remove_subdirectory(
cbf53d23 475 handle, data->u.rmdir.path);
fe9f7760 476 ret_value->_errno = errno;
93bed9fe 477 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
478 lttng_directory_handle_put(handle);
479end:
93bed9fe 480 return ret_value->u.ret;
60b6c79c
MD
481}
482
483static
fe9f7760 484int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 485{
cbf53d23 486 struct lttng_directory_handle *handle;
93bed9fe 487
cbf53d23
JG
488 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
489 if (!handle) {
490 ret_value->u.ret = -1;
491 ret_value->_errno = errno;
492 ret_value->_error = true;
493 goto end;
494 }
93bed9fe
JG
495
496 /* Ownership of dirfd is transferred to the handle. */
497 data->u.rmdir.dirfd = -1;
498
499 ret_value->u.ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 500 handle, data->u.rmdir.path, data->u.rmdir.flags);
fe9f7760 501 ret_value->_errno = errno;
93bed9fe 502 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
503 lttng_directory_handle_put(handle);
504end:
93bed9fe
JG
505 return ret_value->u.ret;
506}
507
508static
509int _rename(struct run_as_data *data, struct run_as_ret *ret_value)
510{
511 const char *old_path, *new_path;
cbf53d23 512 struct lttng_directory_handle *old_handle = NULL, *new_handle = NULL;
93bed9fe
JG
513
514 old_path = data->u.rename.old_path;
515 new_path = data->u.rename.new_path;
516
cbf53d23 517 old_handle = lttng_directory_handle_create_from_dirfd(
93bed9fe 518 data->u.rename.dirfds[0]);
cbf53d23
JG
519 if (!old_handle) {
520 ret_value->u.ret = -1;
521 goto end;
522 }
523 new_handle = lttng_directory_handle_create_from_dirfd(
93bed9fe 524 data->u.rename.dirfds[1]);
cbf53d23
JG
525 if (!new_handle) {
526 ret_value->u.ret = -1;
527 goto end;
528 }
93bed9fe
JG
529
530 /* Ownership of dirfds are transferred to the handles. */
531 data->u.rename.dirfds[0] = data->u.rename.dirfds[1] = -1;
532
533 /* Safe to call as we have transitioned to the requested uid/gid. */
534 ret_value->u.ret = lttng_directory_handle_rename(
cbf53d23
JG
535 old_handle, old_path, new_handle, new_path);
536end:
537 lttng_directory_handle_put(old_handle);
538 lttng_directory_handle_put(new_handle);
93bed9fe
JG
539 ret_value->_errno = errno;
540 ret_value->_error = (ret_value->u.ret) ? true : false;
93bed9fe 541 return ret_value->u.ret;
7567352f 542}
df5b86c8 543
b1b34226 544#ifdef HAVE_ELF_H
241e0a5a
FD
545static
546int _extract_elf_symbol_offset(struct run_as_data *data,
547 struct run_as_ret *ret_value)
548{
549 int ret = 0;
65eefd4c 550 uint64_t offset;
241e0a5a 551
93bed9fe
JG
552 ret_value->_error = false;
553 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
241e0a5a 554 data->u.extract_elf_symbol_offset.function,
65eefd4c 555 &offset);
241e0a5a
FD
556 if (ret) {
557 DBG("Failed to extract ELF function offset");
558 ret_value->_error = true;
559 }
65eefd4c 560 ret_value->u.extract_elf_symbol_offset.offset = offset;
241e0a5a
FD
561
562 return ret;
563}
564
0ef03255
FD
565static
566int _extract_sdt_probe_offsets(struct run_as_data *data,
567 struct run_as_ret *ret_value)
568{
569 int ret = 0;
570 uint64_t *offsets = NULL;
571 uint32_t num_offset;
572
573 ret_value->_error = false;
574
575 /* On success, this call allocates the offsets paramater. */
93bed9fe
JG
576 ret = lttng_elf_get_sdt_probe_offsets(
577 data->u.extract_sdt_probe_offsets.fd,
0ef03255
FD
578 data->u.extract_sdt_probe_offsets.provider_name,
579 data->u.extract_sdt_probe_offsets.probe_name,
580 &offsets, &num_offset);
581
582 if (ret) {
583 DBG("Failed to extract SDT probe offsets");
584 ret_value->_error = true;
585 goto end;
586 }
587
588 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) {
589 DBG("Wrong number of probes.");
590 ret = -1;
591 ret_value->_error = true;
592 goto free_offset;
593 }
594
595 /* Copy the content of the offsets array to the ret struct. */
596 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
597 offsets, num_offset * sizeof(uint64_t));
598
599 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
600
601free_offset:
602 free(offsets);
603end:
604 return ret;
605}
b1b34226
MJ
606#else
607static
608int _extract_elf_symbol_offset(struct run_as_data *data,
609 struct run_as_ret *ret_value)
610{
611 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
612 return -1;
613}
614
615static
616int _extract_sdt_probe_offsets(struct run_as_data *data,
617 struct run_as_ret *ret_value)
618{
619 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
620 return -1;
621}
622#endif
241e0a5a 623
7567352f
MD
624static
625run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
626{
627 switch (cmd) {
628 case RUN_AS_MKDIR:
18710679
JG
629 case RUN_AS_MKDIRAT:
630 return _mkdirat;
631 case RUN_AS_MKDIR_RECURSIVE:
632 case RUN_AS_MKDIRAT_RECURSIVE:
633 return _mkdirat_recursive;
7567352f 634 case RUN_AS_OPEN:
2912cead 635 case RUN_AS_OPENAT:
7567352f
MD
636 return _open;
637 case RUN_AS_UNLINK:
2912cead 638 case RUN_AS_UNLINKAT:
7567352f 639 return _unlink;
93bed9fe
JG
640 case RUN_AS_RMDIR:
641 case RUN_AS_RMDIRAT:
642 return _rmdir;
7567352f 643 case RUN_AS_RMDIR_RECURSIVE:
93bed9fe 644 case RUN_AS_RMDIRAT_RECURSIVE:
7567352f 645 return _rmdir_recursive;
93bed9fe
JG
646 case RUN_AS_RENAME:
647 case RUN_AS_RENAMEAT:
648 return _rename;
241e0a5a
FD
649 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
650 return _extract_elf_symbol_offset;
0ef03255
FD
651 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
652 return _extract_sdt_probe_offsets;
7567352f 653 default:
62a7b8ed 654 ERR("Unknown command %d", (int) cmd);
7567352f
MD
655 return NULL;
656 }
60b6c79c
MD
657}
658
4628484a 659static
93bed9fe 660int do_send_fds(int sock, const int *fds, unsigned int fd_count)
4628484a 661{
7567352f 662 ssize_t len;
93bed9fe
JG
663 unsigned int i;
664
665 for (i = 0; i < fd_count; i++) {
666 if (fds[i] < 0) {
667 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
668 fds[i]);
669 /* Return 0 as this is not a fatal error. */
670 return 0;
671 }
672 }
4628484a 673
93bed9fe
JG
674 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
675 return len < 0 ? -1 : 0;
4628484a
MD
676}
677
678static
93bed9fe 679int do_recv_fds(int sock, int *fds, unsigned int fd_count)
4628484a 680{
93bed9fe
JG
681 int ret = 0;
682 unsigned int i;
7567352f 683 ssize_t len;
4628484a 684
93bed9fe
JG
685 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
686 if (len == 0) {
687 ret = -1;
688 goto end;
da9ee832 689 } else if (len < 0) {
93bed9fe
JG
690 PERROR("Failed to receive file descriptors from socket");
691 ret = -1;
692 goto end;
ca9eb994
JG
693 }
694
93bed9fe
JG
695 for (i = 0; i < fd_count; i++) {
696 if (fds[i] < 0) {
697 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
698 /* Return 0 as this is not a fatal error. */
699 }
700 }
701end:
702 return ret;
4628484a
MD
703}
704
fe9f7760 705static
93bed9fe
JG
706int send_fds_to_worker(const struct run_as_worker *worker,
707 const struct run_as_data *data)
fe9f7760
FD
708{
709 int ret = 0;
93bed9fe 710 unsigned int i;
fe9f7760 711
93bed9fe
JG
712 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
713 goto end;
fe9f7760
FD
714 }
715
93bed9fe
JG
716 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
717 if (COMMAND_IN_FDS(data)[i] < 0) {
718 ERR("Refusing to send invalid fd to worker (fd = %i)",
719 COMMAND_IN_FDS(data)[i]);
720 ret = -1;
721 goto end;
722 }
723 }
ca9eb994 724
93bed9fe
JG
725 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
726 COMMAND_IN_FD_COUNT(data));
fe9f7760 727 if (ret < 0) {
93bed9fe 728 PERROR("Failed to send file descriptor to run-as worker");
fe9f7760 729 ret = -1;
93bed9fe 730 goto end;
fe9f7760 731 }
93bed9fe 732end:
fe9f7760
FD
733 return ret;
734}
735
736static
93bed9fe
JG
737int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
738 struct run_as_ret *run_as_ret)
fe9f7760 739{
93bed9fe
JG
740 int ret = 0;
741 unsigned int i;
fe9f7760 742
93bed9fe
JG
743 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
744 goto end;
fe9f7760
FD
745 }
746
93bed9fe
JG
747 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
748 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 749 if (ret < 0) {
93bed9fe
JG
750 PERROR("Failed to send file descriptor to master process");
751 goto end;
fe9f7760
FD
752 }
753
93bed9fe
JG
754 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
755 int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]);
ca9eb994 756
93bed9fe
JG
757 if (ret_close < 0) {
758 PERROR("Failed to close result file descriptor");
759 }
760 }
761end:
fe9f7760
FD
762 return ret;
763}
764
765static
93bed9fe
JG
766int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
767 struct run_as_ret *run_as_ret)
fe9f7760
FD
768{
769 int ret = 0;
770
93bed9fe
JG
771 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
772 goto end;
fe9f7760
FD
773 }
774
93bed9fe
JG
775 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
776 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 777 if (ret < 0) {
93bed9fe 778 PERROR("Failed to receive file descriptor from run-as worker");
fe9f7760
FD
779 ret = -1;
780 }
93bed9fe 781end:
fe9f7760
FD
782 return ret;
783}
784
785static
93bed9fe 786int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
fe9f7760
FD
787{
788 int ret = 0;
789
93bed9fe
JG
790 if (COMMAND_USE_CWD_FD(data)) {
791 unsigned int i;
792
793 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
794 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
795 }
796 goto end;
fe9f7760
FD
797 }
798
93bed9fe
JG
799 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
800 COMMAND_IN_FD_COUNT(data));
fe9f7760 801 if (ret < 0) {
93bed9fe 802 PERROR("Failed to receive file descriptors from master process");
fe9f7760
FD
803 ret = -1;
804 }
93bed9fe 805end:
fe9f7760
FD
806 return ret;
807}
808
809static
93bed9fe 810int cleanup_received_fds(struct run_as_data *data)
fe9f7760 811{
93bed9fe 812 int ret = 0, i;
fe9f7760 813
93bed9fe
JG
814 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
815 if (COMMAND_IN_FDS(data)[i] == -1) {
816 continue;
817 }
818 ret = close(COMMAND_IN_FDS(data)[i]);
819 if (ret) {
820 PERROR("Failed to close file descriptor received fd in run-as worker");
821 goto end;
822 }
fe9f7760 823 }
93bed9fe 824end:
fe9f7760
FD
825 return ret;
826}
0ef03255 827
a62bbe8c
FD
828static int get_user_infos_from_uid(
829 uid_t uid, char **username, gid_t *primary_gid)
830{
831 int ret;
832 char *buf = NULL;
c043a8d8
FD
833 long raw_get_pw_buf_size;
834 size_t get_pw_buf_size;
a62bbe8c
FD
835 struct passwd pwd;
836 struct passwd *result = NULL;
837
838 /* Fetch the max size for the temporary buffer. */
839 errno = 0;
c043a8d8
FD
840 raw_get_pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
841 if (raw_get_pw_buf_size < 0) {
a62bbe8c
FD
842 if (errno != 0) {
843 PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
844 goto error;
845 }
846
847 /* Limit is indeterminate. */
848 WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
849 "indeterminate; falling back to default buffer size");
c043a8d8 850 raw_get_pw_buf_size = GETPW_BUFFER_FALLBACK_SIZE;
a62bbe8c
FD
851 }
852
c043a8d8
FD
853 get_pw_buf_size = (size_t) raw_get_pw_buf_size;
854
855 buf = zmalloc(get_pw_buf_size);
a62bbe8c
FD
856 if (buf == NULL) {
857 PERROR("Failed to allocate buffer to get password file entries");
858 goto error;
859 }
860
c043a8d8 861 ret = getpwuid_r(uid, &pwd, buf, get_pw_buf_size, &result);
a62bbe8c
FD
862 if (ret < 0) {
863 PERROR("Failed to get user information for user: uid = %d",
864 (int) uid);
865 goto error;
866 }
867
868 if (result == NULL) {
869 ERR("Failed to find user information in password entries: uid = %d",
870 (int) uid);
871 ret = -1;
872 goto error;
873 }
874
875 *username = strdup(result->pw_name);
876 if (*username == NULL) {
877 PERROR("Failed to copy user name");
878 goto error;
879 }
880
881 *primary_gid = result->pw_gid;
882
883end:
884 free(buf);
885 return ret;
886error:
887 *username = NULL;
888 *primary_gid = -1;
889 ret = -1;
890 goto end;
891}
892
893static int demote_creds(
894 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
895{
896 int ret = 0;
897 gid_t primary_gid;
898 char *username = NULL;
899
900 /* Change the group id. */
901 if (prev_gid != new_gid) {
902 ret = setegid(new_gid);
903 if (ret < 0) {
904 PERROR("Failed to set effective group id: new_gid = %d",
905 (int) new_gid);
906 goto end;
907 }
908 }
909
910 /* Change the user id. */
911 if (prev_uid != new_uid) {
912 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
913 if (ret < 0) {
914 goto end;
915 }
916
917 /*
918 * Initialize the supplementary group access list.
919 *
920 * This is needed to handle cases where the supplementary groups
921 * of the user the process is demoting-to would give it access
922 * to a given file/folder, but not it's primary group.
923 *
924 * e.g
925 * username: User1
926 * Primary Group: User1
927 * Secondary group: Disk, Network
928 *
929 * mkdir inside the following directory must work since User1
930 * is part of the Network group.
931 *
932 * drwxrwx--- 2 root Network 4096 Jul 23 17:17 /tmp/my_folder/
933 *
934 *
935 * The order of the following initgroups and seteuid calls is
936 * important here;
937 * Only a root process or one with CAP_SETGID capability can
938 * call the the initgroups() function. We must initialize the
939 * supplementary groups before we change the effective
940 * UID to a less-privileged user.
941 */
942 ret = initgroups(username, primary_gid);
943 if (ret < 0) {
944 PERROR("Failed to init the supplementary group access list: "
945 "username = `%s`, primary gid = %d", username,
946 (int) primary_gid);
947 goto end;
948 }
949
950 ret = seteuid(new_uid);
951 if (ret < 0) {
952 PERROR("Failed to set effective user id: new_uid = %d",
953 (int) new_uid);
954 goto end;
955 }
956 }
957end:
958 free(username);
959 return ret;
960}
961
962static int promote_creds(
963 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
964{
965 int ret = 0;
966 gid_t primary_gid;
967 char *username = NULL;
968
969 /* Change the group id. */
970 if (prev_gid != new_gid) {
971 ret = setegid(new_gid);
972 if (ret < 0) {
973 PERROR("Failed to set effective group id: new_gid = %d",
974 (int) new_gid);
975 goto end;
976 }
977 }
978
979 /* Change the user id. */
980 if (prev_uid != new_uid) {
981 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
982 if (ret < 0) {
983 goto end;
984 }
985
986 /*
987 * seteuid call must be done before the initgroups call because
988 * we need to be privileged (CAP_SETGID) to call initgroups().
989 */
990 ret = seteuid(new_uid);
991 if (ret < 0) {
992 PERROR("Failed to set effective user id: new_uid = %d",
993 (int) new_uid);
994 goto end;
995 }
996
997 /*
998 * Initialize the supplementary group access list.
999 *
1000 * There is a possibility the groups we set in the following
1001 * initgroups() call are not exactly the same as the ones we
1002 * had when we originally demoted. This can happen if the
1003 * /etc/group file is modified after the runas process is
1004 * forked. This is very unlikely.
1005 */
1006 ret = initgroups(username, primary_gid);
1007 if (ret < 0) {
1008 PERROR("Failed to init the supplementary group access "
1009 "list: username = `%s`, primary gid = %d",
1010 username, (int) primary_gid)
1011 goto end;
1012 }
1013 }
1014end:
1015 free(username);
1016 return ret;
1017}
1018
7567352f
MD
1019/*
1020 * Return < 0 on error, 0 if OK, 1 on hangup.
1021 */
c2b75c49 1022static
7567352f 1023int handle_one_cmd(struct run_as_worker *worker)
c2b75c49 1024{
a62bbe8c
FD
1025 int ret = 0, promote_ret;
1026 struct run_as_data data = {};
1027 ssize_t readlen, writelen;
1028 struct run_as_ret sendret = {};
1029 run_as_fct cmd;
1030 uid_t prev_ruid;
1031 gid_t prev_rgid;
7567352f 1032
fe9f7760
FD
1033 /*
1034 * Stage 1: Receive run_as_data struct from the master.
1035 * The structure contains the command type and all the parameters needed for
1036 * its execution
1037 */
7567352f
MD
1038 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
1039 sizeof(data));
1040 if (readlen == 0) {
1041 /* hang up */
1042 ret = 1;
1043 goto end;
1044 }
1045 if (readlen < sizeof(data)) {
1046 PERROR("lttcomm_recv_unix_sock error");
1047 ret = -1;
1048 goto end;
1049 }
c2b75c49 1050
7567352f
MD
1051 cmd = run_as_enum_to_fct(data.cmd);
1052 if (!cmd) {
1053 ret = -1;
1054 goto end;
1055 }
1056
fe9f7760
FD
1057 /*
1058 * Stage 2: Receive file descriptor from master.
1059 * Some commands need a file descriptor as input so if it's needed we
1060 * receive the fd using the Unix socket.
1061 */
93bed9fe 1062 ret = recv_fds_from_master(worker, &data);
fe9f7760
FD
1063 if (ret < 0) {
1064 PERROR("recv_fd_from_master error");
1065 ret = -1;
1066 goto end;
1067 }
1068
a62bbe8c
FD
1069 prev_ruid = getuid();
1070 prev_rgid = getgid();
1071
1072 ret = demote_creds(prev_ruid, prev_rgid, data.uid, data.gid);
1073 if (ret < 0) {
1074 goto write_return;
c2b75c49 1075 }
fe9f7760 1076
c2b75c49
MD
1077 /*
1078 * Also set umask to 0 for mkdir executable bit.
1079 */
1080 umask(0);
fe9f7760
FD
1081
1082 /*
1083 * Stage 3: Execute the command
1084 */
1085 ret = (*cmd)(&data, &sendret);
1086 if (ret < 0) {
1087 DBG("Execution of command returned an error");
1088 }
6d73c4ef
MD
1089
1090write_return:
93bed9fe 1091 ret = cleanup_received_fds(&data);
fe9f7760
FD
1092 if (ret < 0) {
1093 ERR("Error cleaning up FD");
a62bbe8c 1094 goto promote_back;
fe9f7760
FD
1095 }
1096
1097 /*
1098 * Stage 4: Send run_as_ret structure to the master.
1099 * This structure contain the return value of the command and the errno.
1100 */
7567352f
MD
1101 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1102 sizeof(sendret));
6cd525e8 1103 if (writelen < sizeof(sendret)) {
7567352f
MD
1104 PERROR("lttcomm_send_unix_sock error");
1105 ret = -1;
a62bbe8c 1106 goto promote_back;
7567352f 1107 }
fe9f7760
FD
1108
1109 /*
93bed9fe 1110 * Stage 5: Send resulting file descriptors to the master.
fe9f7760 1111 */
93bed9fe 1112 ret = send_fds_to_master(worker, data.cmd, &sendret);
fe9f7760
FD
1113 if (ret < 0) {
1114 DBG("Sending FD to master returned an error");
7567352f 1115 }
fe9f7760 1116
7567352f 1117 ret = 0;
a62bbe8c
FD
1118
1119promote_back:
1120 /* Return to previous uid/gid. */
1121 promote_ret = promote_creds(data.uid, data.gid, prev_ruid, prev_rgid);
1122 if (promote_ret < 0) {
1123 ERR("Failed to promote back to the initial credentials");
1124 }
1125
7567352f
MD
1126end:
1127 return ret;
1128}
1129
1130static
1131int run_as_worker(struct run_as_worker *worker)
1132{
1133 int ret;
1134 ssize_t writelen;
1135 struct run_as_ret sendret;
1136 size_t proc_orig_len;
1137
1138 /*
1139 * Initialize worker. Set a different process cmdline.
1140 */
1141 proc_orig_len = strlen(worker->procname);
1142 memset(worker->procname, 0, proc_orig_len);
1143 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
1144
e1055edb
JG
1145 ret = lttng_prctl(PR_SET_NAME,
1146 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
1147 if (ret && ret != -ENOSYS) {
b8090274
JG
1148 /* Don't fail as this is not essential. */
1149 PERROR("prctl PR_SET_NAME");
6cd525e8 1150 }
7567352f 1151
fe9f7760
FD
1152 memset(&sendret, 0, sizeof(sendret));
1153
7567352f
MD
1154 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1155 sizeof(sendret));
1156 if (writelen < sizeof(sendret)) {
1157 PERROR("lttcomm_send_unix_sock error");
1158 ret = EXIT_FAILURE;
1159 goto end;
1160 }
1161
1162 for (;;) {
1163 ret = handle_one_cmd(worker);
1164 if (ret < 0) {
1165 ret = EXIT_FAILURE;
1166 goto end;
1167 } else if (ret > 0) {
1168 break;
1169 } else {
1170 continue; /* Next command. */
1171 }
1172 }
1173 ret = EXIT_SUCCESS;
1174end:
1175 return ret;
c2b75c49
MD
1176}
1177
60b6c79c 1178static
7567352f
MD
1179int run_as_cmd(struct run_as_worker *worker,
1180 enum run_as_cmd cmd,
1181 struct run_as_data *data,
fe9f7760 1182 struct run_as_ret *ret_value,
7567352f 1183 uid_t uid, gid_t gid)
60b6c79c 1184{
fe9f7760 1185 int ret = 0;
7567352f 1186 ssize_t readlen, writelen;
60b6c79c
MD
1187
1188 /*
1189 * If we are non-root, we can only deal with our own uid.
1190 */
1191 if (geteuid() != 0) {
1192 if (uid != geteuid()) {
fe9f7760
FD
1193 ret = -1;
1194 ret_value->_errno = EPERM;
60b6c79c 1195 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
08797918 1196 (int) uid, (int) geteuid());
df5b86c8 1197 goto end;
60b6c79c 1198 }
60b6c79c
MD
1199 }
1200
7567352f
MD
1201 data->cmd = cmd;
1202 data->uid = uid;
1203 data->gid = gid;
1204
fe9f7760
FD
1205 /*
1206 * Stage 1: Send the run_as_data struct to the worker process
1207 */
7567352f
MD
1208 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
1209 sizeof(*data));
1210 if (writelen < sizeof(*data)) {
1211 PERROR("Error writing message to run_as");
fe9f7760
FD
1212 ret = -1;
1213 ret_value->_errno = EIO;
60b6c79c 1214 goto end;
c2b75c49 1215 }
7567352f 1216
fe9f7760
FD
1217 /*
1218 * Stage 2: Send file descriptor to the worker process if needed
1219 */
93bed9fe 1220 ret = send_fds_to_worker(worker, data);
fe9f7760
FD
1221 if (ret) {
1222 PERROR("do_send_fd error");
1223 ret = -1;
1224 ret_value->_errno = EIO;
1225 goto end;
1226 }
1227
1228 /*
1229 * Stage 3: Wait for the execution of the command
1230 */
1231
1232 /*
1233 * Stage 4: Receive the run_as_ret struct containing the return value and
1234 * errno
1235 */
1236 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1237 sizeof(*ret_value));
da9ee832
JG
1238 if (!readlen) {
1239 ERR("Run-as worker has hung-up during run_as_cmd");
fe9f7760
FD
1240 ret = -1;
1241 ret_value->_errno = EIO;
da9ee832 1242 goto end;
fe9f7760 1243 } else if (readlen < sizeof(*ret_value)) {
7567352f 1244 PERROR("Error reading response from run_as");
fe9f7760
FD
1245 ret = -1;
1246 ret_value->_errno = errno;
033b58a7 1247 goto end;
6cd525e8 1248 }
fe9f7760 1249
ca9eb994
JG
1250 if (ret_value->_error) {
1251 /* Skip stage 5 on error as there will be no fd to receive. */
1252 goto end;
1253 }
1254
fe9f7760
FD
1255 /*
1256 * Stage 5: Receive file descriptor if needed
1257 */
93bed9fe 1258 ret = recv_fds_from_worker(worker, cmd, ret_value);
fe9f7760
FD
1259 if (ret < 0) {
1260 ERR("Error receiving fd");
1261 ret = -1;
1262 ret_value->_errno = EIO;
4c462e79 1263 }
7567352f 1264
60b6c79c 1265end:
fe9f7760 1266 return ret;
60b6c79c
MD
1267}
1268
2d85a600 1269/*
7567352f 1270 * This is for debugging ONLY and should not be considered secure.
2d85a600
MD
1271 */
1272static
fe9f7760
FD
1273int run_as_noworker(enum run_as_cmd cmd,
1274 struct run_as_data *data, struct run_as_ret *ret_value,
1275 uid_t uid, gid_t gid)
2d85a600 1276{
df5b86c8 1277 int ret, saved_errno;
5b73926f 1278 mode_t old_mask;
7567352f 1279 run_as_fct fct;
5b73926f 1280
7567352f
MD
1281 fct = run_as_enum_to_fct(cmd);
1282 if (!fct) {
1283 errno = -ENOSYS;
1284 ret = -1;
1285 goto end;
1286 }
5b73926f 1287 old_mask = umask(0);
fe9f7760
FD
1288 ret = fct(data, ret_value);
1289 saved_errno = ret_value->_errno;
5b73926f 1290 umask(old_mask);
df5b86c8 1291 errno = saved_errno;
7567352f 1292end:
5b73926f 1293 return ret;
2d85a600
MD
1294}
1295
8fec83ea
JG
1296static
1297int reset_sighandler(void)
1298{
1299 int sig;
1300
1301 DBG("Resetting run_as worker signal handlers to default");
1302 for (sig = 1; sig <= 31; sig++) {
1303 (void) signal(sig, SIG_DFL);
1304 }
1305 return 0;
1306}
1307
1308static
1309void worker_sighandler(int sig)
1310{
1311 const char *signame;
1312
1313 /*
1314 * The worker will inherit its parent's signals since they are part of
1315 * the same process group. However, in the case of SIGINT and SIGTERM,
1316 * we want to give the worker a chance to teardown gracefully when its
1317 * parent closes the command socket.
1318 */
1319 switch (sig) {
1320 case SIGINT:
1321 signame = "SIGINT";
1322 break;
1323 case SIGTERM:
1324 signame = "SIGTERM";
1325 break;
1326 default:
1327 signame = NULL;
1328 }
1329
1330 if (signame) {
1331 DBG("run_as worker received signal %s", signame);
1332 } else {
1333 DBG("run_as_worker received signal %d", sig);
1334 }
1335}
1336
1337static
1338int set_worker_sighandlers(void)
1339{
1340 int ret = 0;
1341 sigset_t sigset;
1342 struct sigaction sa;
1343
1344 if ((ret = sigemptyset(&sigset)) < 0) {
1345 PERROR("sigemptyset");
1346 goto end;
1347 }
1348
1349 sa.sa_handler = worker_sighandler;
1350 sa.sa_mask = sigset;
1351 sa.sa_flags = 0;
1352 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1353 PERROR("sigaction SIGINT");
1354 goto end;
1355 }
1356
1357 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1358 PERROR("sigaction SIGTERM");
1359 goto end;
1360 }
1361
1362 DBG("run_as signal handler set for SIGTERM and SIGINT");
1363end:
1364 return ret;
1365}
1366
1367static
929f71ec
JG
1368int run_as_create_worker_no_lock(const char *procname,
1369 post_fork_cleanup_cb clean_up_func,
1370 void *clean_up_user_data)
8fec83ea
JG
1371{
1372 pid_t pid;
1373 int i, ret = 0;
1374 ssize_t readlen;
1375 struct run_as_ret recvret;
1376 struct run_as_worker *worker;
1377
1378 assert(!global_worker);
1379 if (!use_clone()) {
1380 /*
1381 * Don't initialize a worker, all run_as tasks will be performed
1382 * in the current process.
1383 */
1384 ret = 0;
1385 goto end;
1386 }
1387 worker = zmalloc(sizeof(*worker));
1388 if (!worker) {
1389 ret = -ENOMEM;
1390 goto end;
1391 }
1392 worker->procname = strdup(procname);
1393 if (!worker->procname) {
1394 ret = -ENOMEM;
8c96eded 1395 goto error_procname_alloc;
8fec83ea
JG
1396 }
1397 /* Create unix socket. */
1398 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1399 ret = -1;
1400 goto error_sock;
1401 }
1402
1403 /* Fork worker. */
1404 pid = fork();
1405 if (pid < 0) {
1406 PERROR("fork");
1407 ret = -1;
1408 goto error_fork;
1409 } else if (pid == 0) {
1410 /* Child */
1411
1412 reset_sighandler();
1413
1414 set_worker_sighandlers();
929f71ec
JG
1415 if (clean_up_func) {
1416 if (clean_up_func(clean_up_user_data) < 0) {
1417 ERR("Run-as post-fork clean-up failed, exiting.");
1418 exit(EXIT_FAILURE);
1419 }
1420 }
8fec83ea
JG
1421
1422 /* Just close, no shutdown. */
1423 if (close(worker->sockpair[0])) {
1424 PERROR("close");
1425 exit(EXIT_FAILURE);
1426 }
1427
1428 /*
1429 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1430 * Sockpair[1] is used as a control channel with the master
1431 */
1432 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1433 if (i != worker->sockpair[1]) {
1434 (void) close(i);
1435 }
1436 }
1437
1438 worker->sockpair[0] = -1;
1439 ret = run_as_worker(worker);
1440 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1441 PERROR("close");
1442 ret = -1;
1443 }
1444 worker->sockpair[1] = -1;
340cf672
JG
1445 free(worker->procname);
1446 free(worker);
8fec83ea
JG
1447 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1448 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1449 } else {
1450 /* Parent */
1451
1452 /* Just close, no shutdown. */
1453 if (close(worker->sockpair[1])) {
1454 PERROR("close");
1455 ret = -1;
1456 goto error_fork;
1457 }
1458 worker->sockpair[1] = -1;
1459 worker->pid = pid;
1460 /* Wait for worker to become ready. */
1461 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1462 &recvret, sizeof(recvret));
1463 if (readlen < sizeof(recvret)) {
1464 ERR("readlen: %zd", readlen);
1465 PERROR("Error reading response from run_as at creation");
1466 ret = -1;
1467 goto error_fork;
1468 }
1469 global_worker = worker;
1470 }
1471end:
1472 return ret;
1473
1474 /* Error handling. */
1475error_fork:
1476 for (i = 0; i < 2; i++) {
1477 if (worker->sockpair[i] < 0) {
1478 continue;
1479 }
1480 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1481 PERROR("close");
1482 }
1483 worker->sockpair[i] = -1;
1484 }
1485error_sock:
8c96eded
FD
1486 free(worker->procname);
1487error_procname_alloc:
8fec83ea
JG
1488 free(worker);
1489 return ret;
1490}
1491
a01c682b
JR
1492static
1493void run_as_destroy_worker_no_lock(void)
1494{
1495 struct run_as_worker *worker = global_worker;
1496
1497 DBG("Destroying run_as worker");
1498 if (!worker) {
1499 return;
1500 }
1501 /* Close unix socket */
1502 DBG("Closing run_as worker socket");
1503 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1504 PERROR("close");
1505 }
1506 worker->sockpair[0] = -1;
1507 /* Wait for worker. */
1508 for (;;) {
1509 int status;
1510 pid_t wait_ret;
1511
1512 wait_ret = waitpid(worker->pid, &status, 0);
1513 if (wait_ret < 0) {
1514 if (errno == EINTR) {
1515 continue;
1516 }
1517 PERROR("waitpid");
1518 break;
1519 }
1520
1521 if (WIFEXITED(status)) {
1522 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1523 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1524 WEXITSTATUS(status));
1525 break;
1526 } else if (WIFSIGNALED(status)) {
1527 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1528 WTERMSIG(status));
1529 break;
1530 }
1531 }
1532 free(worker->procname);
1533 free(worker);
1534 global_worker = NULL;
1535}
1536
2d85a600 1537static
fe9f7760 1538int run_as_restart_worker(struct run_as_worker *worker)
2d85a600 1539{
fe9f7760
FD
1540 int ret = 0;
1541 char *procname = NULL;
1542
1543 procname = worker->procname;
1544
1545 /* Close socket to run_as worker process and clean up the zombie process */
a01c682b 1546 run_as_destroy_worker_no_lock();
fe9f7760
FD
1547
1548 /* Create a new run_as worker process*/
929f71ec 1549 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
fe9f7760
FD
1550 if (ret < 0 ) {
1551 ERR("Restarting the worker process failed");
1552 ret = -1;
1553 goto err;
1554 }
1555err:
1556 return ret;
1557}
1558
1559static
1560int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1561 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1562{
1563 int ret, saved_errno;
7567352f 1564
8fec83ea 1565 pthread_mutex_lock(&worker_lock);
749b7a0c 1566 if (use_clone()) {
7567352f 1567 DBG("Using run_as worker");
8fec83ea 1568
749b7a0c 1569 assert(global_worker);
749b7a0c 1570
fe9f7760
FD
1571 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1572 saved_errno = ret_value->_errno;
1573
fe9f7760
FD
1574 /*
1575 * If the worker thread crashed the errno is set to EIO. we log
1576 * the error and start a new worker process.
1577 */
1578 if (ret == -1 && saved_errno == EIO) {
1579 DBG("Socket closed unexpectedly... "
1580 "Restarting the worker process");
1581 ret = run_as_restart_worker(global_worker);
fe9f7760
FD
1582 if (ret == -1) {
1583 ERR("Failed to restart worker process.");
1584 goto err;
1585 }
1586 }
2d85a600 1587 } else {
7567352f 1588 DBG("Using run_as without worker");
fe9f7760 1589 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
2d85a600 1590 }
fe9f7760 1591err:
8fec83ea 1592 pthread_mutex_unlock(&worker_lock);
7567352f 1593 return ret;
2d85a600
MD
1594}
1595
90e535ef 1596LTTNG_HIDDEN
e11d277b 1597int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1598{
18710679
JG
1599 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1600}
1601
1602LTTNG_HIDDEN
1603int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1604 uid_t uid, gid_t gid)
1605{
1606 int ret;
93bed9fe
JG
1607 struct run_as_data data = {};
1608 struct run_as_ret run_as_ret = {};
60b6c79c 1609
18710679
JG
1610 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1611 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1612 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1613 ret = lttng_strncpy(data.u.mkdir.path, path,
1614 sizeof(data.u.mkdir.path));
18710679
JG
1615 if (ret) {
1616 ERR("Failed to copy path argument of mkdirat recursive command");
1617 goto error;
1618 }
93bed9fe
JG
1619 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1620 data.u.mkdir.mode = mode;
1621 data.u.mkdir.dirfd = dirfd;
18710679
JG
1622 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1623 &data, &run_as_ret, uid, gid);
1624 errno = run_as_ret._errno;
93bed9fe 1625 ret = run_as_ret.u.ret;
18710679
JG
1626error:
1627 return ret;
60b6c79c
MD
1628}
1629
90e535ef 1630LTTNG_HIDDEN
e11d277b 1631int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1632{
18710679
JG
1633 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1634}
1635
1636LTTNG_HIDDEN
1637int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1638 uid_t uid, gid_t gid)
1639{
1640 int ret;
93bed9fe
JG
1641 struct run_as_data data = {};
1642 struct run_as_ret run_as_ret = {};
fe9f7760 1643
18710679
JG
1644 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1645 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1646 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1647 ret = lttng_strncpy(data.u.mkdir.path, path,
1648 sizeof(data.u.mkdir.path));
18710679
JG
1649 if (ret) {
1650 ERR("Failed to copy path argument of mkdirat command");
1651 goto error;
1652 }
93bed9fe
JG
1653 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1654 data.u.mkdir.mode = mode;
1655 data.u.mkdir.dirfd = dirfd;
18710679
JG
1656 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1657 &data, &run_as_ret, uid, gid);
1658 errno = run_as_ret._errno;
93bed9fe 1659 ret = run_as_ret.u.ret;
18710679
JG
1660error:
1661 return ret;
60b6c79c
MD
1662}
1663
90e535ef 1664LTTNG_HIDDEN
2912cead
JG
1665int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1666 gid_t gid)
1667{
1668 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1669}
1670
1671LTTNG_HIDDEN
1672int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1673 uid_t uid, gid_t gid)
60b6c79c 1674{
93bed9fe
JG
1675 int ret;
1676 struct run_as_data data = {};
1677 struct run_as_ret run_as_ret = {};
fe9f7760 1678
2912cead
JG
1679 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1680 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1681 path, flags, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1682 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1683 if (ret) {
1684 ERR("Failed to copy path argument of open command");
1685 goto error;
1686 }
7567352f
MD
1687 data.u.open.flags = flags;
1688 data.u.open.mode = mode;
93bed9fe 1689 data.u.open.dirfd = dirfd;
2912cead 1690 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
93bed9fe
JG
1691 &data, &run_as_ret, uid, gid);
1692 errno = run_as_ret._errno;
1693 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1694 run_as_ret.u.open.fd;
1695error:
1696 return ret;
60b6c79c 1697}
4628484a
MD
1698
1699LTTNG_HIDDEN
1700int run_as_unlink(const char *path, uid_t uid, gid_t gid)
2912cead
JG
1701{
1702 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1703}
1704
1705LTTNG_HIDDEN
1706int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
4628484a 1707{
93bed9fe
JG
1708 int ret;
1709 struct run_as_data data = {};
1710 struct run_as_ret run_as_ret = {};
fe9f7760 1711
2912cead
JG
1712 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1713 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1714 path, (int) uid, (int) gid);
93bed9fe
JG
1715 ret = lttng_strncpy(data.u.unlink.path, path,
1716 sizeof(data.u.unlink.path));
1717 if (ret) {
1718 goto error;
1719 }
1720 data.u.unlink.dirfd = dirfd;
1721 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1722 &run_as_ret, uid, gid);
1723 errno = run_as_ret._errno;
1724 ret = run_as_ret.u.ret;
1725error:
1726 return ret;
1727}
1728
1729LTTNG_HIDDEN
1730int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1731{
1732 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1733}
1734
1735LTTNG_HIDDEN
1736int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1737{
1738 int ret;
1739 struct run_as_data data = {};
1740 struct run_as_ret run_as_ret = {};
1741
1742 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1743 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1744 path, (int) uid, (int) gid);
1745 ret = lttng_strncpy(data.u.rmdir.path, path,
1746 sizeof(data.u.rmdir.path));
1747 if (ret) {
1748 goto error;
1749 }
1750 data.u.rmdir.dirfd = dirfd;
1751 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1752 &run_as_ret, uid, gid);
1753 errno = run_as_ret._errno;
1754 ret = run_as_ret.u.ret;
1755error:
1756 return ret;
4628484a
MD
1757}
1758
1759LTTNG_HIDDEN
f75c5439 1760int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
4628484a 1761{
f75c5439 1762 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
93bed9fe 1763}
fe9f7760 1764
93bed9fe 1765LTTNG_HIDDEN
f75c5439 1766int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
93bed9fe
JG
1767{
1768 int ret;
1769 struct run_as_data data = {};
1770 struct run_as_ret run_as_ret = {};
4628484a 1771
93bed9fe
JG
1772 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1773 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1774 path, (int) uid, (int) gid);
93bed9fe
JG
1775 ret = lttng_strncpy(data.u.rmdir.path, path,
1776 sizeof(data.u.rmdir.path));
1777 if (ret) {
1778 goto error;
1779 }
1780 data.u.rmdir.dirfd = dirfd;
f75c5439 1781 data.u.rmdir.flags = flags;
93bed9fe
JG
1782 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1783 &data, &run_as_ret, uid, gid);
1784 errno = run_as_ret._errno;
1785 ret = run_as_ret.u.ret;
1786error:
1787 return ret;
1788}
1789
1790LTTNG_HIDDEN
1791int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1792{
1793 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1794}
1795
1796LTTNG_HIDDEN
1797int run_as_renameat(int old_dirfd, const char *old_name,
1798 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1799{
1800 int ret;
1801 struct run_as_data data = {};
1802 struct run_as_ret run_as_ret = {};
1803
1804 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1805 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1806 old_name,
1807 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1808 new_name, (int) uid, (int) gid);
1809 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1810 sizeof(data.u.rename.old_path));
1811 if (ret) {
1812 goto error;
1813 }
1814 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1815 sizeof(data.u.rename.new_path));
1816 if (ret) {
1817 goto error;
1818 }
1819
1820 data.u.rename.dirfds[0] = old_dirfd;
1821 data.u.rename.dirfds[1] = new_dirfd;
1822 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1823 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1824 &data, &run_as_ret, uid, gid);
1825 errno = run_as_ret._errno;
1826 ret = run_as_ret.u.ret;
1827error:
1828 return ret;
7567352f
MD
1829}
1830
241e0a5a
FD
1831LTTNG_HIDDEN
1832int run_as_extract_elf_symbol_offset(int fd, const char* function,
1833 uid_t uid, gid_t gid, uint64_t *offset)
1834{
93bed9fe
JG
1835 int ret;
1836 struct run_as_data data = {};
1837 struct run_as_ret run_as_ret = {};
f726677b 1838
241e0a5a 1839 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
93bed9fe
JG
1840 "with for uid %d and gid %d", fd, function,
1841 (int) uid, (int) gid);
241e0a5a 1842
93bed9fe 1843 data.u.extract_elf_symbol_offset.fd = fd;
241e0a5a
FD
1844
1845 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
241e0a5a 1846 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
93bed9fe
JG
1847 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1848 function,
1849 sizeof(data.u.extract_elf_symbol_offset.function));
1850 if (ret) {
1851 goto error;
1852 }
241e0a5a 1853
93bed9fe
JG
1854 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1855 errno = run_as_ret._errno;
1856 if (run_as_ret._error) {
1857 ret = -1;
1858 goto error;
241e0a5a
FD
1859 }
1860
93bed9fe
JG
1861 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1862error:
1863 return ret;
241e0a5a
FD
1864}
1865
0ef03255
FD
1866LTTNG_HIDDEN
1867int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1868 const char* probe_name, uid_t uid, gid_t gid,
1869 uint64_t **offsets, uint32_t *num_offset)
1870{
93bed9fe
JG
1871 int ret;
1872 struct run_as_data data = {};
1873 struct run_as_ret run_as_ret = {};
f726677b 1874
0ef03255 1875 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
93bed9fe
JG
1876 "provider_name=%s with for uid %d and gid %d", fd,
1877 probe_name, provider_name, (int) uid, (int) gid);
0ef03255 1878
93bed9fe 1879 data.u.extract_sdt_probe_offsets.fd = fd;
0ef03255 1880
93bed9fe
JG
1881 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1882 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1883 if (ret) {
1884 goto error;
1885 }
1886 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1887 provider_name,
1888 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1889 if (ret) {
1890 goto error;
0ef03255
FD
1891 }
1892
93bed9fe
JG
1893 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1894 errno = run_as_ret._errno;
1895 if (run_as_ret._error) {
1896 ret = -1;
1897 goto error;
1898 }
0ef03255 1899
93bed9fe 1900 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
0ef03255
FD
1901 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1902 if (!*offsets) {
93bed9fe
JG
1903 ret = -ENOMEM;
1904 goto error;
0ef03255
FD
1905 }
1906
93bed9fe
JG
1907 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1908 *num_offset * sizeof(uint64_t));
1909error:
1910 return ret;
0ef03255
FD
1911}
1912
7567352f 1913LTTNG_HIDDEN
929f71ec
JG
1914int run_as_create_worker(const char *procname,
1915 post_fork_cleanup_cb clean_up_func,
1916 void *clean_up_user_data)
7567352f 1917{
8fec83ea 1918 int ret;
7567352f 1919
749b7a0c 1920 pthread_mutex_lock(&worker_lock);
929f71ec
JG
1921 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1922 clean_up_user_data);
749b7a0c 1923 pthread_mutex_unlock(&worker_lock);
7567352f
MD
1924 return ret;
1925}
1926
1927LTTNG_HIDDEN
1928void run_as_destroy_worker(void)
1929{
749b7a0c 1930 pthread_mutex_lock(&worker_lock);
a01c682b 1931 run_as_destroy_worker_no_lock();
749b7a0c 1932 pthread_mutex_unlock(&worker_lock);
4628484a 1933}
This page took 0.172233 seconds and 5 git commands to generate.