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