]>
Commit | Line | Data |
---|---|---|
4a2a8b5a LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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 | |
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. | |
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 | |
16 | General Public License for more details. | |
17 | ||
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/>. | |
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 | ||
30 | #include "strv.h" | |
31 | #include "util.h" | |
32 | #include "log.h" | |
33 | #include "sd-daemon.h" | |
34 | ||
35 | static bool arg_ready = false; | |
36 | static pid_t arg_pid = 0; | |
37 | static const char *arg_status = NULL; | |
96551bae | 38 | static bool arg_booted = false; |
4a2a8b5a LP |
39 | |
40 | static int help(void) { | |
41 | ||
42 | printf("%s [options] [VARIABLE=VALUE...]\n\n" | |
43 | "Notify the init system about service status updates.\n\n" | |
44 | " -h --help Show this help\n" | |
45 | " --ready Inform the init system about service start-up completion\n" | |
46 | " --pid[=PID] Set main pid of daemon\n" | |
96551bae LP |
47 | " --status=TEXT Set status text\n" |
48 | " --booted Returns 0 if the system was booted up with systemd, non-zero otherwise\n", | |
4a2a8b5a LP |
49 | program_invocation_short_name); |
50 | ||
51 | return 0; | |
52 | } | |
53 | ||
54 | static int parse_argv(int argc, char *argv[]) { | |
55 | ||
56 | enum { | |
57 | ARG_READY = 0x100, | |
58 | ARG_PID, | |
96551bae LP |
59 | ARG_STATUS, |
60 | ARG_BOOTED | |
4a2a8b5a LP |
61 | }; |
62 | ||
63 | static const struct option options[] = { | |
64 | { "help", no_argument, NULL, 'h' }, | |
65 | { "ready", no_argument, NULL, ARG_READY }, | |
66 | { "pid", optional_argument, NULL, ARG_PID }, | |
67 | { "status", required_argument, NULL, ARG_STATUS }, | |
96551bae | 68 | { "booted", no_argument, NULL, ARG_BOOTED }, |
4a2a8b5a LP |
69 | { NULL, 0, NULL, 0 } |
70 | }; | |
71 | ||
72 | int c; | |
73 | ||
74 | assert(argc >= 0); | |
75 | assert(argv); | |
76 | ||
77 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { | |
78 | ||
79 | switch (c) { | |
80 | ||
81 | case 'h': | |
82 | help(); | |
83 | return 0; | |
84 | ||
85 | case ARG_READY: | |
86 | arg_ready = true; | |
87 | break; | |
88 | ||
89 | case ARG_PID: | |
90 | ||
91 | if (optarg) { | |
92 | if (parse_pid(optarg, &arg_pid) < 0) { | |
93 | log_error("Failed to parse PID %s.", optarg); | |
94 | return -EINVAL; | |
95 | } | |
96 | } else | |
97 | arg_pid = getppid(); | |
98 | ||
99 | break; | |
100 | ||
101 | case ARG_STATUS: | |
102 | arg_status = optarg; | |
103 | break; | |
104 | ||
96551bae LP |
105 | case ARG_BOOTED: |
106 | arg_booted = true; | |
107 | break; | |
108 | ||
4a2a8b5a LP |
109 | case '?': |
110 | return -EINVAL; | |
111 | ||
112 | default: | |
113 | log_error("Unknown option code %c", c); | |
114 | return -EINVAL; | |
115 | } | |
116 | } | |
117 | ||
118 | return 1; | |
119 | } | |
120 | ||
121 | int main(int argc, char* argv[]) { | |
122 | char* our_env[4], **final_env = NULL; | |
123 | unsigned i = 0; | |
124 | char *status = NULL, *cpid = NULL, *n = NULL; | |
125 | int r, retval = 1; | |
126 | ||
127 | log_parse_environment(); | |
128 | ||
129 | if ((r = parse_argv(argc, argv)) <= 0) { | |
130 | retval = r < 0; | |
131 | goto finish; | |
132 | } | |
133 | ||
96551bae LP |
134 | if (arg_booted) |
135 | return sd_booted() <= 0; | |
136 | ||
4a2a8b5a LP |
137 | if (arg_ready) |
138 | our_env[i++] = (char*) "READY=1"; | |
139 | ||
140 | if (arg_status) { | |
141 | if (!(status = strappend("STATUS=", arg_status))) { | |
142 | log_error("Failed to allocate STATUS string."); | |
143 | goto finish; | |
144 | } | |
145 | ||
146 | our_env[i++] = status; | |
147 | } | |
148 | ||
149 | if (arg_pid > 0) { | |
150 | if (asprintf(&cpid, "MAINPID=%lu", (unsigned long) arg_pid) < 0) { | |
151 | log_error("Failed to allocate MAINPID string."); | |
152 | goto finish; | |
153 | } | |
154 | ||
155 | our_env[i++] = cpid; | |
156 | } | |
157 | ||
158 | our_env[i++] = NULL; | |
159 | ||
160 | if (!(final_env = strv_env_merge(2, our_env, argv + optind))) { | |
161 | log_error("Failed to merge string sets."); | |
162 | goto finish; | |
163 | } | |
164 | ||
165 | if (strv_length(final_env) <= 0) { | |
166 | retval = 0; | |
167 | goto finish; | |
168 | } | |
169 | ||
170 | if (!(n = strv_join(final_env, "\n"))) { | |
171 | log_error("Failed to concatenate strings."); | |
172 | goto finish; | |
173 | } | |
174 | ||
175 | if ((r = sd_notify(false, n)) < 0) { | |
176 | log_error("Failed to notify init system: %s", strerror(-r)); | |
177 | goto finish; | |
178 | } | |
179 | ||
180 | retval = r <= 0; | |
181 | ||
182 | finish: | |
183 | free(status); | |
184 | free(cpid); | |
185 | free(n); | |
186 | ||
187 | strv_free(final_env); | |
188 | ||
189 | return retval; | |
190 | } |