2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* "Copyright 1999 George Staikos
|
|
|
|
* This file may be used subject to the terms and conditions of the
|
|
|
|
* GNU General Public License Version 2, or any later version
|
|
|
|
* at your option, as published by the Free Software Foundation.
|
|
|
|
* 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."
|
|
|
|
*
|
|
|
|
* Changelog:
|
|
|
|
* v1.01:
|
|
|
|
* - added -p <preload> to preload values from a file
|
2004-07-15 10:14:42 +05:30
|
|
|
* Horms:
|
|
|
|
* - 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>
|
|
|
|
#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>
|
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>
|
2011-10-09 06:27:45 +05:30
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include "nls.h"
|
2011-10-10 00:59:26 +05:30
|
|
|
#include "xalloc.h"
|
2002-11-27 13:51:30 +05:30
|
|
|
#include "proc/procps.h"
|
2002-12-12 04:25:42 +05:30
|
|
|
#include "proc/version.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
2011-06-05 23:16:49 +05:30
|
|
|
/* Proof that C++ causes brain damage: */
|
2002-12-12 04:25:42 +05:30
|
|
|
typedef int bool;
|
2002-02-02 04:17:29 +05:30
|
|
|
static bool true = 1;
|
|
|
|
static bool false = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals...
|
|
|
|
*/
|
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
static const char PROC_PATH[] = "/proc/sys/";
|
|
|
|
static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
|
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;
|
2011-10-07 12:40:50 +05:30
|
|
|
static char *pattern;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-10-07 12:40:50 +05:30
|
|
|
static int pattern_match(const char *string, const char *pattern);
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 04:54:40 +05:30
|
|
|
static void slashdot(char *restrict p, char old, char new){
|
2011-10-13 02:51:34 +05:30
|
|
|
int warned = 1;
|
2002-10-09 11:53:58 +05:30
|
|
|
p = strpbrk(p,"/.");
|
|
|
|
if(!p) return; /* nothing -- can't be, but oh well */
|
|
|
|
if(*p==new) return; /* already in desired format */
|
|
|
|
while(p){
|
|
|
|
char c = *p;
|
2011-10-13 02:51:34 +05:30
|
|
|
if((*(p+1) == '/' || *(p+1) == '.') && warned) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("separators should not be repeated: %s"), p);
|
2011-10-13 02:51:34 +05:30
|
|
|
warned = 0;
|
|
|
|
}
|
2002-10-09 11:53:58 +05:30
|
|
|
if(c==old) *p=new;
|
|
|
|
if(c==new) *p=old;
|
|
|
|
p = strpbrk(p+1,"/.");
|
|
|
|
}
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the usage format
|
|
|
|
*
|
|
|
|
*/
|
2011-06-05 23:16:49 +05:30
|
|
|
static void __attribute__ ((__noreturn__))
|
|
|
|
Usage(FILE * out)
|
|
|
|
{
|
2011-10-09 06:27:45 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
2011-06-05 23:16:49 +05:30
|
|
|
fprintf(out,
|
2011-12-17 23:02:47 +05:30
|
|
|
_(" %s [options] [variable[=value] ...]\n"), program_invocation_short_name);
|
2011-10-09 06:27:45 +05:30
|
|
|
fputs(USAGE_OPTIONS, out);
|
2011-12-28 02:16:16 +05:30
|
|
|
fputs(_(" -a, --all display all variables\n"
|
|
|
|
" -A alias of -a\n"
|
|
|
|
" -X alias of -a\n"
|
|
|
|
" -b, --binary print value without new line\n"
|
|
|
|
" -e, --ignore ignore unknown variables errors\n"
|
|
|
|
" -N, --names print variable names without values\n"
|
|
|
|
" -n, --values print only values of a variables\n"
|
|
|
|
" -p, --load[=<file>] read values from file\n"
|
|
|
|
" -f alias of -p\n"
|
|
|
|
" --system read values from all system directories\n"
|
|
|
|
" -r, --pattern <expression>\n"
|
|
|
|
" select setting that match expression\n"
|
|
|
|
" -q, --quiet do not echo variable set\n"
|
|
|
|
" -w, --write enable writing a value to variable\n"
|
|
|
|
" -o does nothing\n"
|
|
|
|
" -x does nothing\n"
|
|
|
|
" -d alias of -h\n"), out);
|
2011-10-09 06:27:45 +05:30
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("sysctl(8)"));
|
2011-06-05 23:16:49 +05:30
|
|
|
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip the leading and trailing spaces from a string
|
|
|
|
*
|
|
|
|
*/
|
2002-10-10 03:02:33 +05:30
|
|
|
static char *StripLeadingAndTrailingSpaces(char *oneline) {
|
|
|
|
char *t;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
if (!oneline || !*oneline)
|
|
|
|
return oneline;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
t = oneline;
|
|
|
|
t += strlen(oneline)-1;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
while ((*t==' ' || *t=='\t' || *t=='\n' || *t=='\r') && t!=oneline)
|
|
|
|
*t-- = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
t = oneline;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
while ((*t==' ' || *t=='\t') && *t!=0)
|
|
|
|
t++;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-10 03:02:33 +05:30
|
|
|
return t;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2004-07-15 20:52:23 +05:30
|
|
|
static int DisplayAll(const char *restrict const path);
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2002-11-26 03:12:17 +05:30
|
|
|
* Read a sysctl setting
|
2002-02-02 04:17:29 +05:30
|
|
|
*
|
|
|
|
*/
|
2002-11-26 04:54:40 +05:30
|
|
|
static int ReadSetting(const char *restrict const name) {
|
2002-11-26 03:12:17 +05:30
|
|
|
int rc = 0;
|
2002-11-26 04:54:40 +05:30
|
|
|
char *restrict tmpname;
|
|
|
|
char *restrict outname;
|
2002-11-26 03:12:17 +05:30
|
|
|
char inbuf[1025];
|
2002-11-26 04:54:40 +05:30
|
|
|
FILE *restrict fp;
|
2010-12-20 17:52:24 +05:30
|
|
|
struct stat ts;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 04:54:40 +05:30
|
|
|
if (!name || !*name) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("\"%s\" is an unknown key"), name);
|
2002-11-26 04:54:40 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-10-07 12:40:50 +05:30
|
|
|
/* used to display the output */
|
2011-10-10 00:59:26 +05:30
|
|
|
outname = xstrdup(name);
|
2011-10-07 12:40:50 +05:30
|
|
|
slashdot(outname,'/','.'); /* change / to . */
|
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
/* used to open the file */
|
2011-10-10 00:59:26 +05:30
|
|
|
tmpname = xmalloc(strlen(name)+strlen(PROC_PATH)+2);
|
2002-11-26 03:12:17 +05:30
|
|
|
strcpy(tmpname, PROC_PATH);
|
|
|
|
strcat(tmpname, name);
|
|
|
|
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
/* used to display the output */
|
2011-10-10 00:59:26 +05:30
|
|
|
outname = xstrdup(name);
|
2002-11-26 03:12:17 +05:30
|
|
|
slashdot(outname,'/','.'); /* change / to . */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2010-12-20 17:52:24 +05:30
|
|
|
if (stat(tmpname, &ts) < 0) {
|
|
|
|
if (!IgnoreError) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("cannot stat %s"), tmpname);
|
2010-12-20 17:52:24 +05:30
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ts.st_mode & S_IRUSR) == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
|
|
|
size_t len;
|
|
|
|
len = strlen(tmpname);
|
|
|
|
tmpname[len] = '/';
|
|
|
|
tmpname[len+1] = '\0';
|
|
|
|
rc = DisplayAll(tmpname);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-12-18 20:54:36 +05:30
|
|
|
if (pattern && !pattern_match(outname, pattern)){
|
|
|
|
free(outname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
fp = fopen(tmpname, "r");
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
if (!fp) {
|
|
|
|
switch(errno) {
|
|
|
|
case ENOENT:
|
2002-12-12 04:25:42 +05:30
|
|
|
if (!IgnoreError) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("\"%s\" is an unknown key"), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
break;
|
2002-11-26 03:12:17 +05:30
|
|
|
case EACCES:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("permission denied on key '%s'"), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
break;
|
2002-11-26 03:12:17 +05:30
|
|
|
default:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("reading key \"%s\""), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
} else {
|
2010-12-20 17:52:24 +05:30
|
|
|
errno = 0;
|
2004-07-15 20:52:23 +05:30
|
|
|
if(fgets(inbuf, sizeof inbuf - 1, fp)) {
|
|
|
|
// this loop is required, see
|
|
|
|
// /sbin/sysctl -a | egrep -6 dev.cdrom.info
|
|
|
|
do {
|
|
|
|
if (NameOnly) {
|
|
|
|
fprintf(stdout, "%s\n", outname);
|
|
|
|
} else {
|
|
|
|
/* already has the \n in it */
|
|
|
|
if (PrintName) {
|
|
|
|
fprintf(stdout, "%s = %s", outname, inbuf);
|
|
|
|
} else {
|
|
|
|
if (!PrintNewline) {
|
|
|
|
char *nlptr = strchr(inbuf,'\n');
|
|
|
|
if(nlptr) *nlptr='\0';
|
|
|
|
}
|
|
|
|
fprintf(stdout, "%s", inbuf);
|
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
}
|
2004-07-15 20:52:23 +05:30
|
|
|
} while(fgets(inbuf, sizeof inbuf - 1, fp));
|
|
|
|
} else {
|
|
|
|
switch(errno) {
|
|
|
|
case EACCES:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("permission denied on key '%s'"), outname);
|
2004-07-15 20:52:23 +05:30
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
case EISDIR:{
|
|
|
|
size_t len;
|
|
|
|
len = strlen(tmpname);
|
|
|
|
tmpname[len] = '/';
|
|
|
|
tmpname[len+1] = '\0';
|
2010-12-20 17:52:24 +05:30
|
|
|
fclose(fp);
|
2004-07-15 20:52:23 +05:30
|
|
|
rc = DisplayAll(tmpname);
|
2010-12-20 17:52:24 +05:30
|
|
|
goto out;
|
2004-07-15 20:52:23 +05:30
|
|
|
}
|
|
|
|
default:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("reading key \"%s\""), outname);
|
2004-07-15 20:52:23 +05:30
|
|
|
rc = -1;
|
2010-12-20 17:52:24 +05:30
|
|
|
case 0:
|
2004-07-15 20:52:23 +05:30
|
|
|
break;
|
2002-11-26 03:12:17 +05:30
|
|
|
}
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
fclose(fp);
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2010-12-20 17:52:24 +05:30
|
|
|
out:
|
2002-11-26 03:12:17 +05:30
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return rc;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
/*
|
|
|
|
* Display all the sysctl settings
|
|
|
|
*
|
|
|
|
*/
|
2004-07-15 20:52:23 +05:30
|
|
|
static int DisplayAll(const char *restrict const path) {
|
2002-11-26 03:12:17 +05:30
|
|
|
int rc = 0;
|
|
|
|
int rc2;
|
2002-11-26 04:54:40 +05:30
|
|
|
DIR *restrict dp;
|
|
|
|
struct dirent *restrict de;
|
2002-11-26 03:12:17 +05:30
|
|
|
struct stat ts;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
dp = opendir(path);
|
|
|
|
|
|
|
|
if (!dp) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("unable to open directory \"%s\""), path);
|
2002-11-26 03:12:17 +05:30
|
|
|
rc = -1;
|
|
|
|
} else {
|
2004-07-15 20:52:23 +05:30
|
|
|
readdir(dp); // skip .
|
|
|
|
readdir(dp); // skip ..
|
2002-11-26 03:12:17 +05:30
|
|
|
while (( de = readdir(dp) )) {
|
2004-07-15 20:52:23 +05:30
|
|
|
char *restrict tmpdir;
|
2011-10-10 00:59:26 +05:30
|
|
|
tmpdir = (char *restrict)xmalloc(strlen(path)+strlen(de->d_name)+2);
|
2002-11-26 03:12:17 +05:30
|
|
|
sprintf(tmpdir, "%s%s", path, de->d_name);
|
2004-07-15 20:52:23 +05:30
|
|
|
rc2 = stat(tmpdir, &ts);
|
2002-11-26 03:12:17 +05:30
|
|
|
if (rc2 != 0) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("cannot stat %s"), tmpdir);
|
2002-11-26 03:12:17 +05:30
|
|
|
} else {
|
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
|
|
|
strcat(tmpdir, "/");
|
2004-07-15 20:52:23 +05:30
|
|
|
DisplayAll(tmpdir);
|
2002-11-26 03:12:17 +05:30
|
|
|
} else {
|
|
|
|
rc |= ReadSetting(tmpdir+strlen(PROC_PATH));
|
2002-11-26 04:54:40 +05:30
|
|
|
}
|
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
free(tmpdir);
|
2002-11-26 04:54:40 +05:30
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
closedir(dp);
|
2002-11-26 04:54:40 +05:30
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
return rc;
|
2002-11-26 04:54:40 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a sysctl setting
|
|
|
|
*
|
|
|
|
*/
|
2002-10-10 03:02:33 +05:30
|
|
|
static int WriteSetting(const char *setting) {
|
|
|
|
int rc = 0;
|
|
|
|
const char *name = setting;
|
|
|
|
const char *value;
|
|
|
|
const char *equals;
|
|
|
|
char *tmpname;
|
|
|
|
char *outname;
|
2010-12-20 17:52:24 +05:30
|
|
|
FILE *fp;
|
|
|
|
struct stat ts;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-09 11:53:58 +05:30
|
|
|
if (!name) { /* probably don't want to display this err */
|
2002-02-02 04:17:29 +05:30
|
|
|
return 0;
|
|
|
|
} /* end if */
|
|
|
|
|
2006-12-05 07:46:03 +05:30
|
|
|
equals = strchr(setting, '=');
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
if (!equals) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("\"%s\" must be of the form name=value"), setting);
|
2002-02-02 04:17:29 +05:30
|
|
|
return -1;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-09 11:53:58 +05:30
|
|
|
value = equals + 1; /* point to the value in name=value */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
if (!*name || !*value || name == equals) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("Malformed setting \"%s\""), setting);
|
2002-02-02 04:17:29 +05:30
|
|
|
return -2;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-09 11:53:58 +05:30
|
|
|
/* used to open the file */
|
2011-10-10 00:59:26 +05:30
|
|
|
tmpname = xmalloc(equals-name+1+strlen(PROC_PATH));
|
2002-02-02 04:17:29 +05:30
|
|
|
strcpy(tmpname, PROC_PATH);
|
|
|
|
strncat(tmpname, name, (int)(equals-name));
|
|
|
|
tmpname[equals-name+strlen(PROC_PATH)] = 0;
|
2002-10-09 11:53:58 +05:30
|
|
|
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
|
|
|
|
|
|
|
/* used to display the output */
|
2011-10-10 00:59:26 +05:30
|
|
|
outname = xmalloc(equals-name+1);
|
2002-02-02 04:17:29 +05:30
|
|
|
strncpy(outname, name, (int)(equals-name));
|
|
|
|
outname[equals-name] = 0;
|
2002-10-09 11:53:58 +05:30
|
|
|
slashdot(outname,'/','.'); /* change / to . */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2010-12-20 17:52:24 +05:30
|
|
|
if (stat(tmpname, &ts) < 0) {
|
|
|
|
if (!IgnoreError) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("cannot stat %s"), tmpname);
|
2010-12-20 17:52:24 +05:30
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ts.st_mode & S_IWUSR) == 0) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("setting key \"%s\""), outname);
|
2010-12-20 17:52:24 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(ts.st_mode)) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("setting key \"%s\""), outname);
|
2010-12-20 17:52:24 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
fp = fopen(tmpname, "w");
|
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
switch(errno) {
|
|
|
|
case ENOENT:
|
2002-12-12 04:25:42 +05:30
|
|
|
if (!IgnoreError) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("\"%s\" is an unknown key"), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
break;
|
2002-02-02 04:17:29 +05:30
|
|
|
case EACCES:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("permission denied on key '%s'"), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
break;
|
2002-02-02 04:17:29 +05:30
|
|
|
default:
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("setting key \"%s\""), outname);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
} else {
|
2004-07-15 10:14:42 +05:30
|
|
|
rc = fprintf(fp, "%s\n", value);
|
|
|
|
if (rc < 0) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("setting key \"%s\""), outname);
|
2004-07-15 10:14:42 +05:30
|
|
|
fclose(fp);
|
2002-02-02 04:17:29 +05:30
|
|
|
} else {
|
2004-07-15 10:14:42 +05:30
|
|
|
rc=fclose(fp);
|
|
|
|
if (rc != 0)
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("setting key \"%s\""), outname);
|
2004-07-15 10:14:42 +05:30
|
|
|
}
|
|
|
|
if (rc==0 && !Quiet) {
|
2004-07-15 20:52:23 +05:30
|
|
|
if (NameOnly) {
|
|
|
|
fprintf(stdout, "%s\n", outname);
|
2004-07-15 10:14:42 +05:30
|
|
|
} else {
|
2004-07-15 20:52:23 +05:30
|
|
|
if (PrintName) {
|
|
|
|
fprintf(stdout, "%s = %s\n", outname, value);
|
|
|
|
} else {
|
|
|
|
if (PrintNewline)
|
|
|
|
fprintf(stdout, "%s\n", value);
|
|
|
|
else
|
|
|
|
fprintf(stdout, "%s", value);
|
|
|
|
}
|
2004-07-15 10:14:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2010-12-20 17:52:24 +05:30
|
|
|
out:
|
2002-02-02 04:17:29 +05:30
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
2002-10-10 03:02:33 +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:50 +05:30
|
|
|
static int pattern_match(const char *string, const char *pattern)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
regex_t re;
|
|
|
|
|
|
|
|
if (regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
|
|
|
|
return (0); /* Report error. */
|
|
|
|
}
|
|
|
|
status = regexec(&re, string, (size_t) 0, NULL, 0);
|
|
|
|
regfree(&re);
|
|
|
|
if (status != 0) {
|
|
|
|
return (0); /* Report error. */
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/*
|
2002-11-26 03:12:17 +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
|
|
|
*
|
|
|
|
*/
|
2002-12-12 04:25:42 +05:30
|
|
|
static int Preload(const char *restrict const filename) {
|
2002-12-29 05:27:10 +05:30
|
|
|
char oneline[256];
|
|
|
|
char buffer[256];
|
2002-10-10 03:02:33 +05:30
|
|
|
FILE *fp;
|
2002-11-26 03:12:17 +05:30
|
|
|
char *t;
|
|
|
|
int n = 0;
|
2002-12-12 04:25:42 +05:30
|
|
|
int rc = 0;
|
2002-11-26 03:12:17 +05:30
|
|
|
char *name, *value;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2006-06-25 02:47:51 +05:30
|
|
|
fp = (filename[0]=='-' && !filename[1])
|
|
|
|
? stdin
|
|
|
|
: fopen(filename, "r")
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!fp) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarn(_("cannot open \"%s\""), filename);
|
2002-12-12 04:25:42 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-12-29 05:27:10 +05:30
|
|
|
while (fgets(oneline, sizeof oneline, fp)) {
|
2002-11-26 03:12:17 +05:30
|
|
|
n++;
|
|
|
|
t = StripLeadingAndTrailingSpaces(oneline);
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
if (strlen(t) < 2)
|
|
|
|
continue;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
if (*t == '#' || *t == ';')
|
|
|
|
continue;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
name = strtok(t, "=");
|
|
|
|
if (!name || !*name) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("%s(%d): invalid syntax, continuing..."), filename, n);
|
2002-11-26 03:12:17 +05:30
|
|
|
continue;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
StripLeadingAndTrailingSpaces(name);
|
|
|
|
|
2011-10-07 12:40:50 +05:30
|
|
|
if (pattern && !pattern_match(name, pattern)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
value = strtok(NULL, "\n\r");
|
|
|
|
if (!value || !*value) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("%s(%d): invalid syntax, continuing..."), filename, n);
|
2002-11-26 03:12:17 +05:30
|
|
|
continue;
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
|
|
|
|
while ((*value == ' ' || *value == '\t') && *value != 0)
|
|
|
|
value++;
|
|
|
|
|
2004-07-15 20:52:23 +05:30
|
|
|
// should NameOnly affect this?
|
2002-11-26 03:12:17 +05:30
|
|
|
sprintf(buffer, "%s=%s", name, value);
|
2002-12-12 04:25:42 +05:30
|
|
|
rc |= WriteSetting(buffer);
|
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
|
|
|
|
fclose(fp);
|
2002-12-12 04:25:42 +05:30
|
|
|
return rc;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-10-07 12:40:49 +05:30
|
|
|
struct pair {
|
|
|
|
char* name;
|
|
|
|
char* value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sortpairs(const void* A, const void* B)
|
|
|
|
{
|
|
|
|
const struct pair* a = *(struct pair* const*)A;
|
|
|
|
const struct pair* b = *(struct pair* const*)B;
|
|
|
|
return strcmp(a->name, b->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PreloadSystem(void) {
|
|
|
|
unsigned di, i;
|
|
|
|
const char* dirs[] = {
|
|
|
|
"/run/sysctl.d",
|
|
|
|
"/etc/sysctl.d",
|
|
|
|
"/usr/local/lib/sysctl.d",
|
|
|
|
"/usr/lib/sysctl.d",
|
|
|
|
"/lib/sysctl.d",
|
|
|
|
};
|
|
|
|
struct pair** cfgs = NULL;
|
|
|
|
unsigned ncfgs = 0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (strlen(de->d_name) < 6 || !strcmp(de->d_name+strlen(de->d_name)-6, ".conf"))
|
|
|
|
continue;
|
|
|
|
/* check if config already known */
|
|
|
|
for (i = 0; i < ncfgs; ++i) {
|
|
|
|
if (!strcmp(cfgs[i]->name, de->d_name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < ncfgs) // already in
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ncfgs % nprealloc == 0) {
|
2011-10-10 00:59:26 +05:30
|
|
|
cfgs = xrealloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
|
2011-10-07 12:40:49 +05:30
|
|
|
}
|
2011-10-10 00:59:26 +05:30
|
|
|
cfgs[ncfgs] = xmalloc(sizeof(struct pair) + strlen(de->d_name)*2+2 + strlen(dirs[di])+1);
|
2011-10-07 12:40:49 +05:30
|
|
|
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++;
|
|
|
|
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(cfgs, ncfgs, sizeof(struct cfg*), sortpairs);
|
|
|
|
|
|
|
|
for (i = 0; i < ncfgs; ++i) {
|
|
|
|
if (!Quiet)
|
2011-10-09 06:27:45 +05:30
|
|
|
printf(_("* Applying %s ...\n"), cfgs[i]->value);
|
2011-10-07 12:40:49 +05:30
|
|
|
Preload(cfgs[i]->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Quiet)
|
2011-10-09 06:27:45 +05:30
|
|
|
printf(_("* Applying %s ...\n"), DEFAULT_PRELOAD);
|
2011-10-07 12:40:49 +05:30
|
|
|
return Preload(DEFAULT_PRELOAD);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-11-26 03:12:17 +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[])
|
|
|
|
{
|
2002-11-26 03:12:17 +05:30
|
|
|
bool SwitchesAllowed = true;
|
|
|
|
bool WriteMode = false;
|
2009-11-24 05:30:33 +05:30
|
|
|
bool DisplayAllOpt = false;
|
2011-06-05 23:16:49 +05:30
|
|
|
bool preloadfileOpt = false;
|
2002-11-26 03:12:17 +05:30
|
|
|
int ReturnCode = 0;
|
2011-06-05 23:16:49 +05:30
|
|
|
int c;
|
2002-11-26 03:12:17 +05:30
|
|
|
const char *preloadfile = DEFAULT_PRELOAD;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-10-07 12:40:49 +05:30
|
|
|
enum {
|
|
|
|
SYSTEM_OPTION = CHAR_MAX + 1
|
|
|
|
};
|
2011-06-05 23:16:49 +05:30
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"all", no_argument, NULL, 'a'},
|
|
|
|
{"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'},
|
2011-10-07 12:40:49 +05:30
|
|
|
{"system", no_argument, NULL, SYSTEM_OPTION},
|
2011-10-07 12:40:50 +05:30
|
|
|
{"pattern", required_argument, NULL, 'r'},
|
2011-06-05 23:16:49 +05:30
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2012-01-03 13:18:43 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
PrintName = true;
|
|
|
|
PrintNewline = true;
|
2002-12-12 04:25:42 +05:30
|
|
|
IgnoreError = false;
|
2004-07-15 10:14:42 +05:30
|
|
|
Quiet = false;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-26 03:12:17 +05:30
|
|
|
if (argc < 2) {
|
2011-06-05 23:16:49 +05:30
|
|
|
Usage(stderr);
|
2002-12-12 10:19:39 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-06-05 23:16:49 +05:30
|
|
|
while ((c =
|
2011-10-07 12:40:50 +05:30
|
|
|
getopt_long(argc, argv, "bneNwfp::qoxaAXr:Vdh", longopts,
|
2011-06-05 23:16:49 +05:30
|
|
|
NULL)) != -1)
|
|
|
|
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':
|
|
|
|
SwitchesAllowed = false;
|
|
|
|
WriteMode = true;
|
|
|
|
break;
|
|
|
|
case 'f': /* the NetBSD way */
|
|
|
|
case 'p':
|
|
|
|
preloadfileOpt = true;
|
|
|
|
if (optarg)
|
|
|
|
preloadfile = optarg;
|
2004-07-15 20:52:23 +05:30
|
|
|
break;
|
2011-06-05 23:16:49 +05:30
|
|
|
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;
|
2002-11-26 03:12:17 +05:30
|
|
|
break;
|
2011-10-07 12:40:49 +05:30
|
|
|
case SYSTEM_OPTION:
|
|
|
|
IgnoreError = true;
|
|
|
|
return PreloadSystem();
|
2011-10-07 12:40:50 +05:30
|
|
|
case 'r':
|
2011-10-10 00:59:26 +05:30
|
|
|
pattern = xstrdup(optarg);
|
2011-10-07 12:40:50 +05:30
|
|
|
break;
|
2011-06-05 23:16:49 +05:30
|
|
|
case 'V':
|
2011-10-09 06:27:45 +05:30
|
|
|
printf(PROCPS_NG_VERSION);
|
2011-06-05 23:16:49 +05:30
|
|
|
exit(0);
|
|
|
|
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);
|
2002-12-12 04:25:42 +05:30
|
|
|
}
|
2011-06-05 23:16:49 +05:30
|
|
|
if (DisplayAllOpt)
|
2009-11-24 05:30:33 +05:30
|
|
|
return DisplayAll(PROC_PATH);
|
2011-06-05 23:16:49 +05:30
|
|
|
if (preloadfileOpt)
|
|
|
|
return Preload(preloadfile);
|
|
|
|
|
2011-12-18 18:37:34 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("no variables specified\n"
|
2011-12-18 18:37:34 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
|
|
|
if (NameOnly && Quiet)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("options -N and -q cannot coexist\n"
|
2011-12-18 18:37:34 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
if (WriteMode || index(*argv, '='))
|
|
|
|
ReturnCode = WriteSetting(*argv);
|
|
|
|
else
|
|
|
|
ReturnCode = ReadSetting(*argv);
|
2002-11-26 03:12:17 +05:30
|
|
|
|
2002-12-12 04:25:42 +05:30
|
|
|
return ReturnCode;
|
|
|
|
}
|
2002-11-26 03:12:17 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
|