Changeset View
Standalone View
lib/libc/gen/sysctlbyname.c
/* | /*- | ||||
* ---------------------------------------------------------------------------- | * SPDX-License-Identifier: BSD-2-Clause | ||||
* "THE BEER-WARE LICENSE" (Revision 42): | |||||
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you | |||||
* can do whatever you want with this stuff. If we meet some day, and you think | |||||
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp | |||||
* ---------------------------------------------------------------------------- | |||||
* | * | ||||
* Copyright 2019 Pawel Biernacki, Mysterious Code Ltd. | |||||
* | |||||
* 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. | |||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#ifndef HAVE___SYSCTLBYNAME | |||||
#include <errno.h> | |||||
#include <signal.h> | |||||
#endif | |||||
#include <string.h> | |||||
extern int __sysctlbyname(const char *name, size_t namelen, void *oldp, | |||||
size_t *oldlenp, const void *newp, size_t newlen); | |||||
int | int | ||||
sysctlbyname(const char *name, void *oldp, size_t *oldlenp, | sysctlbyname(const char *name, void *oldp, size_t *oldlenp, | ||||
const void *newp, size_t newlen) | const void *newp, size_t newlen) | ||||
{ | { | ||||
int real_oid[CTL_MAXNAME+2]; | #ifndef HAVE___SYSCTLBYNAME | ||||
struct sigaction sa, osa; | |||||
int oid[CTL_MAXNAME]; | |||||
size_t oidlen; | size_t oidlen; | ||||
#endif | |||||
size_t namelen; | |||||
int error; | |||||
kib: What is the point of creating this single-use variable ? Esp. of type u_int which… | |||||
oidlen = sizeof(real_oid) / sizeof(int); | namelen = strlen(name); | ||||
if (sysctlnametomib(name, real_oid, &oidlen) < 0) | #ifndef HAVE___SYSCTLBYNAME | ||||
memset(&sa, 0, sizeof(sa)); | |||||
sa.sa_handler = SIG_IGN; | |||||
sigfillset(&sa.sa_mask); | |||||
sigaction(SIGSYS, &sa, &osa); | |||||
kibUnsubmitted Not Done Inline ActionsThis is no-go outright. Library must not manipulate global process state. It is both non-thread-safe and non-async-signal-safe. If you want to handle older kernels in libc(why ?) use __FreeBSD_version. Look for examples. kib: This is no-go outright. Library must not manipulate global process state. It is both non… | |||||
kaktusAuthorUnsubmitted Done Inline ActionsI agree, it was just an idea to ease the upgrade process for those installing new libc while running older kernel, because even reboot would be killed by SIGSYS as it use sysctlbyname(3). kaktus: I agree, it was just an idea to ease the upgrade process for those installing new libc while… | |||||
kibUnsubmitted Not Done Inline ActionsMy opinion is that newer libc requires newer kernel, but some people are nicer to users. Look at lib/libc/sys/stat.c for what I mean by checks for __FreeBSD_version. kib: My opinion is that newer libc requires newer kernel, but some people are nicer to users.
Look… | |||||
#endif | |||||
error = __sysctlbyname(name, namelen, oldp, oldlenp, newp, newlen); | |||||
#ifndef HAVE___SYSCTLBYNAME | |||||
sigaction(SIGSYS, &osa, NULL); | |||||
if (error != 0 && errno != ENOSYS) { | |||||
oidlen = sizeof(oid) / sizeof(int); | |||||
if (sysctlnametomib(name, oid, &oidlen) == -1) | |||||
return (-1); | return (-1); | ||||
return (sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen)); | error = sysctl(oid, oidlen, oldp, oldlenp, newp, newlen); | ||||
} | |||||
#endif | |||||
return error; | |||||
Done Inline Actionsthis should be namelen to avoid namespace collision with newlen the strlen call should be done in a separate line mjg: this should be namelen to avoid namespace collision with newlen
the strlen call should be done… | |||||
} | } |
What is the point of creating this single-use variable ? Esp. of type u_int which theoretically causes truncation ?