git: 52c426327403 - main - sndctl(8): Allow read-only sysctls
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 21 May 2025 19:31:36 UTC
The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=52c4263274031d599aebfaed2862ae85b482953a commit 52c4263274031d599aebfaed2862ae85b482953a Author: Christos Margiolis <christos@FreeBSD.org> AuthorDate: 2025-05-21 19:31:23 +0000 Commit: Christos Margiolis <christos@FreeBSD.org> CommitDate: 2025-05-21 19:31:23 +0000 sndctl(8): Allow read-only sysctls Needed by follow-up patch. Sponsored by: The FreeBSD Foundation MFC after: 1 day Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D50398 --- usr.sbin/sndctl/sndctl.c | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/usr.sbin/sndctl/sndctl.c b/usr.sbin/sndctl/sndctl.c index 412ea33e5adf..4d3ae0cf526c 100644 --- a/usr.sbin/sndctl/sndctl.c +++ b/usr.sbin/sndctl/sndctl.c @@ -664,30 +664,37 @@ sysctl_int(const char *buf, const char *arg, int *var) size_t size; int n, prev; - n = strtol(arg, NULL, 10); - if (errno == EINVAL || errno == ERANGE) { - warn("strtol(%s)", arg); - return (-1); - } - size = sizeof(int); /* Read current value. */ if (sysctlbyname(buf, &prev, &size, NULL, 0) < 0) { warn("sysctlbyname(%s)", buf); return (-1); } - /* Apply new value. */ - if (sysctlbyname(buf, NULL, 0, &n, size) < 0) { - warn("sysctlbyname(%s, %d)", buf, n); - return (-1); + + /* Read-only. */ + if (arg != NULL) { + errno = 0; + n = strtol(arg, NULL, 10); + if (errno == EINVAL || errno == ERANGE) { + warn("strtol(%s)", arg); + return (-1); + } + + /* Apply new value. */ + if (sysctlbyname(buf, NULL, 0, &n, size) < 0) { + warn("sysctlbyname(%s, %d)", buf, n); + return (-1); + } } + /* Read back applied value for good measure. */ if (sysctlbyname(buf, &n, &size, NULL, 0) < 0) { warn("sysctlbyname(%s)", buf); return (-1); } - printf("%s: %d -> %d\n", buf, prev, n); + if (arg != NULL) + printf("%s: %d -> %d\n", buf, prev, n); if (var != NULL) *var = n; @@ -708,17 +715,21 @@ sysctl_str(const char *buf, const char *arg, char *var, size_t varsz) return (-1); } - size = strlen(arg); - /* Apply new value. */ - if (sysctlbyname(buf, NULL, 0, arg, size) < 0) { - warn("sysctlbyname(%s, %s)", buf, arg); - return (-1); - } - /* Get size of new string. */ - if (sysctlbyname(buf, NULL, &size, NULL, 0) < 0) { - warn("sysctlbyname(%s)", buf); - return (-1); + /* Read-only. */ + if (arg != NULL) { + size = strlen(arg); + /* Apply new value. */ + if (sysctlbyname(buf, NULL, 0, arg, size) < 0) { + warn("sysctlbyname(%s, %s)", buf, arg); + return (-1); + } + /* Get size of new string. */ + if (sysctlbyname(buf, NULL, &size, NULL, 0) < 0) { + warn("sysctlbyname(%s)", buf); + return (-1); + } } + if ((tmp = calloc(1, size)) == NULL) err(1, "calloc"); /* Read back applied value for good measure. */ @@ -728,7 +739,8 @@ sysctl_str(const char *buf, const char *arg, char *var, size_t varsz) return (-1); } - printf("%s: %s -> %s\n", buf, prev, tmp); + if (arg != NULL) + printf("%s: %s -> %s\n", buf, prev, tmp); if (var != NULL) strlcpy(var, tmp, varsz); free(tmp);