release script fixes
[minix3.git] / commands / service / service.c
blob50c38bd403c6d6681efafff46a5f890e7cb5a3a6
1 /* Utility to start or stop system services. Requests are sent to the
2 * reincarnation server that does the actual work.
4 * Changes:
5 * Nov 22, 2009: added basic live update support (Cristiano Giuffrida)
6 * Jul 22, 2005: Created (Jorrit N. Herder)
7 */
9 #include <stdarg.h>
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <limits.h>
18 #include <lib.h>
19 #include <minix/config.h>
20 #include <minix/com.h>
21 #include <minix/const.h>
22 #include <minix/type.h>
23 #include <minix/ipc.h>
24 #include <minix/rs.h>
25 #include <minix/syslib.h>
26 #include <minix/bitmap.h>
27 #include <minix/paths.h>
28 #include <minix/sef.h>
29 #include <minix/dmap.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <configfile.h>
34 #include <machine/archtypes.h>
35 #include <minix/timers.h>
36 #include <err.h>
37 #include "kernel/proc.h"
39 #include "config.h"
40 #include "proto.h"
42 /* This array defines all known requests. */
43 static char *known_requests[] = {
44 "up",
45 "down",
46 "refresh",
47 "restart",
48 "shutdown",
49 "update",
50 "clone",
51 "edit",
52 "catch for illegal requests"
54 #define ILLEGAL_REQUEST sizeof(known_requests)/sizeof(char *)
56 /* Global error number set for failed system calls. */
57 #define OK 0
59 #define RUN_CMD "run"
60 #define RUN_SCRIPT "/etc/rs.single" /* Default script for 'run' */
61 #define SELF_BINARY "self"
62 #define SELF_REQ_PATH "/dev/null"
63 #define PATH_CONFIG _PATH_SYSTEM_CONF /* Default config file */
64 #define DEFAULT_LU_STATE SEF_LU_STATE_WORK_FREE /* Default lu state */
65 #define DEFAULT_LU_MAXTIME 0 /* Default lu max time */
67 /* Define names for options provided to this utility. */
68 #define OPT_COPY "-c" /* copy executable image */
69 #define OPT_REUSE "-r" /* reuse executable image */
70 #define OPT_NOBLOCK "-n" /* unblock caller immediately */
71 #define OPT_REPLICA "-p" /* create replica for the service */
73 /* Define names for arguments provided to this utility. The first few
74 * arguments are required and have a known index. Thereafter, some optional
75 * argument pairs like "-args arglist" follow.
77 #define ARG_NAME 0 /* own application name */
79 /* The following are relative to optind */
80 #define ARG_REQUEST 0 /* request to perform */
81 #define ARG_PATH 1 /* system service */
82 #define ARG_LABEL 1 /* name of system service */
84 #define MIN_ARG_COUNT 1 /* require an action */
86 #define ARG_ARGS "-args" /* list of arguments to be passed */
87 #define ARG_DEV "-dev" /* major device number for drivers */
88 #define ARG_MAJOR "-major" /* major number */
89 #define ARG_PERIOD "-period" /* heartbeat period in ticks */
90 #define ARG_SCRIPT "-script" /* name of the script to restart a
91 * system service
93 #define ARG_LABELNAME "-label" /* custom label name */
94 #define ARG_CONFIG "-config" /* name of the file with the resource
95 * configuration
98 #define ARG_LU_STATE "-state" /* the live update state required */
99 #define ARG_LU_MAXTIME "-maxtime" /* max time to prepare for the update */
100 #define ARG_DEVMANID "-devid" /* the id of the devman device this
101 driver should be able to access */
103 /* The function parse_arguments() verifies and parses the command line
104 * parameters passed to this utility. Request parameters that are needed
105 * are stored globally in the following variables:
107 static int req_type;
108 static int do_run= 0; /* 'run' command instead of 'up' */
109 static char *req_label = NULL;
110 static char *req_path = NULL;
111 static char *req_path_self = SELF_REQ_PATH;
112 static char *req_args = "";
113 static int req_major = 0;
114 static int devman_id = 0;
115 static long req_period = 0;
116 static char *req_script = NULL;
117 static char *req_config = PATH_CONFIG;
118 static int custom_config_file = 0;
119 static int req_lu_state = DEFAULT_LU_STATE;
120 static int req_lu_maxtime = DEFAULT_LU_MAXTIME;
122 /* Buffer to build "/command arg1 arg2 ..." string to pass to RS server. */
123 static char command[4096];
125 /* An error occurred. Report the problem, print the usage, and exit.
127 static void print_usage(char *app_name, char *problem)
129 fprintf(stderr, "Warning, %s\n", problem);
130 fprintf(stderr, "Usage:\n");
131 fprintf(stderr,
132 " %s [%s %s %s %s] (up|run|edit|update) <binary|%s> [%s <args>] [%s <special>] [%s <major_nr>] [%s <dev_id>] [%s <ticks>] [%s <path>] [%s <name>] [%s <path>] [%s <state>] [%s <time>]\n",
133 app_name, OPT_COPY, OPT_REUSE, OPT_NOBLOCK, OPT_REPLICA, SELF_BINARY,
134 ARG_ARGS, ARG_DEV, ARG_MAJOR, ARG_DEVMANID, ARG_PERIOD, ARG_SCRIPT,
135 ARG_LABELNAME, ARG_CONFIG, ARG_LU_STATE, ARG_LU_MAXTIME);
136 fprintf(stderr, " %s down <label>\n", app_name);
137 fprintf(stderr, " %s refresh <label>\n", app_name);
138 fprintf(stderr, " %s restart <label>\n", app_name);
139 fprintf(stderr, " %s clone <label>\n", app_name);
140 fprintf(stderr, " %s shutdown\n", app_name);
141 fprintf(stderr, "\n");
144 /* A request to the RS server failed. Report and exit.
146 static void failure(int request)
148 fprintf(stderr, "Request 0x%x to RS failed: %s (error %d)\n", request, strerror(errno), errno);
149 exit(errno);
153 /* Parse and verify correctness of arguments. Report problem and exit if an
154 * error is found. Store needed parameters in global variables.
156 static int parse_arguments(int argc, char **argv, u32_t *rss_flags)
158 struct stat stat_buf;
159 char *hz, *buff;
160 int req_nr;
161 int c, i, j;
162 int b_flag, c_flag, r_flag, n_flag, p_flag;
163 int label_required;
165 b_flag = 0;
166 c_flag = 0;
167 r_flag = 0;
168 n_flag = 0;
169 p_flag = 0;
170 while (c= getopt(argc, argv, "rbcnp?"), c != -1)
172 switch(c)
174 case '?':
175 print_usage(argv[ARG_NAME], "wrong number of arguments");
176 exit(EINVAL);
177 case 'b':
178 b_flag = 1;
179 break;
180 case 'c':
181 c_flag = 1;
182 break;
183 case 'r':
184 c_flag = 1; /* -r implies -c */
185 r_flag = 1;
186 break;
187 case 'n':
188 n_flag = 1;
189 break;
190 case 'p':
191 p_flag = 1;
192 break;
193 default:
194 fprintf(stderr, "%s: getopt failed: %c\n",
195 argv[ARG_NAME], c);
196 exit(1);
200 /* Verify argument count. */
201 if (argc < optind+MIN_ARG_COUNT) {
202 print_usage(argv[ARG_NAME], "wrong number of arguments");
203 exit(EINVAL);
206 if (strcmp(argv[optind+ARG_REQUEST], RUN_CMD) == 0)
208 req_nr= RS_UP;
209 do_run= TRUE;
211 else
213 /* Verify request type. */
214 for (req_type=0; req_type< ILLEGAL_REQUEST; req_type++) {
215 if (strcmp(known_requests[req_type],argv[optind+ARG_REQUEST])==0)
216 break;
218 if (req_type == ILLEGAL_REQUEST) {
219 print_usage(argv[ARG_NAME], "illegal request type");
220 exit(ENOSYS);
222 req_nr = RS_RQ_BASE + req_type;
225 *rss_flags = 0;
226 if (req_nr == RS_UP || req_nr == RS_UPDATE || req_nr == RS_EDIT) {
227 u32_t system_hz;
229 if (c_flag)
230 *rss_flags |= RSS_COPY;
232 if(r_flag)
233 *rss_flags |= RSS_REUSE;
235 if(n_flag)
236 *rss_flags |= RSS_NOBLOCK;
238 if(p_flag)
239 *rss_flags |= RSS_REPLICA;
241 if(b_flag)
242 *rss_flags |= RSS_NO_BIN_EXP;
244 req_path = argv[optind+ARG_PATH];
245 if(req_nr == RS_UPDATE && !strcmp(req_path, SELF_BINARY)) {
246 /* Self update needs no real path or configuration file. */
247 req_config = NULL;
248 req_path = req_path_self;
249 *rss_flags |= RSS_SELF_LU;
252 if (do_run)
254 /* Set default recovery script for RUN */
255 req_script = RUN_SCRIPT;
258 /* Verify argument count. */
259 if (argc - 1 < optind+ARG_PATH) {
260 print_usage(argv[ARG_NAME], "action requires a binary to start");
261 exit(EINVAL);
264 /* Verify the name of the binary of the system service. */
265 if(!(*rss_flags & RSS_SELF_LU)) {
266 if (req_path[0] != '/') {
267 print_usage(argv[ARG_NAME], "binary should be absolute path");
268 exit(EINVAL);
270 if (stat(req_path, &stat_buf) == -1) {
271 perror(req_path);
272 fprintf(stderr, "%s: couldn't get stat binary\n", argv[ARG_NAME]);
273 exit(errno);
275 if (! (stat_buf.st_mode & S_IFREG)) {
276 print_usage(argv[ARG_NAME], "binary is not a regular file");
277 exit(EINVAL);
281 /* Get HZ. */
282 system_hz = (u32_t) sysconf(_SC_CLK_TCK);
284 /* Check optional arguments that come in pairs like "-args arglist". */
285 for (i=optind+MIN_ARG_COUNT+1; i<argc; i=i+2) {
286 if (! (i+1 < argc)) {
287 print_usage(argv[ARG_NAME], "optional argument not complete");
288 exit(EINVAL);
290 if (strcmp(argv[i], ARG_ARGS)==0) {
291 req_args = argv[i+1];
293 else if (strcmp(argv[i], ARG_PERIOD)==0) {
294 req_period = strtol(argv[i+1], &hz, 10);
295 if (strcmp(hz,"HZ")==0) req_period *= system_hz;
296 if (req_period < 0) {
297 print_usage(argv[ARG_NAME], "bad period argument");
298 exit(EINVAL);
301 else if (strcmp(argv[i], ARG_DEV)==0) {
302 if (stat(argv[i+1], &stat_buf) == -1) {
303 perror(argv[i+1]);
304 print_usage(argv[ARG_NAME], "couldn't get status of device");
305 exit(errno);
307 if ( ! (stat_buf.st_mode & (S_IFBLK | S_IFCHR))) {
308 print_usage(argv[ARG_NAME], "special file is not a device");
309 exit(EINVAL);
311 if (req_major != 0) {
312 print_usage(argv[ARG_NAME], "major already set");
313 exit(EINVAL);
315 req_major = major(stat_buf.st_rdev);
317 else if (strcmp(argv[i], ARG_MAJOR)==0) {
318 if (req_major != 0) {
319 print_usage(argv[ARG_NAME], "major already set");
320 exit(EINVAL);
322 if (i+1 < argc) {
323 req_major = atoi(argv[i+1]);
324 } else {
325 exit(EINVAL);
328 else if (strcmp(argv[i], ARG_SCRIPT)==0) {
329 req_script = argv[i+1];
331 else if (strcmp(argv[i], ARG_LABELNAME)==0) {
332 req_label = argv[i+1];
334 else if (strcmp(argv[i], ARG_CONFIG)==0) {
335 req_config = argv[i+1];
336 custom_config_file = 1;
338 else if (strcmp(argv[i], ARG_LU_STATE)==0) {
339 errno=0;
340 req_lu_state = strtol(argv[i+1], &buff, 10);
341 if(errno || strcmp(buff, "")) {
342 print_usage(argv[ARG_NAME], "bad live update state");
343 exit(EINVAL);
345 if(req_lu_state == SEF_LU_STATE_NULL) {
346 print_usage(argv[ARG_NAME], "null live update state");
347 exit(EINVAL);
350 else if (strcmp(argv[i], ARG_LU_MAXTIME)==0) {
351 errno=0;
352 req_lu_maxtime = strtol(argv[i+1], &hz, 10);
353 if(errno || (strcmp(hz, "") && strcmp(hz, "HZ"))
354 || req_lu_maxtime<0) {
355 print_usage(argv[ARG_NAME],
356 "bad live update max time");
357 exit(EINVAL);
359 if (strcmp(hz,"HZ")==0) req_lu_maxtime *= system_hz;
360 } else if (strcmp(argv[i], ARG_DEVMANID) == 0) {
361 if (i+1 < argc) {
362 devman_id = atoi(argv[i+1]);
363 } else {
364 exit(EINVAL);
366 } else {
367 print_usage(argv[ARG_NAME], "unknown optional argument given");
368 exit(EINVAL);
372 else if (req_nr == RS_DOWN || req_nr == RS_REFRESH || req_nr == RS_RESTART
373 || req_nr == RS_CLONE) {
375 /* Verify argument count. */
376 if (argc - 1 < optind+ARG_LABEL) {
377 print_usage(argv[ARG_NAME], "action requires a target label");
378 exit(EINVAL);
380 req_label= argv[optind+ARG_LABEL];
382 else if (req_nr == RS_SHUTDOWN) {
383 /* no extra arguments required */
386 label_required = (*rss_flags & RSS_SELF_LU) || (req_nr == RS_EDIT);
387 if(label_required && !req_label) {
388 print_usage(argv[ARG_NAME], "label option mandatory for target action");
389 exit(EINVAL);
392 /* Return the request number if no error were found. */
393 return(req_nr);
396 /* Main program.
398 int main(int argc, char **argv)
400 message m;
401 int result = EXIT_SUCCESS;
402 int request;
403 char *progname = NULL;
404 /* Arguments for RS to start a new service */
405 struct rs_config config;
406 u32_t rss_flags = 0;
408 /* Verify and parse the command line arguments. All arguments are checked
409 * here. If an error occurs, the problem is reported and exit(2) is called.
410 * all needed parameters to perform the request are extracted and stored
411 * global variables.
413 request = parse_arguments(argc, argv, &rss_flags);
415 /* Arguments seem fine. Try to perform the request. Only valid requests
416 * should end up here. The default is used for not yet supported requests.
418 result = OK;
419 memset(&m, 0, sizeof(m));
420 switch(request) {
421 case RS_UPDATE:
422 m.m_rs_update.state = req_lu_state;
423 m.m_rs_update.prepare_maxtime = req_lu_maxtime;
424 /* fall through */
425 case RS_UP:
426 case RS_EDIT:
427 /* Build space-separated command string to be passed to RS server. */
428 progname = strrchr(req_path, '/');
429 assert(progname); /* an absolute path was required */
430 progname++; /* skip last slash */
431 strcpy(command, req_path);
432 command[strlen(req_path)] = ' ';
433 strcpy(command+strlen(req_path)+1, req_args);
435 if (req_config) {
436 assert(progname);
437 memset(&config, 0, sizeof(config));
438 if(!parse_config(progname, custom_config_file, req_config, &config))
439 errx(1, "couldn't parse config");
442 /* Set specifics */
443 config.rs_start.rss_cmd= command;
444 config.rs_start.rss_cmdlen= strlen(command);
445 config.rs_start.rss_major= req_major;
446 config.rs_start.rss_period= req_period;
447 config.rs_start.rss_script= req_script;
448 config.rs_start.devman_id= devman_id;
449 config.rs_start.rss_flags |= rss_flags;
450 if(req_label) {
451 config.rs_start.rss_label.l_addr = req_label;
452 config.rs_start.rss_label.l_len = strlen(req_label);
453 } else {
454 config.rs_start.rss_label.l_addr = progname;
455 config.rs_start.rss_label.l_len = strlen(progname);
457 if (req_script)
458 config.rs_start.rss_scriptlen= strlen(req_script);
459 else
460 config.rs_start.rss_scriptlen= 0;
462 assert(config.rs_start.rss_priority < NR_SCHED_QUEUES);
463 assert(config.rs_start.rss_quantum > 0);
465 m.m_rs_req.addr = (char *) &config.rs_start;
466 break;
467 case RS_DOWN:
468 case RS_REFRESH:
469 case RS_RESTART:
470 case RS_CLONE:
471 m.m_rs_req.addr = req_label;
472 m.m_rs_req.len = strlen(req_label);
473 break;
474 case RS_SHUTDOWN:
475 break;
476 default:
477 print_usage(argv[ARG_NAME], "request is not yet supported");
478 result = EGENERIC;
481 /* Build request message and send the request. */
482 if(result == OK) {
483 if (_syscall(RS_PROC_NR, request, &m) == -1)
484 failure(request);
485 result = m.m_type;
488 return(result);