Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/procps/sysctl.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 983 by niro, Fri Apr 24 18:33:46 2009 UTC revision 984 by niro, Sun May 30 11:32:42 2010 UTC
# Line 7  Line 7 
7   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8   *   *
9   * Changelog:   * Changelog:
10   * v1.01:   * v1.01   - added -p <preload> to preload values from a file
11   * - added -p <preload> to preload values from a file   * v1.01.1 - busybox applet aware by <solar@gentoo.org>
  * v1.01.1  
  * - busybox applet aware by <solar@gentoo.org>  
  *  
12   */   */
13    
14  #include "libbb.h"  #include "libbb.h"
15    
 static int sysctl_read_setting(const char *setting);  
 static int sysctl_write_setting(const char *setting);  
 static int sysctl_display_all(const char *path);  
 static int sysctl_preload_file_and_exit(const char *filename);  
 static void sysctl_dots_to_slashes(char *name);  
   
 static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";  
 static const char PROC_SYS[] ALIGN1 = "/proc/sys/";  
 enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };  
   
 static const char msg_unknown_key[] ALIGN1 =  
  "error: '%s' is an unknown key";  
   
 static void dwrite_str(int fd, const char *buf)  
 {  
  write(fd, buf, strlen(buf));  
 }  
   
16  enum {  enum {
17   FLAG_SHOW_KEYS       = 1 << 0,   FLAG_SHOW_KEYS       = 1 << 0,
18   FLAG_SHOW_KEY_ERRORS = 1 << 1,   FLAG_SHOW_KEY_ERRORS = 1 << 1,
# Line 42  enum { Line 21  enum {
21   FLAG_PRELOAD_FILE    = 1 << 4,   FLAG_PRELOAD_FILE    = 1 << 4,
22   FLAG_WRITE           = 1 << 5,   FLAG_WRITE           = 1 << 5,
23  };  };
24    #define OPTION_STR "neAapw"
25    
26  int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;  static void sysctl_dots_to_slashes(char *name)
 int sysctl_main(int argc UNUSED_PARAM, char **argv)  
27  {  {
28   int retval;   char *cptr, *last_good, *end;
  int opt;  
   
  opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */  
  argv += optind;  
  opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);  
  option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);  
29    
30   if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))   /* Convert minimum number of '.' to '/' so that
31   return sysctl_display_all(PROC_SYS);   * we end up with existing file's name.
32   if (opt & FLAG_PRELOAD_FILE)   *
33   return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);   * Example from bug 3894:
34     * net.ipv4.conf.eth0.100.mc_forwarding ->
35     * net/ipv4/conf/eth0.100/mc_forwarding
36     * NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
37     * therefore we must start from the end, and if
38     * we replaced even one . -> /, start over again,
39     * but never replace dots before the position
40     * where last replacement occurred.
41     *
42     * Another bug we later had is that
43     * net.ipv4.conf.eth0.100
44     * (without .mc_forwarding) was mishandled.
45     *
46     * To set up testing: modprobe 8021q; vconfig add eth0 100
47     */
48     end = name + strlen(name);
49     last_good = name - 1;
50     *end = '.'; /* trick the loop into trying full name too */
51    
52   retval = 0;   again:
53   while (*argv) {   cptr = end;
54   if (opt & FLAG_WRITE)   while (cptr > last_good) {
55   retval |= sysctl_write_setting(*argv);   if (*cptr == '.') {
56   else   *cptr = '\0';
57   retval |= sysctl_read_setting(*argv);   //bb_error_msg("trying:'%s'", name);
58   argv++;   if (access(name, F_OK) == 0) {
59     if (cptr != end) /* prevent trailing '/' */
60     *cptr = '/';
61     //bb_error_msg("replaced:'%s'", name);
62     last_good = cptr;
63     goto again;
64     }
65     *cptr = '.';
66     }
67     cptr--;
68   }   }
69     *end = '\0';
70    }
71    
72   return retval;  static int sysctl_act_on_setting(char *setting)
 } /* end sysctl_main() */  
   
 /* Set sysctl's from a conf file. Format example:  
  * # Controls IP packet forwarding  
  * net.ipv4.ip_forward = 0  
  */  
 static int sysctl_preload_file_and_exit(const char *filename)  
73  {  {
74   char *token[2];   int fd, retval = EXIT_SUCCESS;
75   parser_t *parser;   char *cptr, *outname;
76     char *value = value; /* for compiler */
77    
78   parser = config_open(filename);   outname = xstrdup(setting);
 // TODO: ';' is comment char too  
  while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {  
 #if 0  
  char *s = xasprintf("%s=%s", token[0], token[1]);  
  sysctl_write_setting(s);  
  free(s);  
 #else /* Save ~4 bytes by using parser internals */  
  sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition  
  sysctl_write_setting(parser->line);  
 #endif  
  }  
  if (ENABLE_FEATURE_CLEAN_UP)  
  config_close(parser);  
  return 0;  
 } /* end sysctl_preload_file_and_exit() */  
79    
80  static int sysctl_write_setting(const char *setting)   cptr = outname;
81  {   while (*cptr) {
82   int retval;   if (*cptr == '/')
83   const char *name;   *cptr = '.';
84   const char *value;   cptr++;
  const char *equals;  
  char *tmpname, *outname, *cptr;  
  int fd;  
   
  name = setting;  
  equals = strchr(setting, '=');  
  if (!equals) {  
  bb_error_msg("error: '%s' must be of the form name=value", setting);  
  return EXIT_FAILURE;  
85   }   }
86    
87   value = equals + 1; /* point to the value in name=value */   if (option_mask32 & FLAG_WRITE) {
88   if (name == equals || !*value) {   cptr = strchr(setting, '=');
89   bb_error_msg("error: malformed setting '%s'", setting);   if (cptr == NULL) {
90   return EXIT_FAILURE;   bb_error_msg("error: '%s' must be of the form name=value",
91     outname);
92     retval = EXIT_FAILURE;
93     goto end;
94     }
95     value = cptr + 1; /* point to the value in name=value */
96     if (setting == cptr || !*value) {
97     bb_error_msg("error: malformed setting '%s'", outname);
98     retval = EXIT_FAILURE;
99     goto end;
100     }
101     *cptr = '\0';
102     outname[cptr - setting] = '\0';
103     /* procps 3.2.7 actually uses these flags */
104     fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
105     } else {
106     fd = open(setting, O_RDONLY);
107   }   }
108    
  tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);  
  outname = xstrdup(tmpname + strlen_PROC_SYS);  
   
  sysctl_dots_to_slashes(tmpname);  
   
  while ((cptr = strchr(outname, '/')) != NULL)  
  *cptr = '.';  
   
  fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);  
109   if (fd < 0) {   if (fd < 0) {
110   switch (errno) {   switch (errno) {
111   case ENOENT:   case ENOENT:
112   if (option_mask32 & FLAG_SHOW_KEY_ERRORS)   if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
113   bb_error_msg(msg_unknown_key, outname);   bb_error_msg("error: '%s' is an unknown key", outname);
114   break;   break;
115   default:   default:
116   bb_perror_msg("error setting key '%s'", outname);   bb_perror_msg("error %sing key '%s'",
117     option_mask32 & FLAG_WRITE ?
118     "sett" : "read",
119     outname);
120   break;   break;
121   }   }
122   retval = EXIT_FAILURE;   retval = EXIT_FAILURE;
123   } else {   goto end;
124   dwrite_str(fd, value);   }
125    
126     if (option_mask32 & FLAG_WRITE) {
127    //TODO: procps 3.2.7 writes "value\n", note trailing "\n"
128     xwrite_str(fd, value);
129   close(fd);   close(fd);
130   if (option_mask32 & FLAG_SHOW_KEYS) {   if (option_mask32 & FLAG_SHOW_KEYS)
131   printf("%s = ", outname);   printf("%s = ", outname);
  }  
132   puts(value);   puts(value);
133   retval = EXIT_SUCCESS;   } else {
134   }   char c;
   
  free(tmpname);  
  free(outname);  
  return retval;  
 } /* end sysctl_write_setting() */  
   
 static int sysctl_read_setting(const char *name)  
 {  
  int retval;  
  char *tmpname, *outname, *cptr;  
  char inbuf[1025];  
  FILE *fp;  
   
  if (!*name) {  
  if (option_mask32 & FLAG_SHOW_KEY_ERRORS)  
  bb_error_msg(msg_unknown_key, name);  
  return -1;  
  }  
   
  tmpname = concat_path_file(PROC_SYS, name);  
  outname = xstrdup(tmpname + strlen_PROC_SYS);  
   
  sysctl_dots_to_slashes(tmpname);  
   
  while ((cptr = strchr(outname, '/')) != NULL)  
  *cptr = '.';  
135    
136   fp = fopen_for_read(tmpname);   value = cptr = xmalloc_read(fd, NULL);
137   if (fp == NULL) {   close(fd);
138   switch (errno) {   if (value == NULL) {
  case ENOENT:  
  if (option_mask32 & FLAG_SHOW_KEY_ERRORS)  
  bb_error_msg(msg_unknown_key, outname);  
  break;  
  default:  
139   bb_perror_msg("error reading key '%s'", outname);   bb_perror_msg("error reading key '%s'", outname);
140   break;   goto end;
141   }   }
142   retval = EXIT_FAILURE;  
143   } else {   /* dev.cdrom.info and sunrpc.transports, for example,
144   while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {   * are multi-line. Try "sysctl sunrpc.transports"
145   if (option_mask32 & FLAG_SHOW_KEYS) {   */
146     while ((c = *cptr) != '\0') {
147     if (option_mask32 & FLAG_SHOW_KEYS)
148   printf("%s = ", outname);   printf("%s = ", outname);
149     while (1) {
150     fputc(c, stdout);
151     cptr++;
152     if (c == '\n')
153     break;
154     c = *cptr;
155     if (c == '\0')
156     break;
157   }   }
  fputs(inbuf, stdout);  
158   }   }
159   fclose(fp);   free(value);
  retval = EXIT_SUCCESS;  
160   }   }
161     end:
  free(tmpname);  
162   free(outname);   free(outname);
163   return retval;   return retval;
164  } /* end sysctl_read_setting() */  }
165    
166  static int sysctl_display_all(const char *path)  static int sysctl_act_recursive(const char *path)
167  {  {
168   int retval = EXIT_SUCCESS;   DIR *dirp;
169   DIR *dp;   struct stat buf;
170   struct dirent *de;   struct dirent *entry;
171   char *tmpdir;   char *next;
172   struct stat ts;   int retval = 0;
173    
174   dp = opendir(path);   stat(path, &buf);
175   if (!dp) {   if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
176   return EXIT_FAILURE;   dirp = opendir(path);
177   }   if (dirp == NULL)
178   while ((de = readdir(dp)) != NULL) {   return -1;
179   tmpdir = concat_subpath_file(path, de->d_name);   while ((entry = readdir(dirp)) != NULL) {
180   if (tmpdir == NULL)   next = concat_subpath_file(path, entry->d_name);
181   continue; /* . or .. */   if (next == NULL)
182   if (stat(tmpdir, &ts) != 0) {   continue; /* d_name is "." or ".." */
183   bb_perror_msg(tmpdir);   /* if path was ".", drop "./" prefix: */
184   } else if (S_ISDIR(ts.st_mode)) {   retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
185   retval |= sysctl_display_all(tmpdir);      next + 2 : next);
186   } else {   free(next);
  retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);  
187   }   }
188   free(tmpdir);   closedir(dirp);
189   } /* end while */   } else {
190   closedir(dp);   char *name = xstrdup(path);
191     retval |= sysctl_act_on_setting(name);
192     free(name);
193     }
194    
195   return retval;   return retval;
196  } /* end sysctl_display_all() */  }
197    
198  static void sysctl_dots_to_slashes(char *name)  /* Set sysctl's from a conf file. Format example:
199     * # Controls IP packet forwarding
200     * net.ipv4.ip_forward = 0
201     */
202    static int sysctl_handle_preload_file(const char *filename)
203  {  {
204   char *cptr, *last_good, *end;   char *token[2];
205     parser_t *parser;
206    
207   /* It can be good as-is! */   parser = config_open(filename);
208   if (access(name, F_OK) == 0)   /* Must do it _after_ config_open(): */
209   return;   xchdir("/proc/sys");
210     /* xchroot(".") - if you are paranoid */
211    
212    //TODO: ';' is comment char too
213    //TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
214    // (but _whitespace_ from ends should be trimmed first (and we do it right))
215    //TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
216     while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
217     char *tp;
218     sysctl_dots_to_slashes(token[0]);
219     tp = xasprintf("%s=%s", token[0], token[1]);
220     sysctl_act_recursive(tp);
221     free(tp);
222     }
223     if (ENABLE_FEATURE_CLEAN_UP)
224     config_close(parser);
225     return 0;
226    }
227    
228   /* Example from bug 3894:  int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
229   * net.ipv4.conf.eth0.100.mc_forwarding ->  int sysctl_main(int argc UNUSED_PARAM, char **argv)
230   * net/ipv4/conf/eth0.100/mc_forwarding. NB:  {
231   * net/ipv4/conf/eth0/mc_forwarding *also exists*,   int retval;
232   * therefore we must start from the end, and if   int opt;
233   * we replaced even one . -> /, start over again,  
234   * but never replace dots before the position   opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
235   * where replacement occurred. */   argv += optind;
236   end = name + strlen(name) - 1;   opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
237   last_good = name - 1;   option_mask32 = opt;
238   again:  
239   cptr = end;   if (opt & FLAG_PRELOAD_FILE) {
240   while (cptr > last_good) {   option_mask32 |= FLAG_WRITE;
241   if (*cptr == '.') {   /* xchdir("/proc/sys") is inside */
242   *cptr = '\0';   return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
243   if (access(name, F_OK) == 0) {   }
244   *cptr = '/';   xchdir("/proc/sys");
245   last_good = cptr;   /* xchroot(".") - if you are paranoid */
246   goto again;   if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
247   }   return sysctl_act_recursive(".");
  *cptr = '.';  
  }  
  cptr--;  
248   }   }
249  } /* end sysctl_dots_to_slashes() */  
250     retval = 0;
251     while (*argv) {
252     sysctl_dots_to_slashes(*argv);
253     retval |= sysctl_act_recursive(*argv);
254     argv++;
255     }
256    
257     return retval;
258    }

Legend:
Removed from v.983  
changed lines
  Added in v.984