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