source: trunk/minix/servers/rs/service.c@ 9

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

Minix 3.1.2a

File size: 8.2 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 "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
35extern 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 */
57PRIVATE int req_type;
58PRIVATE int req_pid;
59PRIVATE char *req_path;
60PRIVATE char *req_args;
61PRIVATE int req_major;
62PRIVATE long req_period;
63PRIVATE char *req_priv;
64
65/* Buffer to build "/command arg1 arg2 ..." string to pass to RS server. */
66PRIVATE char command[4096];
67
68/* An error occurred. Report the problem, print the usage, and exit.
69 */
70PRIVATE 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 */
85PRIVATE 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 */
95PRIVATE 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 perror(req_path);
134 fprintf(stderr, "couldn't get stat binary\n");
135 exit(errno);
136 }
137 if (! (stat_buf.st_mode & S_IFREG)) {
138 print_usage(argv[ARG_NAME], "binary is not a regular file");
139 exit(EINVAL);
140 }
141
142 /* Check optional arguments that come in pairs like "-args arglist". */
143 for (i=MIN_ARG_COUNT+1; i<argc; i=i+2) {
144 if (! (i+1 < argc)) {
145 print_usage(argv[ARG_NAME], "optional argument not complete");
146 exit(EINVAL);
147 }
148 if (strcmp(argv[i], ARG_ARGS)==0) {
149 req_args = argv[i+1];
150 }
151 else if (strcmp(argv[i], ARG_PERIOD)==0) {
152 req_period = strtol(argv[i+1], &hz, 10);
153 if (strcmp(hz,"HZ")==0) req_period *= HZ;
154 if (req_period < 1) {
155 print_usage(argv[ARG_NAME], "period is at least be one tick");
156 exit(EINVAL);
157 }
158 }
159 else if (strcmp(argv[i], ARG_DEV)==0) {
160 if (stat(argv[i+1], &stat_buf) == -1) {
161 print_usage(argv[ARG_NAME], "couldn't get status of device");
162 exit(errno);
163 }
164 if ( ! (stat_buf.st_mode & (S_IFBLK | S_IFCHR))) {
165 print_usage(argv[ARG_NAME], "special file is not a device");
166 exit(EINVAL);
167 }
168 req_major = (stat_buf.st_rdev >> MAJOR) & BYTE;
169 }
170 else if (strcmp(argv[i], ARG_ARGS)==0) {
171 req_priv = argv[i+1];
172 }
173 else {
174 print_usage(argv[ARG_NAME], "unknown optional argument given");
175 exit(EINVAL);
176 }
177 }
178 }
179 else if (req_nr == RS_DOWN || req_nr == RS_REFRESH) {
180
181 /* Verify argument count. */
182 if (argc - 1 < ARG_PID) {
183 print_usage(argv[ARG_NAME], "action requires a pid to stop");
184 exit(EINVAL);
185 }
186 if (! (req_pid = atoi(argv[ARG_PID])) > 0) {
187 print_usage(argv[ARG_NAME], "pid must be greater than zero");
188 exit(EINVAL);
189 }
190 }
191 else if (req_nr == RS_RESCUE) {
192
193 /* Verify argument count. */
194 if (argc - 1 < ARG_PATH) {
195 print_usage(argv[ARG_NAME], "action requires rescue directory");
196 exit(EINVAL);
197 }
198 req_path = argv[ARG_PATH];
199 if (req_path[0] != '/') {
200 print_usage(argv[ARG_NAME], "rescue dir should be absolute path");
201 exit(EINVAL);
202 }
203 if (stat(argv[ARG_PATH], &stat_buf) == -1) {
204 print_usage(argv[ARG_NAME], "couldn't get status of directory");
205 exit(errno);
206 }
207 if ( ! (stat_buf.st_mode & S_IFDIR)) {
208 print_usage(argv[ARG_NAME], "file is not a directory");
209 exit(EINVAL);
210 }
211 }
212 else if (req_nr == RS_SHUTDOWN) {
213 /* no extra arguments required */
214 }
215
216 /* Return the request number if no error were found. */
217 return(req_nr);
218}
219
220
221/* Main program.
222 */
223PUBLIC int main(int argc, char **argv)
224{
225 message m;
226 int result;
227 int request;
228 int s;
229
230 /* Verify and parse the command line arguments. All arguments are checked
231 * here. If an error occurs, the problem is reported and exit(2) is called.
232 * all needed parameters to perform the request are extracted and stored
233 * global variables.
234 */
235 request = parse_arguments(argc, argv);
236
237 /* Arguments seem fine. Try to perform the request. Only valid requests
238 * should end up here. The default is used for not yet supported requests.
239 */
240 switch(request) {
241 case RS_UP:
242 /* Build space-separated command string to be passed to RS server. */
243 strcpy(command, req_path);
244 command[strlen(req_path)] = ' ';
245 strcpy(command+strlen(req_path)+1, req_args);
246
247 /* Build request message and send the request. */
248 m.RS_CMD_ADDR = command;
249 m.RS_CMD_LEN = strlen(command);
250 m.RS_DEV_MAJOR = req_major;
251 m.RS_PERIOD = req_period;
252 if (OK != (s=_taskcall(RS_PROC_NR, request, &m)))
253 failure(s);
254 result = m.m_type;
255 break;
256 case RS_DOWN:
257 case RS_REFRESH:
258 m.RS_PID = req_pid;
259 if (OK != (s=_taskcall(RS_PROC_NR, request, &m)))
260 failure(s);
261 break;
262 case RS_RESCUE:
263 m.RS_CMD_ADDR = req_path;
264 m.RS_CMD_LEN = strlen(req_path);
265 if (OK != (s=_taskcall(RS_PROC_NR, request, &m)))
266 failure(s);
267 break;
268 case RS_SHUTDOWN:
269 if (OK != (s=_taskcall(RS_PROC_NR, request, &m)))
270 failure(s);
271 break;
272 default:
273 print_usage(argv[ARG_NAME], "request is not yet supported");
274 result = EGENERIC;
275 }
276 return(result);
277}
278
Note: See TracBrowser for help on using the repository browser.