Index: sbin/sysctl/sysctl.c =================================================================== --- sbin/sysctl/sysctl.c +++ sbin/sysctl/sysctl.c @@ -949,6 +949,46 @@ return (0); } +/* + * This displays information aout a sysctl + * + * Returns zero if anything was actually output. + * Returns one if didn't know what to do with this. + * Return minus one if we had errors. + */ +static int +show_info(char *name, const char *sep, int ctltype, int *qoid, int nlen) +{ + u_char buf[BUFSIZ]; + const char *prntype; + int error = 0, i; + size_t j; + + if (!nflag) + printf("%s%s", name, sep); + if (ctl_typename[ctltype] != NULL) + prntype = ctl_typename[ctltype]; + else { + prntype = "unknown"; + error++; + } + if (tflag && dflag) + printf("%s%s", prntype, sep); + else if (tflag) { + printf("%s", prntype); + return (error); + } + + qoid[1] = CTL_SYSCTL_OIDDESCR; + bzero(buf, BUFSIZ); + j = sizeof(buf); + i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); + if (i < 0) + return (1); + printf("%s", buf); + return (error); +} + /* * This formats and outputs the value of one variable * @@ -960,9 +1000,9 @@ show_var(int *oid, int nlen, bool honor_skip) { static int skip_len = 0, skip_oid[CTL_MAXNAME]; - u_char buf[BUFSIZ], *val, *oval, *p; + u_char *val, *oval, *p; char name[BUFSIZ], fmt[BUFSIZ]; - const char *sep, *sep1, *prntype; + const char *sep, *sep1; int qoid[CTL_MAXNAME+2]; uintmax_t umv; intmax_t mv; @@ -977,7 +1017,6 @@ /* Silence GCC. */ umv = mv = intlen = 0; - bzero(buf, BUFSIZ); bzero(fmt, BUFSIZ); bzero(name, BUFSIZ); qoid[0] = CTL_SYSCTL; @@ -1008,25 +1047,8 @@ sep = ": "; ctltype = (kind & CTLTYPE); - if (tflag || dflag) { - if (!nflag) - printf("%s%s", name, sep); - if (ctl_typename[ctltype] != NULL) - prntype = ctl_typename[ctltype]; - else - prntype = "unknown"; - if (tflag && dflag) - printf("%s%s", prntype, sep); - else if (tflag) { - printf("%s", prntype); - return (0); - } - qoid[1] = CTL_SYSCTL_OIDDESCR; - j = sizeof(buf); - i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); - printf("%s", buf); - return (0); - } + if (tflag || dflag) + return show_info(name, sep, ctltype, qoid, nlen); /* keep track of encountered skip nodes, ignoring descendants */ if ((skip_len == 0 || skip_len >= nlen * (int)sizeof(int)) && Index: sbin/sysctl/tests/Makefile =================================================================== --- /dev/null +++ sbin/sysctl/tests/Makefile @@ -0,0 +1,5 @@ +# $FreeBSD$ + +ATF_TESTS_SH= sysctl_test + +.include Index: sbin/sysctl/tests/Makefile.depend =================================================================== --- /dev/null +++ sbin/sysctl/tests/Makefile.depend @@ -0,0 +1,11 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Index: sbin/sysctl/tests/sysctl_test.sh =================================================================== --- /dev/null +++ sbin/sysctl/tests/sysctl_test.sh @@ -0,0 +1,119 @@ +# Copyright (c) 2022 Yoshihiro Ota +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +sysctl_name="kern.ostype" +sysctl_value="FreeBSD" +sysctl_type="string" +sysctl_description="Operating system type" + +atf_test_case sysctl_by_name +sysctl_by_name_head() +{ + atf_set "descr" "Verify name without any arguments" +} +sysctl_by_name_body() +{ + atf_check -o "inline:${sysctl_name}: ${sysctl_value}\n" sysctl ${sysctl_name} +} + + +atf_test_case sysctl_nflag +sysctl_nflag() +{ + atf_set "descr" "Verify -n argument" +} +sysctl_nflag_body() +{ + atf_check -o "inline:${sysctl_value}\n" sysctl -n ${sysctl_name} +} + + +atf_test_case sysctl_eflag +sysctl_eflag() +{ + atf_set "descr" "Verify -e argument" +} +sysctl_eflag_body() +{ + atf_check -o "inline:${sysctl_name}=${sysctl_value}\n" sysctl -e ${sysctl_name} +} + + +atf_test_case sysctl_tflag +sysctl_tflag() +{ + atf_set "descr" "Verify -t argument" +} +sysctl_tflag_body() +{ + atf_check -o "inline:${sysctl_name}: ${sysctl_type}\n" sysctl -t ${sysctl_name} +} + + +atf_test_case sysctl_dflag +sysctl_dflag() +{ + atf_set "descr" "Verify -d argument" +} +sysctl_dflag_body() +{ + atf_check -o "inline:${sysctl_name}: ${sysctl_description}\n" sysctl -d ${sysctl_name} +} + + +atf_test_case sysctl_tflag_dflag +sysctl_tflag_dflag() +{ + atf_set "descr" "Verify -t -d arguments" +} +sysctl_tflag_dflag_body() +{ + atf_check -o "inline:${sysctl_name}: ${sysctl_type}: ${sysctl_description}\n" sysctl -t -d ${sysctl_name} + atf_check -o "inline:${sysctl_name}: ${sysctl_type}: ${sysctl_description}\n" sysctl -d -t ${sysctl_name} +} + + +atf_test_case sysctl_nflag_tflag_dflag +sysctl_nflag_tflag_dflag() +{ + atf_set "descr" "Verify -n -t -d arguments" +} +sysctl_nflag_tflag_dflag_body() +{ + atf_check -o "inline:${sysctl_type}: ${sysctl_description}\n" sysctl -n -t -d ${sysctl_name} +} + + +atf_init_test_cases() +{ + atf_add_test_case sysctl_by_name + atf_add_test_case sysctl_nflag + atf_add_test_case sysctl_eflag + atf_add_test_case sysctl_tflag + atf_add_test_case sysctl_dflag + atf_add_test_case sysctl_tflag_dflag + atf_add_test_case sysctl_nflag_tflag_dflag +}