The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/servers/rs/service.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* Utility to start or stop system services.  Requests are sent to the 
    2  * reincarnation server that does the actual work. 
    3  *
    4  * Changes:
    5  *   Jul 22, 2005:      Created  (Jorrit N. Herder)
    6  */
    7 
    8 #include <stdlib.h>
    9 #include <stdio.h>
   10 #include <string.h>
   11 #include <errno.h>
   12 #include <minix/config.h>
   13 #include <minix/com.h>
   14 #include <minix/const.h>
   15 #include <minix/type.h>
   16 #include <minix/ipc.h>
   17 #include <minix/syslib.h>
   18 #include <sys/types.h>
   19 #include <sys/stat.h>
   20 
   21 
   22 /* This array defines all known requests. */
   23 PRIVATE char *known_requests[] = {
   24   "up", 
   25   "down",
   26   "refresh", 
   27   "rescue", 
   28   "shutdown", 
   29   "catch for illegal requests"
   30 };
   31 #define ILLEGAL_REQUEST  sizeof(known_requests)/sizeof(char *)
   32 
   33 /* Global error number set for failed system calls. */
   34 #define OK 0
   35 extern int errno;
   36 
   37 /* Define names for arguments provided to this utility. The first few 
   38  * arguments are required and have a known index. Thereafter, some optional
   39  * argument pairs like "-args arglist" follow.
   40  */
   41 #define ARG_NAME        0               /* own application name */
   42 #define ARG_REQUEST     1               /* request to perform */
   43 #define ARG_PATH        2               /* rescue dir or system service */
   44 #define ARG_PID         2               /* pid of system service */
   45 
   46 #define MIN_ARG_COUNT   2               /* require an action */
   47 
   48 #define ARG_ARGS        "-args"         /* list of arguments to be passed */
   49 #define ARG_DEV         "-dev"          /* major device number for drivers */
   50 #define ARG_PRIV        "-priv"         /* required privileges */
   51 #define ARG_PERIOD      "-period"       /* heartbeat period in ticks */
   52 
   53 /* The function parse_arguments() verifies and parses the command line 
   54  * parameters passed to this utility. Request parameters that are needed
   55  * are stored globally in the following variables:
   56  */
   57 PRIVATE int req_type;
   58 PRIVATE int req_pid;
   59 PRIVATE char *req_path;
   60 PRIVATE char *req_args;
   61 PRIVATE int req_major;
   62 PRIVATE long req_period;
   63 PRIVATE char *req_priv;
   64 
   65 /* Buffer to build "/command arg1 arg2 ..." string to pass to RS server. */
   66 PRIVATE char command[4096];     
   67 
   68 /* An error occurred. Report the problem, print the usage, and exit. 
   69  */
   70 PRIVATE void print_usage(char *app_name, char *problem) 
   71 {
   72   printf("Warning, %s\n", problem);
   73   printf("Usage:\n");
   74   printf("    %s up <binary> [%s <args>] [%s <special>] [%s <ticks>]\n", 
   75         app_name, ARG_ARGS, ARG_DEV, ARG_PERIOD);
   76   printf("    %s down <pid>\n", app_name);
   77   printf("    %s refresh <pid>\n", app_name);
   78   printf("    %s rescue <dir>\n", app_name);
   79   printf("    %s shutdown\n", app_name);
   80   printf("\n");
   81 }
   82 
   83 /* A request to the RS server failed. Report and exit. 
   84  */
   85 PRIVATE void failure(int num) 
   86 {
   87   printf("Request to RS failed: %s (%d)\n", strerror(num), num);
   88   exit(num);
   89 }
   90 
   91 
   92 /* Parse and verify correctness of arguments. Report problem and exit if an 
   93  * error is found. Store needed parameters in global variables.
   94  */
   95 PRIVATE int parse_arguments(int argc, char **argv)
   96 {
   97   struct stat stat_buf;
   98   char *hz;
   99   int req_nr;
  100   int i;
  101 
  102   /* Verify argument count. */ 
  103   if (argc < MIN_ARG_COUNT) {
  104       print_usage(argv[ARG_NAME], "wrong number of arguments");
  105       exit(EINVAL);
  106   }
  107 
  108   /* Verify request type. */
  109   for (req_type=0; req_type< ILLEGAL_REQUEST; req_type++) {
  110       if (strcmp(known_requests[req_type],argv[ARG_REQUEST])==0) break;
  111   }
  112   if (req_type == ILLEGAL_REQUEST) {
  113       print_usage(argv[ARG_NAME], "illegal request type");
  114       exit(ENOSYS);
  115   }
  116   req_nr = RS_RQ_BASE + req_type;
  117 
  118   if (req_nr == RS_UP) {
  119 
  120       /* Verify argument count. */ 
  121       if (argc - 1 < ARG_PATH) {
  122           print_usage(argv[ARG_NAME], "action requires a binary to start");
  123           exit(EINVAL);
  124       }
  125 
  126       /* Verify the name of the binary of the system service. */
  127       req_path = argv[ARG_PATH];
  128       if (req_path[0] != '/') {
  129           print_usage(argv[ARG_NAME], "binary should be absolute path");
  130           exit(EINVAL);
  131       }
  132       if (stat(req_path, &stat_buf) == -1) {
  133           print_usage(argv[ARG_NAME], "couldn't get status of binary");
  134           exit(errno);
  135       }
  136       if (! (stat_buf.st_mode & S_IFREG)) {
  137           print_usage(argv[ARG_NAME], "binary is not a regular file");
  138           exit(EINVAL);
  139       }
  140 
  141       /* Check optional arguments that come in pairs like "-args arglist". */
  142       for (i=MIN_ARG_COUNT+1; i<argc; i=i+2) {
  143           if (! (i+1 < argc)) {
  144               print_usage(argv[ARG_NAME], "optional argument not complete");
  145               exit(EINVAL);
  146           }
  147           if (strcmp(argv[i], ARG_ARGS)==0) {
  148               req_args = argv[i+1];
  149           }
  150           else if (strcmp(argv[i], ARG_PERIOD)==0) {
  151               req_period = strtol(argv[i+1], &hz, 10);
  152               if (strcmp(hz,"HZ")==0) req_period *= HZ;
  153               if (req_period < 1) {
  154                   print_usage(argv[ARG_NAME], "period is at least be one tick");
  155                   exit(EINVAL);
  156               }
  157           }
  158           else if (strcmp(argv[i], ARG_DEV)==0) {
  159               if (stat(argv[i+1], &stat_buf) == -1) {
  160                   print_usage(argv[ARG_NAME], "couldn't get status of device");
  161                   exit(errno);
  162               }
  163               if ( ! (stat_buf.st_mode & (S_IFBLK | S_IFCHR))) {
  164                   print_usage(argv[ARG_NAME], "special file is not a device");
  165                   exit(EINVAL);
  166               } 
  167               req_major = (stat_buf.st_rdev >> MAJOR) & BYTE;
  168           }
  169           else if (strcmp(argv[i], ARG_ARGS)==0) {
  170               req_priv = argv[i+1];
  171           }
  172           else {
  173               print_usage(argv[ARG_NAME], "unknown optional argument given");
  174               exit(EINVAL);
  175           }
  176       }
  177   }
  178   else if (req_nr == RS_DOWN || req_nr == RS_REFRESH) {
  179 
  180       /* Verify argument count. */ 
  181       if (argc - 1 < ARG_PID) {
  182           print_usage(argv[ARG_NAME], "action requires a pid to stop");
  183           exit(EINVAL);
  184       }
  185       if (! (req_pid = atoi(argv[ARG_PID])) > 0) {
  186           print_usage(argv[ARG_NAME], "pid must be greater than zero");
  187           exit(EINVAL);
  188       }
  189   } 
  190   else if (req_nr == RS_RESCUE) {
  191 
  192       /* Verify argument count. */ 
  193       if (argc - 1 < ARG_PATH) {
  194           print_usage(argv[ARG_NAME], "action requires rescue directory");
  195           exit(EINVAL);
  196       }
  197       req_path = argv[ARG_PATH];
  198       if (req_path[0] != '/') {
  199           print_usage(argv[ARG_NAME], "rescue dir should be absolute path");
  200           exit(EINVAL);
  201       }
  202       if (stat(argv[ARG_PATH], &stat_buf) == -1) {
  203           print_usage(argv[ARG_NAME], "couldn't get status of directory");
  204           exit(errno);
  205       }
  206       if ( ! (stat_buf.st_mode & S_IFDIR)) {
  207           print_usage(argv[ARG_NAME], "file is not a directory");
  208           exit(EINVAL);
  209       } 
  210   } 
  211   else if (req_nr == RS_SHUTDOWN) {
  212         /* no extra arguments required */
  213   }
  214 
  215   /* Return the request number if no error were found. */
  216   return(req_nr);
  217 }
  218 
  219 
  220 /* Main program. 
  221  */
  222 PUBLIC int main(int argc, char **argv)
  223 {
  224   message m;
  225   int result;
  226   int request;
  227   int s;
  228 
  229   /* Verify and parse the command line arguments. All arguments are checked
  230    * here. If an error occurs, the problem is reported and exit(2) is called. 
  231    * all needed parameters to perform the request are extracted and stored
  232    * global variables. 
  233    */
  234   request = parse_arguments(argc, argv);
  235 
  236   /* Arguments seem fine. Try to perform the request. Only valid requests 
  237    * should end up here. The default is used for not yet supported requests. 
  238    */
  239   switch(request) {
  240   case RS_UP:
  241       /* Build space-separated command string to be passed to RS server. */
  242       strcpy(command, req_path);
  243       command[strlen(req_path)] = ' ';
  244       strcpy(command+strlen(req_path)+1, req_args);
  245 
  246       /* Build request message and send the request. */
  247       m.RS_CMD_ADDR = command;
  248       m.RS_CMD_LEN = strlen(command);
  249       m.RS_DEV_MAJOR = req_major;
  250       m.RS_PERIOD = req_period;
  251       if (OK != (s=_taskcall(RS_PROC_NR, request, &m))) 
  252           failure(s);
  253       result = m.m_type;
  254       break;
  255   case RS_DOWN:
  256   case RS_REFRESH:
  257       m.RS_PID = req_pid;
  258       if (OK != (s=_taskcall(RS_PROC_NR, request, &m))) 
  259           failure(s);
  260       break;
  261   case RS_RESCUE:
  262       m.RS_CMD_ADDR = req_path;
  263       m.RS_CMD_LEN = strlen(req_path);
  264       if (OK != (s=_taskcall(RS_PROC_NR, request, &m))) 
  265           failure(s);
  266       break;
  267   case RS_SHUTDOWN:
  268       if (OK != (s=_taskcall(RS_PROC_NR, request, &m))) 
  269           failure(s);
  270       break;
  271   default:
  272       print_usage(argv[ARG_NAME], "request is not yet supported");
  273       result = EGENERIC;
  274   }
  275   return(result);
  276 }
  277 

Cache object: af02c7dadeabf75e52f8f65736dbe8e9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.