1 /* Utility to start or stop system services. Requests are sent to the
2 * reincarnation server that does the actual work.
5 * Nov 22, 2009: added basic live update support (Cristiano Giuffrida)
6 * Jul 22, 2005: Created (Jorrit N. Herder)
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>
25 #include <minix/syslib.h>
26 #include <minix/bitmap.h>
28 #include <minix/sef.h>
29 #include <minix/dmap.h>
30 #include <sys/types.h>
32 #include <configfile.h>
34 #include <machine/archtypes.h>
37 #include "kernel/proc.h"
42 /* This array defines all known requests. */
43 static char *known_requests
[] = {
52 "catch for illegal requests"
54 #define ILLEGAL_REQUEST sizeof(known_requests)/sizeof(char *)
56 /* Global error number set for failed system calls. */
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_DEVSTYLE "-devstyle" /* device style */
90 #define ARG_PERIOD "-period" /* heartbeat period in ticks */
91 #define ARG_SCRIPT "-script" /* name of the script to restart a
94 #define ARG_LABELNAME "-label" /* custom label name */
95 #define ARG_CONFIG "-config" /* name of the file with the resource
99 #define ARG_LU_STATE "-state" /* the live update state required */
100 #define ARG_LU_MAXTIME "-maxtime" /* max time to prepare for the update */
101 #define ARG_DEVMANID "-devid" /* the id of the devman device this
102 driver should be able to access */
104 /* The function parse_arguments() verifies and parses the command line
105 * parameters passed to this utility. Request parameters that are needed
106 * are stored globally in the following variables:
109 static int do_run
= 0; /* 'run' command instead of 'up' */
110 static char *req_label
= NULL
;
111 static char *req_path
= NULL
;
112 static char *req_path_self
= SELF_REQ_PATH
;
113 static char *req_args
= "";
114 static int req_major
= 0;
115 static int devman_id
= 0;
116 static int req_dev_style
= STYLE_NDEV
;
117 static long req_period
= 0;
118 static char *req_script
= NULL
;
119 static char *req_config
= PATH_CONFIG
;
120 static int custom_config_file
= 0;
121 static int req_lu_state
= DEFAULT_LU_STATE
;
122 static int req_lu_maxtime
= DEFAULT_LU_MAXTIME
;
124 /* Buffer to build "/command arg1 arg2 ..." string to pass to RS server. */
125 static char command
[4096];
127 /* An error occurred. Report the problem, print the usage, and exit.
129 static void print_usage(char *app_name
, char *problem
)
131 fprintf(stderr
, "Warning, %s\n", problem
);
132 fprintf(stderr
, "Usage:\n");
134 " %s [%s %s %s %s] (up|run|edit|update) <binary|%s> [%s <args>] [%s <special>] [%s <style>] [%s <major_nr>] [%s <dev_id>] [%s <ticks>] [%s <path>] [%s <name>] [%s <path>] [%s <state>] [%s <time>]\n",
135 app_name
, OPT_COPY
, OPT_REUSE
, OPT_NOBLOCK
, OPT_REPLICA
, SELF_BINARY
,
136 ARG_ARGS
, ARG_DEV
, ARG_DEVSTYLE
, ARG_MAJOR
, ARG_DEVMANID
, ARG_PERIOD
, ARG_SCRIPT
,
137 ARG_LABELNAME
, ARG_CONFIG
, ARG_LU_STATE
, ARG_LU_MAXTIME
);
138 fprintf(stderr
, " %s down <label>\n", app_name
);
139 fprintf(stderr
, " %s refresh <label>\n", app_name
);
140 fprintf(stderr
, " %s restart <label>\n", app_name
);
141 fprintf(stderr
, " %s clone <label>\n", app_name
);
142 fprintf(stderr
, " %s shutdown\n", app_name
);
143 fprintf(stderr
, "\n");
146 /* A request to the RS server failed. Report and exit.
148 static void failure(int request
)
150 fprintf(stderr
, "Request 0x%x to RS failed: %s (error %d)\n", request
, strerror(errno
), errno
);
155 /* Parse and verify correctness of arguments. Report problem and exit if an
156 * error is found. Store needed parameters in global variables.
158 static int parse_arguments(int argc
, char **argv
, u32_t
*rss_flags
)
160 struct stat stat_buf
;
164 int c_flag
, r_flag
, n_flag
, p_flag
;
171 while (c
= getopt(argc
, argv
, "rcnp?"), c
!= -1)
176 print_usage(argv
[ARG_NAME
], "wrong number of arguments");
182 c_flag
= 1; /* -r implies -c */
192 fprintf(stderr
, "%s: getopt failed: %c\n",
198 /* Verify argument count. */
199 if (argc
< optind
+MIN_ARG_COUNT
) {
200 print_usage(argv
[ARG_NAME
], "wrong number of arguments");
204 if (strcmp(argv
[optind
+ARG_REQUEST
], RUN_CMD
) == 0)
211 /* Verify request type. */
212 for (req_type
=0; req_type
< ILLEGAL_REQUEST
; req_type
++) {
213 if (strcmp(known_requests
[req_type
],argv
[optind
+ARG_REQUEST
])==0)
216 if (req_type
== ILLEGAL_REQUEST
) {
217 print_usage(argv
[ARG_NAME
], "illegal request type");
220 req_nr
= RS_RQ_BASE
+ req_type
;
224 if (req_nr
== RS_UP
|| req_nr
== RS_UPDATE
|| req_nr
== RS_EDIT
) {
228 *rss_flags
|= RSS_COPY
;
231 *rss_flags
|= RSS_REUSE
;
234 *rss_flags
|= RSS_NOBLOCK
;
237 *rss_flags
|= RSS_REPLICA
;
239 req_path
= argv
[optind
+ARG_PATH
];
240 if(req_nr
== RS_UPDATE
&& !strcmp(req_path
, SELF_BINARY
)) {
241 /* Self update needs no real path or configuration file. */
243 req_path
= req_path_self
;
244 *rss_flags
|= RSS_SELF_LU
;
249 /* Set default recovery script for RUN */
250 req_script
= RUN_SCRIPT
;
253 /* Verify argument count. */
254 if (argc
- 1 < optind
+ARG_PATH
) {
255 print_usage(argv
[ARG_NAME
], "action requires a binary to start");
259 /* Verify the name of the binary of the system service. */
260 if(!(*rss_flags
& RSS_SELF_LU
)) {
261 if (req_path
[0] != '/') {
262 print_usage(argv
[ARG_NAME
], "binary should be absolute path");
265 if (stat(req_path
, &stat_buf
) == -1) {
267 fprintf(stderr
, "%s: couldn't get stat binary\n", argv
[ARG_NAME
]);
270 if (! (stat_buf
.st_mode
& S_IFREG
)) {
271 print_usage(argv
[ARG_NAME
], "binary is not a regular file");
277 system_hz
= (u32_t
) sysconf(_SC_CLK_TCK
);
279 /* Check optional arguments that come in pairs like "-args arglist". */
280 for (i
=optind
+MIN_ARG_COUNT
+1; i
<argc
; i
=i
+2) {
281 if (! (i
+1 < argc
)) {
282 print_usage(argv
[ARG_NAME
], "optional argument not complete");
285 if (strcmp(argv
[i
], ARG_ARGS
)==0) {
286 req_args
= argv
[i
+1];
288 else if (strcmp(argv
[i
], ARG_PERIOD
)==0) {
289 req_period
= strtol(argv
[i
+1], &hz
, 10);
290 if (strcmp(hz
,"HZ")==0) req_period
*= system_hz
;
291 if (req_period
< 0) {
292 print_usage(argv
[ARG_NAME
], "bad period argument");
296 else if (strcmp(argv
[i
], ARG_DEV
)==0) {
297 if (stat(argv
[i
+1], &stat_buf
) == -1) {
299 print_usage(argv
[ARG_NAME
], "couldn't get status of device");
302 if ( ! (stat_buf
.st_mode
& (S_IFBLK
| S_IFCHR
))) {
303 print_usage(argv
[ARG_NAME
], "special file is not a device");
306 if (req_major
!= 0) {
307 print_usage(argv
[ARG_NAME
], "major already set");
310 req_major
= major(stat_buf
.st_rdev
);
311 if(req_dev_style
== STYLE_NDEV
) {
312 req_dev_style
= STYLE_DEV
;
315 else if (strcmp(argv
[i
], ARG_MAJOR
)==0) {
316 if (req_major
!= 0) {
317 print_usage(argv
[ARG_NAME
], "major already set");
321 req_major
= atoi(argv
[i
+1]);
325 if(req_dev_style
== STYLE_NDEV
) {
326 req_dev_style
= STYLE_DEV
;
329 else if (strcmp(argv
[i
], ARG_DEVSTYLE
)==0) {
330 char* dev_style_keys
[] = { "STYLE_DEV", "STYLE_DEVA", "STYLE_TTY",
331 "STYLE_CTTY", "STYLE_CLONE", "STYLE_CLONE_A", NULL
};
332 int dev_style_values
[] = { STYLE_DEV
, STYLE_DEVA
, STYLE_TTY
,
333 STYLE_CTTY
, STYLE_CLONE
, STYLE_CLONE_A
};
334 for(j
=0;dev_style_keys
[j
]!=NULL
;j
++) {
335 if(!strcmp(dev_style_keys
[j
], argv
[i
+1])) {
339 if(dev_style_keys
[j
] == NULL
) {
340 print_usage(argv
[ARG_NAME
], "bad device style");
343 req_dev_style
= dev_style_values
[j
];
345 else if (strcmp(argv
[i
], ARG_SCRIPT
)==0) {
346 req_script
= argv
[i
+1];
348 else if (strcmp(argv
[i
], ARG_LABELNAME
)==0) {
349 req_label
= argv
[i
+1];
351 else if (strcmp(argv
[i
], ARG_CONFIG
)==0) {
352 req_config
= argv
[i
+1];
353 custom_config_file
= 1;
355 else if (strcmp(argv
[i
], ARG_LU_STATE
)==0) {
357 req_lu_state
= strtol(argv
[i
+1], &buff
, 10);
358 if(errno
|| strcmp(buff
, "")) {
359 print_usage(argv
[ARG_NAME
], "bad live update state");
362 if(req_lu_state
== SEF_LU_STATE_NULL
) {
363 print_usage(argv
[ARG_NAME
], "null live update state");
367 else if (strcmp(argv
[i
], ARG_LU_MAXTIME
)==0) {
369 req_lu_maxtime
= strtol(argv
[i
+1], &hz
, 10);
370 if(errno
|| (strcmp(hz
, "") && strcmp(hz
, "HZ"))
371 || req_lu_maxtime
<0) {
372 print_usage(argv
[ARG_NAME
],
373 "bad live update max time");
376 if (strcmp(hz
,"HZ")==0) req_lu_maxtime
*= system_hz
;
377 } else if (strcmp(argv
[i
], ARG_DEVMANID
) == 0) {
379 devman_id
= atoi(argv
[i
+1]);
384 print_usage(argv
[ARG_NAME
], "unknown optional argument given");
389 else if (req_nr
== RS_DOWN
|| req_nr
== RS_REFRESH
|| req_nr
== RS_RESTART
390 || req_nr
== RS_CLONE
) {
392 /* Verify argument count. */
393 if (argc
- 1 < optind
+ARG_LABEL
) {
394 print_usage(argv
[ARG_NAME
], "action requires a target label");
397 req_label
= argv
[optind
+ARG_LABEL
];
399 else if (req_nr
== RS_SHUTDOWN
) {
400 /* no extra arguments required */
403 label_required
= (*rss_flags
& RSS_SELF_LU
) || (req_nr
== RS_EDIT
);
404 if(label_required
&& !req_label
) {
405 print_usage(argv
[ARG_NAME
], "label option mandatory for target action");
409 /* Return the request number if no error were found. */
415 int main(int argc
, char **argv
)
418 int result
= EXIT_SUCCESS
;
420 char *progname
= NULL
;
421 /* Arguments for RS to start a new service */
422 struct rs_config config
;
425 /* Verify and parse the command line arguments. All arguments are checked
426 * here. If an error occurs, the problem is reported and exit(2) is called.
427 * all needed parameters to perform the request are extracted and stored
430 request
= parse_arguments(argc
, argv
, &rss_flags
);
432 /* Arguments seem fine. Try to perform the request. Only valid requests
433 * should end up here. The default is used for not yet supported requests.
438 m
.RS_LU_STATE
= req_lu_state
;
439 m
.RS_LU_PREPARE_MAXTIME
= req_lu_maxtime
;
443 /* Build space-separated command string to be passed to RS server. */
444 progname
= strrchr(req_path
, '/');
445 assert(progname
); /* an absolute path was required */
446 progname
++; /* skip last slash */
447 strcpy(command
, req_path
);
448 command
[strlen(req_path
)] = ' ';
449 strcpy(command
+strlen(req_path
)+1, req_args
);
453 memset(&config
, 0, sizeof(config
));
454 if(!parse_config(progname
, custom_config_file
, req_config
, &config
))
455 errx(1, "couldn't parse config");
459 config
.rs_start
.rss_cmd
= command
;
460 config
.rs_start
.rss_cmdlen
= strlen(command
);
461 config
.rs_start
.rss_major
= req_major
;
462 config
.rs_start
.rss_dev_style
= req_dev_style
;
463 config
.rs_start
.rss_period
= req_period
;
464 config
.rs_start
.rss_script
= req_script
;
465 config
.rs_start
.devman_id
= devman_id
;
466 config
.rs_start
.rss_flags
|= rss_flags
;
468 config
.rs_start
.rss_label
.l_addr
= req_label
;
469 config
.rs_start
.rss_label
.l_len
= strlen(req_label
);
471 config
.rs_start
.rss_label
.l_addr
= progname
;
472 config
.rs_start
.rss_label
.l_len
= strlen(progname
);
475 config
.rs_start
.rss_scriptlen
= strlen(req_script
);
477 config
.rs_start
.rss_scriptlen
= 0;
479 assert(config
.rs_start
.rss_priority
< NR_SCHED_QUEUES
);
480 assert(config
.rs_start
.rss_quantum
> 0);
482 m
.RS_CMD_ADDR
= (char *) &config
.rs_start
;
488 m
.RS_CMD_ADDR
= req_label
;
489 m
.RS_CMD_LEN
= strlen(req_label
);
494 print_usage(argv
[ARG_NAME
], "request is not yet supported");
498 /* Build request message and send the request. */
500 if (_syscall(RS_PROC_NR
, request
, &m
) == -1)