]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/notify/notify.c
vxlan: fix assert
[thirdparty/systemd.git] / src / notify / notify.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "sd-daemon.h"
29
30 #include "env-util.h"
31 #include "formats-util.h"
32 #include "log.h"
33 #include "strv.h"
34 #include "util.h"
35
36 static bool arg_ready = false;
37 static pid_t arg_pid = 0;
38 static const char *arg_status = NULL;
39 static bool arg_booted = false;
40
41 static void help(void) {
42 printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
43 "Notify the init system about service status updates.\n\n"
44 " -h --help Show this help\n"
45 " --version Show package version\n"
46 " --ready Inform the init system about service start-up completion\n"
47 " --pid[=PID] Set main pid of daemon\n"
48 " --status=TEXT Set status text\n"
49 " --booted Check if the system was booted up with systemd\n",
50 program_invocation_short_name);
51 }
52
53 static int parse_argv(int argc, char *argv[]) {
54
55 enum {
56 ARG_READY = 0x100,
57 ARG_VERSION,
58 ARG_PID,
59 ARG_STATUS,
60 ARG_BOOTED,
61 };
62
63 static const struct option options[] = {
64 { "help", no_argument, NULL, 'h' },
65 { "version", no_argument, NULL, ARG_VERSION },
66 { "ready", no_argument, NULL, ARG_READY },
67 { "pid", optional_argument, NULL, ARG_PID },
68 { "status", required_argument, NULL, ARG_STATUS },
69 { "booted", no_argument, NULL, ARG_BOOTED },
70 {}
71 };
72
73 int c;
74
75 assert(argc >= 0);
76 assert(argv);
77
78 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
79
80 switch (c) {
81
82 case 'h':
83 help();
84 return 0;
85
86 case ARG_VERSION:
87 return version();
88
89 case ARG_READY:
90 arg_ready = true;
91 break;
92
93 case ARG_PID:
94
95 if (optarg) {
96 if (parse_pid(optarg, &arg_pid) < 0) {
97 log_error("Failed to parse PID %s.", optarg);
98 return -EINVAL;
99 }
100 } else
101 arg_pid = getppid();
102
103 break;
104
105 case ARG_STATUS:
106 arg_status = optarg;
107 break;
108
109 case ARG_BOOTED:
110 arg_booted = true;
111 break;
112
113 case '?':
114 return -EINVAL;
115
116 default:
117 assert_not_reached("Unhandled option");
118 }
119 }
120
121 if (optind >= argc &&
122 !arg_ready &&
123 !arg_status &&
124 !arg_pid &&
125 !arg_booted) {
126 help();
127 return -EINVAL;
128 }
129
130 return 1;
131 }
132
133 int main(int argc, char* argv[]) {
134 _cleanup_free_ char *status = NULL, *cpid = NULL, *n = NULL;
135 _cleanup_strv_free_ char **final_env = NULL;
136 char* our_env[4];
137 unsigned i = 0;
138 int r;
139
140 log_parse_environment();
141 log_open();
142
143 r = parse_argv(argc, argv);
144 if (r <= 0)
145 goto finish;
146
147 if (arg_booted)
148 return sd_booted() <= 0;
149
150 if (arg_ready)
151 our_env[i++] = (char*) "READY=1";
152
153 if (arg_status) {
154 status = strappend("STATUS=", arg_status);
155 if (!status) {
156 r = log_oom();
157 goto finish;
158 }
159
160 our_env[i++] = status;
161 }
162
163 if (arg_pid > 0) {
164 if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) {
165 r = log_oom();
166 goto finish;
167 }
168
169 our_env[i++] = cpid;
170 }
171
172 our_env[i++] = NULL;
173
174 final_env = strv_env_merge(2, our_env, argv + optind);
175 if (!final_env) {
176 r = log_oom();
177 goto finish;
178 }
179
180 if (strv_length(final_env) <= 0) {
181 r = 0;
182 goto finish;
183 }
184
185 n = strv_join(final_env, "\n");
186 if (!n) {
187 r = log_oom();
188 goto finish;
189 }
190
191 r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n);
192 if (r < 0) {
193 log_error_errno(r, "Failed to notify init system: %m");
194 goto finish;
195 } else if (r == 0) {
196 log_error("No status data could be sent: $NOTIFY_SOCKET was not set");
197 r = -EOPNOTSUPP;
198 }
199
200 finish:
201 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
202 }