[procps] [PATCH 1/2] sysctl: add --system switch

  • From: Ludwig Nussel <ludwig.nussel@xxxxxxx>
  • To: procps@xxxxxxxxxxxxx
  • Date: Tue, 24 May 2011 11:52:00 +0200

instead of requiring distributions to construct a loop around sysctl
in boot scripts just scan a set of default directories if the --system
switch is used.
---
 sysctl.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/sysctl.c b/sysctl.c
index 9be79ce..3445efe 100644
--- a/sysctl.c
+++ b/sysctl.c
@@ -453,6 +453,37 @@ static int Preload(const char *restrict const filename) {
    return rc;
 }
 
+static int PreloadSystem(void) {
+   unsigned i;
+   const char* dirs[] = {
+      "/lib/sysctl.d",
+      "/usr/lib/sysctl.d",
+      "/usr/local/lib/sysctl.d",
+      "/etc/sysctl.d",
+   };
+   for (i=0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
+      struct dirent* de;
+      DIR* dp = opendir(dirs[i]);
+      if (!dp)
+         continue;
+      while (( de = readdir(dp) )) {
+         char buf[PATH_MAX];
+         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;
+         snprintf(buf, sizeof(buf), "%s/%s", dirs[i], de->d_name);
+         if (!Quiet)
+             printf("* Applying %s ...\n", buf);
+         Preload(buf);
+      }
+      closedir(dp);
+    }
+    if (!Quiet)
+        printf("* Applying %s ...\n", DEFAULT_PRELOAD);
+    return Preload(DEFAULT_PRELOAD);
+}
 
 
 /*
@@ -488,6 +519,10 @@ int main(int argc, char *argv[]) {
                  fprintf(stdout, "sysctl (%s)\n",procps_version);
                  exit(0);
               }
+              if (!strcmp("--system",*argv)) {
+                 IgnoreError = true;
+                 return PreloadSystem();
+              }
               fprintf(stderr, ERR_UNKNOWN_PARAMETER, *argv);
               return Usage(me);
          }
-- 
1.7.3.4


Other related posts:

  • » [procps] [PATCH 1/2] sysctl: add --system switch - Ludwig Nussel