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