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