1 /*
2  *  Author:  Daniel De Graaf <dgdegra@tycho.nsa.gov>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 2,
6  *  as published by the Free Software Foundation.
7  */
8 
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <xenctrl.h>
13 #include <fcntl.h>
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <inttypes.h>
19 
usage(char ** argv)20 static void usage(char **argv)
21 {
22 	fprintf(stderr, "Usage: %s name value\n", argv[0]);
23 	exit(1);
24 }
25 
str2bool(const char * str)26 static int str2bool(const char *str)
27 {
28 	if (str[0] == '0' || str[0] == '1')
29 		return (str[0] == '1');
30 	if (!strcasecmp(str, "enabled") || !strcasecmp(str, "on") || !strcasecmp(str, "y"))
31 		return 1;
32 	if (!strcasecmp(str, "disabled") || !strcasecmp(str, "off") || !strcasecmp(str, "n"))
33 		return 0;
34 	fprintf(stderr, "Unknown value %s\n", str);
35 	exit(1);
36 }
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40 	int err = 0;
41 	xc_interface *xch;
42 	int value;
43 
44 	if (argc != 3)
45 		usage(argv);
46 
47 	value = str2bool(argv[2]);
48 
49 	xch = xc_interface_open(0,0,0);
50 	if ( !xch )
51 	{
52 		fprintf(stderr, "Unable to create interface to xenctrl: %s\n",
53 				strerror(errno));
54 		err = 1;
55 		goto done;
56 	}
57 
58 	err = xc_flask_setbool(xch, argv[1], value, 1);
59 	if (err) {
60 		fprintf(stderr, "xc_flask_setbool: Unable to set boolean %s=%s: %s (%d)",
61 			argv[1], argv[2], strerror(errno), err);
62 		err = 2;
63 		goto done;
64 	}
65 
66  done:
67 	if ( xch )
68 		xc_interface_close(xch);
69 
70 	return err;
71 }
72