summaryrefslogtreecommitdiffstats
path: root/src/shared/daemon-util.c
blob: d8aa259ee97199a883552bd2227741e4e1414edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "daemon-util.h"
#include "fd-util.h"
#include "log.h"
#include "string-util.h"
#include "time-util.h"

static int notify_remove_fd_warn(const char *name) {
        int r;

        assert(name);

        r = sd_notifyf(/* unset_environment = */ false,
                       "FDSTOREREMOVE=1\n"
                       "FDNAME=%s", name);
        if (r < 0)
                return log_warning_errno(r,
                                         "Failed to remove file descriptor \"%s\" from the store, ignoring: %m",
                                         name);

        return 0;
}

int notify_remove_fd_warnf(const char *format, ...) {
        _cleanup_free_ char *p = NULL;
        va_list ap;
        int r;

        assert(format);

        va_start(ap, format);
        r = vasprintf(&p, format, ap);
        va_end(ap);
        if (r < 0)
                return log_oom();

        return notify_remove_fd_warn(p);
}

int close_and_notify_warn(int fd, const char *name) {
        if (name)
                (void) notify_remove_fd_warn(name);

        return safe_close(fd);
}

int notify_push_fd(int fd, const char *name) {
        _cleanup_free_ char *state = NULL;

        assert(fd >= 0);
        assert(name);

        state = strjoin("FDSTORE=1\n"
                        "FDNAME=", name);
        if (!state)
                return -ENOMEM;

        return sd_pid_notify_with_fds(0, /* unset_environment = */ false, state, &fd, 1);
}

int notify_push_fdf(int fd, const char *format, ...) {
        _cleanup_free_ char *name = NULL;
        va_list ap;
        int r;

        assert(fd >= 0);
        assert(format);

        va_start(ap, format);
        r = vasprintf(&name, format, ap);
        va_end(ap);
        if (r < 0)
                return -ENOMEM;

        return notify_push_fd(fd, name);
}

int notify_reloading_full(const char *status) {
        int r;

        r = sd_notifyf(/* unset_environment = */ false,
                       "RELOADING=1\n"
                       "MONOTONIC_USEC=" USEC_FMT
                       "%s%s",
                       now(CLOCK_MONOTONIC),
                       status ? "\nSTATUS=" : "", strempty(status));
        if (r < 0)
                return log_debug_errno(r, "Failed to notify service manager for reloading status: %m");

        return 0;
}