]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4a2a8b5a | 2 | |
4a2a8b5a | 3 | #include <errno.h> |
6f2deb84 LP |
4 | #include <getopt.h> |
5 | #include <stdio.h> | |
4a2a8b5a | 6 | #include <stdlib.h> |
6f2deb84 | 7 | #include <unistd.h> |
4a2a8b5a | 8 | |
6f2deb84 | 9 | #include "sd-daemon.h" |
81527be1 | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
d6b4d1c7 | 12 | #include "build.h" |
4d1a6904 | 13 | #include "env-util.h" |
6e4a3245 LP |
14 | #include "fd-util.h" |
15 | #include "fdset.h" | |
f97b34a6 | 16 | #include "format-util.h" |
6f2deb84 | 17 | #include "log.h" |
5e332028 | 18 | #include "main-func.h" |
6bedfcbb | 19 | #include "parse-util.h" |
294bf0c3 | 20 | #include "pretty-print.h" |
19b761a0 | 21 | #include "process-util.h" |
07630cea | 22 | #include "string-util.h" |
6f2deb84 | 23 | #include "strv.h" |
6f63c5b8 | 24 | #include "terminal-util.h" |
4f07ddfa | 25 | #include "time-util.h" |
65c6b990 | 26 | #include "user-util.h" |
4a2a8b5a LP |
27 | |
28 | static bool arg_ready = false; | |
fd0f4da5 LP |
29 | static bool arg_reloading = false; |
30 | static bool arg_stopping = false; | |
4a2a8b5a LP |
31 | static pid_t arg_pid = 0; |
32 | static const char *arg_status = NULL; | |
96551bae | 33 | static bool arg_booted = false; |
65c6b990 LP |
34 | static uid_t arg_uid = UID_INVALID; |
35 | static gid_t arg_gid = GID_INVALID; | |
4f07ddfa | 36 | static bool arg_no_block = false; |
9175338e LP |
37 | static char **arg_env = NULL; |
38 | static char **arg_exec = NULL; | |
6e4a3245 LP |
39 | static FDSet *arg_fds = NULL; |
40 | static char *arg_fdname = NULL; | |
9175338e LP |
41 | |
42 | STATIC_DESTRUCTOR_REGISTER(arg_env, strv_freep); | |
43 | STATIC_DESTRUCTOR_REGISTER(arg_exec, strv_freep); | |
6e4a3245 LP |
44 | STATIC_DESTRUCTOR_REGISTER(arg_fds, fdset_freep); |
45 | STATIC_DESTRUCTOR_REGISTER(arg_fdname, freep); | |
4a2a8b5a | 46 | |
37ec0fdd LP |
47 | static int help(void) { |
48 | _cleanup_free_ char *link = NULL; | |
49 | int r; | |
50 | ||
51 | r = terminal_urlify_man("systemd-notify", "1", &link); | |
52 | if (r < 0) | |
53 | return log_oom(); | |
54 | ||
6f63c5b8 | 55 | printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n" |
9175338e | 56 | "%s [OPTIONS...] --exec [VARIABLE=VALUE...] ; CMDLINE...\n" |
6f63c5b8 | 57 | "\n%sNotify the init system about service status updates.%s\n\n" |
b57b0625 ZJS |
58 | " -h --help Show this help\n" |
59 | " --version Show package version\n" | |
fd0f4da5 LP |
60 | " --ready Inform the service manager about service start-up/reload\n" |
61 | " completion\n" | |
62 | " --reloading Inform the service manager about configuration reloading\n" | |
63 | " --stopping Inform the service manager about service shutdown\n" | |
65c6b990 LP |
64 | " --pid[=PID] Set main PID of daemon\n" |
65 | " --uid=USER Set user to send from\n" | |
b57b0625 | 66 | " --status=TEXT Set status text\n" |
37ec0fdd | 67 | " --booted Check if the system was booted up with systemd\n" |
4f07ddfa | 68 | " --no-block Do not wait until operation finished\n" |
9175338e | 69 | " --exec Execute command line separated by ';' once done\n" |
6e4a3245 LP |
70 | " --fd=FD Pass specified file descriptor with along with message\n" |
71 | " --fdname=NAME Name to assign to passed file descriptor(s)\n" | |
bc556335 DDM |
72 | "\nSee the %s for details.\n", |
73 | program_invocation_short_name, | |
9175338e | 74 | program_invocation_short_name, |
bc556335 DDM |
75 | ansi_highlight(), |
76 | ansi_normal(), | |
77 | link); | |
37ec0fdd LP |
78 | |
79 | return 0; | |
4a2a8b5a LP |
80 | } |
81 | ||
9dcd43b1 LP |
82 | static pid_t manager_pid(void) { |
83 | const char *e; | |
84 | pid_t pid; | |
85 | int r; | |
86 | ||
87 | /* If we run as a service managed by systemd --user the $MANAGERPID environment variable points to | |
88 | * the service manager's PID. */ | |
89 | e = getenv("MANAGERPID"); | |
90 | if (!e) | |
91 | return 0; | |
92 | ||
93 | r = parse_pid(e, &pid); | |
94 | if (r < 0) { | |
95 | log_warning_errno(r, "$MANAGERPID is set to an invalid PID, ignoring: %s", e); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | return pid; | |
100 | } | |
101 | ||
7e26863e MY |
102 | static pid_t pid_parent_if_possible(void) { |
103 | pid_t parent_pid = getppid(); | |
104 | ||
105 | /* Don't send from PID 1 or the service manager's PID (which might be distinct from 1, if we are a | |
106 | * --user service). That'd just be confusing for the service manager. */ | |
107 | if (parent_pid <= 1 || | |
108 | parent_pid == manager_pid()) | |
109 | return getpid_cached(); | |
110 | ||
111 | return parent_pid; | |
112 | } | |
113 | ||
4a2a8b5a LP |
114 | static int parse_argv(int argc, char *argv[]) { |
115 | ||
116 | enum { | |
117 | ARG_READY = 0x100, | |
fd0f4da5 LP |
118 | ARG_RELOADING, |
119 | ARG_STOPPING, | |
9aac0b2c | 120 | ARG_VERSION, |
4a2a8b5a | 121 | ARG_PID, |
96551bae | 122 | ARG_STATUS, |
6624768c | 123 | ARG_BOOTED, |
65c6b990 | 124 | ARG_UID, |
9175338e LP |
125 | ARG_NO_BLOCK, |
126 | ARG_EXEC, | |
6e4a3245 LP |
127 | ARG_FD, |
128 | ARG_FDNAME, | |
4a2a8b5a LP |
129 | }; |
130 | ||
131 | static const struct option options[] = { | |
6624768c | 132 | { "help", no_argument, NULL, 'h' }, |
9aac0b2c | 133 | { "version", no_argument, NULL, ARG_VERSION }, |
6624768c | 134 | { "ready", no_argument, NULL, ARG_READY }, |
fd0f4da5 LP |
135 | { "reloading", no_argument, NULL, ARG_RELOADING }, |
136 | { "stopping", no_argument, NULL, ARG_STOPPING }, | |
6624768c LP |
137 | { "pid", optional_argument, NULL, ARG_PID }, |
138 | { "status", required_argument, NULL, ARG_STATUS }, | |
139 | { "booted", no_argument, NULL, ARG_BOOTED }, | |
65c6b990 | 140 | { "uid", required_argument, NULL, ARG_UID }, |
4f07ddfa | 141 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
9175338e | 142 | { "exec", no_argument, NULL, ARG_EXEC }, |
6e4a3245 LP |
143 | { "fd", required_argument, NULL, ARG_FD }, |
144 | { "fdname", required_argument, NULL, ARG_FDNAME }, | |
eb9da376 | 145 | {} |
4a2a8b5a LP |
146 | }; |
147 | ||
5d2a48da | 148 | _cleanup_fdset_free_ FDSet *passed = NULL; |
9175338e | 149 | bool do_exec = false; |
08ba0a95 | 150 | int c, r; |
4a2a8b5a LP |
151 | |
152 | assert(argc >= 0); | |
153 | assert(argv); | |
154 | ||
ee8c4568 | 155 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { |
4a2a8b5a LP |
156 | |
157 | switch (c) { | |
158 | ||
159 | case 'h': | |
37ec0fdd | 160 | return help(); |
4a2a8b5a | 161 | |
9aac0b2c | 162 | case ARG_VERSION: |
3f6fd1ba | 163 | return version(); |
9aac0b2c | 164 | |
4a2a8b5a LP |
165 | case ARG_READY: |
166 | arg_ready = true; | |
167 | break; | |
168 | ||
fd0f4da5 LP |
169 | case ARG_RELOADING: |
170 | arg_reloading = true; | |
171 | break; | |
172 | ||
173 | case ARG_STOPPING: | |
174 | arg_stopping = true; | |
175 | break; | |
176 | ||
4a2a8b5a | 177 | case ARG_PID: |
7e26863e MY |
178 | if (isempty(optarg) || streq(optarg, "auto")) |
179 | arg_pid = pid_parent_if_possible(); | |
180 | else if (streq(optarg, "parent")) | |
4a2a8b5a | 181 | arg_pid = getppid(); |
9dcd43b1 | 182 | else if (streq(optarg, "self")) |
19b761a0 | 183 | arg_pid = getpid_cached(); |
9dcd43b1 LP |
184 | else { |
185 | r = parse_pid(optarg, &arg_pid); | |
186 | if (r < 0) | |
187 | return log_error_errno(r, "Failed to parse PID %s.", optarg); | |
188 | } | |
4a2a8b5a LP |
189 | |
190 | break; | |
191 | ||
192 | case ARG_STATUS: | |
193 | arg_status = optarg; | |
194 | break; | |
195 | ||
96551bae LP |
196 | case ARG_BOOTED: |
197 | arg_booted = true; | |
198 | break; | |
199 | ||
65c6b990 LP |
200 | case ARG_UID: { |
201 | const char *u = optarg; | |
202 | ||
fafff8f1 | 203 | r = get_user_creds(&u, &arg_uid, &arg_gid, NULL, NULL, 0); |
65c6b990 LP |
204 | if (r == -ESRCH) /* If the user doesn't exist, then accept it anyway as numeric */ |
205 | r = parse_uid(u, &arg_uid); | |
206 | if (r < 0) | |
207 | return log_error_errno(r, "Can't resolve user %s: %m", optarg); | |
208 | ||
209 | break; | |
210 | } | |
211 | ||
4f07ddfa KKD |
212 | case ARG_NO_BLOCK: |
213 | arg_no_block = true; | |
214 | break; | |
215 | ||
9175338e LP |
216 | case ARG_EXEC: |
217 | do_exec = true; | |
218 | break; | |
219 | ||
6e4a3245 LP |
220 | case ARG_FD: { |
221 | _cleanup_close_ int owned_fd = -EBADF; | |
222 | int fdnr; | |
223 | ||
e652663a | 224 | fdnr = parse_fd(optarg); |
6e4a3245 | 225 | if (fdnr < 0) |
e652663a | 226 | return log_error_errno(fdnr, "Failed to parse file descriptor: %s", optarg); |
6e4a3245 LP |
227 | |
228 | if (!passed) { | |
229 | /* Take possession of all passed fds */ | |
a3dff21a | 230 | r = fdset_new_fill(/* filter_cloexec= */ 0, &passed); |
6e4a3245 LP |
231 | if (r < 0) |
232 | return log_error_errno(r, "Failed to take possession of passed file descriptors: %m"); | |
6e4a3245 LP |
233 | } |
234 | ||
235 | if (fdnr < 3) { | |
236 | /* For stdin/stdout/stderr we want to keep the fd, too, hence make a copy */ | |
237 | owned_fd = fcntl(fdnr, F_DUPFD_CLOEXEC, 3); | |
238 | if (owned_fd < 0) | |
239 | return log_error_errno(errno, "Failed to duplicate file descriptor: %m"); | |
240 | } else { | |
241 | /* Otherwise, move the fd over */ | |
242 | owned_fd = fdset_remove(passed, fdnr); | |
243 | if (owned_fd < 0) | |
244 | return log_error_errno(owned_fd, "Specified file descriptor '%i' not passed or specified more than once: %m", fdnr); | |
245 | } | |
246 | ||
247 | if (!arg_fds) { | |
248 | arg_fds = fdset_new(); | |
249 | if (!arg_fds) | |
250 | return log_oom(); | |
251 | } | |
252 | ||
253 | r = fdset_consume(arg_fds, TAKE_FD(owned_fd)); | |
254 | if (r < 0) | |
255 | return log_error_errno(r, "Failed to add file descriptor to set: %m"); | |
256 | break; | |
257 | } | |
258 | ||
259 | case ARG_FDNAME: | |
260 | if (!fdname_is_valid(optarg)) | |
261 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File descriptor name invalid: %s", optarg); | |
262 | ||
263 | if (free_and_strdup(&arg_fdname, optarg) < 0) | |
264 | return log_oom(); | |
265 | ||
266 | break; | |
267 | ||
4a2a8b5a LP |
268 | case '?': |
269 | return -EINVAL; | |
270 | ||
271 | default: | |
04499a70 | 272 | assert_not_reached(); |
4a2a8b5a | 273 | } |
ee8c4568 | 274 | } |
4a2a8b5a | 275 | |
6e4a3245 LP |
276 | if (arg_fdname && fdset_isempty(arg_fds)) |
277 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No file descriptors passed, but --fdname= set, refusing."); | |
278 | ||
953134a5 MY |
279 | bool have_env = arg_ready || arg_stopping || arg_reloading || arg_status || arg_pid > 0 || !fdset_isempty(arg_fds); |
280 | size_t n_arg_env; | |
08ba0a95 | 281 | |
9175338e LP |
282 | if (do_exec) { |
283 | int i; | |
284 | ||
285 | for (i = optind; i < argc; i++) | |
286 | if (streq(argv[i], ";")) | |
287 | break; | |
288 | ||
289 | if (i >= argc) | |
290 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --exec is used argument list must contain ';' separator, refusing."); | |
291 | if (i+1 == argc) | |
08ba0a95 | 292 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty command line specified after ';' separator, refusing."); |
9175338e LP |
293 | |
294 | arg_exec = strv_copy_n(argv + i + 1, argc - i - 1); | |
295 | if (!arg_exec) | |
296 | return log_oom(); | |
297 | ||
953134a5 | 298 | n_arg_env = i - optind; |
9175338e | 299 | } else |
953134a5 MY |
300 | n_arg_env = argc - optind; |
301 | ||
302 | have_env = have_env || n_arg_env > 0; | |
303 | ||
304 | if (!have_env && !arg_booted) { | |
305 | if (do_exec) | |
306 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No notify message specified while --exec, refusing."); | |
9175338e | 307 | |
953134a5 MY |
308 | /* No argument at all? */ |
309 | help(); | |
310 | return -EINVAL; | |
311 | } | |
312 | ||
ed5f1097 MY |
313 | if (have_env && arg_booted) |
314 | log_warning("Notify message specified along with --booted, ignoring."); | |
315 | ||
953134a5 MY |
316 | if (n_arg_env > 0) { |
317 | arg_env = strv_copy_n(argv + optind, n_arg_env); | |
9175338e LP |
318 | if (!arg_env) |
319 | return log_oom(); | |
320 | } | |
321 | ||
6e4a3245 LP |
322 | if (!fdset_isempty(passed)) |
323 | log_warning("Warning: %u more file descriptors passed than referenced with --fd=.", fdset_size(passed)); | |
324 | ||
4a2a8b5a LP |
325 | return 1; |
326 | } | |
327 | ||
aac0b2e8 | 328 | static int run(int argc, char* argv[]) { |
08ba0a95 | 329 | _cleanup_free_ char *status = NULL, *cpid = NULL, *msg = NULL, *monotonic_usec = NULL, *fdn = NULL; |
be8f4e9e | 330 | _cleanup_strv_free_ char **final_env = NULL; |
08ba0a95 | 331 | const char *our_env[9]; |
fd0f4da5 | 332 | size_t i = 0; |
be8f4e9e | 333 | int r; |
4a2a8b5a | 334 | |
08ba0a95 | 335 | log_setup(); |
4a2a8b5a | 336 | |
6c12b52e | 337 | r = parse_argv(argc, argv); |
be8f4e9e | 338 | if (r <= 0) |
aac0b2e8 | 339 | return r; |
4a2a8b5a | 340 | |
8ab34a49 JB |
341 | if (arg_booted) { |
342 | r = sd_booted(); | |
343 | if (r < 0) | |
344 | log_debug_errno(r, "Failed to determine whether we are booted with systemd, assuming we aren't: %m"); | |
345 | else | |
346 | log_debug("The system %s booted with systemd.", r ? "was" : "was not"); | |
347 | ||
348 | return r <= 0; | |
349 | } | |
96551bae | 350 | |
fd0f4da5 | 351 | if (arg_reloading) { |
08ba0a95 | 352 | our_env[i++] = "RELOADING=1"; |
fd0f4da5 LP |
353 | |
354 | if (asprintf(&monotonic_usec, "MONOTONIC_USEC=" USEC_FMT, now(CLOCK_MONOTONIC)) < 0) | |
355 | return log_oom(); | |
356 | ||
357 | our_env[i++] = monotonic_usec; | |
358 | } | |
359 | ||
4a2a8b5a | 360 | if (arg_ready) |
08ba0a95 | 361 | our_env[i++] = "READY=1"; |
4a2a8b5a | 362 | |
fd0f4da5 | 363 | if (arg_stopping) |
08ba0a95 | 364 | our_env[i++] = "STOPPING=1"; |
fd0f4da5 | 365 | |
4a2a8b5a | 366 | if (arg_status) { |
b910cc72 | 367 | status = strjoin("STATUS=", arg_status); |
aac0b2e8 ZJS |
368 | if (!status) |
369 | return log_oom(); | |
4a2a8b5a LP |
370 | |
371 | our_env[i++] = status; | |
372 | } | |
373 | ||
374 | if (arg_pid > 0) { | |
aac0b2e8 ZJS |
375 | if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) |
376 | return log_oom(); | |
4a2a8b5a LP |
377 | |
378 | our_env[i++] = cpid; | |
379 | } | |
380 | ||
6e4a3245 | 381 | if (!fdset_isempty(arg_fds)) { |
08ba0a95 | 382 | our_env[i++] = "FDSTORE=1"; |
6e4a3245 LP |
383 | |
384 | if (arg_fdname) { | |
385 | fdn = strjoin("FDNAME=", arg_fdname); | |
386 | if (!fdn) | |
387 | return log_oom(); | |
388 | ||
389 | our_env[i++] = fdn; | |
390 | } | |
391 | } | |
392 | ||
4a2a8b5a LP |
393 | our_env[i++] = NULL; |
394 | ||
08ba0a95 | 395 | final_env = strv_env_merge((char**) our_env, arg_env); |
aac0b2e8 ZJS |
396 | if (!final_env) |
397 | return log_oom(); | |
953134a5 | 398 | assert(!strv_isempty(final_env)); |
4a2a8b5a | 399 | |
08ba0a95 MY |
400 | msg = strv_join(final_env, "\n"); |
401 | if (!msg) | |
aac0b2e8 | 402 | return log_oom(); |
4a2a8b5a | 403 | |
5238e957 | 404 | /* If this is requested change to the requested UID/GID. Note that we only change the real UID here, and leave |
65c6b990 LP |
405 | the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the |
406 | ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */ | |
407 | ||
aac0b2e8 | 408 | if (arg_gid != GID_INVALID && |
f5fbe71d | 409 | setregid(arg_gid, GID_INVALID) < 0) |
aac0b2e8 | 410 | return log_error_errno(errno, "Failed to change GID: %m"); |
65c6b990 | 411 | |
aac0b2e8 | 412 | if (arg_uid != UID_INVALID && |
f5fbe71d | 413 | setreuid(arg_uid, UID_INVALID) < 0) |
aac0b2e8 | 414 | return log_error_errno(errno, "Failed to change UID: %m"); |
65c6b990 | 415 | |
7e26863e MY |
416 | /* If --pid= is explicitly specified, use it as source pid. Otherwise, pretend the message originates |
417 | * from our parent, i.e. --pid=auto */ | |
418 | if (arg_pid <= 0) | |
419 | arg_pid = pid_parent_if_possible(); | |
6e4a3245 LP |
420 | |
421 | if (fdset_isempty(arg_fds)) | |
7e26863e | 422 | r = sd_pid_notify(arg_pid, /* unset_environment= */ false, msg); |
6e4a3245 LP |
423 | else { |
424 | _cleanup_free_ int *a = NULL; | |
425 | int k; | |
426 | ||
427 | k = fdset_to_array(arg_fds, &a); | |
428 | if (k < 0) | |
429 | return log_error_errno(k, "Failed to convert file descriptor set to array: %m"); | |
430 | ||
7e26863e | 431 | r = sd_pid_notify_with_fds(arg_pid, /* unset_environment= */ false, msg, a, k); |
6e4a3245 LP |
432 | |
433 | } | |
aac0b2e8 ZJS |
434 | if (r < 0) |
435 | return log_error_errno(r, "Failed to notify init system: %m"); | |
baaa35ad ZJS |
436 | if (r == 0) |
437 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
438 | "No status data could be sent: $NOTIFY_SOCKET was not set"); | |
4f07ddfa | 439 | |
6e4a3245 LP |
440 | arg_fds = fdset_free(arg_fds); /* Close before we execute anything */ |
441 | ||
4f07ddfa | 442 | if (!arg_no_block) { |
7e26863e | 443 | r = sd_pid_notify_barrier(arg_pid, /* unset_environment= */ false, 5 * USEC_PER_SEC); |
4f07ddfa KKD |
444 | if (r < 0) |
445 | return log_error_errno(r, "Failed to invoke barrier: %m"); | |
446 | if (r == 0) | |
447 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
448 | "No status data could be sent: $NOTIFY_SOCKET was not set"); | |
449 | } | |
450 | ||
9175338e | 451 | if (arg_exec) { |
9175338e LP |
452 | execvp(arg_exec[0], arg_exec); |
453 | ||
a3158ff3 MY |
454 | _cleanup_free_ char *cmdline = strv_join(arg_exec, " "); |
455 | return log_error_errno(errno, "Failed to execute command line: %s", strnull(cmdline)); | |
9175338e LP |
456 | } |
457 | ||
09e766e7 LP |
458 | /* The DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE() boilerplate will send the exit status via |
459 | * sd_notify(). Which is normally fine, but very confusing in systemd-notify, whose purpose is to | |
460 | * send user-controllable notification messages, and not implicit ones. Let's turn if off, by | |
461 | * unsetting the $NOTIFY_SOCKET environment variable. */ | |
462 | (void) unsetenv("NOTIFY_SOCKET"); | |
aac0b2e8 | 463 | return 0; |
4a2a8b5a | 464 | } |
aac0b2e8 | 465 | |
6cdd6d1a | 466 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |