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>
27 #include <minix/paths.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>
35 #include <minix/timers.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_PERIOD "-period" /* heartbeat period in ticks */
90 #define ARG_SCRIPT "-script" /* name of the script to restart a
93 #define ARG_LABELNAME "-label" /* custom label name */
94 #define ARG_CONFIG "-config" /* name of the file with the resource
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:
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");
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
);
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
;
162 int b_flag
, c_flag
, r_flag
, n_flag
, p_flag
;
170 while (c
= getopt(argc
, argv
, "rbcnp?"), c
!= -1)
175 print_usage(argv
[ARG_NAME
], "wrong number of arguments");
184 c_flag
= 1; /* -r implies -c */
194 fprintf(stderr
, "%s: getopt failed: %c\n",
200 /* Verify argument count. */
201 if (argc
< optind
+MIN_ARG_COUNT
) {
202 print_usage(argv
[ARG_NAME
], "wrong number of arguments");
206 if (strcmp(argv
[optind
+ARG_REQUEST
], RUN_CMD
) == 0)
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)
218 if (req_type
== ILLEGAL_REQUEST
) {
219 print_usage(argv
[ARG_NAME
], "illegal request type");
222 req_nr
= RS_RQ_BASE
+ req_type
;
226 if (req_nr
== RS_UP
|| req_nr
== RS_UPDATE
|| req_nr
== RS_EDIT
) {
230 *rss_flags
|= RSS_COPY
;
233 *rss_flags
|= RSS_REUSE
;
236 *rss_flags
|= RSS_NOBLOCK
;
239 *rss_flags
|= RSS_REPLICA
;
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. */
248 req_path
= req_path_self
;
249 *rss_flags
|= RSS_SELF_LU
;
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");
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");
270 if (stat(req_path
, &stat_buf
) == -1) {
272 fprintf(stderr
, "%s: couldn't get stat binary\n", argv
[ARG_NAME
]);
275 if (! (stat_buf
.st_mode
& S_IFREG
)) {
276 print_usage(argv
[ARG_NAME
], "binary is not a regular file");
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");
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");
301 else if (strcmp(argv
[i
], ARG_DEV
)==0) {
302 if (stat(argv
[i
+1], &stat_buf
) == -1) {
304 print_usage(argv
[ARG_NAME
], "couldn't get status of device");
307 if ( ! (stat_buf
.st_mode
& (S_IFBLK
| S_IFCHR
))) {
308 print_usage(argv
[ARG_NAME
], "special file is not a device");
311 if (req_major
!= 0) {
312 print_usage(argv
[ARG_NAME
], "major already set");
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");
323 req_major
= atoi(argv
[i
+1]);
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) {
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");
345 if(req_lu_state
== SEF_LU_STATE_NULL
) {
346 print_usage(argv
[ARG_NAME
], "null live update state");
350 else if (strcmp(argv
[i
], ARG_LU_MAXTIME
)==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");
359 if (strcmp(hz
,"HZ")==0) req_lu_maxtime
*= system_hz
;
360 } else if (strcmp(argv
[i
], ARG_DEVMANID
) == 0) {
362 devman_id
= atoi(argv
[i
+1]);
367 print_usage(argv
[ARG_NAME
], "unknown optional argument given");
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");
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");
392 /* Return the request number if no error were found. */
398 int main(int argc
, char **argv
)
401 int result
= EXIT_SUCCESS
;
403 char *progname
= NULL
;
404 /* Arguments for RS to start a new service */
405 struct rs_config config
;
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
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.
419 memset(&m
, 0, sizeof(m
));
422 m
.m_rs_update
.state
= req_lu_state
;
423 m
.m_rs_update
.prepare_maxtime
= req_lu_maxtime
;
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
);
437 memset(&config
, 0, sizeof(config
));
438 if(!parse_config(progname
, custom_config_file
, req_config
, &config
))
439 errx(1, "couldn't parse config");
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
;
451 config
.rs_start
.rss_label
.l_addr
= req_label
;
452 config
.rs_start
.rss_label
.l_len
= strlen(req_label
);
454 config
.rs_start
.rss_label
.l_addr
= progname
;
455 config
.rs_start
.rss_label
.l_len
= strlen(progname
);
458 config
.rs_start
.rss_scriptlen
= strlen(req_script
);
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
;
471 m
.m_rs_req
.addr
= req_label
;
472 m
.m_rs_req
.len
= strlen(req_label
);
477 print_usage(argv
[ARG_NAME
], "request is not yet supported");
481 /* Build request message and send the request. */
483 if (_syscall(RS_PROC_NR
, request
, &m
) == -1)