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/>.
28 #include "sd-daemon.h"
31 #include "formats-util.h"
33 #include "string-util.h"
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;
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
);
54 static int parse_argv(int argc
, char *argv
[]) {
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
},
79 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
);
118 assert_not_reached("Unhandled option");
122 if (optind
>= argc
&&
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
;
141 log_parse_environment();
144 r
= parse_argv(argc
, argv
);
149 return sd_booted() <= 0;
152 our_env
[i
++] = (char*) "READY=1";
155 status
= strappend("STATUS=", arg_status
);
161 our_env
[i
++] = status
;
165 if (asprintf(&cpid
, "MAINPID="PID_FMT
, arg_pid
) < 0) {
175 final_env
= strv_env_merge(2, our_env
, argv
+ optind
);
181 if (strv_length(final_env
) <= 0) {
186 n
= strv_join(final_env
, "\n");
192 r
= sd_pid_notify(arg_pid
? arg_pid
: getppid(), false, n
);
194 log_error_errno(r
, "Failed to notify init system: %m");
197 log_error("No status data could be sent: $NOTIFY_SOCKET was not set");
202 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;