source: branches/minix3-book/servers/rs/service.c@ 4

Last change on this file since 4 was 4, checked in by Mattia Monga, 13 years ago

Importazione sorgenti libro

File size: 5.4 KB
Line 
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. */
23PRIVATE char *known_requests[] = {
24 "up",
25 "down",
26 "catch for illegal requests"
27};
28#define ILLEGAL_REQUEST sizeof(known_requests)/sizeof(char *)
29
30/* Global error number set for failed system calls. */
31#define OK 0
32extern int errno;
33
34/* Define names for arguments provided to this utility. The first few
35 * arguments are required and have a known index. Thereafter, some optional
36 * argument pairs like "-args arglist" follow.
37 */
38#define ARG_NAME 0 /* own application name */
39#define ARG_REQUEST 1 /* request to perform */
40#define ARG_PATH 2 /* binary of system service */
41
42#define MIN_ARG_COUNT 3 /* minimum number of arguments */
43
44#define ARG_ARGS "-args" /* list of arguments to be passed */
45#define ARG_DEV "-dev" /* major device number for drivers */
46#define ARG_PRIV "-priv" /* required privileges */
47
48/* The function parse_arguments() verifies and parses the command line
49 * parameters passed to this utility. Request parameters that are needed
50 * are stored globally in the following variables:
51 */
52PRIVATE int req_type;
53PRIVATE char *req_path;
54PRIVATE char *req_args;
55PRIVATE int req_major;
56PRIVATE char *req_priv;
57
58/* An error occurred. Report the problem, print the usage, and exit.
59 */
60PRIVATE void print_usage(char *app_name, char *problem)
61{
62 printf("Warning, %s\n", problem);
63 printf("Usage:\n");
64 printf(" %s <request> <binary> [%s <args>] [%s <special>]\n",
65 app_name, ARG_ARGS, ARG_DEV);
66 printf("\n");
67}
68
69/* An unexpected, unrecoverable error occurred. Report and exit.
70 */
71PRIVATE void panic(char *app_name, char *mess, int num)
72{
73 printf("Panic in %s: %s", app_name, mess);
74 if (num != NO_NUM) printf(": %d", num);
75 printf("\n");
76 exit(EGENERIC);
77}
78
79
80/* Parse and verify correctness of arguments. Report problem and exit if an
81 * error is found. Store needed parameters in global variables.
82 */
83PRIVATE int parse_arguments(int argc, char **argv)
84{
85 struct stat stat_buf;
86 int i;
87
88 /* Verify argument count. */
89 if (! argc >= MIN_ARG_COUNT) {
90 print_usage(argv[ARG_NAME], "wrong number of arguments");
91 exit(EINVAL);
92 }
93
94 /* Verify request type. */
95 for (req_type=0; req_type< ILLEGAL_REQUEST; req_type++) {
96 if (strcmp(known_requests[req_type],argv[ARG_REQUEST])==0) break;
97 }
98 if (req_type == ILLEGAL_REQUEST) {
99 print_usage(argv[ARG_NAME], "illegal request type");
100 exit(ENOSYS);
101 }
102
103 /* Verify the name of the binary of the system service. */
104 req_path = argv[ARG_PATH];
105 if (req_path[0] != '/') {
106 print_usage(argv[ARG_NAME], "binary should be absolute path");
107 exit(EINVAL);
108 }
109 if (stat(req_path, &stat_buf) == -1) {
110 print_usage(argv[ARG_NAME], "couldn't get status of binary");
111 exit(errno);
112 }
113 if (! (stat_buf.st_mode & S_IFREG)) {
114 print_usage(argv[ARG_NAME], "binary is not a regular file");
115 exit(EINVAL);
116 }
117
118 /* Check optional arguments that come in pairs like "-args arglist". */
119 for (i=MIN_ARG_COUNT; i<argc; i=i+2) {
120 if (! (i+1 < argc)) {
121 print_usage(argv[ARG_NAME], "optional argument not complete");
122 exit(EINVAL);
123 }
124 if (strcmp(argv[i], ARG_ARGS)==0) {
125 req_args = argv[i+1];
126 }
127 else if (strcmp(argv[i], ARG_DEV)==0) {
128 if (stat(argv[i+1], &stat_buf) == -1) {
129 print_usage(argv[ARG_NAME], "couldn't get status of device node");
130 exit(errno);
131 }
132 if ( ! (stat_buf.st_mode & (S_IFBLK | S_IFCHR))) {
133 print_usage(argv[ARG_NAME], "special file is not a device node");
134 exit(EINVAL);
135 }
136 req_major = (stat_buf.st_rdev >> MAJOR) & BYTE;
137 }
138 else if (strcmp(argv[i], ARG_ARGS)==0) {
139 req_priv = argv[i+1];
140 }
141 else {
142 print_usage(argv[ARG_NAME], "unknown optional argument given");
143 exit(EINVAL);
144 }
145 }
146
147 /* Return the request number if no error were found. */
148 return(i);
149}
150
151
152/* Main program.
153 */
154PUBLIC int main(int argc, char **argv)
155{
156 message m;
157 int result;
158 int s;
159
160 /* Verify and parse the command line arguments. All arguments are checked
161 * here. If an error occurs, the problem is reported and exit(2) is called.
162 * all needed parameters to perform the request are extracted and stored
163 * global variables.
164 */
165 parse_arguments(argc, argv);
166
167 /* Arguments seem fine. Try to perform the request. Only valid requests
168 * should end up here. The default is used for not yet supported requests.
169 */
170 switch(req_type+SRV_RQ_BASE) {
171 case SRV_UP:
172 m.SRV_PATH_ADDR = req_path;
173 m.SRV_PATH_LEN = strlen(req_path);
174 m.SRV_ARGS_ADDR = req_args;
175 m.SRV_ARGS_LEN = strlen(req_args);
176 m.SRV_DEV_MAJOR = req_major;
177 if (OK != (s=_taskcall(RS_PROC_NR, SRV_UP, &m)))
178 panic(argv[ARG_NAME], "sendrec to manager server failed", s);
179 result = m.m_type;
180 break;
181 case SRV_DOWN:
182 case SRV_STATUS:
183 default:
184 print_usage(argv[ARG_NAME], "request is not yet supported");
185 result = EGENERIC;
186 }
187 return(result);
188}
189
Note: See TracBrowser for help on using the repository browser.