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