]> granicus.if.org Git - procps-ng/commitdiff
sysctl: restore old -p handling
authorMike Frysinger <vapier@gentoo.org>
Thu, 3 May 2012 05:24:20 +0000 (01:24 -0400)
committerSami Kerola <kerolasa@iki.fi>
Mon, 7 May 2012 07:01:10 +0000 (09:01 +0200)
The previous version of sysctl had the form:
sysctl -p [file]
In other words, it required a space between the -p and the [file].
Omitting the space would lead to an error.

The new version though is the opposite:
sysctl -p[file]
In other words, it requires there to not be a space.

Considering the old behavior has been around for a decade, and runtime
checking for this mismatch in behavior is silly, and supporting the old
syntax is trivial, add support for it.

When '-p regexp' is glob is used to make reqular expression to be
expanded to argument list, which also means that -p option will
allow multiple files being specified as input.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
sysctl.8
sysctl.c

index e1f6cb0724034795183dec58bc5d50f1ceb487fd..fc1c9dd3d04a827e80da2df493ece8ce98bdf9a1 100644 (file)
--- a/sysctl.8
+++ b/sysctl.8
@@ -12,6 +12,8 @@ sysctl \- configure kernel parameters at runtime
 .SH SYNOPSIS
 .B sysctl
 [\fIoptions\fR] [\fIvariable\fR[\fI=value\fR]] [...]
+.BR
+.B sysctl \-p [file or regexp] [...]
 .SH DESCRIPTION
 .B sysctl
 is used to modify kernel parameters at runtime.  The parameters available
@@ -58,6 +60,10 @@ Use this option when you want to change a sysctl setting.
 \fB\-p\fR[\fIFILE\fR], \fB\-\-load\fR[=\fIFILE\fR]
 Load in sysctl settings from the file specified or /etc/sysctl.conf if none
 given.  Specifying \- as filename means reading data from standard input.
+Using this option will mean arguments to
+.B sysctl
+are files, which are read in order they are specified.  The file argument can
+may be specified as reqular expression.
 .TP
 \fB\-a\fR, \fB\-\-all\fR
 Display all values currently available.
index 587e26d967ee94b0a58d995b53abb13b35c66c68..92351706daff6f52b38bd4ee177078f2a862d36e 100644 (file)
--- a/sysctl.c
+++ b/sysctl.c
@@ -29,6 +29,7 @@
 #include <dirent.h>
 #include <errno.h>
 #include <getopt.h>
+#include <glob.h>
 #include <libgen.h>
 #include <limits.h>
 #include <regex.h>
@@ -492,53 +493,61 @@ static int Preload(const char *restrict const filename)
        int n = 0;
        int rc = 0;
        char *name, *value;
+       glob_t globbuf;
+       int globerr;
+       int j;
+
+       globerr = glob(filename, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf);
+       if (globerr != 0 && globerr != GLOB_NOMATCH)
+               xerr(EXIT_FAILURE, _("glob failed"));
+
+       for (j = 0; j < globbuf.gl_pathc; j++) {
+               fp = (globbuf.gl_pathv[j][0] == '-' && !globbuf.gl_pathv[j][1])
+                   ? stdin : fopen(globbuf.gl_pathv[j], "r");
+               if (!fp) {
+                       xwarn(_("cannot open \"%s\""), globbuf.gl_pathv[j]);
+                       return -1;
+               }
 
-       fp = (filename[0] == '-' && !filename[1])
-           ? stdin : fopen(filename, "r");
+               while (fgets(oneline, sizeof oneline, fp)) {
+                       n++;
+                       t = StripLeadingAndTrailingSpaces(oneline);
 
-       if (!fp) {
-               xwarn(_("cannot open \"%s\""), filename);
-               return -1;
-       }
+                       if (strlen(t) < 2)
+                               continue;
 
-       while (fgets(oneline, sizeof oneline, fp)) {
-               n++;
-               t = StripLeadingAndTrailingSpaces(oneline);
+                       if (*t == '#' || *t == ';')
+                               continue;
 
-               if (strlen(t) < 2)
-                       continue;
+                       name = strtok(t, "=");
+                       if (!name || !*name) {
+                               xwarnx(_("%s(%d): invalid syntax, continuing..."),
+                                      globbuf.gl_pathv[j], n);
+                               continue;
+                       }
 
-               if (*t == '#' || *t == ';')
-                       continue;
+                       StripLeadingAndTrailingSpaces(name);
 
-               name = strtok(t, "=");
-               if (!name || !*name) {
-                       xwarnx(_("%s(%d): invalid syntax, continuing..."),
-                              filename, n);
-                       continue;
-               }
+                       if (pattern && !pattern_match(name, pattern))
+                               continue;
 
-               StripLeadingAndTrailingSpaces(name);
+                       value = strtok(NULL, "\n\r");
+                       if (!value || !*value) {
+                               xwarnx(_("%s(%d): invalid syntax, continuing..."),
+                                      globbuf.gl_pathv[j], n);
+                               continue;
+                       }
 
-               if (pattern && !pattern_match(name, pattern))
-                       continue;
+                       while ((*value == ' ' || *value == '\t') && *value != 0)
+                               value++;
 
-               value = strtok(NULL, "\n\r");
-               if (!value || !*value) {
-                       xwarnx(_("%s(%d): invalid syntax, continuing..."),
-                              filename, n);
-                       continue;
+                       /* should NameOnly affect this? */
+                       sprintf(buffer, "%s=%s", name, value);
+                       rc |= WriteSetting(buffer);
                }
 
-               while ((*value == ' ' || *value == '\t') && *value != 0)
-                       value++;
-
-               /* should NameOnly affect this? */
-               sprintf(buffer, "%s=%s", name, value);
-               rc |= WriteSetting(buffer);
+               fclose(fp);
        }
-
-       fclose(fp);
        return rc;
 }
 
@@ -641,7 +650,7 @@ int main(int argc, char *argv[])
        bool preloadfileOpt = false;
        int ReturnCode = 0;
        int c;
-       const char *preloadfile = DEFAULT_PRELOAD;
+       const char *preloadfile = NULL;
 
        enum {
                DEPRECATED_OPTION = CHAR_MAX + 1,
@@ -744,13 +753,27 @@ int main(int argc, char *argv[])
                }
        }
 
+       argc -= optind;
+       argv += optind;
+
        if (DisplayAllOpt)
                return DisplayAll(PROC_PATH);
-       if (preloadfileOpt)
-               return Preload(preloadfile);
 
-       argc -= optind;
-       argv += optind;
+       if (preloadfileOpt) {
+               int ret = EXIT_SUCCESS, i;
+               if (!preloadfile) {
+                       if (!argc) {
+                               ret != Preload(DEFAULT_PRELOAD);
+                       }
+               } else {
+                       /* This happens when -pfile option is
+                        * used without space. */
+                       Preload(preloadfile);
+               }
+               for (i = 0; i < argc; i++)
+                       Preload(argv[i]);
+               return ret;
+       }
 
        if (argc < 1)
                xerrx(EXIT_FAILURE, _("no variables specified\n"