]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/notify/notify.c
Remove unnecessary casts in printfs
[thirdparty/systemd.git] / src / notify / notify.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4a2a8b5a
LP
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
5430f7f2
LP
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
4a2a8b5a
LP
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
5430f7f2 16 Lesser General Public License for more details.
4a2a8b5a 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
4a2a8b5a
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <stdio.h>
23#include <getopt.h>
24#include <error.h>
25#include <errno.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <string.h>
29
81527be1
LP
30#include <systemd/sd-daemon.h>
31
4a2a8b5a
LP
32#include "strv.h"
33#include "util.h"
34#include "log.h"
1bc5d57a 35#include "sd-readahead.h"
9aac0b2c 36#include "build.h"
4d1a6904 37#include "env-util.h"
4a2a8b5a
LP
38
39static bool arg_ready = false;
40static pid_t arg_pid = 0;
41static const char *arg_status = NULL;
96551bae 42static bool arg_booted = false;
6624768c 43static const char *arg_readahead = NULL;
4a2a8b5a
LP
44
45static int help(void) {
46
2e33c433 47 printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
4a2a8b5a 48 "Notify the init system about service status updates.\n\n"
6624768c 49 " -h --help Show this help\n"
9aac0b2c 50 " --version Show package version\n"
6624768c
LP
51 " --ready Inform the init system about service start-up completion\n"
52 " --pid[=PID] Set main pid of daemon\n"
53 " --status=TEXT Set status text\n"
54 " --booted Returns 0 if the system was booted up with systemd, non-zero otherwise\n"
55 " --readahead=ACTION Controls read-ahead operations\n",
4a2a8b5a
LP
56 program_invocation_short_name);
57
58 return 0;
59}
60
61static int parse_argv(int argc, char *argv[]) {
62
63 enum {
64 ARG_READY = 0x100,
9aac0b2c 65 ARG_VERSION,
4a2a8b5a 66 ARG_PID,
96551bae 67 ARG_STATUS,
6624768c
LP
68 ARG_BOOTED,
69 ARG_READAHEAD
4a2a8b5a
LP
70 };
71
72 static const struct option options[] = {
6624768c 73 { "help", no_argument, NULL, 'h' },
9aac0b2c 74 { "version", no_argument, NULL, ARG_VERSION },
6624768c
LP
75 { "ready", no_argument, NULL, ARG_READY },
76 { "pid", optional_argument, NULL, ARG_PID },
77 { "status", required_argument, NULL, ARG_STATUS },
78 { "booted", no_argument, NULL, ARG_BOOTED },
79 { "readahead", required_argument, NULL, ARG_READAHEAD },
eb9da376 80 {}
4a2a8b5a
LP
81 };
82
83 int c;
84
85 assert(argc >= 0);
86 assert(argv);
87
88 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
89
90 switch (c) {
91
92 case 'h':
eb9da376 93 return help();
4a2a8b5a 94
9aac0b2c
LP
95 case ARG_VERSION:
96 puts(PACKAGE_STRING);
9aac0b2c
LP
97 puts(SYSTEMD_FEATURES);
98 return 0;
99
4a2a8b5a
LP
100 case ARG_READY:
101 arg_ready = true;
102 break;
103
104 case ARG_PID:
105
106 if (optarg) {
107 if (parse_pid(optarg, &arg_pid) < 0) {
108 log_error("Failed to parse PID %s.", optarg);
109 return -EINVAL;
110 }
111 } else
112 arg_pid = getppid();
113
114 break;
115
116 case ARG_STATUS:
117 arg_status = optarg;
118 break;
119
96551bae
LP
120 case ARG_BOOTED:
121 arg_booted = true;
122 break;
123
6624768c
LP
124 case ARG_READAHEAD:
125 arg_readahead = optarg;
126 break;
127
4a2a8b5a
LP
128 case '?':
129 return -EINVAL;
130
131 default:
eb9da376 132 assert_not_reached("Unhandled option");
4a2a8b5a
LP
133 }
134 }
135
2f02ce40
LP
136 if (optind >= argc &&
137 !arg_ready &&
138 !arg_status &&
139 !arg_pid &&
6624768c
LP
140 !arg_booted &&
141 !arg_readahead) {
2f02ce40
LP
142 help();
143 return -EINVAL;
144 }
145
4a2a8b5a
LP
146 return 1;
147}
148
149int main(int argc, char* argv[]) {
150 char* our_env[4], **final_env = NULL;
151 unsigned i = 0;
152 char *status = NULL, *cpid = NULL, *n = NULL;
22f4096c 153 int r, retval = EXIT_FAILURE;
4a2a8b5a
LP
154
155 log_parse_environment();
2396fb04 156 log_open();
4a2a8b5a 157
6c12b52e
LP
158 r = parse_argv(argc, argv);
159 if (r <= 0) {
22f4096c 160 retval = r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
4a2a8b5a
LP
161 goto finish;
162 }
163
96551bae
LP
164 if (arg_booted)
165 return sd_booted() <= 0;
166
6624768c
LP
167 if (arg_readahead) {
168 if ((r = sd_readahead(arg_readahead)) < 0) {
169 log_error("Failed to issue read-ahead control command: %s", strerror(-r));
170 goto finish;
171 }
172 }
173
4a2a8b5a
LP
174 if (arg_ready)
175 our_env[i++] = (char*) "READY=1";
176
177 if (arg_status) {
178 if (!(status = strappend("STATUS=", arg_status))) {
179 log_error("Failed to allocate STATUS string.");
180 goto finish;
181 }
182
183 our_env[i++] = status;
184 }
185
186 if (arg_pid > 0) {
de0671ee 187 if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) {
4a2a8b5a
LP
188 log_error("Failed to allocate MAINPID string.");
189 goto finish;
190 }
191
192 our_env[i++] = cpid;
193 }
194
195 our_env[i++] = NULL;
196
197 if (!(final_env = strv_env_merge(2, our_env, argv + optind))) {
198 log_error("Failed to merge string sets.");
199 goto finish;
200 }
201
202 if (strv_length(final_env) <= 0) {
22f4096c 203 retval = EXIT_SUCCESS;
4a2a8b5a
LP
204 goto finish;
205 }
206
207 if (!(n = strv_join(final_env, "\n"))) {
208 log_error("Failed to concatenate strings.");
209 goto finish;
210 }
211
212 if ((r = sd_notify(false, n)) < 0) {
213 log_error("Failed to notify init system: %s", strerror(-r));
214 goto finish;
215 }
216
22f4096c 217 retval = r <= 0 ? EXIT_FAILURE : EXIT_SUCCESS;
4a2a8b5a
LP
218
219finish:
220 free(status);
221 free(cpid);
222 free(n);
223
224 strv_free(final_env);
225
226 return retval;
227}