vm: fix potential null deref
[minix.git] / commands / svrctl / svrctl.c
blob6338281c368efc1dce81db444fdd36869c66cf55
1 #include <sys/svrctl.h>
2 #include <sys/types.h>
3 #include <ctype.h>
4 #include <lib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
10 static void usage(void);
12 #define VFS "vfs"
13 #define PM "pm"
14 #define SET "set"
15 #define GET "get"
17 static char *bin_name;
19 int main (int argc, char *argv[])
21 int r, param;
22 endpoint_t proc_e = NONE;
23 struct sysgetenv sysgetenv;
24 message m;
25 char *to_whom, *operation, *what, *value;
26 unsigned i;
28 bin_name = argv[0];
29 if (argc < 4 || argc > 5) usage();
30 if (geteuid() != 0) {
31 fprintf(stderr, "You have to be root to run this utility\n");
32 exit(EXIT_FAILURE);
35 /* Make some parameters lower case to ease comparing */
36 to_whom = argv[1];
37 operation = argv[2];
38 what = argv[3];
39 for (i = 0; i < strlen(to_whom); ++i) to_whom[i] = tolower(to_whom[i]);
40 for (i = 0; i < strlen(operation); ++i) operation[i] = tolower(operation[i]);
41 for (i = 0; i < strlen(what); ++i) what[i] = tolower(what[i]);
43 if (!strncmp(to_whom, VFS, strlen(VFS)+1)) proc_e = VFS_PROC_NR;
44 else if (!strncmp(to_whom, PM, strlen(PM)+1)) proc_e = PM_PROC_NR;
45 else usage();
47 sysgetenv.key = what;
48 sysgetenv.keylen = strlen(what) + 1;
50 if (!strncmp(operation, SET, strlen(SET)+1)) {
51 if (argc != 5) usage();
52 value = argv[4];
53 sysgetenv.val = value;
54 sysgetenv.vallen = strlen(value) + 1;
56 if (proc_e == VFS_PROC_NR)
57 param = VFSSETPARAM;
58 else if (proc_e == PM_PROC_NR)
59 param = PMSETPARAM;
60 else
61 usage();
63 m.m2_i1 = param;
64 m.m2_p1 = (char *) &sysgetenv;
65 r = _syscall(proc_e, SVRCTL, &m);
66 if (r != 0) {
67 if (errno == ESRCH)
68 fprintf(stderr, "invalid parameter: %s\n", what);
69 else if (errno == EINVAL)
70 fprintf(stderr, "invalid value: %s\n", value);
71 else
72 perror("");
73 exit(EXIT_FAILURE);
75 return(EXIT_SUCCESS);
76 } else if (!strncmp(operation, GET, strlen(GET)+1)) {
77 char get_param_buffer[4096];
79 memset(get_param_buffer, '\0', sizeof(get_param_buffer));
80 sysgetenv.val = get_param_buffer;
81 sysgetenv.vallen = sizeof(get_param_buffer) - 1;
83 if (proc_e == VFS_PROC_NR)
84 param = VFSGETPARAM;
85 else if (proc_e == PM_PROC_NR)
86 param = PMGETPARAM;
87 else
88 usage();
90 m.m2_i1 = param;
91 m.m2_p1 = (char *) &sysgetenv;
92 r = _syscall(proc_e, SVRCTL, &m);
93 if (r != 0) {
94 if (errno == ESRCH)
95 fprintf(stderr, "invalid parameter: %s\n", what);
96 else
97 perror("");
98 return(EXIT_FAILURE);
99 } else {
100 if (sysgetenv.vallen > 0) {
101 get_param_buffer[sysgetenv.vallen] = '\0';
102 printf("%s\n", get_param_buffer);
105 return(EXIT_SUCCESS);
106 } else
107 usage();
109 return(EXIT_FAILURE);
112 static void usage()
114 fprintf(stderr, "Usage:\n");
115 fprintf(stderr, " %s <vfs|pm> set <request> <value>\n", bin_name);
116 fprintf(stderr, " %s <vfs|pm> get <request>\n", bin_name);
117 exit(EXIT_FAILURE);