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