From: David van Moolenbroek Date: Wed, 28 Oct 2015 15:06:03 +0000 (+0000) Subject: Start using sysctl(3) throughout userland X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F3247%2F2;p=minix.git Start using sysctl(3) throughout userland This patch mostly removes MINIX3-specific workarounds for missing sysctl support. Change-Id: I21e35e887359619ba72c252aa43d9017301a46ac --- diff --git a/bin/sh/eval.c b/bin/sh/eval.c index cc56af6e0..7299a5b84 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -631,12 +631,9 @@ out: static const char * syspath(void) { -#if !defined(__minix) static char *sys_path = NULL; static int mib[] = {CTL_USER, USER_CS_PATH}; -#endif /* !defined(__minix) */ static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin"; -#if !defined(__minix) size_t len; if (sys_path == NULL) { @@ -651,10 +648,6 @@ syspath(void) } } return sys_path; -#else - /* On Minix no support for CTL_USER. */ - return def_path; -#endif /* !defined(__minix) */ } static int diff --git a/etc/usr/rc b/etc/usr/rc index 9f2438f83..a12a3e3a0 100644 --- a/etc/usr/rc +++ b/etc/usr/rc @@ -401,6 +401,9 @@ start|autoboot) trap 2 fi + # Load the stored hostname into the sysctl database. + test -r /etc/hostname.file && hostname $(cat /etc/hostname.file) + # Recover files being edited when the system crashed. test -f /usr/bin/elvprsv && elvprsv /usr/tmp/elv* diff --git a/include/stdlib.h b/include/stdlib.h index 817546c93..3d2d0800c 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -300,9 +300,7 @@ __aconst char *devname(dev_t, mode_t) __RENAME(__devname50); int humanize_number(char *, size_t, int64_t, const char *, int, int); int dehumanize_number(const char *, int64_t *); -#if !defined(__minix) devmajor_t getdevmajor(const char *, mode_t); -#endif /* !defined(__minix) */ int getloadavg(double [], int); int getenv_r(const char *, char *, size_t); diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 695347aff..07768bf74 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -20,7 +20,7 @@ SRCS+= _errno.c .if defined(__MINIX) # Unsupported by Minix. -# closefrom.c confstr.c extattr.c getdevmajor.c \ +# closefrom.c confstr.c extattr.c \ # pthread_atfork.c # # To be ported @@ -36,7 +36,7 @@ SRCS+= alarm.c alphasort.c arc4random.c assert.c asysctl.c \ errc.c errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \ fmtcheck.c fmtmsg.c fnmatch.c fstab.c ftok.c \ fts.c ftw.c getbsize.c getcap.c getcwd.c \ - getdomainname.c getgrent.c \ + getdevmajor.c getdomainname.c getgrent.c \ getgrouplist.c getgroupmembership.c gethostname.c \ getloadavg.c getlogin.c getmntinfo.c \ getnetgrent.c getpagesize.c \ diff --git a/lib/libc/gen/sysconf.c b/lib/libc/gen/sysconf.c index 52d22eb00..3ff5d84fd 100644 --- a/lib/libc/gen/sysconf.c +++ b/lib/libc/gen/sysconf.c @@ -349,8 +349,10 @@ sysconf(int name) return _POSIX_SHARED_MEMORY_OBJECTS; case _SC_SHELL: return _POSIX_SHELL; +#ifndef __minix case _SC_SPAWN: return _POSIX_SPAWN; +#endif /* !__minix */ case _SC_SYMLOOP_MAX: return MAXSYMLINKS; diff --git a/libexec/ld.elf_so/expand.c b/libexec/ld.elf_so/expand.c index 7f784ea3f..7e5ae97b6 100644 --- a/libexec/ld.elf_so/expand.c +++ b/libexec/ld.elf_so/expand.c @@ -62,23 +62,19 @@ static const struct { ADD(PLATFORM) /* uname -p */ }; -#if !defined(__minix) static int mib[3][2] = { { CTL_KERN, KERN_OSTYPE }, { CTL_KERN, KERN_OSRELEASE }, { CTL_HW, HW_MACHINE_ARCH }, }; -#endif /* !defined(__minix) */ static size_t expand(char *buf, const char *execname, int what, size_t bl) { const char *p, *ep; char *bp = buf; -#if !defined(__minix) size_t len; char name[32]; -#endif /* !defined(__minix) */ switch (what) { case 0: /* HWCAP XXX: Not yet */ @@ -92,7 +88,6 @@ expand(char *buf, const char *execname, int what, size_t bl) xerr(1, "bad execname `%s' in AUX vector", execname); break; -#if !defined(__minix) case 3: /* OSNAME */ case 4: /* OSREL */ case 5: /* PLATFORM */ @@ -103,7 +98,6 @@ expand(char *buf, const char *execname, int what, size_t bl) } ep = (p = name) + len - 1; break; -#endif /* !defined(__minix) */ default: return 0; } diff --git a/minix/lib/libc/gen/getdomainname.c b/minix/lib/libc/gen/getdomainname.c deleted file mode 100644 index c913cee6c..000000000 --- a/minix/lib/libc/gen/getdomainname.c +++ /dev/null @@ -1,29 +0,0 @@ -/* getdomainname() Author: Kees J. Bot - * 2 Dec 1994 - */ -#define nil 0 -#include "namespace.h" -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(getdomainname, _getdomainname) -#endif - -int getdomainname(char *result, size_t size) -{ - char nodename[256]; - char *domain; - - if (gethostname(nodename, sizeof(nodename)) < 0) - return -1; - nodename[sizeof(nodename)-1]= 0; - if ((domain = strchr(nodename, '.')) != NULL) - strncpy(result, domain+1, size); - else - result[0] = '\0'; - - if (size > 0) result[size-1]= 0; - return 0; -} diff --git a/minix/lib/libc/gen/gethostname.c b/minix/lib/libc/gen/gethostname.c deleted file mode 100644 index a7f309113..000000000 --- a/minix/lib/libc/gen/gethostname.c +++ /dev/null @@ -1,31 +0,0 @@ -/* gethostname(2) system call emulation */ -#include -#include "namespace.h" - -#include -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(gethostname, _gethostname) -#endif - -int gethostname(char *buf, size_t len) -{ - int fd; - int r; - char *nl; - - if ((fd= open(_PATH_HOSTNAME_FILE, O_RDONLY)) < 0) return -1; - - r= read(fd, buf, len); - close(fd); - if (r == -1) return -1; - - buf[len-1]= '\0'; - if ((nl= strchr(buf, '\n')) != NULL) *nl= '\0'; - return 0; -} diff --git a/minix/lib/libc/gen/getloadavg.c b/minix/lib/libc/gen/getloadavg.c deleted file mode 100644 index af2cf5cac..000000000 --- a/minix/lib/libc/gen/getloadavg.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include "namespace.h" - -#include -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(getloadavg, _getloadavg) -#endif - -/* Retrieve system load average information. */ -int getloadavg(double *loadavg, int nelem) -{ - FILE *fp; - int i; - - if(nelem < 1) { - errno = ENOSPC; - return -1; - } - - if((fp = fopen(_PATH_PROC "loadavg", "r")) == NULL) - return -1; - - for(i = 0; i < nelem; i++) - if(fscanf(fp, "%lf", &loadavg[i]) != 1) - break; - - fclose(fp); - - if (i == 0) { - errno = ENOENT; - return -1; - } - - return i; -} diff --git a/minix/lib/libc/gen/getpagesize.c b/minix/lib/libc/gen/getpagesize.c deleted file mode 100644 index 71294f6a8..000000000 --- a/minix/lib/libc/gen/getpagesize.c +++ /dev/null @@ -1,20 +0,0 @@ -/* -getpagesize.c -*/ -#include -#include -#include "namespace.h" - -#include -#include - -#include - -#ifdef __weak_alias -__weak_alias(getpagesize, _getpagesize) -#endif - -int getpagesize(void) -{ - return PAGE_SIZE; -} diff --git a/minix/lib/libc/gen/sethostname.c b/minix/lib/libc/gen/sethostname.c deleted file mode 100644 index 7d8a64ba6..000000000 --- a/minix/lib/libc/gen/sethostname.c +++ /dev/null @@ -1,66 +0,0 @@ -/* gethostname(2) system call emulation */ -#include -#include "namespace.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(sethostname, _sethostname) -#endif - -int sethostname(const char *buf, size_t len) -{ - int fd; - int r; - int tmperr; - char name[20]; - strlcpy(name, "/tmp/hostname.XXXXX",sizeof(name)); - fd = mkstemp(name); - - if (fd == -1) - return -1; - - r = fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - - if (r == -1) { - tmperr = errno; - close(fd); - unlink(name); - errno = tmperr; - return -1; - } - - r = write(fd, buf, len); - tmperr = errno; - fsync(fd); - close(fd); - - if (r == -1) { - unlink(name); - errno = tmperr; - return -1; - } - - if ((size_t)r < len) { - unlink(name); - errno = ENOSPC; - return -1; - } - - r = rename(name, _PATH_HOSTNAME_FILE); - - if (r == -1) { - tmperr = errno; - unlink(name); - errno = tmperr; - } - - return 0; -} diff --git a/minix/lib/libc/gen/sysconf.c b/minix/lib/libc/gen/sysconf.c deleted file mode 100644 index 21d2c4d74..000000000 --- a/minix/lib/libc/gen/sysconf.c +++ /dev/null @@ -1,85 +0,0 @@ -/* sysconf.c POSIX 4.8.1 - * long int sysconf(int name); - * - * POSIX allows some of the values in to be increased at - * run time. The sysconf() function allows such values to be checked - * at run time. MINIX does not use this facility - the run time - * limits are those given in . - */ -#include -#include "namespace.h" - -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(sysconf, __sysconf) -#endif - -static u32_t get_hz(void) -{ - FILE *fp; - u32_t hz; - int r; - - if ((fp = fopen(_PATH_PROC "hz", "r")) != NULL) - { - r = fscanf(fp, "%u", &hz); - - fclose(fp); - - if (r == 1) - return hz; - } - - return DEFAULT_HZ; -} - -long int sysconf(name) -int name; /* property being inspected */ -{ - switch(name) { - case _SC_ARG_MAX: - return (long) ARG_MAX; - - case _SC_CHILD_MAX: - return (long) CHILD_MAX; - - case _SC_CLK_TCK: - return (long) get_hz(); - - case _SC_NGROUPS_MAX: - return (long) NGROUPS_MAX; - - case _SC_OPEN_MAX: - return (long) OPEN_MAX; - - case _SC_JOB_CONTROL: - return -1L; /* no job control */ - - case _SC_SAVED_IDS: - return -1L; /* no saved uid/gid */ - - case _SC_VERSION: - return (long) _POSIX_VERSION; - - case _SC_STREAM_MAX: - return (long) _POSIX_STREAM_MAX; - - case _SC_TZNAME_MAX: - return (long) _POSIX_TZNAME_MAX; - - case _SC_PAGESIZE: - return getpagesize(); - - case _SC_LINE_MAX: - return (long) LINE_MAX; - - default: - errno = EINVAL; - return -1L; - } -} diff --git a/minix/lib/libc/gen/uname.c b/minix/lib/libc/gen/uname.c deleted file mode 100644 index 2759e66a2..000000000 --- a/minix/lib/libc/gen/uname.c +++ /dev/null @@ -1,66 +0,0 @@ -/* uname(3) - describe the machine. Author: Kees J. Bot - * 5 Dec 1992 - */ -#include -#include "namespace.h" - -#include -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(uname, _uname) -#endif - -#define uts_get(field, string) \ - if (sysuname(_UTS_GET, field, name->string, sizeof(name->string)) < 0) \ - return -1; \ - name->string[sizeof(name->string)-1]= 0; - -int uname(struct utsname *name) -{ - int hf, n, err; - char *nl; - - /* Get each of the strings with a sysuname call. Null terminate them, - * because the buffers in the kernel may grow before this and the - * programs are recompiled. - */ - uts_get(_UTS_SYSNAME, sysname); - uts_get(_UTS_NODENAME, nodename); - uts_get(_UTS_RELEASE, release); - uts_get(_UTS_VERSION, version); - uts_get(_UTS_MACHINE, machine); - uts_get(_UTS_ARCH, arch); -#if 0 - uts_get(_UTS_KERNEL, kernel); - uts_get(_UTS_HOSTNAME, hostname); - uts_get(_UTS_BUS, bus); -#endif - - /* Try to read the node name from /etc/hostname.file. This information - * should be stored in the kernel. - */ - if ((hf = open("/etc/hostname.file", O_RDONLY)) < 0) { - if (errno != ENOENT) return(-1); - } else { - n = read(hf, name->nodename, sizeof(name->nodename) - 1); - err = errno; - close(hf); - errno = err; - if (n < 0) return(-1); - name->nodename[n] = 0; - if ((nl = strchr(name->nodename, '\n')) != NULL) { - memset(nl, 0, (name->nodename + - sizeof(name->nodename)) - nl); - } - } - return 0; -} - -/* - * $PchId: _uname.c,v 1.4 1995/11/27 20:09:08 philip Exp $ - */ diff --git a/minix/lib/libc/sys/getrlimit.c b/minix/lib/libc/sys/getrlimit.c index d39481625..1df733b56 100644 --- a/minix/lib/libc/sys/getrlimit.c +++ b/minix/lib/libc/sys/getrlimit.c @@ -27,7 +27,6 @@ int getrlimit(int resource, struct rlimit *rlp) case RLIMIT_CORE: case RLIMIT_RSS: case RLIMIT_MEMLOCK: - case RLIMIT_NPROC: case RLIMIT_SBSIZE: case RLIMIT_AS: /* case RLIMIT_VMEM: Same as RLIMIT_AS */ @@ -38,6 +37,10 @@ int getrlimit(int resource, struct rlimit *rlp) limit = RLIM_INFINITY; break; + case RLIMIT_NPROC: + limit = CHILD_MAX; + break; + case RLIMIT_NOFILE: limit = OPEN_MAX; break; diff --git a/minix/usr.bin/w/minix_proc.c b/minix/usr.bin/w/minix_proc.c index 665196c9e..c7204b1c3 100644 --- a/minix/usr.bin/w/minix_proc.c +++ b/minix/usr.bin/w/minix_proc.c @@ -251,23 +251,3 @@ minix_proc_compare(const struct minix_proc * p1, const struct minix_proc * p2) */ return p1->p_pid < p2->p_pid; } - -/* - * Obtain the system uptime in seconds. Return 0 on success, with the uptime - * stored in the given time_t field. Return -1 on failure. - */ -int -minix_getuptime(time_t *timep) -{ - FILE *fp; - int r; - - if ((fp = fopen(_PATH_PROC "uptime", "r")) == NULL) - return -1; - - r = fscanf(fp, "%llu", timep); - - fclose(fp); - - return (r == 1) ? 0 : -1; -} diff --git a/minix/usr.bin/w/minix_proc.h b/minix/usr.bin/w/minix_proc.h index a38bedbe9..a216dbb45 100644 --- a/minix/usr.bin/w/minix_proc.h +++ b/minix/usr.bin/w/minix_proc.h @@ -44,6 +44,4 @@ int minix_proc_compare(const struct minix_proc *p1, #undef proc_compare_wrapper #define proc_compare_wrapper minix_proc_compare -int minix_getuptime(time_t *timep); - #endif /* !_W_MINIX_PROC_H */ diff --git a/sbin/init/init.c b/sbin/init/init.c index 4609d23b4..c13f4f312 100644 --- a/sbin/init/init.c +++ b/sbin/init/init.c @@ -544,9 +544,6 @@ minixpowerdown(int sig) static int has_securelevel(void) { -#if defined(__minix) - return 0; -#else #ifdef KERN_SECURELVL int name[2], curlevel; size_t len; @@ -563,7 +560,6 @@ has_securelevel(void) #else return 0; #endif -#endif /* defined(__minix) */ } /* @@ -1815,7 +1811,6 @@ do_setttyent(void) static int createsysctlnode(void) { -#if !defined(__minix) struct sysctlnode node; int mib[2]; size_t len; @@ -1857,7 +1852,6 @@ createsysctlnode(void) warning("could not create init.root node: %m"); return -1; } -#endif /* !defined(__minix) */ return 0; } @@ -1865,7 +1859,6 @@ createsysctlnode(void) static int shouldchroot(void) { -#if !defined(__minix) struct sysctlnode node; size_t len, cnt; int mib; @@ -1904,9 +1897,6 @@ shouldchroot(void) return 0; return 1; -#else - return 0; -#endif /* !defined(__minix) */ } #endif /* !LETS_GET_SMALL && CHROOT */ diff --git a/sbin/mknod/mknod.c b/sbin/mknod/mknod.c index b44b1043b..824152923 100644 --- a/sbin/mknod/mknod.c +++ b/sbin/mknod/mknod.c @@ -331,14 +331,8 @@ callPack(pack_t *f, int n, u_long *numbers) #ifdef KERN_DRIVERS static void -#if defined(__minix) -__dead -#endif /* defined(__minix) */ get_device_info(void) { -#if defined(__minix) - err(1, "no kern.drivers on minix" ); -#else static int mib[2] = {CTL_KERN, KERN_DRIVERS}; size_t len; @@ -351,7 +345,6 @@ get_device_info(void) err(1, "kern.drivers" ); num_drivers = len / sizeof *kern_drivers; -#endif /* defined(__minix) */ } static void diff --git a/usr.bin/make/main.c b/usr.bin/make/main.c index 48d6ece11..73f4aaa9c 100644 --- a/usr.bin/make/main.c +++ b/usr.bin/make/main.c @@ -885,7 +885,7 @@ main(int argc, char **argv) } if (!machine_arch) { -#if defined(MAKE_NATIVE) && !defined(__minix) +#if defined(MAKE_NATIVE) static char machine_arch_buf[sizeof(utsname.machine)]; const int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; size_t len = sizeof(machine_arch_buf); diff --git a/usr.bin/uname/uname.c b/usr.bin/uname/uname.c index c8f902258..de5e08eb8 100644 --- a/usr.bin/uname/uname.c +++ b/usr.bin/uname/uname.c @@ -44,11 +44,7 @@ __RCSID("$NetBSD: uname.c,v 1.11 2011/09/06 18:35:13 joerg Exp $"); #include #include -#if defined(__minix) -#include -#else #include -#endif /* !defined(__minix) */ #include __dead static void usage(void); @@ -117,16 +113,12 @@ main(int argc, char **argv) /* NOTREACHED */ } if (print_mask & PRINT_MACHINE_ARCH) { -#if defined(__minix) - strlcpy(machine_arch, u.arch, sizeof(machine_arch)); -#else int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; size_t len = sizeof (machine_arch); if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch, &len, NULL, 0) < 0) err(EXIT_FAILURE, "sysctl"); -#endif /* defined(__minix) */ } if (print_mask & PRINT_SYSNAME) { diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index 1ac2173cf..5cfba1b73 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -144,9 +144,7 @@ main(int argc, char **argv) char *memf, *nlistf, *usrnp; const char *options; time_t then; -#if !defined(__minix) size_t len; -#endif /* !defined(__minix) */ #ifdef SUPPORT_UTMP struct utmp *ut; #endif @@ -216,11 +214,9 @@ main(int argc, char **argv) (void)time(&now); if (use_sysctl) { -#if !defined(__minix) len = sizeof(curtain); if (sysctlbyname("security.curtain", &curtain, &len, NULL, 0) == -1) -#endif /* !defined(__minix) */ curtain = 0; } @@ -482,12 +478,8 @@ pr_header(time_t *nowp, int nusers) double avenrun[3]; time_t uptime; int days, hrs, mins; -#if !defined(__minix) int mib[2]; size_t size, i; -#else - size_t i; -#endif /* !defined(__minix) */ char buf[256]; /* @@ -504,16 +496,12 @@ pr_header(time_t *nowp, int nusers) * Print how long system has been up. * (Found by looking getting "boottime" from the kernel) */ -#if !defined(__minix) mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; size = sizeof(boottime); if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) { uptime = now - boottime.tv_sec; -#else - if (minix_getuptime(&uptime) != -1) { -#endif /* !defined(__minix) */ uptime += 30; if (uptime > SECSPERMIN) { days = uptime / SECSPERDAY; diff --git a/usr.bin/whereis/whereis.c b/usr.bin/whereis/whereis.c index 5be0578b4..d5d39f6c7 100644 --- a/usr.bin/whereis/whereis.c +++ b/usr.bin/whereis/whereis.c @@ -60,11 +60,7 @@ main(int argc, char *argv[]) { struct stat sb; size_t len; -#if defined(__minix) - int ch; -#else int ch, mib[2]; -#endif /* defined(__minix) */ char *p, *std, path[MAXPATHLEN]; const char *t; int which = strcmp(getprogname(), "which") == 0; @@ -102,18 +98,6 @@ main(int argc, char *argv[]) errx(1, "PATH environment variable is not set"); } else { /* Retrieve the standard path. */ -#if defined(__minix) - /* - Note: This path is currently defined here and should probably be defined - here, in "ash" or in "sh". - To minimize code changes, the path has been hard coded into this file. - However, if this path needs to be used in other ported programs, please - move this define to and add the include to this file - and all files that use _PATH_USER_CS_PATH. - */ - #define _PATH_USER_CS_PATH "/usr/X11R7/sbin:/usr/local/sbin:/usr/pkg/sbin:/usr/sbin:/sbin:/usr/X11R7/bin:/usr/local/bin:/usr/pkg/bin:/usr/bin:/bin:/usr/games" - std = strdup(_PATH_USER_CS_PATH); -#else mib[0] = CTL_USER; mib[1] = USER_CS_PATH; if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) @@ -124,7 +108,6 @@ main(int argc, char *argv[]) err(1, NULL); if (sysctl(mib, 2, std, &len, NULL, 0) == -1) err(1, "sysctl: user.cs_path"); -#endif /* defined(__minix) */ } /* For each path, for each program... */ diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 66ca55e94..eae28cac0 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -4025,10 +4025,6 @@ getLocalFQDN(void) char *hostname; size_t len; -#ifdef __minix - len = MAXHOSTNAMELEN + 1; -#endif /* __minix */ - mib[0] = CTL_KERN; mib[1] = KERN_HOSTNAME; sysctl(mib, 2, NULL, &len, NULL, 0);