]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/notify/notify.c
Merge pull request #8135 from shawnl/arg_host
[thirdparty/systemd.git] / src / notify / notify.c
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 "parse-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "terminal-util.h"
19 #include "user-util.h"
20 #include "util.h"
21
22 static bool arg_ready = false;
23 static pid_t arg_pid = 0;
24 static const char *arg_status = NULL;
25 static bool arg_booted = false;
26 static uid_t arg_uid = UID_INVALID;
27 static gid_t arg_gid = GID_INVALID;
28
29 static int help(void) {
30 _cleanup_free_ char *link = NULL;
31 int r;
32
33 r = terminal_urlify_man("systemd-notify", "1", &link);
34 if (r < 0)
35 return log_oom();
36
37 printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
38 "Notify the init system about service status updates.\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " --ready Inform the init system about service start-up completion\n"
42 " --pid[=PID] Set main PID of daemon\n"
43 " --uid=USER Set user to send from\n"
44 " --status=TEXT Set status text\n"
45 " --booted Check if the system was booted up with systemd\n"
46 "\nSee the %s for details.\n"
47 , program_invocation_short_name
48 , link
49 );
50
51 return 0;
52 }
53
54 static int parse_argv(int argc, char *argv[]) {
55
56 enum {
57 ARG_READY = 0x100,
58 ARG_VERSION,
59 ARG_PID,
60 ARG_STATUS,
61 ARG_BOOTED,
62 ARG_UID,
63 };
64
65 static const struct option options[] = {
66 { "help", no_argument, NULL, 'h' },
67 { "version", no_argument, NULL, ARG_VERSION },
68 { "ready", no_argument, NULL, ARG_READY },
69 { "pid", optional_argument, NULL, ARG_PID },
70 { "status", required_argument, NULL, ARG_STATUS },
71 { "booted", no_argument, NULL, ARG_BOOTED },
72 { "uid", required_argument, NULL, ARG_UID },
73 {}
74 };
75
76 int c, r;
77
78 assert(argc >= 0);
79 assert(argv);
80
81 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
82
83 switch (c) {
84
85 case 'h':
86 return help();
87
88 case ARG_VERSION:
89 return version();
90
91 case ARG_READY:
92 arg_ready = true;
93 break;
94
95 case ARG_PID:
96
97 if (optarg) {
98 if (parse_pid(optarg, &arg_pid) < 0) {
99 log_error("Failed to parse PID %s.", optarg);
100 return -EINVAL;
101 }
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 int main(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 goto finish;
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 r = log_oom();
171 goto finish;
172 }
173
174 our_env[i++] = status;
175 }
176
177 if (arg_pid > 0) {
178 if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) {
179 r = log_oom();
180 goto finish;
181 }
182
183 our_env[i++] = cpid;
184 }
185
186 our_env[i++] = NULL;
187
188 final_env = strv_env_merge(2, our_env, argv + optind);
189 if (!final_env) {
190 r = log_oom();
191 goto finish;
192 }
193
194 if (strv_isempty(final_env)) {
195 r = 0;
196 goto finish;
197 }
198
199 n = strv_join(final_env, "\n");
200 if (!n) {
201 r = log_oom();
202 goto finish;
203 }
204
205 /* If this is requested change to the requested UID/GID. Note thta we only change the real UID here, and leave
206 the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the
207 ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */
208
209 if (arg_gid != GID_INVALID)
210 if (setregid(arg_gid, (gid_t) -1) < 0) {
211 r = log_error_errno(errno, "Failed to change GID: %m");
212 goto finish;
213 }
214
215 if (arg_uid != UID_INVALID)
216 if (setreuid(arg_uid, (uid_t) -1) < 0) {
217 r = log_error_errno(errno, "Failed to change UID: %m");
218 goto finish;
219 }
220
221 r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n);
222 if (r < 0) {
223 log_error_errno(r, "Failed to notify init system: %m");
224 goto finish;
225 } else if (r == 0) {
226 log_error("No status data could be sent: $NOTIFY_SOCKET was not set");
227 r = -EOPNOTSUPP;
228 }
229
230 finish:
231 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
232 }