]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <getopt.h> | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | ||
9 | #include "sd-daemon.h" | |
10 | ||
11 | #include "alloc-util.h" | |
12 | #include "env-util.h" | |
13 | #include "format-util.h" | |
14 | #include "log.h" | |
15 | #include "main-func.h" | |
16 | #include "parse-util.h" | |
17 | #include "pretty-print.h" | |
18 | #include "string-util.h" | |
19 | #include "strv.h" | |
20 | #include "user-util.h" | |
21 | #include "util.h" | |
22 | ||
23 | static bool arg_ready = false; | |
24 | static pid_t arg_pid = 0; | |
25 | static const char *arg_status = NULL; | |
26 | static bool arg_booted = false; | |
27 | static uid_t arg_uid = UID_INVALID; | |
28 | static gid_t arg_gid = GID_INVALID; | |
29 | ||
30 | static int help(void) { | |
31 | _cleanup_free_ char *link = NULL; | |
32 | int r; | |
33 | ||
34 | r = terminal_urlify_man("systemd-notify", "1", &link); | |
35 | if (r < 0) | |
36 | return log_oom(); | |
37 | ||
38 | printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n" | |
39 | "Notify the init system about service status updates.\n\n" | |
40 | " -h --help Show this help\n" | |
41 | " --version Show package version\n" | |
42 | " --ready Inform the init system about service start-up completion\n" | |
43 | " --pid[=PID] Set main PID of daemon\n" | |
44 | " --uid=USER Set user to send from\n" | |
45 | " --status=TEXT Set status text\n" | |
46 | " --booted Check if the system was booted up with systemd\n" | |
47 | "\nSee the %s for details.\n" | |
48 | , program_invocation_short_name | |
49 | , link | |
50 | ); | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static int parse_argv(int argc, char *argv[]) { | |
56 | ||
57 | enum { | |
58 | ARG_READY = 0x100, | |
59 | ARG_VERSION, | |
60 | ARG_PID, | |
61 | ARG_STATUS, | |
62 | ARG_BOOTED, | |
63 | ARG_UID, | |
64 | }; | |
65 | ||
66 | static const struct option options[] = { | |
67 | { "help", no_argument, NULL, 'h' }, | |
68 | { "version", no_argument, NULL, ARG_VERSION }, | |
69 | { "ready", no_argument, NULL, ARG_READY }, | |
70 | { "pid", optional_argument, NULL, ARG_PID }, | |
71 | { "status", required_argument, NULL, ARG_STATUS }, | |
72 | { "booted", no_argument, NULL, ARG_BOOTED }, | |
73 | { "uid", required_argument, NULL, ARG_UID }, | |
74 | {} | |
75 | }; | |
76 | ||
77 | int c, r; | |
78 | ||
79 | assert(argc >= 0); | |
80 | assert(argv); | |
81 | ||
82 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { | |
83 | ||
84 | switch (c) { | |
85 | ||
86 | case 'h': | |
87 | return help(); | |
88 | ||
89 | case ARG_VERSION: | |
90 | return version(); | |
91 | ||
92 | case ARG_READY: | |
93 | arg_ready = true; | |
94 | break; | |
95 | ||
96 | case ARG_PID: | |
97 | ||
98 | if (optarg) { | |
99 | if (parse_pid(optarg, &arg_pid) < 0) | |
100 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
101 | "Failed to parse PID %s.", optarg); | |
102 | } else | |
103 | arg_pid = getppid(); | |
104 | ||
105 | break; | |
106 | ||
107 | case ARG_STATUS: | |
108 | arg_status = optarg; | |
109 | break; | |
110 | ||
111 | case ARG_BOOTED: | |
112 | arg_booted = true; | |
113 | break; | |
114 | ||
115 | case ARG_UID: { | |
116 | const char *u = optarg; | |
117 | ||
118 | r = get_user_creds(&u, &arg_uid, &arg_gid, NULL, NULL, 0); | |
119 | if (r == -ESRCH) /* If the user doesn't exist, then accept it anyway as numeric */ | |
120 | r = parse_uid(u, &arg_uid); | |
121 | if (r < 0) | |
122 | return log_error_errno(r, "Can't resolve user %s: %m", optarg); | |
123 | ||
124 | break; | |
125 | } | |
126 | ||
127 | case '?': | |
128 | return -EINVAL; | |
129 | ||
130 | default: | |
131 | assert_not_reached("Unhandled option"); | |
132 | } | |
133 | } | |
134 | ||
135 | if (optind >= argc && | |
136 | !arg_ready && | |
137 | !arg_status && | |
138 | !arg_pid && | |
139 | !arg_booted) { | |
140 | help(); | |
141 | return -EINVAL; | |
142 | } | |
143 | ||
144 | return 1; | |
145 | } | |
146 | ||
147 | static int run(int argc, char* argv[]) { | |
148 | _cleanup_free_ char *status = NULL, *cpid = NULL, *n = NULL; | |
149 | _cleanup_strv_free_ char **final_env = NULL; | |
150 | char* our_env[4]; | |
151 | unsigned i = 0; | |
152 | int r; | |
153 | ||
154 | log_parse_environment(); | |
155 | log_open(); | |
156 | ||
157 | r = parse_argv(argc, argv); | |
158 | if (r <= 0) | |
159 | return r; | |
160 | ||
161 | if (arg_booted) | |
162 | return sd_booted() <= 0; | |
163 | ||
164 | if (arg_ready) | |
165 | our_env[i++] = (char*) "READY=1"; | |
166 | ||
167 | if (arg_status) { | |
168 | status = strappend("STATUS=", arg_status); | |
169 | if (!status) | |
170 | return log_oom(); | |
171 | ||
172 | our_env[i++] = status; | |
173 | } | |
174 | ||
175 | if (arg_pid > 0) { | |
176 | if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) | |
177 | return log_oom(); | |
178 | ||
179 | our_env[i++] = cpid; | |
180 | } | |
181 | ||
182 | our_env[i++] = NULL; | |
183 | ||
184 | final_env = strv_env_merge(2, our_env, argv + optind); | |
185 | if (!final_env) | |
186 | return log_oom(); | |
187 | ||
188 | if (strv_isempty(final_env)) | |
189 | return 0; | |
190 | ||
191 | n = strv_join(final_env, "\n"); | |
192 | if (!n) | |
193 | return log_oom(); | |
194 | ||
195 | /* If this is requested change to the requested UID/GID. Note thta we only change the real UID here, and leave | |
196 | the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the | |
197 | ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */ | |
198 | ||
199 | if (arg_gid != GID_INVALID && | |
200 | setregid(arg_gid, (gid_t) -1) < 0) | |
201 | return log_error_errno(errno, "Failed to change GID: %m"); | |
202 | ||
203 | if (arg_uid != UID_INVALID && | |
204 | setreuid(arg_uid, (uid_t) -1) < 0) | |
205 | return log_error_errno(errno, "Failed to change UID: %m"); | |
206 | ||
207 | r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n); | |
208 | if (r < 0) | |
209 | return log_error_errno(r, "Failed to notify init system: %m"); | |
210 | if (r == 0) | |
211 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
212 | "No status data could be sent: $NOTIFY_SOCKET was not set"); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | DEFINE_MAIN_FUNCTION(run); |