2002-02-02 04:17:29 +05:30
|
|
|
/*
|
|
|
|
* Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
|
|
|
|
*
|
|
|
|
* "Copyright 1999 George Staikos
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-02-02 04:17:29 +05:30
|
|
|
*
|
2021-09-13 17:37:37 +05:30
|
|
|
* Part of this code comes from systemd, especially sysctl.c
|
2002-02-02 04:17:29 +05:30
|
|
|
* Changelog:
|
|
|
|
* v1.01:
|
|
|
|
* - added -p <preload> to preload values from a file
|
2012-01-25 13:48:38 +05:30
|
|
|
* Horms:
|
2004-07-15 10:14:42 +05:30
|
|
|
* - added -q to be quiet when modifying values
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
|
|
|
* Changes by Albert Cahalan, 2002.
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
|
|
|
|
2011-06-05 23:16:49 +05:30
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2012-05-03 10:54:20 +05:30
|
|
|
#include <glob.h>
|
2011-06-05 23:16:49 +05:30
|
|
|
#include <libgen.h>
|
2011-10-07 12:40:49 +05:30
|
|
|
#include <limits.h>
|
2011-10-07 12:40:50 +05:30
|
|
|
#include <regex.h>
|
2012-05-03 10:32:52 +05:30
|
|
|
#include <stdbool.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-05 23:16:49 +05:30
|
|
|
#include <string.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2011-06-05 23:16:49 +05:30
|
|
|
#include <unistd.h>
|
2021-09-13 17:37:37 +05:30
|
|
|
#include <ctype.h>
|
2011-10-09 06:27:45 +05:30
|
|
|
|
|
|
|
#include "c.h"
|
2012-03-23 18:02:24 +05:30
|
|
|
#include "fileutils.h"
|
2011-10-09 06:27:45 +05:30
|
|
|
#include "nls.h"
|
2011-10-10 00:59:26 +05:30
|
|
|
#include "xalloc.h"
|
2018-04-01 11:30:00 +05:30
|
|
|
#include "procio.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals...
|
|
|
|
*/
|
2002-10-10 03:02:33 +05:30
|
|
|
static const char PROC_PATH[] = "/proc/sys/";
|
|
|
|
static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
|
2012-02-14 01:51:43 +05:30
|
|
|
static const char *DEPRECATED[] = {
|
|
|
|
"base_reachable_time",
|
|
|
|
"retrans_time",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
static bool IgnoreDeprecated;
|
2004-07-15 20:52:23 +05:30
|
|
|
static bool NameOnly;
|
2002-02-02 04:17:29 +05:30
|
|
|
static bool PrintName;
|
|
|
|
static bool PrintNewline;
|
2002-12-12 04:25:42 +05:30
|
|
|
static bool IgnoreError;
|
2004-07-15 10:14:42 +05:30
|
|
|
static bool Quiet;
|
2021-09-13 17:37:37 +05:30
|
|
|
static bool DryRun;
|
2011-10-07 12:40:50 +05:30
|
|
|
static char *pattern;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2018-01-18 16:08:02 +05:30
|
|
|
#define LINELEN 4096
|
|
|
|
static char *iobuf;
|
|
|
|
static size_t iolen = LINELEN;
|
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
typedef struct SysctlSetting {
|
|
|
|
char *key;
|
|
|
|
char *path;
|
|
|
|
char *value;
|
|
|
|
bool ignore_failure;
|
|
|
|
bool glob_exclude;
|
|
|
|
struct SysctlSetting *next;
|
|
|
|
} SysctlSetting;
|
|
|
|
|
|
|
|
typedef struct SettingList {
|
|
|
|
struct SysctlSetting *head;
|
|
|
|
struct SysctlSetting *tail;
|
|
|
|
} SettingList;
|
|
|
|
|
|
|
|
#define GLOB_CHARS "*?["
|
|
|
|
static inline bool string_is_glob(const char *p)
|
|
|
|
{
|
|
|
|
return !!strpbrk(p, GLOB_CHARS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
/* Function prototypes. */
|
2012-11-02 23:20:55 +05:30
|
|
|
static int pattern_match(const char *string, const char *pat);
|
2012-01-25 13:48:38 +05:30
|
|
|
static int DisplayAll(const char *restrict const path);
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2021-07-20 18:06:15 +05:30
|
|
|
static inline bool is_proc_path(
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
char *resolved_path;
|
|
|
|
|
|
|
|
if ( (resolved_path = realpath(path, NULL)) == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strncmp(PROC_PATH, resolved_path, strlen(PROC_PATH)) == 0) {
|
|
|
|
free(resolved_path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
xwarnx(_("Path is not under %s: %s"), PROC_PATH, path);
|
|
|
|
free(resolved_path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
static void slashdot(char *restrict p, char old, char new)
|
|
|
|
{
|
|
|
|
int warned = 1;
|
|
|
|
p = strpbrk(p, "/.");
|
|
|
|
if (!p)
|
|
|
|
/* nothing -- can't be, but oh well */
|
|
|
|
return;
|
|
|
|
if (*p == new)
|
|
|
|
/* already in desired format */
|
|
|
|
return;
|
|
|
|
while (p) {
|
|
|
|
char c = *p;
|
|
|
|
if ((*(p + 1) == '/' || *(p + 1) == '.') && warned) {
|
|
|
|
xwarnx(_("separators should not be repeated: %s"), p);
|
|
|
|
warned = 0;
|
|
|
|
}
|
|
|
|
if (c == old)
|
|
|
|
*p = new;
|
|
|
|
if (c == new)
|
|
|
|
*p = old;
|
|
|
|
p = strpbrk(p + 1, "/.");
|
|
|
|
}
|
2002-10-09 11:53:58 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2022-01-02 11:30:09 +05:30
|
|
|
#if 0 // avoid '-Wunused-function' warning
|
2021-09-13 17:37:37 +05:30
|
|
|
static void setting_free(SysctlSetting *s) {
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(s->key);
|
|
|
|
free(s->path);
|
|
|
|
free(s->value);
|
|
|
|
free(s);
|
|
|
|
}
|
2022-01-02 11:30:09 +05:30
|
|
|
#endif
|
2021-09-13 17:37:37 +05:30
|
|
|
|
|
|
|
static SysctlSetting *setting_new(
|
|
|
|
const char *key,
|
|
|
|
const char *value,
|
|
|
|
bool ignore_failure,
|
|
|
|
bool glob_exclude) {
|
|
|
|
|
|
|
|
SysctlSetting *s = NULL;
|
|
|
|
char *path = NULL;
|
|
|
|
int proc_len;
|
|
|
|
|
|
|
|
proc_len = strlen(PROC_PATH);
|
|
|
|
/* used to open the file */
|
|
|
|
path = xmalloc(strlen(key) + proc_len + 2);
|
|
|
|
strcpy(path, PROC_PATH);
|
|
|
|
if (key[0] == '-')
|
|
|
|
strcat(path + proc_len, key+1);
|
|
|
|
else
|
|
|
|
strcat(path + proc_len, key);
|
|
|
|
/* change . to / */
|
|
|
|
slashdot(path + proc_len, '.', '/');
|
|
|
|
|
|
|
|
s = xmalloc(sizeof(SysctlSetting));
|
|
|
|
|
|
|
|
*s = (SysctlSetting) {
|
|
|
|
.key = strdup(key),
|
|
|
|
.path = path,
|
|
|
|
.value = value? strdup(value): NULL,
|
|
|
|
.ignore_failure = ignore_failure,
|
|
|
|
.glob_exclude = glob_exclude,
|
|
|
|
.next = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void settinglist_add(SettingList *l, SysctlSetting *s) {
|
|
|
|
SysctlSetting *old_tail;
|
|
|
|
|
|
|
|
if (!l)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (l->head == NULL)
|
|
|
|
l->head = s;
|
|
|
|
|
|
|
|
if (l->tail != NULL) {
|
|
|
|
old_tail = l->tail;
|
|
|
|
old_tail->next = s;
|
|
|
|
}
|
|
|
|
l->tail = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SysctlSetting *settinglist_findpath(const SettingList *l, const char *path) {
|
|
|
|
SysctlSetting *node;
|
|
|
|
|
|
|
|
for (node=l->head; node != NULL; node = node->next) {
|
|
|
|
if (strcmp(node->path, path) == 0)
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
static int pattern_match(const char *string, const char *pat);
|
|
|
|
static int DisplayAll(const char *restrict const path);
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Display the usage format
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2011-06-05 23:16:49 +05:30
|
|
|
static void __attribute__ ((__noreturn__))
|
|
|
|
Usage(FILE * out)
|
|
|
|
{
|
2012-01-25 13:48:38 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out,
|
|
|
|
_(" %s [options] [variable[=value] ...]\n"),
|
|
|
|
program_invocation_short_name);
|
|
|
|
fputs(USAGE_OPTIONS, out);
|
2013-10-11 04:37:10 +05:30
|
|
|
fputs(_(" -a, --all display all variables\n"), out);
|
|
|
|
fputs(_(" -A alias of -a\n"), out);
|
|
|
|
fputs(_(" -X alias of -a\n"), out);
|
|
|
|
fputs(_(" --deprecated include deprecated parameters to listing\n"), out);
|
2021-09-13 17:37:37 +05:30
|
|
|
fputs(_(" --dry-run Print the key and values but do not write\n"), out);
|
2013-10-11 04:37:10 +05:30
|
|
|
fputs(_(" -b, --binary print value without new line\n"), out);
|
|
|
|
fputs(_(" -e, --ignore ignore unknown variables errors\n"), out);
|
|
|
|
fputs(_(" -N, --names print variable names without values\n"), out);
|
2018-04-11 10:30:00 +05:30
|
|
|
fputs(_(" -n, --values print only values of the given variable(s)\n"), out);
|
2013-10-11 04:37:10 +05:30
|
|
|
fputs(_(" -p, --load[=<file>] read values from file\n"), out);
|
|
|
|
fputs(_(" -f alias of -p\n"), out);
|
|
|
|
fputs(_(" --system read values from all system directories\n"), out);
|
|
|
|
fputs(_(" -r, --pattern <expression>\n"
|
|
|
|
" select setting that match expression\n"), out);
|
|
|
|
fputs(_(" -q, --quiet do not echo variable set\n"), out);
|
|
|
|
fputs(_(" -w, --write enable writing a value to variable\n"), out);
|
|
|
|
fputs(_(" -o does nothing\n"), out);
|
|
|
|
fputs(_(" -x does nothing\n"), out);
|
|
|
|
fputs(_(" -d alias of -h\n"), out);
|
2012-01-25 13:48:38 +05:30
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("sysctl(8)"));
|
|
|
|
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
/*
|
|
|
|
* Strip left/leading side of a string
|
|
|
|
*/
|
|
|
|
static char *lstrip(char *line)
|
|
|
|
{
|
|
|
|
char *start;
|
|
|
|
|
|
|
|
if (!line || !*line)
|
|
|
|
return line;
|
|
|
|
|
|
|
|
start = line;
|
|
|
|
while(isspace(*start)) start++;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip right/trailing side of a string
|
|
|
|
* by placing a \0
|
|
|
|
*/
|
|
|
|
static void rstrip(char *line)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if (!line || !*line)
|
|
|
|
return;
|
|
|
|
|
|
|
|
end = line + strlen(line) - 1;
|
|
|
|
while(end > line && isspace(*end)) end--;
|
|
|
|
|
|
|
|
end[1] = '\0';
|
|
|
|
}
|
|
|
|
|
2022-01-02 11:30:09 +05:30
|
|
|
#if 0 // avoid '-Wunused-function' warning
|
2002-02-02 04:17:29 +05:30
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Strip the leading and trailing spaces from a string
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2012-01-25 13:48:38 +05:30
|
|
|
static char *StripLeadingAndTrailingSpaces(char *oneline)
|
|
|
|
{
|
|
|
|
char *t;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
if (!oneline || !*oneline)
|
|
|
|
return oneline;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
t = oneline;
|
|
|
|
t += strlen(oneline) - 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
while ((*t == ' ' || *t == '\t' || *t == '\n' || *t == '\r') && t != oneline)
|
|
|
|
*t-- = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
t = oneline;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
while ((*t == ' ' || *t == '\t') && *t != 0)
|
|
|
|
t++;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
return t;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2022-01-02 11:30:09 +05:30
|
|
|
#endif
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Read a sysctl setting
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2012-01-25 13:48:38 +05:30
|
|
|
static int ReadSetting(const char *restrict const name)
|
|
|
|
{
|
2021-09-13 17:37:37 +05:30
|
|
|
int rc = EXIT_SUCCESS;
|
2012-01-25 13:48:38 +05:30
|
|
|
char *restrict tmpname;
|
|
|
|
char *restrict outname;
|
2018-01-18 16:08:02 +05:30
|
|
|
ssize_t rlen;
|
2012-01-25 13:48:38 +05:30
|
|
|
FILE *restrict fp;
|
|
|
|
struct stat ts;
|
|
|
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
xwarnx(_("\"%s\" is an unknown key"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
/* used to display the output */
|
|
|
|
outname = xstrdup(name);
|
|
|
|
/* change / to . */
|
|
|
|
slashdot(outname, '/', '.');
|
|
|
|
|
|
|
|
/* used to open the file */
|
|
|
|
tmpname = xmalloc(strlen(name) + strlen(PROC_PATH) + 2);
|
|
|
|
strcpy(tmpname, PROC_PATH);
|
|
|
|
strcat(tmpname, name);
|
|
|
|
/* change . to / */
|
|
|
|
slashdot(tmpname + strlen(PROC_PATH), '.', '/');
|
|
|
|
|
|
|
|
/* used to display the output */
|
|
|
|
outname = xstrdup(name);
|
|
|
|
/* change / to . */
|
|
|
|
slashdot(outname, '/', '.');
|
|
|
|
|
|
|
|
if (stat(tmpname, &ts) < 0) {
|
|
|
|
if (!IgnoreError) {
|
|
|
|
xwarn(_("cannot stat %s"), tmpname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ts.st_mode & S_IRUSR) == 0)
|
|
|
|
goto out;
|
|
|
|
|
2021-07-20 18:06:15 +05:30
|
|
|
if (!is_proc_path(tmpname)) {
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
|
|
|
size_t len;
|
|
|
|
len = strlen(tmpname);
|
|
|
|
tmpname[len] = '/';
|
|
|
|
tmpname[len + 1] = '\0';
|
|
|
|
rc = DisplayAll(tmpname);
|
|
|
|
goto out;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
if (pattern && !pattern_match(outname, pattern)) {
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_SUCCESS;
|
2017-05-22 18:01:34 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NameOnly) {
|
|
|
|
fprintf(stdout, "%s\n", outname);
|
|
|
|
goto out;
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
|
|
|
|
2018-01-18 16:08:02 +05:30
|
|
|
fp = fprocopen(tmpname, "r");
|
2012-01-25 13:48:38 +05:30
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
if (!IgnoreError) {
|
|
|
|
xwarnx(_("\"%s\" is an unknown key"), outname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
xwarnx(_("permission denied on key '%s'"), outname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
break;
|
2017-07-07 17:39:12 +05:30
|
|
|
case EIO: /* Ignore stable_secret below /proc/sys/net/ipv6/conf */
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2017-07-07 17:39:12 +05:30
|
|
|
break;
|
2012-01-25 13:48:38 +05:30
|
|
|
default:
|
|
|
|
xwarn(_("reading key \"%s\""), outname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errno = 0;
|
2018-01-18 16:08:02 +05:30
|
|
|
if ((rlen = getline(&iobuf, &iolen, fp)) > 0) {
|
2012-01-25 13:48:38 +05:30
|
|
|
/* this loop is required, see
|
|
|
|
* /sbin/sysctl -a | egrep -6 dev.cdrom.info
|
|
|
|
*/
|
|
|
|
do {
|
2017-05-22 18:01:34 +05:30
|
|
|
char *nlptr;
|
|
|
|
if (PrintName) {
|
|
|
|
fprintf(stdout, "%s = ", outname);
|
|
|
|
do {
|
2018-01-18 16:08:02 +05:30
|
|
|
fprintf(stdout, "%s", iobuf);
|
|
|
|
nlptr = &iobuf[strlen(iobuf) - 1];
|
2017-05-22 18:01:34 +05:30
|
|
|
/* already has the \n in it */
|
|
|
|
if (*nlptr == '\n')
|
|
|
|
break;
|
2018-01-18 16:08:02 +05:30
|
|
|
} while ((rlen = getline(&iobuf, &iolen, fp)) > 0);
|
2017-05-22 18:01:34 +05:30
|
|
|
if (*nlptr != '\n')
|
|
|
|
putchar('\n');
|
|
|
|
} else {
|
|
|
|
if (!PrintNewline) {
|
2018-01-18 16:08:02 +05:30
|
|
|
nlptr = strchr(iobuf, '\n');
|
2017-05-22 18:01:34 +05:30
|
|
|
if (nlptr)
|
|
|
|
*nlptr = '\0';
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
2018-01-18 16:08:02 +05:30
|
|
|
fprintf(stdout, "%s", iobuf);
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
2018-01-18 16:08:02 +05:30
|
|
|
} while ((rlen = getline(&iobuf, &iolen, fp)) > 0);
|
2012-01-25 13:48:38 +05:30
|
|
|
} else {
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
|
|
|
xwarnx(_("permission denied on key '%s'"),
|
|
|
|
outname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
break;
|
|
|
|
case EISDIR: {
|
|
|
|
size_t len;
|
|
|
|
len = strlen(tmpname);
|
|
|
|
tmpname[len] = '/';
|
|
|
|
tmpname[len + 1] = '\0';
|
|
|
|
fclose(fp);
|
|
|
|
rc = DisplayAll(tmpname);
|
|
|
|
goto out;
|
|
|
|
}
|
2017-07-07 17:39:12 +05:30
|
|
|
case EIO: /* Ignore stable_secret below /proc/sys/net/ipv6/conf */
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2017-07-07 17:39:12 +05:30
|
|
|
break;
|
2012-01-25 13:48:38 +05:30
|
|
|
default:
|
|
|
|
xwarnx(_("reading key \"%s\""), outname);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return rc;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-11-02 23:20:55 +05:30
|
|
|
static int is_deprecated(char *filename)
|
2012-02-14 01:51:43 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; strlen(DEPRECATED[i]); i++) {
|
|
|
|
if (strcmp(DEPRECATED[i], filename) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Display all the sysctl settings
|
2002-11-26 03:12:17 +05:30
|
|
|
*/
|
2012-01-25 13:48:38 +05:30
|
|
|
static int DisplayAll(const char *restrict const path)
|
|
|
|
{
|
2021-09-13 17:37:37 +05:30
|
|
|
int rc = EXIT_SUCCESS;
|
2012-01-25 13:48:38 +05:30
|
|
|
int rc2;
|
|
|
|
DIR *restrict dp;
|
|
|
|
struct dirent *restrict de;
|
|
|
|
struct stat ts;
|
|
|
|
|
|
|
|
dp = opendir(path);
|
|
|
|
|
|
|
|
if (!dp) {
|
|
|
|
xwarnx(_("unable to open directory \"%s\""), path);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2012-01-25 13:48:38 +05:30
|
|
|
} else {
|
|
|
|
readdir(dp); /* skip . */
|
|
|
|
readdir(dp); /* skip .. */
|
|
|
|
while ((de = readdir(dp))) {
|
|
|
|
char *restrict tmpdir;
|
2012-02-14 01:51:43 +05:30
|
|
|
if (IgnoreDeprecated && is_deprecated(de->d_name))
|
|
|
|
continue;
|
2012-01-25 13:48:38 +05:30
|
|
|
tmpdir =
|
|
|
|
(char *restrict) xmalloc(strlen(path) +
|
|
|
|
strlen(de->d_name) +
|
|
|
|
2);
|
|
|
|
sprintf(tmpdir, "%s%s", path, de->d_name);
|
|
|
|
rc2 = stat(tmpdir, &ts);
|
|
|
|
if (rc2 != 0) {
|
|
|
|
xwarn(_("cannot stat %s"), tmpdir);
|
|
|
|
} else {
|
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
|
|
|
strcat(tmpdir, "/");
|
|
|
|
DisplayAll(tmpdir);
|
|
|
|
} else {
|
|
|
|
rc |=
|
|
|
|
ReadSetting(tmpdir +
|
|
|
|
strlen(PROC_PATH));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tmpdir);
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
}
|
|
|
|
return rc;
|
2002-11-26 04:54:40 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Write a sysctl setting
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2021-09-13 17:37:37 +05:30
|
|
|
static int WriteSetting(
|
|
|
|
const char *key,
|
|
|
|
const char *path,
|
|
|
|
const char *value,
|
2022-01-08 11:30:00 +05:30
|
|
|
const bool ignore_failure)
|
|
|
|
{
|
2021-09-13 17:37:37 +05:30
|
|
|
int rc = EXIT_SUCCESS;
|
|
|
|
FILE *fp;
|
2022-01-08 11:30:00 +05:30
|
|
|
struct stat ts;
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
if (!key || !path)
|
|
|
|
return rc;
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2022-01-08 11:30:00 +05:30
|
|
|
if (stat(path, &ts) < 0) {
|
|
|
|
if (!IgnoreError) {
|
|
|
|
xwarn(_("cannot stat %s"), path);
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
}
|
2021-09-13 17:37:37 +05:30
|
|
|
return rc;
|
2022-01-08 11:30:00 +05:30
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2022-01-08 11:30:00 +05:30
|
|
|
if (!is_proc_path(path)) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2021-07-20 18:06:15 +05:30
|
|
|
|
2022-01-08 11:30:00 +05:30
|
|
|
if ((ts.st_mode & S_IWUSR) == 0) {
|
|
|
|
xwarn(_("setting key \"%s\""), key);
|
2021-09-13 17:37:37 +05:30
|
|
|
return rc;
|
2022-01-08 11:30:00 +05:30
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2022-01-08 11:30:00 +05:30
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
|
|
|
xwarn(_("setting key \"%s\""), key);
|
2021-09-13 17:37:37 +05:30
|
|
|
return rc;
|
2022-01-08 11:30:00 +05:30
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
if (!DryRun) {
|
|
|
|
if ((fp = fprocopen(path, "w")) == NULL) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
if (!IgnoreError) {
|
|
|
|
xwarnx(_("\"%s\" is an unknown key%s"),
|
|
|
|
key, (ignore_failure?_(", ignoring"):""));
|
2022-01-08 11:30:00 +05:30
|
|
|
if (!ignore_failure)
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
2021-09-13 17:37:37 +05:30
|
|
|
case EPERM:
|
|
|
|
case EROFS:
|
|
|
|
case EACCES:
|
|
|
|
xwarnx(_("permission denied on key \"%s\"%s"),
|
|
|
|
key, (ignore_failure?_(", ignoring"):""));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xwarn(_("setting key \"%s\"%s"),
|
|
|
|
key, (ignore_failure?_(", ignoring"):""));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ignore_failure && errno != ENOENT)
|
2022-01-08 11:30:00 +05:30
|
|
|
rc = EXIT_FAILURE;
|
2021-09-13 17:37:37 +05:30
|
|
|
} else {
|
2022-01-08 11:30:00 +05:30
|
|
|
if (0 < fprintf(fp, "%s\n", value))
|
|
|
|
rc = EXIT_SUCCESS;
|
2021-09-13 17:37:37 +05:30
|
|
|
if (close_stream(fp) != 0) {
|
|
|
|
xwarn(_("setting key \"%s\""), path);
|
2021-09-15 16:37:43 +05:30
|
|
|
return EXIT_FAILURE;
|
2021-09-13 17:37:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((rc == EXIT_SUCCESS && !Quiet) || DryRun) {
|
|
|
|
if (NameOnly) {
|
|
|
|
printf("%s\n", value);
|
|
|
|
} else {
|
|
|
|
if (PrintName) {
|
|
|
|
printf("%s = %s\n", path, value);
|
|
|
|
} else {
|
|
|
|
if (PrintNewline)
|
|
|
|
printf("%s\n", value);
|
|
|
|
else
|
|
|
|
printf("%s", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse each configuration line, there are multiple ways of specifying
|
|
|
|
* a key/value here:
|
|
|
|
*
|
|
|
|
* key = value simple setting
|
|
|
|
* -key = value ignore errors
|
|
|
|
* key.pattern.*.with.glob = value set keys that match glob
|
|
|
|
* -key.pattern.exclude.with.glob dont set this value
|
|
|
|
* key.pattern.override.with.glob = value set this glob match to value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static SysctlSetting *parse_setting_line(
|
|
|
|
const char *path,
|
|
|
|
const int linenum,
|
|
|
|
char *line)
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
bool glob_exclude = FALSE;
|
|
|
|
bool ignore_failure = FALSE;
|
|
|
|
|
|
|
|
key = lstrip(line);
|
|
|
|
if (strlen(key) < 2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* skip over comments */
|
|
|
|
if (key[0] == '#' || key[0] == ';')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (pattern && !pattern_match(key, pattern))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
value = strchr(key, '=');
|
|
|
|
if (value == NULL) {
|
|
|
|
if (key[0] == '-') {
|
|
|
|
glob_exclude = TRUE;
|
|
|
|
key++;
|
|
|
|
value = NULL;
|
|
|
|
rstrip(key);
|
|
|
|
} else {
|
|
|
|
xwarnx(_("%s(%d): invalid syntax, continuing..."),
|
|
|
|
path, linenum);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value[0]='\0';
|
|
|
|
if (key[0] == '-') {
|
|
|
|
ignore_failure = TRUE;
|
|
|
|
key++;
|
|
|
|
}
|
|
|
|
value++; // skip over =
|
|
|
|
value=lstrip(value);
|
|
|
|
rstrip(value);
|
|
|
|
rstrip(key);
|
|
|
|
}
|
|
|
|
return setting_new(key, value, ignore_failure, glob_exclude);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go through the setting list, expand and sort out
|
|
|
|
* setting globs and actually write the settings out
|
|
|
|
*/
|
|
|
|
static int write_setting_list(const SettingList *sl)
|
|
|
|
{
|
|
|
|
SysctlSetting *node;
|
|
|
|
int rc = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
for (node=sl->head; node != NULL; node=node->next) {
|
|
|
|
if (node->glob_exclude)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (string_is_glob(node->path)) {
|
|
|
|
glob_t globbuf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (glob(node->path, 0, NULL, &globbuf) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(i=0; i < globbuf.gl_pathc; i++) {
|
|
|
|
if (settinglist_findpath(sl, globbuf.gl_pathv[i]))
|
|
|
|
continue; // override or exclude
|
|
|
|
|
|
|
|
rc |= WriteSetting(node->key, globbuf.gl_pathv[i], node->value,
|
|
|
|
node->ignore_failure);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc |= WriteSetting(node->key, node->path, node->value,
|
|
|
|
node->ignore_failure);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-11-02 23:20:55 +05:30
|
|
|
static int pattern_match(const char *string, const char *pat)
|
2011-10-07 12:40:50 +05:30
|
|
|
{
|
|
|
|
int status;
|
|
|
|
regex_t re;
|
|
|
|
|
2012-11-02 23:20:55 +05:30
|
|
|
if (regcomp(&re, pat, REG_EXTENDED | REG_NOSUB) != 0)
|
2012-01-25 13:48:38 +05:30
|
|
|
return (0);
|
2011-10-07 12:40:50 +05:30
|
|
|
status = regexec(&re, string, (size_t) 0, NULL, 0);
|
|
|
|
regfree(&re);
|
2012-01-25 13:48:38 +05:30
|
|
|
if (status != 0)
|
|
|
|
return (0);
|
2011-10-07 12:40:50 +05:30
|
|
|
return (1);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Preload the sysctl's from the conf file. We parse the file and then
|
|
|
|
* reform it (strip out whitespace).
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2021-09-13 17:37:37 +05:30
|
|
|
static int Preload(SettingList *setlist, const char *restrict const filename)
|
2012-01-25 13:48:38 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int n = 0;
|
2021-09-13 17:37:37 +05:30
|
|
|
int rc = EXIT_SUCCESS;
|
2018-01-18 15:36:55 +05:30
|
|
|
ssize_t rlen;
|
2012-05-03 10:54:20 +05:30
|
|
|
glob_t globbuf;
|
|
|
|
int globerr;
|
2014-08-28 19:18:29 +05:30
|
|
|
int globflg;
|
2012-05-03 10:54:20 +05:30
|
|
|
int j;
|
|
|
|
|
2014-08-28 19:18:29 +05:30
|
|
|
globflg = GLOB_NOCHECK;
|
|
|
|
#ifdef GLOB_BRACE
|
|
|
|
globflg |= GLOB_BRACE;
|
|
|
|
#endif
|
2013-10-01 11:04:36 +05:30
|
|
|
#ifdef GLOB_TILDE
|
2014-08-28 19:18:29 +05:30
|
|
|
globflg |= GLOB_TILDE;
|
2013-10-01 11:04:36 +05:30
|
|
|
#else
|
|
|
|
if (filename[0] == '~')
|
|
|
|
xwarnx(_("GLOB_TILDE is not supported on your platform, "
|
|
|
|
"the tilde in \"%s\" won't be expanded."), filename);
|
|
|
|
#endif
|
2014-08-28 19:18:29 +05:30
|
|
|
globerr = glob(filename, globflg, NULL, &globbuf);
|
2013-10-01 11:04:36 +05:30
|
|
|
|
2012-05-03 10:54:20 +05:30
|
|
|
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]);
|
2021-09-13 17:37:37 +05:30
|
|
|
return EXIT_FAILURE;
|
2012-05-03 10:54:20 +05:30
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2018-01-18 16:08:02 +05:30
|
|
|
while ((rlen = getline(&iobuf, &iolen, fp)) > 0) {
|
2021-09-13 17:37:37 +05:30
|
|
|
SysctlSetting *setting;
|
2018-01-18 15:36:55 +05:30
|
|
|
|
2012-05-03 10:54:20 +05:30
|
|
|
n++;
|
2012-01-25 13:48:38 +05:30
|
|
|
|
2018-01-18 15:36:55 +05:30
|
|
|
if (rlen < 2)
|
|
|
|
continue;
|
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
if ( (setting = parse_setting_line(globbuf.gl_pathv[j], n, iobuf))
|
|
|
|
== NULL)
|
|
|
|
continue;
|
|
|
|
settinglist_add(setlist, setting);
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
|
|
|
|
2012-05-03 10:54:20 +05:30
|
|
|
fclose(fp);
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
|
|
|
return rc;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-10-07 12:40:49 +05:30
|
|
|
struct pair {
|
2012-01-25 13:48:38 +05:30
|
|
|
char *name;
|
|
|
|
char *value;
|
2011-10-07 12:40:49 +05:30
|
|
|
};
|
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
static int sortpairs(const void *A, const void *B)
|
2011-10-07 12:40:49 +05:30
|
|
|
{
|
2012-01-25 13:48:38 +05:30
|
|
|
const struct pair *a = *(struct pair * const *) A;
|
|
|
|
const struct pair *b = *(struct pair * const *) B;
|
|
|
|
return strcmp(a->name, b->name);
|
2011-10-07 12:40:49 +05:30
|
|
|
}
|
|
|
|
|
2021-09-13 17:37:37 +05:30
|
|
|
static int PreloadSystem(SettingList *setlist)
|
2012-01-25 13:48:38 +05:30
|
|
|
{
|
|
|
|
unsigned di, i;
|
|
|
|
const char *dirs[] = {
|
|
|
|
"/etc/sysctl.d",
|
2020-02-27 16:26:13 +05:30
|
|
|
"/run/sysctl.d",
|
2012-01-25 13:48:38 +05:30
|
|
|
"/usr/local/lib/sysctl.d",
|
|
|
|
"/usr/lib/sysctl.d",
|
|
|
|
"/lib/sysctl.d",
|
|
|
|
};
|
|
|
|
struct pair **cfgs = NULL;
|
|
|
|
unsigned ncfgs = 0;
|
2021-09-13 17:37:37 +05:30
|
|
|
int rc = EXIT_SUCCESS;
|
2013-09-20 18:04:32 +05:30
|
|
|
struct stat ts;
|
2012-01-25 13:48:38 +05:30
|
|
|
enum { nprealloc = 16 };
|
|
|
|
|
|
|
|
for (di = 0; di < sizeof(dirs) / sizeof(dirs[0]); ++di) {
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *dp = opendir(dirs[di]);
|
|
|
|
if (!dp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((de = readdir(dp))) {
|
|
|
|
if (!strcmp(de->d_name, ".")
|
|
|
|
|| !strcmp(de->d_name, ".."))
|
|
|
|
continue;
|
2012-05-03 10:37:58 +05:30
|
|
|
if (strlen(de->d_name) < 5
|
|
|
|
|| strcmp(de->d_name + strlen(de->d_name) - 5, ".conf"))
|
2012-01-25 13:48:38 +05:30
|
|
|
continue;
|
|
|
|
/* check if config already known */
|
|
|
|
for (i = 0; i < ncfgs; ++i) {
|
2012-02-06 01:32:34 +05:30
|
|
|
if (cfgs && !strcmp(cfgs[i]->name, de->d_name))
|
2012-01-25 13:48:38 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < ncfgs)
|
|
|
|
/* already in */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ncfgs % nprealloc == 0)
|
2012-02-06 01:32:34 +05:30
|
|
|
cfgs =
|
|
|
|
xrealloc(cfgs,
|
|
|
|
sizeof(struct pair *) * (ncfgs +
|
|
|
|
nprealloc));
|
|
|
|
|
|
|
|
if (cfgs) {
|
|
|
|
cfgs[ncfgs] =
|
|
|
|
xmalloc(sizeof(struct pair) +
|
|
|
|
strlen(de->d_name) * 2 + 2 +
|
|
|
|
strlen(dirs[di]) + 1);
|
|
|
|
cfgs[ncfgs]->name =
|
|
|
|
(char *)cfgs[ncfgs] + sizeof(struct pair);
|
|
|
|
strcpy(cfgs[ncfgs]->name, de->d_name);
|
|
|
|
cfgs[ncfgs]->value =
|
|
|
|
(char *)cfgs[ncfgs] + sizeof(struct pair) +
|
|
|
|
strlen(cfgs[ncfgs]->name) + 1;
|
|
|
|
sprintf(cfgs[ncfgs]->value, "%s/%s", dirs[di],
|
|
|
|
de->d_name);
|
|
|
|
ncfgs++;
|
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("internal error"));
|
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
}
|
|
|
|
qsort(cfgs, ncfgs, sizeof(struct cfg *), sortpairs);
|
|
|
|
|
|
|
|
for (i = 0; i < ncfgs; ++i) {
|
|
|
|
if (!Quiet)
|
|
|
|
printf(_("* Applying %s ...\n"), cfgs[i]->value);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc |= Preload(setlist, cfgs[i]->value);
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2013-09-20 18:04:32 +05:30
|
|
|
|
2015-06-26 18:33:11 +05:30
|
|
|
if (stat(DEFAULT_PRELOAD, &ts) == 0 && S_ISREG(ts.st_mode)) {
|
2013-09-20 18:04:32 +05:30
|
|
|
if (!Quiet)
|
|
|
|
printf(_("* Applying %s ...\n"), DEFAULT_PRELOAD);
|
2021-09-13 17:37:37 +05:30
|
|
|
rc |= Preload(setlist, DEFAULT_PRELOAD);
|
2013-09-20 18:04:32 +05:30
|
|
|
}
|
2014-01-03 00:19:36 +05:30
|
|
|
|
|
|
|
/* cleaning */
|
|
|
|
for (i = 0; i < ncfgs; ++i) {
|
|
|
|
free(cfgs[i]);
|
|
|
|
}
|
|
|
|
if (cfgs) free(cfgs);
|
|
|
|
|
2013-09-20 18:04:32 +05:30
|
|
|
return rc;
|
2012-01-25 13:48:38 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2012-01-25 13:48:38 +05:30
|
|
|
* Main...
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2011-06-05 23:16:49 +05:30
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2012-01-25 13:48:38 +05:30
|
|
|
bool WriteMode = false;
|
|
|
|
bool DisplayAllOpt = false;
|
|
|
|
bool preloadfileOpt = false;
|
|
|
|
int ReturnCode = 0;
|
|
|
|
int c;
|
2022-01-02 11:30:09 +05:30
|
|
|
int rc = 0;
|
2012-05-03 10:54:20 +05:30
|
|
|
const char *preloadfile = NULL;
|
2021-09-13 17:37:37 +05:30
|
|
|
SettingList *setlist;
|
2012-01-25 13:48:38 +05:30
|
|
|
|
|
|
|
enum {
|
2012-02-14 01:51:43 +05:30
|
|
|
DEPRECATED_OPTION = CHAR_MAX + 1,
|
2021-09-13 17:37:37 +05:30
|
|
|
SYSTEM_OPTION,
|
|
|
|
DRYRUN_OPTION
|
2012-01-25 13:48:38 +05:30
|
|
|
};
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"all", no_argument, NULL, 'a'},
|
2012-02-14 01:51:43 +05:30
|
|
|
{"deprecated", no_argument, NULL, DEPRECATED_OPTION},
|
2021-09-13 17:37:37 +05:30
|
|
|
{"dry-run", no_argument, NULL, DRYRUN_OPTION},
|
2012-01-25 13:48:38 +05:30
|
|
|
{"binary", no_argument, NULL, 'b'},
|
|
|
|
{"ignore", no_argument, NULL, 'e'},
|
|
|
|
{"names", no_argument, NULL, 'N'},
|
|
|
|
{"values", no_argument, NULL, 'n'},
|
|
|
|
{"load", optional_argument, NULL, 'p'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"write", no_argument, NULL, 'w'},
|
|
|
|
{"system", no_argument, NULL, SYSTEM_OPTION},
|
|
|
|
{"pattern", required_argument, NULL, 'r'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2011-06-05 23:16:49 +05:30
|
|
|
|
2013-02-20 23:01:48 +05:30
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
2012-01-03 13:18:43 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
2013-02-20 23:01:48 +05:30
|
|
|
#endif
|
2012-01-25 13:48:38 +05:30
|
|
|
setlocale(LC_ALL, "");
|
2012-01-03 13:18:43 +05:30
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
PrintName = true;
|
|
|
|
PrintNewline = true;
|
|
|
|
IgnoreError = false;
|
|
|
|
Quiet = false;
|
2012-02-14 01:51:43 +05:30
|
|
|
IgnoreDeprecated = true;
|
2021-09-13 17:37:37 +05:30
|
|
|
DryRun = false;
|
|
|
|
setlist = xmalloc(sizeof(SettingList));
|
|
|
|
setlist->head = NULL;
|
|
|
|
setlist->tail = NULL;
|
2012-01-25 13:48:38 +05:30
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
Usage(stderr);
|
|
|
|
|
|
|
|
while ((c =
|
|
|
|
getopt_long(argc, argv, "bneNwfp::qoxaAXr:Vdh", longopts,
|
2012-03-07 17:19:09 +05:30
|
|
|
NULL)) != -1) {
|
2012-01-25 13:48:38 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
/* This is "binary" format, which means more for BSD. */
|
|
|
|
PrintNewline = false;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case 'n':
|
|
|
|
PrintName = false;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
/*
|
|
|
|
* For FreeBSD, -e means a "%s=%s\n" format.
|
|
|
|
* ("%s: %s\n" default). We (and NetBSD) use
|
|
|
|
* "%s = %s\n" always, and -e to ignore errors.
|
|
|
|
*/
|
|
|
|
IgnoreError = true;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
NameOnly = true;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
WriteMode = true;
|
|
|
|
break;
|
|
|
|
case 'f': /* the NetBSD way */
|
|
|
|
case 'p':
|
|
|
|
preloadfileOpt = true;
|
|
|
|
if (optarg)
|
|
|
|
preloadfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
Quiet = true;
|
|
|
|
break;
|
|
|
|
case 'o': /* BSD: binary values too, 1st 16 bytes in hex */
|
|
|
|
case 'x': /* BSD: binary values too, whole thing in hex */
|
|
|
|
/* does nothing */ ;
|
|
|
|
break;
|
|
|
|
case 'a': /* string and integer values (for Linux, all of them) */
|
|
|
|
case 'A': /* same as -a -o */
|
|
|
|
case 'X': /* same as -a -x */
|
|
|
|
DisplayAllOpt = true;
|
|
|
|
break;
|
2012-02-14 01:51:43 +05:30
|
|
|
case DEPRECATED_OPTION:
|
|
|
|
IgnoreDeprecated = false;
|
|
|
|
break;
|
2012-01-25 13:48:38 +05:30
|
|
|
case SYSTEM_OPTION:
|
|
|
|
IgnoreError = true;
|
2021-09-13 17:37:37 +05:30
|
|
|
rc |= PreloadSystem(setlist);
|
|
|
|
rc |= write_setting_list(setlist);
|
|
|
|
return rc;
|
|
|
|
case DRYRUN_OPTION:
|
|
|
|
DryRun = true;
|
|
|
|
break;
|
2012-01-25 13:48:38 +05:30
|
|
|
case 'r':
|
|
|
|
pattern = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf(PROCPS_NG_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'd': /* BSD: print description ("vm.kvm_size: Size of KVM") */
|
|
|
|
case 'h': /* BSD: human-readable (did FreeBSD 5 make -e default?) */
|
|
|
|
case '?':
|
|
|
|
Usage(stdout);
|
|
|
|
default:
|
|
|
|
Usage(stderr);
|
|
|
|
}
|
2012-03-07 17:19:09 +05:30
|
|
|
}
|
|
|
|
|
2012-05-03 10:54:20 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2018-01-18 16:08:02 +05:30
|
|
|
iobuf = xmalloc(iolen);
|
2017-07-07 17:39:11 +05:30
|
|
|
|
2012-01-25 13:48:38 +05:30
|
|
|
if (DisplayAllOpt)
|
|
|
|
return DisplayAll(PROC_PATH);
|
|
|
|
|
2012-05-03 10:54:20 +05:30
|
|
|
if (preloadfileOpt) {
|
|
|
|
int ret = EXIT_SUCCESS, i;
|
|
|
|
if (!preloadfile) {
|
|
|
|
if (!argc) {
|
2021-09-13 17:37:37 +05:30
|
|
|
ret |= Preload(setlist, DEFAULT_PRELOAD);
|
2012-05-03 10:54:20 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* This happens when -pfile option is
|
|
|
|
* used without space. */
|
2021-09-13 17:37:37 +05:30
|
|
|
ret |= Preload(setlist, preloadfile);
|
2012-05-03 10:54:20 +05:30
|
|
|
}
|
|
|
|
for (i = 0; i < argc; i++)
|
2021-09-13 17:37:37 +05:30
|
|
|
ret |= Preload(setlist, argv[i]);
|
|
|
|
ret |= write_setting_list(setlist);
|
2012-05-03 10:54:20 +05:30
|
|
|
return ret;
|
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
xerrx(EXIT_FAILURE, _("no variables specified\n"
|
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
|
|
|
if (NameOnly && Quiet)
|
|
|
|
xerrx(EXIT_FAILURE, _("options -N and -q cannot coexist\n"
|
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
2012-03-07 17:19:09 +05:30
|
|
|
for ( ; *argv; argv++) {
|
2021-09-13 17:37:37 +05:30
|
|
|
if (WriteMode || strchr(*argv, '=')) {
|
|
|
|
SysctlSetting *s;
|
|
|
|
if ( (s = parse_setting_line("command line", 0, *argv)) != NULL)
|
|
|
|
ReturnCode |= WriteSetting(s->key, s->path, s->value,
|
|
|
|
s->ignore_failure);
|
|
|
|
else
|
|
|
|
ReturnCode |= EXIT_FAILURE;
|
|
|
|
} else
|
2012-03-07 17:19:09 +05:30
|
|
|
ReturnCode += ReadSetting(*argv);
|
|
|
|
}
|
2012-01-25 13:48:38 +05:30
|
|
|
return ReturnCode;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|