| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-daemon.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "daemon-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "time-util.h" |
| 13 |
|
| 14 |
int notify_remove_fd_warn(const char *name) { |
| 15 |
int r; |
| 16 |
|
| 17 |
assert(name); |
| 18 |
|
| 19 |
r = sd_notifyf(/* unset_environment= */ false, |
| 20 |
"FDSTOREREMOVE=1\n" |
| 21 |
"FDNAME=%s", name); |
| 22 |
if (r < 0) |
| 23 |
return log_full_errno( |
| 24 |
ERRNO_IS_NEG_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 25 |
"Failed to remove file descriptor \"%s\" from the store, ignoring: %m", |
| 26 |
name); |
| 27 |
|
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
int notify_remove_fd_warnf(const char *format, ...) { |
| 32 |
_cleanup_free_ char *p = NULL; |
| 33 |
va_list ap; |
| 34 |
int r; |
| 35 |
|
| 36 |
assert(format); |
| 37 |
|
| 38 |
va_start(ap, format); |
| 39 |
r = vasprintf(&p, format, ap); |
| 40 |
va_end(ap); |
| 41 |
if (r < 0) |
| 42 |
return log_oom(); |
| 43 |
|
| 44 |
return notify_remove_fd_warn(p); |
| 45 |
} |
| 46 |
|
| 47 |
int close_and_notify_warn(int fd, const char *name) { |
| 48 |
if (name) |
| 49 |
(void) notify_remove_fd_warn(name); |
| 50 |
|
| 51 |
return safe_close(fd); |
| 52 |
} |
| 53 |
|
| 54 |
int notify_push_fd(int fd, const char *name) { |
| 55 |
_cleanup_free_ char *state = NULL; |
| 56 |
|
| 57 |
assert(fd >= 0); |
| 58 |
assert(name); |
| 59 |
|
| 60 |
state = strjoin("FDSTORE=1\n" |
| 61 |
"FDNAME=", name); |
| 62 |
if (!state) |
| 63 |
return -ENOMEM; |
| 64 |
|
| 65 |
/* Remove existing fds with the same name in fdstore. */ |
| 66 |
(void) notify_remove_fd_warn(name); |
| 67 |
|
| 68 |
return sd_pid_notify_with_fds(0, /* unset_environment= */ false, state, &fd, 1); |
| 69 |
} |
| 70 |
|
| 71 |
int notify_push_fdf(int fd, const char *format, ...) { |
| 72 |
_cleanup_free_ char *name = NULL; |
| 73 |
va_list ap; |
| 74 |
int r; |
| 75 |
|
| 76 |
assert(fd >= 0); |
| 77 |
assert(format); |
| 78 |
|
| 79 |
va_start(ap, format); |
| 80 |
r = vasprintf(&name, format, ap); |
| 81 |
va_end(ap); |
| 82 |
if (r < 0) |
| 83 |
return -ENOMEM; |
| 84 |
|
| 85 |
return notify_push_fd(fd, name); |
| 86 |
} |
| 87 |
|
| 88 |
bool fdstore_detected(void) { |
| 89 |
static int cached = -1; |
| 90 |
int r; |
| 91 |
|
| 92 |
if (cached >= 0) |
| 93 |
return cached; |
| 94 |
|
| 95 |
const char *e = getenv("FDSTORE"); |
| 96 |
if (isempty(e)) |
| 97 |
return (cached = 0); |
| 98 |
|
| 99 |
unsigned u; |
| 100 |
r = safe_atou(e, &u); |
| 101 |
if (r < 0) { |
| 102 |
log_debug_errno(r, "Failed to parse 'FDSTORE=%s', ignoring: %m", e); |
| 103 |
return (cached = 0); |
| 104 |
} |
| 105 |
|
| 106 |
return (cached = u > 0); |
| 107 |
} |
| 108 |
|
| 109 |
int notify_reloading_full(const char *status) { |
| 110 |
int r; |
| 111 |
|
| 112 |
r = sd_notifyf(/* unset_environment= */ false, |
| 113 |
"RELOADING=1\n" |
| 114 |
"MONOTONIC_USEC=" USEC_FMT |
| 115 |
"%s%s", |
| 116 |
now(CLOCK_MONOTONIC), |
| 117 |
status ? "\nSTATUS=" : "", strempty(status)); |
| 118 |
if (r < 0) |
| 119 |
return log_debug_errno(r, "Failed to notify service manager for reloading status: %m"); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |