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 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.
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.
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/>.
30 #include <systemd/sd-daemon.h>
35 #include "sd-readahead.h"
39 static bool arg_ready = false;
40 static pid_t arg_pid = 0;
41 static const char *arg_status = NULL;
42 static bool arg_booted = false;
43 static const char *arg_readahead = NULL;
45 static int help(void) {
47 printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n\n"
48 "Notify the init system about service status updates.\n\n"
49 " -h --help Show this help\n"
50 " --version Show package version\n"
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",
56 program_invocation_short_name);
61 static int parse_argv(int argc, char *argv[]) {
72 static const struct option options[] = {
73 { "help", no_argument, NULL, 'h' },
74 { "version", no_argument, NULL, ARG_VERSION },
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 },
88 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
97 puts(SYSTEMD_FEATURES);
107 if (parse_pid(optarg, &arg_pid) < 0) {
108 log_error("Failed to parse PID %s.", optarg);
125 arg_readahead = optarg;
132 assert_not_reached("Unhandled option");
136 if (optind >= argc &&
149 int main(int argc, char* argv[]) {
150 _cleanup_free_ char *status = NULL, *cpid = NULL, *n = NULL;
151 _cleanup_strv_free_ char **final_env = NULL;
156 log_parse_environment();
159 r = parse_argv(argc, argv);
164 return sd_booted() <= 0;
167 r = sd_readahead(arg_readahead);
169 log_error("Failed to issue read-ahead control command: %s", strerror(-r));
175 our_env[i++] = (char*) "READY=1";
178 status = strappend("STATUS=", arg_status);
184 our_env[i++] = status;
188 if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) {
198 final_env = strv_env_merge(2, our_env, argv + optind);
204 if (strv_length(final_env) <= 0) {
209 n = strv_join(final_env, "\n");
215 r = sd_pid_notify(arg_pid, false, n);
217 log_error("Failed to notify init system: %s", strerror(-r));
225 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;