Home | History | Annotate | Line # | Download | only in common
nfs_nfssvc.c revision 1.1
      1 /*	$NetBSD: nfs_nfssvc.c,v 1.1 2013/09/30 07:19:33 dholland Exp $	*/
      2 /*-
      3  * Copyright (c) 1989, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Rick Macklem at The University of Guelph.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 /* __FBSDID("FreeBSD: head/sys/nfs/nfs_nfssvc.c 243782 2012-12-02 01:16:04Z rmacklem "); */
     37 __RCSID("$NetBSD: nfs_nfssvc.c,v 1.1 2013/09/30 07:19:33 dholland Exp $");
     38 
     39 #include "opt_nfs.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/sysproto.h>
     44 #include <sys/kernel.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/priv.h>
     47 #include <sys/proc.h>
     48 #include <sys/lock.h>
     49 #include <sys/mutex.h>
     50 #include <sys/module.h>
     51 #include <sys/sysent.h>
     52 #include <sys/syscall.h>
     53 #include <sys/sysproto.h>
     54 
     55 #include <security/audit/audit.h>
     56 
     57 #include <nfs/nfssvc.h>
     58 
     59 static int nfssvc_offset = SYS_nfssvc;
     60 static struct sysent nfssvc_prev_sysent;
     61 MAKE_SYSENT(nfssvc);
     62 
     63 /*
     64  * This tiny module simply handles the nfssvc() system call. The other
     65  * nfs modules that use the system call register themselves by setting
     66  * the nfsd_call_xxx function pointers non-NULL.
     67  */
     68 
     69 int (*nfsd_call_nfsserver)(struct thread *, struct nfssvc_args *) = NULL;
     70 int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *) = NULL;
     71 int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *) = NULL;
     72 int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *) = NULL;
     73 
     74 /*
     75  * Nfs server psuedo system call for the nfsd's
     76  */
     77 int
     78 sys_nfssvc(struct thread *td, struct nfssvc_args *uap)
     79 {
     80 	int error;
     81 
     82 	KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
     83 
     84 	AUDIT_ARG_CMD(uap->flag);
     85 
     86 	/* Allow anyone to get the stats. */
     87 	if ((uap->flag & ~NFSSVC_GETSTATS) != 0) {
     88 		error = priv_check(td, PRIV_NFS_DAEMON);
     89 		if (error != 0)
     90 			return (error);
     91 	}
     92 	error = EINVAL;
     93 	if ((uap->flag & (NFSSVC_ADDSOCK | NFSSVC_OLDNFSD | NFSSVC_NFSD)) &&
     94 	    nfsd_call_nfsserver != NULL)
     95 		error = (*nfsd_call_nfsserver)(td, uap);
     96 	else if ((uap->flag & (NFSSVC_CBADDSOCK | NFSSVC_NFSCBD |
     97 	    NFSSVC_DUMPMNTOPTS)) && nfsd_call_nfscl != NULL)
     98 		error = (*nfsd_call_nfscl)(td, uap);
     99 	else if ((uap->flag & (NFSSVC_IDNAME | NFSSVC_GETSTATS |
    100 	    NFSSVC_GSSDADDPORT | NFSSVC_GSSDADDFIRST | NFSSVC_GSSDDELETEALL |
    101 	    NFSSVC_NFSUSERDPORT | NFSSVC_NFSUSERDDELPORT)) &&
    102 	    nfsd_call_nfscommon != NULL)
    103 		error = (*nfsd_call_nfscommon)(td, uap);
    104 	else if ((uap->flag & (NFSSVC_NFSDNFSD | NFSSVC_NFSDADDSOCK |
    105 	    NFSSVC_PUBLICFH | NFSSVC_V4ROOTEXPORT | NFSSVC_NOPUBLICFH |
    106 	    NFSSVC_STABLERESTART | NFSSVC_ADMINREVOKE |
    107 	    NFSSVC_DUMPCLIENTS | NFSSVC_DUMPLOCKS | NFSSVC_BACKUPSTABLE |
    108 	    NFSSVC_SUSPENDNFSD | NFSSVC_RESUMENFSD)) &&
    109 	    nfsd_call_nfsd != NULL)
    110 		error = (*nfsd_call_nfsd)(td, uap);
    111 	if (error == EINTR || error == ERESTART)
    112 		error = 0;
    113 	return (error);
    114 }
    115 
    116 /*
    117  * Called once to initialize data structures...
    118  */
    119 static int
    120 nfssvc_modevent(module_t mod, int type, void *data)
    121 {
    122 	static int registered;
    123 	int error = 0;
    124 
    125 	switch (type) {
    126 	case MOD_LOAD:
    127 		error = syscall_register(&nfssvc_offset, &nfssvc_sysent,
    128 		    &nfssvc_prev_sysent);
    129 		if (error)
    130 			break;
    131 		registered = 1;
    132 		break;
    133 
    134 	case MOD_UNLOAD:
    135 		if (nfsd_call_nfsserver != NULL || nfsd_call_nfscommon != NULL
    136 		    || nfsd_call_nfscl != NULL || nfsd_call_nfsd != NULL) {
    137 			error = EBUSY;
    138 			break;
    139 		}
    140 		if (registered)
    141 			syscall_deregister(&nfssvc_offset, &nfssvc_prev_sysent);
    142 		registered = 0;
    143 		break;
    144 	default:
    145 		error = EOPNOTSUPP;
    146 		break;
    147 	}
    148 	return error;
    149 }
    150 static moduledata_t nfssvc_mod = {
    151 	"nfssvc",
    152 	nfssvc_modevent,
    153 	NULL,
    154 };
    155 DECLARE_MODULE(nfssvc, nfssvc_mod, SI_SUB_VFS, SI_ORDER_ANY);
    156 
    157 /* So that loader and kldload(2) can find us, wherever we are.. */
    158 MODULE_VERSION(nfssvc, 1);
    159 
    160