sysctl: restore old -p handling
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>
This commit is contained in:
committed by
Sami Kerola
parent
156dd0b5a3
commit
e2987888e2
6
sysctl.8
6
sysctl.8
@ -12,6 +12,8 @@ sysctl \- configure kernel parameters at runtime
|
|||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B sysctl
|
.B sysctl
|
||||||
[\fIoptions\fR] [\fIvariable\fR[\fI=value\fR]] [...]
|
[\fIoptions\fR] [\fIvariable\fR[\fI=value\fR]] [...]
|
||||||
|
.BR
|
||||||
|
.B sysctl \-p [file or regexp] [...]
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.B sysctl
|
.B sysctl
|
||||||
is used to modify kernel parameters at runtime. The parameters available
|
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]
|
\fB\-p\fR[\fIFILE\fR], \fB\-\-load\fR[=\fIFILE\fR]
|
||||||
Load in sysctl settings from the file specified or /etc/sysctl.conf if none
|
Load in sysctl settings from the file specified or /etc/sysctl.conf if none
|
||||||
given. Specifying \- as filename means reading data from standard input.
|
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
|
.TP
|
||||||
\fB\-a\fR, \fB\-\-all\fR
|
\fB\-a\fR, \fB\-\-all\fR
|
||||||
Display all values currently available.
|
Display all values currently available.
|
||||||
|
111
sysctl.c
111
sysctl.c
@ -29,6 +29,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <glob.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
@ -492,53 +493,61 @@ static int Preload(const char *restrict const filename)
|
|||||||
int n = 0;
|
int n = 0;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
char *name, *value;
|
char *name, *value;
|
||||||
|
glob_t globbuf;
|
||||||
|
int globerr;
|
||||||
|
int j;
|
||||||
|
|
||||||
fp = (filename[0] == '-' && !filename[1])
|
globerr = glob(filename, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf);
|
||||||
? stdin : fopen(filename, "r");
|
if (globerr != 0 && globerr != GLOB_NOMATCH)
|
||||||
|
xerr(EXIT_FAILURE, _("glob failed"));
|
||||||
|
|
||||||
if (!fp) {
|
for (j = 0; j < globbuf.gl_pathc; j++) {
|
||||||
xwarn(_("cannot open \"%s\""), filename);
|
fp = (globbuf.gl_pathv[j][0] == '-' && !globbuf.gl_pathv[j][1])
|
||||||
return -1;
|
? stdin : fopen(globbuf.gl_pathv[j], "r");
|
||||||
}
|
if (!fp) {
|
||||||
|
xwarn(_("cannot open \"%s\""), globbuf.gl_pathv[j]);
|
||||||
while (fgets(oneline, sizeof oneline, fp)) {
|
return -1;
|
||||||
n++;
|
|
||||||
t = StripLeadingAndTrailingSpaces(oneline);
|
|
||||||
|
|
||||||
if (strlen(t) < 2)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (*t == '#' || *t == ';')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
name = strtok(t, "=");
|
|
||||||
if (!name || !*name) {
|
|
||||||
xwarnx(_("%s(%d): invalid syntax, continuing..."),
|
|
||||||
filename, n);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StripLeadingAndTrailingSpaces(name);
|
while (fgets(oneline, sizeof oneline, fp)) {
|
||||||
|
n++;
|
||||||
|
t = StripLeadingAndTrailingSpaces(oneline);
|
||||||
|
|
||||||
if (pattern && !pattern_match(name, pattern))
|
if (strlen(t) < 2)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
value = strtok(NULL, "\n\r");
|
if (*t == '#' || *t == ';')
|
||||||
if (!value || !*value) {
|
continue;
|
||||||
xwarnx(_("%s(%d): invalid syntax, continuing..."),
|
|
||||||
filename, n);
|
name = strtok(t, "=");
|
||||||
continue;
|
if (!name || !*name) {
|
||||||
|
xwarnx(_("%s(%d): invalid syntax, continuing..."),
|
||||||
|
globbuf.gl_pathv[j], n);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
StripLeadingAndTrailingSpaces(name);
|
||||||
|
|
||||||
|
if (pattern && !pattern_match(name, pattern))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
value = strtok(NULL, "\n\r");
|
||||||
|
if (!value || !*value) {
|
||||||
|
xwarnx(_("%s(%d): invalid syntax, continuing..."),
|
||||||
|
globbuf.gl_pathv[j], n);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((*value == ' ' || *value == '\t') && *value != 0)
|
||||||
|
value++;
|
||||||
|
|
||||||
|
/* should NameOnly affect this? */
|
||||||
|
sprintf(buffer, "%s=%s", name, value);
|
||||||
|
rc |= WriteSetting(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((*value == ' ' || *value == '\t') && *value != 0)
|
fclose(fp);
|
||||||
value++;
|
|
||||||
|
|
||||||
/* should NameOnly affect this? */
|
|
||||||
sprintf(buffer, "%s=%s", name, value);
|
|
||||||
rc |= WriteSetting(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -641,7 +650,7 @@ int main(int argc, char *argv[])
|
|||||||
bool preloadfileOpt = false;
|
bool preloadfileOpt = false;
|
||||||
int ReturnCode = 0;
|
int ReturnCode = 0;
|
||||||
int c;
|
int c;
|
||||||
const char *preloadfile = DEFAULT_PRELOAD;
|
const char *preloadfile = NULL;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
DEPRECATED_OPTION = CHAR_MAX + 1,
|
DEPRECATED_OPTION = CHAR_MAX + 1,
|
||||||
@ -744,14 +753,28 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DisplayAllOpt)
|
|
||||||
return DisplayAll(PROC_PATH);
|
|
||||||
if (preloadfileOpt)
|
|
||||||
return Preload(preloadfile);
|
|
||||||
|
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
|
if (DisplayAllOpt)
|
||||||
|
return DisplayAll(PROC_PATH);
|
||||||
|
|
||||||
|
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)
|
if (argc < 1)
|
||||||
xerrx(EXIT_FAILURE, _("no variables specified\n"
|
xerrx(EXIT_FAILURE, _("no variables specified\n"
|
||||||
"Try `%s --help' for more information."),
|
"Try `%s --help' for more information."),
|
||||||
|
Reference in New Issue
Block a user