sysctl -e
This commit is contained in:
parent
cf16b01a99
commit
c0e2e83dbd
2
Makefile
2
Makefile
@ -146,7 +146,7 @@ w.o: w.c
|
|||||||
|
|
||||||
############ prog.o --> prog
|
############ prog.o --> prog
|
||||||
|
|
||||||
pmap w uptime tload free vmstat utmp pgrep skill: % : %.o $(LIBPROC)
|
pmap w uptime tload free sysctl vmstat utmp pgrep skill: % : %.o $(LIBPROC)
|
||||||
$(CC) $(LDFLAGS) -o $@ $^
|
$(CC) $(LDFLAGS) -o $@ $^
|
||||||
|
|
||||||
top: % : %.o $(LIBPROC)
|
top: % : %.o $(LIBPROC)
|
||||||
|
13
sysctl.8
13
sysctl.8
@ -10,15 +10,15 @@
|
|||||||
.SH NAME
|
.SH NAME
|
||||||
sysctl \- configure kernel parameters at runtime
|
sysctl \- configure kernel parameters at runtime
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B "sysctl [-n] variable ..."
|
.B "sysctl [-n] [-e] variable ..."
|
||||||
.br
|
.br
|
||||||
.B "sysctl [-n] -w variable=value ..."
|
.B "sysctl [-n] [-e] -w variable=value ..."
|
||||||
.br
|
.br
|
||||||
.B "sysctl [-n] -p <filename> (default /etc/sysctl.conf)"
|
.B "sysctl [-n] [-e] -p <filename> (default /etc/sysctl.conf)"
|
||||||
.br
|
.br
|
||||||
.B "sysctl [-n] -a"
|
.B "sysctl [-n] [-e] -a"
|
||||||
.br
|
.br
|
||||||
.B "sysctl [-n] -A"
|
.B "sysctl [-n] [-e] -A"
|
||||||
.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
|
||||||
@ -42,6 +42,9 @@ quotes. This requires the -w parameter to use.
|
|||||||
.B "-n"
|
.B "-n"
|
||||||
Use this option to disable printing of the key name when printing values.
|
Use this option to disable printing of the key name when printing values.
|
||||||
.TP
|
.TP
|
||||||
|
.B "-e"
|
||||||
|
Use this option to ignore errors about unknown keys.
|
||||||
|
.TP
|
||||||
.B "-w"
|
.B "-w"
|
||||||
Use this option when you want to change a sysctl setting.
|
Use this option when you want to change a sysctl setting.
|
||||||
.TP
|
.TP
|
||||||
|
98
sysctl.c
98
sysctl.c
@ -27,16 +27,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "proc/procps.h"
|
#include "proc/procps.h"
|
||||||
|
#include "proc/version.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* Additional types we might need.
|
// Proof that C++ causes brain damage:
|
||||||
*/
|
|
||||||
typedef int bool;
|
typedef int bool;
|
||||||
|
|
||||||
static bool true = 1;
|
static bool true = 1;
|
||||||
static bool false = 0;
|
static bool false = 0;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Globals...
|
* Globals...
|
||||||
*/
|
*/
|
||||||
@ -45,6 +43,7 @@ static const char PROC_PATH[] = "/proc/sys/";
|
|||||||
static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
|
static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
|
||||||
static bool PrintName;
|
static bool PrintName;
|
||||||
static bool PrintNewline;
|
static bool PrintNewline;
|
||||||
|
static bool IgnoreError;
|
||||||
|
|
||||||
/* error messages */
|
/* error messages */
|
||||||
static const char ERR_UNKNOWN_PARAMETER[] = "error: Unknown parameter '%s'\n";
|
static const char ERR_UNKNOWN_PARAMETER[] = "error: Unknown parameter '%s'\n";
|
||||||
@ -78,13 +77,13 @@ static void slashdot(char *restrict p, char old, char new){
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static int Usage(const char *restrict const name) {
|
static int Usage(const char *restrict const name) {
|
||||||
printf("usage: %s [-n] variable ... \n"
|
printf("usage: %s [-n] [-e] variable ... \n"
|
||||||
" %s [-n] -w variable=value ... \n"
|
" %s [-n] [-e] -w variable=value ... \n"
|
||||||
" %s [-n] -a \n"
|
" %s [-n] [-e] -a \n"
|
||||||
" %s [-n] -p <file> (default /etc/sysctl.conf) \n"
|
" %s [-n] [-e] -p <file> (default /etc/sysctl.conf) \n"
|
||||||
" %s [-n] -A\n", name, name, name, name, name);
|
" %s [-n] [-e] -A\n", name, name, name, name, name);
|
||||||
return -1;
|
return -1;
|
||||||
} /* end Usage() */
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -109,7 +108,7 @@ static char *StripLeadingAndTrailingSpaces(char *oneline) {
|
|||||||
t++;
|
t++;
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
} /* end StripLeadingAndTrailingSpaces() */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -144,16 +143,20 @@ static int ReadSetting(const char *restrict const name) {
|
|||||||
if (!fp) {
|
if (!fp) {
|
||||||
switch(errno) {
|
switch(errno) {
|
||||||
case ENOENT:
|
case ENOENT:
|
||||||
|
if (!IgnoreError) {
|
||||||
fprintf(stderr, ERR_INVALID_KEY, outname);
|
fprintf(stderr, ERR_INVALID_KEY, outname);
|
||||||
|
rc = -1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case EACCES:
|
case EACCES:
|
||||||
fprintf(stderr, ERR_PERMISSION_DENIED, outname);
|
fprintf(stderr, ERR_PERMISSION_DENIED, outname);
|
||||||
|
rc = -1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, ERR_UNKNOWN_READING, errno, outname);
|
fprintf(stderr, ERR_UNKNOWN_READING, errno, outname);
|
||||||
break;
|
|
||||||
} /* end switch */
|
|
||||||
rc = -1;
|
rc = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
while(fgets(inbuf, 1024, fp)) {
|
while(fgets(inbuf, 1024, fp)) {
|
||||||
/* already has the \n in it */
|
/* already has the \n in it */
|
||||||
@ -166,14 +169,14 @@ static int ReadSetting(const char *restrict const name) {
|
|||||||
}
|
}
|
||||||
fprintf(stdout, "%s", inbuf);
|
fprintf(stdout, "%s", inbuf);
|
||||||
}
|
}
|
||||||
} /* endwhile */
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
free(tmpname);
|
free(tmpname);
|
||||||
free(outname);
|
free(outname);
|
||||||
return rc;
|
return rc;
|
||||||
} /* end ReadSetting() */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -240,14 +243,14 @@ static int WriteSetting(const char *setting) {
|
|||||||
if (!equals) {
|
if (!equals) {
|
||||||
fprintf(stderr, ERR_NO_EQUALS, setting);
|
fprintf(stderr, ERR_NO_EQUALS, setting);
|
||||||
return -1;
|
return -1;
|
||||||
} /* end if */
|
}
|
||||||
|
|
||||||
value = equals + 1; /* point to the value in name=value */
|
value = equals + 1; /* point to the value in name=value */
|
||||||
|
|
||||||
if (!*name || !*value || name == equals) {
|
if (!*name || !*value || name == equals) {
|
||||||
fprintf(stderr, ERR_MALFORMED_SETTING, setting);
|
fprintf(stderr, ERR_MALFORMED_SETTING, setting);
|
||||||
return -2;
|
return -2;
|
||||||
} /* end if */
|
}
|
||||||
|
|
||||||
/* used to open the file */
|
/* used to open the file */
|
||||||
tmpname = malloc(equals-name+1+strlen(PROC_PATH));
|
tmpname = malloc(equals-name+1+strlen(PROC_PATH));
|
||||||
@ -267,16 +270,20 @@ static int WriteSetting(const char *setting) {
|
|||||||
if (!fp) {
|
if (!fp) {
|
||||||
switch(errno) {
|
switch(errno) {
|
||||||
case ENOENT:
|
case ENOENT:
|
||||||
|
if (!IgnoreError) {
|
||||||
fprintf(stderr, ERR_INVALID_KEY, outname);
|
fprintf(stderr, ERR_INVALID_KEY, outname);
|
||||||
|
rc = -1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case EACCES:
|
case EACCES:
|
||||||
fprintf(stderr, ERR_PERMISSION_DENIED, outname);
|
fprintf(stderr, ERR_PERMISSION_DENIED, outname);
|
||||||
|
rc = -1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, ERR_UNKNOWN_WRITING, errno, outname);
|
fprintf(stderr, ERR_UNKNOWN_WRITING, errno, outname);
|
||||||
break;
|
|
||||||
} /* end switch */
|
|
||||||
rc = -1;
|
rc = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "%s\n", value);
|
fprintf(fp, "%s\n", value);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
@ -289,12 +296,12 @@ static int WriteSetting(const char *setting) {
|
|||||||
else
|
else
|
||||||
fprintf(stdout, "%s", value);
|
fprintf(stdout, "%s", value);
|
||||||
}
|
}
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
free(tmpname);
|
free(tmpname);
|
||||||
free(outname);
|
free(outname);
|
||||||
return rc;
|
return rc;
|
||||||
} /* end WriteSetting() */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -303,18 +310,19 @@ static int WriteSetting(const char *setting) {
|
|||||||
* - we parse the file and then reform it (strip out whitespace)
|
* - we parse the file and then reform it (strip out whitespace)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static void Preload(const char *restrict const filename) {
|
static int Preload(const char *restrict const filename) {
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char oneline[257];
|
char oneline[257];
|
||||||
char buffer[257];
|
char buffer[257];
|
||||||
char *t;
|
char *t;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
int rc = 0;
|
||||||
char *name, *value;
|
char *name, *value;
|
||||||
|
|
||||||
if (!filename || ((fp = fopen(filename, "r")) == NULL)) {
|
if (!filename || ((fp = fopen(filename, "r")) == NULL)) {
|
||||||
fprintf(stderr, ERR_PRELOAD_FILE, filename);
|
fprintf(stderr, ERR_PRELOAD_FILE, filename);
|
||||||
return;
|
return -1;
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
while (fgets(oneline, 256, fp)) {
|
while (fgets(oneline, 256, fp)) {
|
||||||
oneline[256] = 0;
|
oneline[256] = 0;
|
||||||
@ -331,7 +339,7 @@ static void Preload(const char *restrict const filename) {
|
|||||||
if (!name || !*name) {
|
if (!name || !*name) {
|
||||||
fprintf(stderr, WARN_BAD_LINE, filename, n);
|
fprintf(stderr, WARN_BAD_LINE, filename, n);
|
||||||
continue;
|
continue;
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
StripLeadingAndTrailingSpaces(name);
|
StripLeadingAndTrailingSpaces(name);
|
||||||
|
|
||||||
@ -339,17 +347,18 @@ static void Preload(const char *restrict const filename) {
|
|||||||
if (!value || !*value) {
|
if (!value || !*value) {
|
||||||
fprintf(stderr, WARN_BAD_LINE, filename, n);
|
fprintf(stderr, WARN_BAD_LINE, filename, n);
|
||||||
continue;
|
continue;
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
while ((*value == ' ' || *value == '\t') && *value != 0)
|
while ((*value == ' ' || *value == '\t') && *value != 0)
|
||||||
value++;
|
value++;
|
||||||
|
|
||||||
sprintf(buffer, "%s=%s", name, value);
|
sprintf(buffer, "%s=%s", name, value);
|
||||||
WriteSetting(buffer);
|
rc |= WriteSetting(buffer);
|
||||||
} /* endwhile */
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
} /* end Preload() */
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -366,6 +375,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
PrintName = true;
|
PrintName = true;
|
||||||
PrintNewline = true;
|
PrintNewline = true;
|
||||||
|
IgnoreError = false;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
return Usage(me);
|
return Usage(me);
|
||||||
@ -383,6 +393,9 @@ int main(int argc, char **argv) {
|
|||||||
case 'n':
|
case 'n':
|
||||||
PrintName = false;
|
PrintName = false;
|
||||||
break;
|
break;
|
||||||
|
case 'e':
|
||||||
|
IgnoreError = true;
|
||||||
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
SwitchesAllowed = false;
|
SwitchesAllowed = false;
|
||||||
WriteMode = true;
|
WriteMode = true;
|
||||||
@ -391,32 +404,33 @@ int main(int argc, char **argv) {
|
|||||||
argv++;
|
argv++;
|
||||||
if (argv && *argv && **argv) {
|
if (argv && *argv && **argv) {
|
||||||
preloadfile = *argv;
|
preloadfile = *argv;
|
||||||
} /* endif */
|
}
|
||||||
|
return Preload(preloadfile);
|
||||||
Preload(preloadfile);
|
|
||||||
return(0);
|
|
||||||
break;
|
|
||||||
case 'a': /* string and integer values (for Linux, all of them) */
|
case 'a': /* string and integer values (for Linux, all of them) */
|
||||||
case 'A': /* the above, including "opaques" (would be unprintable) */
|
case 'A': /* the above, including "opaques" (would be unprintable) */
|
||||||
case 'X': /* the above, with opaques completly printed in hex */
|
case 'X': /* the above, with opaques completly printed in hex */
|
||||||
SwitchesAllowed = false;
|
SwitchesAllowed = false;
|
||||||
return DisplayAll(PROC_PATH, ((*argv)[1] == 'a') ? false : true);
|
return DisplayAll(PROC_PATH, ((*argv)[1] == 'a') ? false : true);
|
||||||
|
case 'V':
|
||||||
|
fprintf(stdout, "sysctl (%s)\n",procps_version);
|
||||||
|
exit(0);
|
||||||
case 'h':
|
case 'h':
|
||||||
case '?':
|
case '?':
|
||||||
return Usage(me);
|
return Usage(me);
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, ERR_UNKNOWN_PARAMETER, *argv);
|
fprintf(stderr, ERR_UNKNOWN_PARAMETER, *argv);
|
||||||
return Usage(me);
|
return Usage(me);
|
||||||
} /* end switch */
|
}
|
||||||
} else {
|
} else {
|
||||||
SwitchesAllowed = false;
|
SwitchesAllowed = false;
|
||||||
if (WriteMode)
|
if (WriteMode)
|
||||||
ReturnCode = WriteSetting(*argv);
|
ReturnCode = WriteSetting(*argv);
|
||||||
else ReadSetting(*argv);
|
else
|
||||||
} /* end if */
|
ReturnCode = ReadSetting(*argv);
|
||||||
} /* end for */
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ReturnCode;
|
return ReturnCode;
|
||||||
} /* end main */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user