sysctl: print dotted keys again

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 6389deca5b
 https://www.freelists.org/post/procps/some-procpsn4400-fixes,4
 https://repo.saltproject.io/

Signed-off-by: Craig Small <csmall@dropbear.xyz>
This commit is contained in:
Craig Small
2022-04-09 14:18:28 +10:00
parent 4fbf8d22a9
commit b159c198c9
4 changed files with 33 additions and 11 deletions

View File

@@ -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;
}