1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 #include "sd-daemon.h"
34 #include "sd-readahead.h"
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 static const char *arg_readahead = NULL;
42 static int help(void) {
44 printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
45 "Notify the init system about service status updates.\n\n"
46 " -h --help Show this help\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 Returns 0 if the system was booted up with systemd, non-zero otherwise\n"
51 " --readahead=ACTION Controls read-ahead operations\n",
52 program_invocation_short_name);
57 static int parse_argv(int argc, char *argv[]) {
67 static const struct option options[] = {
68 { "help", no_argument, NULL, 'h' },
69 { "ready", no_argument, NULL, ARG_READY },
70 { "pid", optional_argument, NULL, ARG_PID },
71 { "status", required_argument, NULL, ARG_STATUS },
72 { "booted", no_argument, NULL, ARG_BOOTED },
73 { "readahead", required_argument, NULL, ARG_READAHEAD },
82 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
97 if (parse_pid(optarg, &arg_pid) < 0) {
98 log_error("Failed to parse PID %s.", optarg);
115 arg_readahead = optarg;
122 log_error("Unknown option code %c", c);
127 if (optind >= argc &&
140 int main(int argc, char* argv[]) {
141 char* our_env[4], **final_env = NULL;
143 char *status = NULL, *cpid = NULL, *n = NULL;
144 int r, retval = EXIT_FAILURE;
146 log_parse_environment();
149 if ((r = parse_argv(argc, argv)) <= 0) {
150 retval = r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
155 return sd_booted() <= 0;
158 if ((r = sd_readahead(arg_readahead)) < 0) {
159 log_error("Failed to issue read-ahead control command: %s", strerror(-r));
165 our_env[i++] = (char*) "READY=1";
168 if (!(status = strappend("STATUS=", arg_status))) {
169 log_error("Failed to allocate STATUS string.");
173 our_env[i++] = status;
177 if (asprintf(&cpid, "MAINPID=%lu", (unsigned long) arg_pid) < 0) {
178 log_error("Failed to allocate MAINPID string.");
187 if (!(final_env = strv_env_merge(2, our_env, argv + optind))) {
188 log_error("Failed to merge string sets.");
192 if (strv_length(final_env) <= 0) {
193 retval = EXIT_SUCCESS;
197 if (!(n = strv_join(final_env, "\n"))) {
198 log_error("Failed to concatenate strings.");
202 if ((r = sd_notify(false, n)) < 0) {
203 log_error("Failed to notify init system: %s", strerror(-r));
207 retval = r <= 0 ? EXIT_FAILURE : EXIT_SUCCESS;
214 strv_free(final_env);