- make --system switch compatible with udev/systemd scheme

OBS-URL: https://build.opensuse.org/package/show/Base:System/procps?expand=0&rev=52
This commit is contained in:
Ludwig Nussel 2011-08-26 12:15:37 +00:00 committed by Git OBS Bridge
parent 6c4076f27e
commit 96ecb2706c
4 changed files with 148 additions and 70 deletions

View File

@ -1,58 +1,117 @@
From 45915cfc03fb82b68425445063a0bcebab1ff230 Mon Sep 17 00:00:00 2001 From f09de20e534804464d89d76c607ac877573ad9fe Mon Sep 17 00:00:00 2001
From: Ludwig Nussel <ludwig.nussel@suse.de> From: Ludwig Nussel <ludwig.nussel@suse.de>
Date: Wed, 18 May 2011 08:16:39 +0200 Date: Fri, 26 Aug 2011 11:11:50 +0200
Subject: [PATCH procps 1/3] add --system switch Subject: [PATCH 1/3] add --system switch
instead of requiring distributions to construct a loop around sysctl instead of requiring distributions to construct a loop around sysctl
in boot scripts just scan a set of default directories if the --system in boot scripts just scan a set of default directories if the --system
switch is used. switch is used.
Config files are applied in alphabetic order of their base name.
Each base name is only applied once according to the directory
preference. /etc/sysctl.conf is always applied last.
--- ---
sysctl.c | 35 +++++++++++++++++++++++++++++++++++ sysctl.8 | 5 ++++
1 files changed, 35 insertions(+), 0 deletions(-) sysctl.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+), 0 deletions(-)
diff --git a/sysctl.8 b/sysctl.8
index e26c4fb..d2b0ceb 100644
--- a/sysctl.8
+++ b/sysctl.8
@@ -64,6 +64,11 @@ Display all values currently available.
.TP
.B "-A"
Display all values currently available in table form.
+.TP
+.B "--system"
+Load settings from system configuration files (/lib/sysctl.d/*.conf,
+/usr/lib/sysctl.d/*.conf, /usr/local/lib/sysctl.d/*.conf,
+/etc/sysctl.d/*.conf, /run/sysctl.d/*.conf, /etc/sysctl.conf)
.SH EXAMPLES
.TP
/sbin/sysctl -a
diff --git a/sysctl.c b/sysctl.c diff --git a/sysctl.c b/sysctl.c
index 9be79ce..3445efe 100644 index 9be79ce..bf4e529 100644
--- a/sysctl.c --- a/sysctl.c
+++ b/sysctl.c +++ b/sysctl.c
@@ -453,6 +453,37 @@ static int Preload(const char *restrict const filename) { @@ -453,6 +453,76 @@ static int Preload(const char *restrict const filename) {
return rc; return rc;
} }
+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) { +static int PreloadSystem(void) {
+ unsigned i; + unsigned di, i;
+ const char* dirs[] = { + const char* dirs[] = {
+ "/lib/sysctl.d", + "/run/sysctl.d",
+ "/usr/lib/sysctl.d",
+ "/usr/local/lib/sysctl.d",
+ "/etc/sysctl.d", + "/etc/sysctl.d",
+ "/usr/local/lib/sysctl.d",
+ "/usr/lib/sysctl.d",
+ "/lib/sysctl.d",
+ }; + };
+ for (i=0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { + struct pair** cfgs = NULL;
+ unsigned ncfgs = 0;
+ enum { nprealloc = 16 };
+
+ for (di=0; di < sizeof(dirs)/sizeof(dirs[0]); ++di) {
+ struct dirent* de; + struct dirent* de;
+ DIR* dp = opendir(dirs[i]); + DIR* dp = opendir(dirs[di]);
+ if (!dp) + if (!dp)
+ continue; + continue;
+ while (( de = readdir(dp) )) { + while (( de = readdir(dp) )) {
+ char buf[PATH_MAX]; + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
+ if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { + continue;
+ continue; + }
+ } + if (strlen(de->d_name) < 6 || !strcmp(de->d_name+strlen(de->d_name)-6, ".conf"))
+ if (strlen(de->d_name) < 6 || !strcmp(de->d_name+strlen(de->d_name)-6, ".conf")) + continue;
+ continue; + /* check if config already known */
+ snprintf(buf, sizeof(buf), "%s/%s", dirs[i], de->d_name); + for (i = 0; i < ncfgs; ++i) {
+ if (!Quiet) + if (!strcmp(cfgs[i]->name, de->d_name))
+ printf("* Applying %s ...\n", buf); + break;
+ Preload(buf); + }
+ if (i < ncfgs) // already in
+ continue;
+
+ if (ncfgs % nprealloc == 0) {
+ cfgs = realloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
+ }
+ cfgs[ncfgs] = malloc(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++;
+
+ } + }
+ closedir(dp); + closedir(dp);
+ } + }
+ if (!Quiet) +
+ printf("* Applying %s ...\n", DEFAULT_PRELOAD); + qsort(cfgs, ncfgs, sizeof(struct cfg*), sortpairs);
+ return Preload(DEFAULT_PRELOAD); +
+ for (i = 0; i < ncfgs; ++i) {
+ if (!Quiet)
+ printf("* Applying %s ...\n", cfgs[i]->value);
+ Preload(cfgs[i]->value);
+ }
+
+ if (!Quiet)
+ printf("* Applying %s ...\n", DEFAULT_PRELOAD);
+ return Preload(DEFAULT_PRELOAD);
+} +}
/* /*
@@ -488,6 +519,10 @@ int main(int argc, char *argv[]) { @@ -488,6 +558,10 @@ int main(int argc, char *argv[]) {
fprintf(stdout, "sysctl (%s)\n",procps_version); fprintf(stdout, "sysctl (%s)\n",procps_version);
exit(0); exit(0);
} }

View File

@ -1,28 +1,23 @@
From b73ff507f616c74ac94e7b1bef2ce51fa9bb2806 Mon Sep 17 00:00:00 2001 From 596511f915e210d27c84eaca0a5b4c7c48ec96e0 Mon Sep 17 00:00:00 2001
From: Ludwig Nussel <ludwig.nussel@suse.de> From: Ludwig Nussel <ludwig.nussel@suse.de>
Date: Wed, 18 May 2011 08:20:09 +0200 Date: Fri, 26 Aug 2011 11:31:39 +0200
Subject: [PATCH procps 2/3] implement --pattern option Subject: [PATCH 2/3] implement --pattern option
Useful for e.g network hook scripts together with --system to only apply Useful for e.g network hook scripts together with --system to only apply
sysctls for a specific network interface. sysctls for a specific network interface.
--- ---
sysctl.8 | 14 ++++++++++++++ sysctl.8 | 10 ++++++++-
sysctl.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sysctl.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 66 insertions(+), 0 deletions(-) 2 files changed, 67 insertions(+), 6 deletions(-)
diff --git a/sysctl.8 b/sysctl.8 diff --git a/sysctl.8 b/sysctl.8
index e26c4fb..9f6de65 100644 index d2b0ceb..406bb59 100644
--- a/sysctl.8 --- a/sysctl.8
+++ b/sysctl.8 +++ b/sysctl.8
@@ -64,6 +64,16 @@ Display all values currently available. @@ -69,6 +69,11 @@ Display all values currently available in table form.
.TP Load settings from system configuration files (/lib/sysctl.d/*.conf,
.B "-A" /usr/lib/sysctl.d/*.conf, /usr/local/lib/sysctl.d/*.conf,
Display all values currently available in table form. /etc/sysctl.d/*.conf, /run/sysctl.d/*.conf, /etc/sysctl.conf)
+.TP
+.B "--system"
+Load settings from system configuration files (/lib/sysctl.d/*.conf,
+/usr/lib/sysctl.d/*.conf, /usr/local/lib/sysctl.d/*.conf,
+/etc/sysctl.d/*.conf, /etc/sysctl.conf)
+.TP +.TP
+.B "--pattern" PATTERN +.B "--pattern" PATTERN
+Ignore settings that don't patch PATTERN. A star '*' is recognized +Ignore settings that don't patch PATTERN. A star '*' is recognized
@ -31,10 +26,11 @@ index e26c4fb..9f6de65 100644
.SH EXAMPLES .SH EXAMPLES
.TP .TP
/sbin/sysctl -a /sbin/sysctl -a
@@ -73,6 +83,10 @@ Display all values currently available in table form. @@ -77,7 +82,10 @@ Load settings from system configuration files (/lib/sysctl.d/*.conf,
.TP
/sbin/sysctl -w kernel.domainname="example.com" /sbin/sysctl -w kernel.domainname="example.com"
.TP .TP
/sbin/sysctl -p /etc/sysctl.conf -/sbin/sysctl -p /etc/sysctl.conf
+.TP +.TP
+/sbin/sysctl --pattern 'net.ipv4.conf.*.forwarding' -a +/sbin/sysctl --pattern 'net.ipv4.conf.*.forwarding' -a
+.TP +.TP
@ -43,7 +39,7 @@ index e26c4fb..9f6de65 100644
.I /proc/sys .I /proc/sys
.I /etc/sysctl.conf .I /etc/sysctl.conf
diff --git a/sysctl.c b/sysctl.c diff --git a/sysctl.c b/sysctl.c
index 3445efe..b68170b 100644 index bf4e529..1867777 100644
--- a/sysctl.c --- a/sysctl.c
+++ b/sysctl.c +++ b/sysctl.c
@@ -50,6 +50,7 @@ static bool PrintName; @@ -50,6 +50,7 @@ static bool PrintName;
@ -62,21 +58,37 @@ index 3445efe..b68170b 100644
static void slashdot(char *restrict p, char old, char new){ static void slashdot(char *restrict p, char old, char new){
p = strpbrk(p,"/."); p = strpbrk(p,"/.");
@@ -145,6 +147,10 @@ static int ReadSetting(const char *restrict const name) { @@ -135,16 +137,21 @@ static int ReadSetting(const char *restrict const name) {
outname = strdup(name); return -1;
slashdot(outname,'/','.'); /* change / to . */ }
+ /* used to display the output */
+ outname = strdup(name);
+ slashdot(outname,'/','.'); /* change / to . */
+
+ if (pattern && !pattern_match(outname)){ + if (pattern && !pattern_match(outname)){
+ goto out; + free(outname);
+ return 0;
+ } + }
+ +
/* used to open the file */
tmpname = malloc(strlen(name)+strlen(PROC_PATH)+2);
strcpy(tmpname, PROC_PATH);
strcat(tmpname, name);
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
- /* used to display the output */
- outname = strdup(name);
- slashdot(outname,'/','.'); /* change / to . */
-
if (stat(tmpname, &ts) < 0) { if (stat(tmpname, &ts) < 0) {
if (!IgnoreError) { if (!IgnoreError) {
perror(tmpname); perror(tmpname);
@@ -391,7 +397,39 @@ out: @@ -391,7 +398,39 @@ out:
return rc; return rc;
} }
-
+static int pattern_match(const char* name) { +static int pattern_match(const char* name) {
+ const char* p = pattern; + const char* p = pattern;
+ if (!p || !name) + if (!p || !name)
@ -101,7 +113,7 @@ index 3445efe..b68170b 100644
+ } + }
+ if (*p != *name) + if (*p != *name)
+ return 0; + return 0;
+
+ ++p; + ++p;
+ ++name; + ++name;
+ continue; + continue;
@ -113,7 +125,7 @@ index 3445efe..b68170b 100644
/* /*
* Preload the sysctl's from the conf file * Preload the sysctl's from the conf file
@@ -435,6 +473,10 @@ static int Preload(const char *restrict const filename) { @@ -435,6 +474,10 @@ static int Preload(const char *restrict const filename) {
StripLeadingAndTrailingSpaces(name); StripLeadingAndTrailingSpaces(name);
@ -124,7 +136,7 @@ index 3445efe..b68170b 100644
value = strtok(NULL, "\n\r"); value = strtok(NULL, "\n\r");
if (!value || !*value) { if (!value || !*value) {
fprintf(stderr, WARN_BAD_LINE, filename, n); fprintf(stderr, WARN_BAD_LINE, filename, n);
@@ -523,6 +565,16 @@ int main(int argc, char *argv[]) { @@ -562,6 +605,16 @@ int main(int argc, char *argv[]) {
IgnoreError = true; IgnoreError = true;
return PreloadSystem(); return PreloadSystem();
} }

View File

@ -1,14 +1,14 @@
From 40c2bfe16c9a9e9562c686afa9d6b7f754a5c8d9 Mon Sep 17 00:00:00 2001 From 0925e01c49599c541ea3d40a7e30a313bb77efce Mon Sep 17 00:00:00 2001
From: Ludwig Nussel <ludwig.nussel@suse.de> From: Ludwig Nussel <ludwig.nussel@suse.de>
Date: Tue, 17 May 2011 16:35:18 +0200 Date: Tue, 17 May 2011 16:35:18 +0200
Subject: [PATCH procps 3/3] read sysctls also from /boot/sysctl.conf-$kernelversion Subject: [PATCH 3/3] read sysctls also from /boot/sysctl.conf-$kernelversion
--- ---
sysctl.c | 10 ++++++++++ sysctl.c | 11 +++++++++++
1 files changed, 10 insertions(+), 0 deletions(-) 1 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/sysctl.c b/sysctl.c diff --git a/sysctl.c b/sysctl.c
index b68170b..6967ca8 100644 index 1867777..a41539b 100644
--- a/sysctl.c --- a/sysctl.c
+++ b/sysctl.c +++ b/sysctl.c
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
@ -19,17 +19,18 @@ index b68170b..6967ca8 100644
#include <dirent.h> #include <dirent.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@@ -497,12 +498,21 @@ static int Preload(const char *restrict const filename) { @@ -510,6 +511,7 @@ static int sortpairs(const void* A, const void* B)
static int PreloadSystem(void) { static int PreloadSystem(void) {
unsigned i; unsigned di, i;
+ struct utsname uts; + struct utsname uts;
const char* dirs[] = { const char* dirs[] = {
"/lib/sysctl.d", "/run/sysctl.d",
"/usr/lib/sysctl.d",
"/usr/local/lib/sysctl.d",
"/etc/sysctl.d", "/etc/sysctl.d",
}; @@ -556,6 +558,15 @@ static int PreloadSystem(void) {
qsort(cfgs, ncfgs, sizeof(struct cfg*), sortpairs);
+ if (uname(&uts) == 0) { + if (uname(&uts) == 0) {
+ char buf[PATH_MAX]; + char buf[PATH_MAX];
+ snprintf(buf, sizeof(buf), "/boot/sysctl.conf-%s", uts.release); + snprintf(buf, sizeof(buf), "/boot/sysctl.conf-%s", uts.release);
@ -38,9 +39,10 @@ index b68170b..6967ca8 100644
+ Preload(buf); + Preload(buf);
+ } + }
+ } + }
for (i=0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { +
struct dirent* de; for (i = 0; i < ncfgs; ++i) {
DIR* dp = opendir(dirs[i]); if (!Quiet)
printf("* Applying %s ...\n", cfgs[i]->value);
-- --
1.7.3.4 1.7.3.4

View File

@ -1,3 +1,8 @@
-------------------------------------------------------------------
Fri Aug 26 09:36:03 UTC 2011 - lnussel@suse.de
- make --system switch compatible with udev/systemd scheme
------------------------------------------------------------------- -------------------------------------------------------------------
Wed Aug 24 10:09:36 UTC 2011 - werner@suse.de Wed Aug 24 10:09:36 UTC 2011 - werner@suse.de