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