xref: /minix3/minix/commands/svrctl/svrctl.c (revision f737eea6368426d3302d0075f13f73c4bc646e24)
1433d6423SLionel Sambuc #define _MINIX_SYSTEM 1
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc #include <sys/svrctl.h>
4433d6423SLionel Sambuc #include <sys/types.h>
5433d6423SLionel Sambuc #include <ctype.h>
6433d6423SLionel Sambuc #include <lib.h>
7433d6423SLionel Sambuc #include <stdio.h>
8433d6423SLionel Sambuc #include <stdlib.h>
9433d6423SLionel Sambuc #include <string.h>
10433d6423SLionel Sambuc #include <unistd.h>
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc static void usage(void);
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc #define VFS			"vfs"
15433d6423SLionel Sambuc #define PM			"pm"
16433d6423SLionel Sambuc #define SET			"set"
17433d6423SLionel Sambuc #define GET			"get"
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc static char *bin_name;
20433d6423SLionel Sambuc 
main(int argc,char * argv[])21433d6423SLionel Sambuc int main (int argc, char *argv[])
22433d6423SLionel Sambuc {
23*f737eea6SDavid van Moolenbroek   unsigned long param;
24433d6423SLionel Sambuc   endpoint_t proc_e = NONE;
25433d6423SLionel Sambuc   struct sysgetenv sysgetenv;
26433d6423SLionel Sambuc   char *to_whom, *operation, *what, *value;
27433d6423SLionel Sambuc   unsigned i;
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc   bin_name = argv[0];
30433d6423SLionel Sambuc   if (argc < 4 || argc > 5) usage();
31433d6423SLionel Sambuc   if (geteuid() != 0) {
32433d6423SLionel Sambuc 	fprintf(stderr, "You have to be root to run this utility\n");
33433d6423SLionel Sambuc 	exit(EXIT_FAILURE);
34433d6423SLionel Sambuc   }
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc   /* Make some parameters lower case to ease comparing */
37433d6423SLionel Sambuc   to_whom = argv[1];
38433d6423SLionel Sambuc   operation = argv[2];
39433d6423SLionel Sambuc   what = argv[3];
40433d6423SLionel Sambuc   for (i = 0; i < strlen(to_whom); ++i)   to_whom[i] = tolower(to_whom[i]);
41433d6423SLionel Sambuc   for (i = 0; i < strlen(operation); ++i) operation[i] = tolower(operation[i]);
42433d6423SLionel Sambuc   for (i = 0; i < strlen(what); ++i)      what[i] = tolower(what[i]);
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc   if (!strncmp(to_whom, VFS, strlen(VFS)+1)) proc_e = VFS_PROC_NR;
45433d6423SLionel Sambuc   else if (!strncmp(to_whom, PM, strlen(PM)+1)) proc_e = PM_PROC_NR;
46433d6423SLionel Sambuc   else usage();
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc   sysgetenv.key = what;
49433d6423SLionel Sambuc   sysgetenv.keylen = strlen(what) + 1;
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc   if (!strncmp(operation, SET, strlen(SET)+1)) {
52433d6423SLionel Sambuc 	if (argc != 5) usage();
53433d6423SLionel Sambuc 	value = argv[4];
54433d6423SLionel Sambuc 	sysgetenv.val = value;
55433d6423SLionel Sambuc 	sysgetenv.vallen = strlen(value) + 1;
56433d6423SLionel Sambuc 
57433d6423SLionel Sambuc 	if (proc_e == VFS_PROC_NR)
58433d6423SLionel Sambuc 		param = VFSSETPARAM;
59433d6423SLionel Sambuc 	else if (proc_e == PM_PROC_NR)
60433d6423SLionel Sambuc 		param = PMSETPARAM;
61433d6423SLionel Sambuc 	else
62433d6423SLionel Sambuc 		usage();
63433d6423SLionel Sambuc 
64433d6423SLionel Sambuc 	if (svrctl(param, &sysgetenv) != 0) {
65433d6423SLionel Sambuc 		if (errno == ESRCH)
66433d6423SLionel Sambuc 			fprintf(stderr, "invalid parameter: %s\n", what);
67433d6423SLionel Sambuc 		else if (errno == EINVAL)
68433d6423SLionel Sambuc 			fprintf(stderr, "invalid value: %s\n", value);
69433d6423SLionel Sambuc 		else
70433d6423SLionel Sambuc 			perror("");
71433d6423SLionel Sambuc 		exit(EXIT_FAILURE);
72433d6423SLionel Sambuc 	}
73433d6423SLionel Sambuc 	return(EXIT_SUCCESS);
74433d6423SLionel Sambuc   } else if (!strncmp(operation, GET, strlen(GET)+1)) {
75433d6423SLionel Sambuc 	char get_param_buffer[4096];
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc 	memset(get_param_buffer, '\0', sizeof(get_param_buffer));
78433d6423SLionel Sambuc 	sysgetenv.val = get_param_buffer;
79433d6423SLionel Sambuc 	sysgetenv.vallen = sizeof(get_param_buffer) - 1;
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc 	if (proc_e == VFS_PROC_NR)
82433d6423SLionel Sambuc 		param = VFSGETPARAM;
83433d6423SLionel Sambuc 	else if (proc_e == PM_PROC_NR)
84433d6423SLionel Sambuc 		param = PMGETPARAM;
85433d6423SLionel Sambuc 	else
86433d6423SLionel Sambuc 		usage();
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc 	if (svrctl(param, &sysgetenv) != 0) {
89433d6423SLionel Sambuc 		if (errno == ESRCH)
90433d6423SLionel Sambuc 			fprintf(stderr, "invalid parameter: %s\n", what);
91433d6423SLionel Sambuc 		else
92433d6423SLionel Sambuc 			perror("");
93433d6423SLionel Sambuc 		return(EXIT_FAILURE);
94433d6423SLionel Sambuc 	} else {
95433d6423SLionel Sambuc 		if (sysgetenv.vallen > 0) {
96433d6423SLionel Sambuc 			get_param_buffer[sysgetenv.vallen] = '\0';
97433d6423SLionel Sambuc 			printf("%s\n", get_param_buffer);
98433d6423SLionel Sambuc 		}
99433d6423SLionel Sambuc 	}
100433d6423SLionel Sambuc 	return(EXIT_SUCCESS);
101433d6423SLionel Sambuc   } else
102433d6423SLionel Sambuc 	usage();
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc   return(EXIT_FAILURE);
105433d6423SLionel Sambuc }
106433d6423SLionel Sambuc 
usage()107433d6423SLionel Sambuc static void usage()
108433d6423SLionel Sambuc {
109433d6423SLionel Sambuc   fprintf(stderr, "Usage:\n");
110433d6423SLionel Sambuc   fprintf(stderr, "  %s <vfs|pm> set <request> <value>\n", bin_name);
111433d6423SLionel Sambuc   fprintf(stderr, "  %s <vfs|pm> get <request>\n", bin_name);
112433d6423SLionel Sambuc   exit(EXIT_FAILURE);
113433d6423SLionel Sambuc }
114