kern_xxx.c revision 8876
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
348876Srgrimes * $Id: kern_xxx.c,v 1.9 1994/12/04 19:58:43 phk Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
391541Srgrimes#include <sys/kernel.h>
401541Srgrimes#include <sys/proc.h>
411541Srgrimes#include <sys/reboot.h>
421541Srgrimes#include <vm/vm.h>
431541Srgrimes#include <sys/sysctl.h>
441549Srgrimes#include <sys/utsname.h>
453308Sphk#include <sys/signalvar.h>
461541Srgrimes
474959Sphk/* This implements a "TEXT_SET" for cleanup functions */
484959Sphk
494959Sphkstatic void
504959Sphkdummy_cleanup() {}
514959SphkTEXT_SET(cleanup_set, dummy_cleanup);
528876Srgrimes
534959Sphktypedef void (*cleanup_func_t)(void);
544959Sphkextern const struct linker_set cleanup_set;
554959Sphkstatic const cleanup_func_t *cleanups =
564959Sphk        (const cleanup_func_t *)&cleanup_set.ls_items[0];
574959Sphk
581541Srgrimesstruct reboot_args {
591541Srgrimes	int	opt;
601541Srgrimes};
611541Srgrimes/* ARGSUSED */
621549Srgrimesint
631541Srgrimesreboot(p, uap, retval)
641541Srgrimes	struct proc *p;
651541Srgrimes	struct reboot_args *uap;
661541Srgrimes	int *retval;
671541Srgrimes{
681541Srgrimes	int error;
691541Srgrimes
703098Sphk	if ((error = suser(p->p_ucred, &p->p_acflag)))
711541Srgrimes		return (error);
724959Sphk
734959Sphk	if (!uap->opt & RB_NOSYNC) {
744959Sphk		printf("\ncleaning up... ");
754959Sphk                while(*cleanups) {
764959Sphk                        (**cleanups++)();
774959Sphk                }
784959Sphk	}
794959Sphk
801541Srgrimes	boot(uap->opt);
811541Srgrimes	return (0);
821541Srgrimes}
831541Srgrimes
841541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
851541Srgrimes
861541Srgrimesstruct gethostname_args {
871541Srgrimes	char	*hostname;
881541Srgrimes	u_int	len;
891541Srgrimes};
901541Srgrimes/* ARGSUSED */
911549Srgrimesint
921541Srgrimesogethostname(p, uap, retval)
931541Srgrimes	struct proc *p;
941541Srgrimes	struct gethostname_args *uap;
951541Srgrimes	int *retval;
961541Srgrimes{
971541Srgrimes	int name;
981541Srgrimes
991541Srgrimes	name = KERN_HOSTNAME;
1002904Sache	return (kern_sysctl(&name, 1, uap->hostname, &uap->len, 0, 0, p));
1011541Srgrimes}
1021541Srgrimes
1031541Srgrimesstruct sethostname_args {
1041541Srgrimes	char	*hostname;
1051541Srgrimes	u_int	len;
1061541Srgrimes};
1071541Srgrimes/* ARGSUSED */
1081549Srgrimesint
1091541Srgrimesosethostname(p, uap, retval)
1101541Srgrimes	struct proc *p;
1111541Srgrimes	register struct sethostname_args *uap;
1121541Srgrimes	int *retval;
1131541Srgrimes{
1141541Srgrimes	int name;
1151541Srgrimes	int error;
1161541Srgrimes
1173098Sphk	if ((error = suser(p->p_ucred, &p->p_acflag)))
1181541Srgrimes		return (error);
1191541Srgrimes	name = KERN_HOSTNAME;
1202904Sache	return (kern_sysctl(&name, 1, 0, 0, uap->hostname, uap->len, p));
1211541Srgrimes}
1221541Srgrimes
1231541Srgrimesstruct gethostid_args {
1241541Srgrimes	int	dummy;
1251541Srgrimes};
1261541Srgrimes/* ARGSUSED */
1271549Srgrimesint
1281541Srgrimesogethostid(p, uap, retval)
1291541Srgrimes	struct proc *p;
1301541Srgrimes	struct gethostid_args *uap;
1311541Srgrimes	int *retval;
1321541Srgrimes{
1331541Srgrimes
1341541Srgrimes	*(long *)retval = hostid;
1351541Srgrimes	return (0);
1361541Srgrimes}
1371541Srgrimes#endif /* COMPAT_43 || COMPAT_SUNOS */
1381541Srgrimes
1391541Srgrimes#ifdef COMPAT_43
1401541Srgrimesstruct sethostid_args {
1411541Srgrimes	long	hostid;
1421541Srgrimes};
1431541Srgrimes/* ARGSUSED */
1441549Srgrimesint
1451541Srgrimesosethostid(p, uap, retval)
1461541Srgrimes	struct proc *p;
1471541Srgrimes	struct sethostid_args *uap;
1481541Srgrimes	int *retval;
1491541Srgrimes{
1501541Srgrimes	int error;
1511541Srgrimes
1523098Sphk	if ((error = suser(p->p_ucred, &p->p_acflag)))
1531541Srgrimes		return (error);
1541541Srgrimes	hostid = uap->hostid;
1551541Srgrimes	return (0);
1561541Srgrimes}
1571541Srgrimes
1581549Srgrimesint
1591541Srgrimesoquota()
1601541Srgrimes{
1611541Srgrimes
1621541Srgrimes	return (ENOSYS);
1631541Srgrimes}
1641541Srgrimes#endif /* COMPAT_43 */
1651549Srgrimes
1661549Srgrimesvoid
1671549Srgrimesshutdown_nice(void)
1681549Srgrimes{
1691549Srgrimes	register struct proc *p;
1701549Srgrimes
1711549Srgrimes	/* Send a signal to init(8) and have it shutdown the world */
1721549Srgrimes	p = pfind(1);
1731549Srgrimes	psignal(p, SIGINT);
1741549Srgrimes
1751549Srgrimes	return;
1761549Srgrimes}
1771549Srgrimes
1781549Srgrimes
1791549Srgrimesstruct uname_args {
1801549Srgrimes        struct utsname  *name;
1811549Srgrimes};
1821549Srgrimes
1831549Srgrimes/* ARGSUSED */
1841549Srgrimesint
1851549Srgrimesuname(p, uap, retval)
1861549Srgrimes	struct proc *p;
1871549Srgrimes	struct uname_args *uap;
1881549Srgrimes	int *retval;
1891549Srgrimes{
1901549Srgrimes	int name;
1911549Srgrimes	int len;
1921549Srgrimes	int rtval;
1931549Srgrimes	char *s, *us;
1941549Srgrimes
1951549Srgrimes	name = KERN_OSTYPE;
1961549Srgrimes	len = sizeof uap->name->sysname;
1971549Srgrimes	rtval = kern_sysctl(&name, 1, uap->name->sysname, &len, 0, 0, p);
1981549Srgrimes	if( rtval) return rtval;
1991549Srgrimes	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
2001549Srgrimes
2011549Srgrimes	name = KERN_HOSTNAME;
2021549Srgrimes	len = sizeof uap->name->nodename;
2031549Srgrimes	rtval = kern_sysctl(&name, 1, uap->name->nodename, &len, 0, 0, p);
2041549Srgrimes	if( rtval) return rtval;
2051549Srgrimes	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
2061549Srgrimes
2071549Srgrimes	name = KERN_OSRELEASE;
2081549Srgrimes	len = sizeof uap->name->release;
2091549Srgrimes	rtval = kern_sysctl(&name, 1, uap->name->release, &len, 0, 0, p);
2101549Srgrimes	if( rtval) return rtval;
2111549Srgrimes	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
2121549Srgrimes
2131549Srgrimes/*
2141549Srgrimes	name = KERN_VERSION;
2151549Srgrimes	len = sizeof uap->name->version;
2161549Srgrimes	rtval = kern_sysctl(&name, 1, uap->name->version, &len, 0, 0, p);
2171549Srgrimes	if( rtval) return rtval;
2181549Srgrimes	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
2191549Srgrimes*/
2201549Srgrimes
2211549Srgrimes/*
2221549Srgrimes * this stupid hackery to make the version field look like FreeBSD 1.1
2231549Srgrimes */
2241549Srgrimes	for(s = version; *s && *s != '#'; s++);
2251549Srgrimes
2261549Srgrimes	for(us = uap->name->version; *s && *s != ':'; s++) {
2271549Srgrimes		rtval = subyte( us++, *s);
2281549Srgrimes		if( rtval)
2291549Srgrimes			return rtval;
2301549Srgrimes	}
2311549Srgrimes	rtval = subyte( us++, 0);
2321549Srgrimes	if( rtval)
2331549Srgrimes		return rtval;
2341549Srgrimes
2351549Srgrimes	name = HW_MACHINE;
2361549Srgrimes	len = sizeof uap->name->machine;
2371549Srgrimes	rtval = hw_sysctl(&name, 1, uap->name->machine, &len, 0, 0, p);
2381549Srgrimes	if( rtval) return rtval;
2391549Srgrimes	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
2401549Srgrimes
2411549Srgrimes	return 0;
2421549Srgrimes}
2431549Srgrimes
2441549Srgrimesstruct getdomainname_args {
2451549Srgrimes        char    *domainname;
2461549Srgrimes        u_int   len;
2471549Srgrimes};
2481549Srgrimes
2491549Srgrimes/* ARGSUSED */
2501549Srgrimesint
2511549Srgrimesgetdomainname(p, uap, retval)
2521549Srgrimes        struct proc *p;
2531549Srgrimes        struct getdomainname_args *uap;
2541549Srgrimes        int *retval;
2551549Srgrimes{
2561549Srgrimes	if (uap->len > domainnamelen + 1)
2571549Srgrimes		uap->len = domainnamelen + 1;
2581549Srgrimes	return (copyout((caddr_t)domainname, (caddr_t)uap->domainname, uap->len));
2591549Srgrimes}
2601549Srgrimes
2611549Srgrimesstruct setdomainname_args {
2621549Srgrimes        char    *domainname;
2631549Srgrimes        u_int   len;
2641549Srgrimes};
2651549Srgrimes
2661549Srgrimes/* ARGSUSED */
2671549Srgrimesint
2681549Srgrimessetdomainname(p, uap, retval)
2691549Srgrimes        struct proc *p;
2701549Srgrimes        struct setdomainname_args *uap;
2711549Srgrimes        int *retval;
2721549Srgrimes{
2731549Srgrimes        int error;
2741549Srgrimes
2753098Sphk        if ((error = suser(p->p_ucred, &p->p_acflag)))
2761549Srgrimes                return (error);
2771549Srgrimes        if (uap->len > sizeof (domainname) - 1)
2781549Srgrimes                return EINVAL;
2791549Srgrimes        domainnamelen = uap->len;
2801549Srgrimes        error = copyin((caddr_t)uap->domainname, domainname, uap->len);
2811549Srgrimes        domainname[domainnamelen] = 0;
2821549Srgrimes        return (error);
2831549Srgrimes}
2841549Srgrimes
285