Add support for setting sysctl values in the configuration.
[util-vserver.git] / src / vsysctl.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2007 Daniel Hokka Zakrisson
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24 #include <lib/internal.h>
25
26 #include <vserver.h>
27
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <dirent.h>
34
35 #define ENSC_WRAPPERS_PREFIX    "vsysctl: "
36 #define ENSC_WRAPPERS_UNISTD    1
37 #define ENSC_WRAPPERS_FCNTL     1
38 #define ENSC_WRAPPERS_DIRENT    1
39 #define ENSC_WRAPPERS_VSERVER   1
40 #define ENSC_WRAPPERS_IO        1
41 #include <wrappers.h>
42
43
44 #define PROC_SYS_DIRECTORY      "/proc/sys"
45
46
47 #define CMD_HELP                0x1000
48 #define CMD_VERSION             0x1001
49 #define CMD_XID                 0x4000
50 #define CMD_DIR                 0x4001
51
52 int             wrapper_exit_code  =  1;
53
54 struct option const
55 CMDLINE_OPTIONS[] = {
56   { "help",       no_argument,       0, CMD_HELP },
57   { "version",    no_argument,       0, CMD_VERSION },
58   { "xid",        required_argument, 0, CMD_XID },
59   { "dir",        required_argument, 0, CMD_DIR },
60   {0,0,0,0}
61 };
62
63 static void
64 showHelp(int fd, char const *cmd)
65 {
66   WRITE_MSG(fd, "Usage: ");
67   WRITE_STR(fd, cmd);
68   WRITE_MSG(fd,
69             " --xid <xid> --dir <directory> -- <command> <args>*\n"
70             "\n"
71             "Please report bugs to " PACKAGE_BUGREPORT "\n");
72
73   exit(0);
74 }
75
76 static void
77 showVersion()
78 {
79   WRITE_MSG(1,
80             "vsysctl " VERSION " -- sets sysctl values during guest boot\n"
81             "This program is part of " PACKAGE_STRING "\n\n"
82             "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
83             VERSION_COPYRIGHT_DISCLAIMER);
84   exit(0);
85 }
86
87 void handle_setting(const char *dir, const char *name)
88 {
89   int   len_dir = strlen(dir), len_name = strlen(name);
90   char  filename[len_dir+1+len_name+sizeof("/setting")];
91   char  setting[128], value[128], *ptr;
92   int   fd;
93   size_t setting_len, value_len;
94
95   strcpy(filename, dir);
96   *(filename+len_dir) = '/';
97   strcpy(filename+len_dir+1, name);
98
99 #define READFILE(f) \
100   strcpy(filename+len_dir+1+len_name, "/" #f); \
101   fd = EopenD(filename, O_RDONLY, 0); \
102   f##_len = Eread(fd, f, sizeof(f)); \
103   if (f##_len == sizeof(f)) { \
104     errno = EOVERFLOW; \
105     perror(ENSC_WRAPPERS_PREFIX "read"); \
106     exit(EXIT_FAILURE); \
107   } \
108   f[f##_len] = '\0'; \
109   Eclose(fd);
110
111   READFILE(setting);
112   READFILE(value);
113
114   /* replace all . with / in setting to get a filename */
115   for (ptr = strchr(setting, '.'); ptr; ptr = strchr(ptr, '.'))
116     *ptr = '/';
117
118   /* we just want the first line, and not the linefeed */
119   if ((ptr = strchr(setting, '\n')) == NULL)
120     *ptr = '\0';
121
122   fd = EopenD(setting, O_WRONLY, 0);
123   EwriteAll(fd, value, value_len);
124   Eclose(fd);
125 }
126
127 int main(int argc, char *argv[])
128 {
129   xid_t         xid     = VC_NOCTX;
130   const char    *dir    = NULL;
131   
132   while (1) {
133     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
134     if (c==-1) break;
135
136     switch (c) {
137       case CMD_HELP     :  showHelp(1, argv[0]);
138       case CMD_VERSION  :  showVersion();
139       case CMD_XID      :  xid = Evc_xidopt2xid(optarg, true);  break;
140       case CMD_DIR      :  dir = optarg;                        break;
141
142       default           :
143         WRITE_MSG(2, "Try '");
144         WRITE_STR(2, argv[0]);
145         WRITE_MSG(2, " --help' for more information.\n");
146         return EXIT_FAILURE;
147         break;
148     }
149   }
150
151   if (dir != NULL) {
152     int           curdir = EopenD(".", O_RDONLY, 0);
153     DIR           *dp;
154     struct dirent *de;
155
156     Echdir(PROC_SYS_DIRECTORY);
157
158     dp = Eopendir(dir);
159     while ((de = Ereaddir(dp)) != NULL) {
160       if (*de->d_name == '.')
161         continue;
162       handle_setting(dir, de->d_name);
163     }
164     Eclosedir(dp);
165
166     Efchdir(curdir);
167   }
168
169   Eexecvp(argv[optind], argv+optind);
170   return EXIT_FAILURE;
171 }