kernel: scheduling fix for ARM
[minix.git] / commands / service / service.c
blob65fe1998fcf000228f5a105d7d5d225ecb8a517e
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 <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 <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_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
92 * system service
94 #define ARG_LABELNAME "-label" /* custom label name */
95 #define ARG_CONFIG "-config" /* name of the file with the resource
96 * configuration
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:
108 static int req_type;
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");
133 fprintf(stderr,
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);
151 exit(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;
161 char *hz, *buff;
162 int req_nr;
163 int c, i, j;
164 int c_flag, r_flag, n_flag, p_flag;
165 int label_required;
167 c_flag = 0;
168 r_flag = 0;
169 n_flag = 0;
170 p_flag = 0;
171 while (c= getopt(argc, argv, "rcnp?"), c != -1)
173 switch(c)
175 case '?':
176 print_usage(argv[ARG_NAME], "wrong number of arguments");
177 exit(EINVAL);
178 case 'c':
179 c_flag = 1;
180 break;
181 case 'r':
182 c_flag = 1; /* -r implies -c */
183 r_flag = 1;
184 break;
185 case 'n':
186 n_flag = 1;
187 break;
188 case 'p':
189 p_flag = 1;
190 break;
191 default:
192 fprintf(stderr, "%s: getopt failed: %c\n",
193 argv[ARG_NAME], c);
194 exit(1);
198 /* Verify argument count. */
199 if (argc < optind+MIN_ARG_COUNT) {
200 print_usage(argv[ARG_NAME], "wrong number of arguments");
201 exit(EINVAL);
204 if (strcmp(argv[optind+ARG_REQUEST], RUN_CMD) == 0)
206 req_nr= RS_UP;
207 do_run= TRUE;
209 else
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)
214 break;
216 if (req_type == ILLEGAL_REQUEST) {
217 print_usage(argv[ARG_NAME], "illegal request type");
218 exit(ENOSYS);
220 req_nr = RS_RQ_BASE + req_type;
223 *rss_flags = 0;
224 if (req_nr == RS_UP || req_nr == RS_UPDATE || req_nr == RS_EDIT) {
225 u32_t system_hz;
227 if (c_flag)
228 *rss_flags |= RSS_COPY;
230 if(r_flag)
231 *rss_flags |= RSS_REUSE;
233 if(n_flag)
234 *rss_flags |= RSS_NOBLOCK;
236 if(p_flag)
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. */
242 req_config = NULL;
243 req_path = req_path_self;
244 *rss_flags |= RSS_SELF_LU;
247 if (do_run)
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");
256 exit(EINVAL);
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");
263 exit(EINVAL);
265 if (stat(req_path, &stat_buf) == -1) {
266 perror(req_path);
267 fprintf(stderr, "%s: couldn't get stat binary\n", argv[ARG_NAME]);
268 exit(errno);
270 if (! (stat_buf.st_mode & S_IFREG)) {
271 print_usage(argv[ARG_NAME], "binary is not a regular file");
272 exit(EINVAL);
276 /* Get HZ. */
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");
283 exit(EINVAL);
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");
293 exit(EINVAL);
296 else if (strcmp(argv[i], ARG_DEV)==0) {
297 if (stat(argv[i+1], &stat_buf) == -1) {
298 perror(argv[i+1]);
299 print_usage(argv[ARG_NAME], "couldn't get status of device");
300 exit(errno);
302 if ( ! (stat_buf.st_mode & (S_IFBLK | S_IFCHR))) {
303 print_usage(argv[ARG_NAME], "special file is not a device");
304 exit(EINVAL);
306 if (req_major != 0) {
307 print_usage(argv[ARG_NAME], "major already set");
308 exit(EINVAL);
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");
318 exit(EINVAL);
320 if (i+1 < argc) {
321 req_major = atoi(argv[i+1]);
322 } else {
323 exit(EINVAL);
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])) {
336 break;
339 if(dev_style_keys[j] == NULL) {
340 print_usage(argv[ARG_NAME], "bad device style");
341 exit(EINVAL);
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) {
356 errno=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");
360 exit(EINVAL);
362 if(req_lu_state == SEF_LU_STATE_NULL) {
363 print_usage(argv[ARG_NAME], "null live update state");
364 exit(EINVAL);
367 else if (strcmp(argv[i], ARG_LU_MAXTIME)==0) {
368 errno=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");
374 exit(EINVAL);
376 if (strcmp(hz,"HZ")==0) req_lu_maxtime *= system_hz;
377 } else if (strcmp(argv[i], ARG_DEVMANID) == 0) {
378 if (i+1 < argc) {
379 devman_id = atoi(argv[i+1]);
380 } else {
381 exit(EINVAL);
383 } else {
384 print_usage(argv[ARG_NAME], "unknown optional argument given");
385 exit(EINVAL);
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");
395 exit(EINVAL);
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");
406 exit(EINVAL);
409 /* Return the request number if no error were found. */
410 return(req_nr);
413 /* Main program.
415 int main(int argc, char **argv)
417 message m;
418 int result = EXIT_SUCCESS;
419 int request;
420 char *progname = NULL;
421 /* Arguments for RS to start a new service */
422 struct rs_config config;
423 u32_t rss_flags = 0;
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
428 * global variables.
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.
435 result = OK;
436 switch(request) {
437 case RS_UPDATE:
438 m.RS_LU_STATE = req_lu_state;
439 m.RS_LU_PREPARE_MAXTIME = req_lu_maxtime;
440 /* fall through */
441 case RS_UP:
442 case RS_EDIT:
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);
451 if (req_config) {
452 assert(progname);
453 memset(&config, 0, sizeof(config));
454 if(!parse_config(progname, custom_config_file, req_config, &config))
455 errx(1, "couldn't parse config");
458 /* Set specifics */
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;
467 if(req_label) {
468 config.rs_start.rss_label.l_addr = req_label;
469 config.rs_start.rss_label.l_len = strlen(req_label);
470 } else {
471 config.rs_start.rss_label.l_addr = progname;
472 config.rs_start.rss_label.l_len = strlen(progname);
474 if (req_script)
475 config.rs_start.rss_scriptlen= strlen(req_script);
476 else
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;
483 break;
484 case RS_DOWN:
485 case RS_REFRESH:
486 case RS_RESTART:
487 case RS_CLONE:
488 m.RS_CMD_ADDR = req_label;
489 m.RS_CMD_LEN = strlen(req_label);
490 break;
491 case RS_SHUTDOWN:
492 break;
493 default:
494 print_usage(argv[ARG_NAME], "request is not yet supported");
495 result = EGENERIC;
498 /* Build request message and send the request. */
499 if(result == OK) {
500 if (_syscall(RS_PROC_NR, request, &m) == -1)
501 failure(request);
502 result = m.m_type;
505 return(result);