]> granicus.if.org Git - procps-ng/commitdiff
sysctl: print dotted keys again
authorCraig Small <csmall@dropbear.xyz>
Sat, 9 Apr 2022 04:18:28 +0000 (14:18 +1000)
committerCraig Small <csmall@dropbear.xyz>
Sat, 9 Apr 2022 04:18:28 +0000 (14:18 +1000)
When the globbing update was put into sysctl, you could no longer
simply use the keys because one key could potentially be
multiple paths once the glob expansion occured.  Using the path
instead gave a unique output.

Except certain programs, such as salt, expected the output to use
the dotted path "kernel.hostname" and not "kernel/hostname".

We can no longer use the original key, so now for each path:
  Copy the path
  strip off /proc/
  convert all / to .

The sysctl testsuite was also updated to check for a few different
types of conversion failures.

References:
 commit 6389deca5bf667f5fab5912acde78ba8e0febbc7
 https://www.freelists.org/post/procps/some-procpsn4400-fixes,4
 https://repo.saltproject.io/

Signed-off-by: Craig Small <csmall@dropbear.xyz>
NEWS
sysctl.c
testsuite/sysctl.test/sysctl_write.exp
testsuite/sysctl_slash_test.conf [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 50297d7b439dbcfd6fab478bfd4e079e334719ad..aef9c1a74c84b6e3e74517e0b3f037afbc67a5c0 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ procps-ng-NEXT
     Re-add elogind support                                 merge #151
   * ps: threads again display when -L is used with -q      issue #234
   * ps: proper aix format string behavior was restored
+  * sysctl: print dotted keys again
 
 procps-ng-4.0.0
 ---------------
index 430f7591ac3b57561d2d1b7b650652b10375264d..c711692f8eca175a77ab42ce2eafd2d63b5b0a96 100644 (file)
--- a/sysctl.c
+++ b/sysctl.c
@@ -169,7 +169,7 @@ static SysctlSetting *setting_new(
         strcat(path + proc_len, key+1);
     else
         strcat(path + proc_len, key);
-    /* change . to / */
+    /* change . to / for path */
     slashdot(path + proc_len, '.', '/');
 
     s = xmalloc(sizeof(SysctlSetting));
@@ -535,6 +535,7 @@ static int WriteSetting(
     int rc = EXIT_SUCCESS;
     FILE *fp;
     struct stat ts;
+    char *dotted_key;
 
     if (!key || !path)
         return rc;
@@ -551,13 +552,22 @@ static int WriteSetting(
         return EXIT_FAILURE;
     }
 
+    /* Convert the globbed path into a dotted key */
+    if ( (dotted_key = strdup(path + strlen(PROC_PATH))) == NULL) {
+       xerrx(EXIT_FAILURE, _("strdup key"));
+       return EXIT_FAILURE;
+    }
+    slashdot(dotted_key, '/', '.');
+
     if ((ts.st_mode & S_IWUSR) == 0) {
-        xwarn(_("setting key \"%s\""), key);
+        xwarn(_("setting key \"%s\""), dotted_key);
+       free(dotted_key);
         return rc;
     }
 
     if (S_ISDIR(ts.st_mode)) {
-        xwarn(_("setting key \"%s\""), key);
+        xwarn(_("setting key \"%s\""), dotted_key);
+       free(dotted_key);
         return rc;
     }
 
@@ -567,7 +577,7 @@ static int WriteSetting(
             case ENOENT:
                 if (!IgnoreError) {
                     xwarnx(_("\"%s\" is an unknown key%s"),
-                           key, (ignore_failure?_(", ignoring"):""));
+                           dotted_key, (ignore_failure?_(", ignoring"):""));
                     if (!ignore_failure)
                         rc = EXIT_FAILURE;
                 }
@@ -576,11 +586,11 @@ static int WriteSetting(
             case EROFS:
             case EACCES:
                 xwarnx(_("permission denied on key \"%s\"%s"),
-                       key, (ignore_failure?_(", ignoring"):""));
+                       dotted_key, (ignore_failure?_(", ignoring"):""));
                 break;
             default:
                 xwarn(_("setting key \"%s\"%s"),
-                      key, (ignore_failure?_(", ignoring"):""));
+                      dotted_key, (ignore_failure?_(", ignoring"):""));
                 break;
             }
             if (!ignore_failure && errno != ENOENT)
@@ -589,7 +599,8 @@ static int WriteSetting(
             if (0 < fprintf(fp, "%s\n", value))
                 rc = EXIT_SUCCESS;
             if (close_stream(fp) != 0) {
-                xwarn(_("setting key \"%s\""), path);
+                xwarn(_("setting key \"%s\""), dotted_key);
+               free(dotted_key);
                 return EXIT_FAILURE;
             }
         }
@@ -599,7 +610,7 @@ static int WriteSetting(
             printf("%s\n", value);
         } else {
             if (PrintName) {
-                printf("%s = %s\n", path, value);
+                printf("%s = %s\n", dotted_key, value);
             } else {
                 if (PrintNewline)
                     printf("%s\n", value);
@@ -608,6 +619,7 @@ static int WriteSetting(
             }
         }
     }
+    free(dotted_key);
     return rc;
 }
 
index bf0923f42cdfdf7dc936a83543554a45ddc0847c..b6b944361f0b4b5939e0d1fe1d028b4b09f5d4dd 100644 (file)
@@ -3,11 +3,19 @@ set sysctl ${topdir}sysctl
 
 set test "sysctl write from command line"
 spawn $sysctl --dry-run kernel.hostname=procps-test
-expect_pass "$test" "/proc/sys/kernel/hostname = procps-test"
+expect_pass "$test" "kernel.hostname = procps-test"
+
+set test "sysctl write from command line using slash"
+spawn $sysctl --dry-run kernel/hostname=procps-test
+expect_pass "$test" "kernel.hostname = procps-test"
 
 set test "sysctl write from configuration file"
 spawn $sysctl --dry-run -f ${topdir}testsuite/sysctl_glob_test.conf
-expect_pass "$test" "/proc/sys/fs/protected_fifos = 2\\s+/proc/sys/fs/protected_symlinks = 2\\s+/proc/sys/fs/protected_hardlinks = 1"
+expect_pass "$test" "fs.protected_fifos = 2\\s+fs.protected_symlinks = 2\\s+fs.protected_hardlinks = 1"
+
+set test "sysctl write from file with slashes"
+spawn $sysctl --dry-run -f ${topdir}testsuite/sysctl_slash_test.conf
+expect_pass "$test" "kernel.hostname = procps-test"
 
 set hostname_file "/proc/sys/kernel/hostname"
 if {[file exists ${hostname_file}]} {
@@ -25,7 +33,7 @@ if {[file exists ${hostname_file}]} {
     expect_spawn_retval "$test" 0
   }
 } else {
-  unsupported "sysctl write: hostname file doe not exist"
+  unsupported "sysctl write: hostname file does not exist"
 }
 
 set test "sysctl write above /proc"
diff --git a/testsuite/sysctl_slash_test.conf b/testsuite/sysctl_slash_test.conf
new file mode 100644 (file)
index 0000000..77e9b37
--- /dev/null
@@ -0,0 +1 @@
+kernel/hostname = procps-test