Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.31
      1  1.31  pgoyette /*	$NetBSD: sysv_ipc.c,v 1.31 2015/12/03 02:51:00 pgoyette Exp $	*/
      2   1.7       cgd 
      3  1.13   mycroft /*-
      4  1.20        ad  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
      5  1.13   mycroft  * All rights reserved.
      6  1.13   mycroft  *
      7  1.13   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13   mycroft  * by Charles M. Hannum.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.6   hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.6   hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     17   1.6   hpeyerl  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  *
     19  1.13   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.13   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.13   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.13   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.13   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.13   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.13   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.13   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.13   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.13   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.13   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       cgd  */
     31  1.15     lukem 
     32  1.15     lukem #include <sys/cdefs.h>
     33  1.31  pgoyette __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.31 2015/12/03 02:51:00 pgoyette Exp $");
     34  1.18  christos 
     35  1.28  pgoyette #ifdef _KERNEL_OPT
     36  1.18  christos #include "opt_sysv.h"
     37  1.31  pgoyette #include "opt_compat_netbsd.h"
     38  1.28  pgoyette #endif
     39  1.28  pgoyette 
     40  1.28  pgoyette #include <sys/syscall.h>
     41  1.28  pgoyette #include <sys/syscallargs.h>
     42  1.28  pgoyette #include <sys/syscallvar.h>
     43   1.2   mycroft #include <sys/param.h>
     44   1.2   mycroft #include <sys/kernel.h>
     45   1.2   mycroft #include <sys/proc.h>
     46   1.2   mycroft #include <sys/ipc.h>
     47  1.18  christos #ifdef SYSVMSG
     48  1.18  christos #include <sys/msg.h>
     49  1.18  christos #endif
     50  1.18  christos #ifdef SYSVSEM
     51  1.18  christos #include <sys/sem.h>
     52  1.18  christos #endif
     53  1.18  christos #ifdef SYSVSHM
     54  1.18  christos #include <sys/shm.h>
     55  1.18  christos #endif
     56   1.4   hpeyerl #include <sys/systm.h>
     57  1.23     rmind #include <sys/kmem.h>
     58  1.28  pgoyette #include <sys/module.h>
     59  1.10   mycroft #include <sys/mount.h>
     60  1.10   mycroft #include <sys/vnode.h>
     61  1.12   mycroft #include <sys/stat.h>
     62  1.18  christos #include <sys/sysctl.h>
     63  1.17      elad #include <sys/kauth.h>
     64   1.1       cgd 
     65  1.28  pgoyette /*
     66  1.28  pgoyette  * Values in support of System V compatible shared memory.	XXX
     67  1.28  pgoyette  * (originally located in sys/conf/param.c)
     68  1.28  pgoyette  */
     69  1.28  pgoyette #ifdef SYSVSHM
     70  1.28  pgoyette #if !defined(SHMMAX) && defined(SHMMAXPGS)
     71  1.28  pgoyette #define	SHMMAX	SHMMAXPGS	/* shminit() performs a `*= PAGE_SIZE' */
     72  1.28  pgoyette #elif !defined(SHMMAX)
     73  1.28  pgoyette #define SHMMAX 0
     74  1.28  pgoyette #endif
     75  1.28  pgoyette #ifndef	SHMMIN
     76  1.28  pgoyette #define	SHMMIN	1
     77  1.28  pgoyette #endif
     78  1.28  pgoyette #ifndef	SHMMNI
     79  1.28  pgoyette #define	SHMMNI	128		/* <64k, see IPCID_TO_IX in ipc.h */
     80  1.28  pgoyette #endif
     81  1.28  pgoyette #ifndef	SHMSEG
     82  1.28  pgoyette #define	SHMSEG	128
     83  1.28  pgoyette #endif
     84  1.28  pgoyette 
     85  1.28  pgoyette struct	shminfo shminfo = {
     86  1.28  pgoyette 	SHMMAX,
     87  1.28  pgoyette 	SHMMIN,
     88  1.28  pgoyette 	SHMMNI,
     89  1.28  pgoyette 	SHMSEG,
     90  1.28  pgoyette 	0
     91  1.28  pgoyette };
     92  1.28  pgoyette #endif
     93  1.28  pgoyette 
     94  1.28  pgoyette /*
     95  1.28  pgoyette  * Values in support of System V compatible semaphores.
     96  1.28  pgoyette  */
     97  1.28  pgoyette #ifdef SYSVSEM
     98  1.28  pgoyette struct	seminfo seminfo = {
     99  1.28  pgoyette 	SEMMAP,		/* # of entries in semaphore map */
    100  1.28  pgoyette 	SEMMNI,		/* # of semaphore identifiers */
    101  1.28  pgoyette 	SEMMNS,		/* # of semaphores in system */
    102  1.28  pgoyette 	SEMMNU,		/* # of undo structures in system */
    103  1.28  pgoyette 	SEMMSL,		/* max # of semaphores per id */
    104  1.28  pgoyette 	SEMOPM,		/* max # of operations per semop call */
    105  1.28  pgoyette 	SEMUME,		/* max # of undo entries per process */
    106  1.28  pgoyette 	SEMUSZ,		/* size in bytes of undo structure */
    107  1.28  pgoyette 	SEMVMX,		/* semaphore maximum value */
    108  1.28  pgoyette 	SEMAEM		/* adjust on exit max value */
    109  1.28  pgoyette };
    110  1.22  christos #endif
    111  1.22  christos 
    112  1.28  pgoyette /*
    113  1.28  pgoyette  * Values in support of System V compatible messages.
    114  1.28  pgoyette  */
    115  1.28  pgoyette #ifdef SYSVMSG
    116  1.28  pgoyette struct	msginfo msginfo = {
    117  1.28  pgoyette 	MSGMAX,		/* max chars in a message */
    118  1.28  pgoyette 	MSGMNI,		/* # of message queue identifiers */
    119  1.28  pgoyette 	MSGMNB,		/* max chars in a queue */
    120  1.28  pgoyette 	MSGTQL,		/* max messages in system */
    121  1.28  pgoyette 	MSGSSZ,		/* size of a message segment */
    122  1.28  pgoyette 			/* (must be small power of 2 greater than 4) */
    123  1.28  pgoyette 	MSGSEG		/* number of message segments */
    124  1.28  pgoyette };
    125  1.28  pgoyette #endif
    126  1.28  pgoyette 
    127  1.31  pgoyette #if defined(COMPAT_50)
    128  1.31  pgoyette int sysctl_kern_sysvipc50(SYSCTLFN_PROTO);
    129  1.31  pgoyette #endif
    130  1.31  pgoyette 
    131  1.28  pgoyette MODULE(MODULE_CLASS_EXEC, sysv_ipc, NULL);
    132  1.28  pgoyette 
    133  1.28  pgoyette SYSCTL_SETUP_PROTO(sysctl_ipc_setup);
    134  1.28  pgoyette 
    135  1.28  pgoyette static struct sysctllog *sysctl_sysvipc_clog = NULL;
    136  1.28  pgoyette 
    137  1.28  pgoyette static const struct syscall_package sysvipc_syscalls[] = {
    138  1.31  pgoyette #if defined(SYSVSHM)
    139  1.28  pgoyette 	{ SYS___shmctl50, 0, (sy_call_t *)sys___shmctl50 },
    140  1.28  pgoyette 	{ SYS_shmat, 0, (sy_call_t *)sys_shmat },
    141  1.28  pgoyette 	{ SYS_shmdt, 0, (sy_call_t *)sys_shmdt },
    142  1.28  pgoyette 	{ SYS_shmget, 0, (sy_call_t *)sys_shmget },
    143  1.31  pgoyette #if defined(COMPAT_10) && !defined(_LP64)
    144  1.31  pgoyette 	{ SYS_compat_10_oshmsys, 0, (sy_call_t *)compat_10_sys_shmsys },
    145  1.31  pgoyette #endif
    146  1.31  pgoyette #if defined(COMPAT_14)
    147  1.31  pgoyette 	{ SYS_compat_14_shmctl, 0, (sy_call_t *)compat_14_sys_shmctl },
    148  1.31  pgoyette #endif
    149  1.31  pgoyette #if defined(COMPAT_50)
    150  1.31  pgoyette 	{ SYS_compat_50___shmctl13, 0, (sy_call_t *)compat_50_sys___shmctl13 },
    151  1.29  pgoyette #endif
    152  1.31  pgoyette #endif	/* SYSVSHM */
    153  1.31  pgoyette 
    154  1.31  pgoyette #if defined(SYSVSEM)
    155  1.28  pgoyette 	{ SYS_____semctl50, 0, (sy_call_t *)sys_____semctl50 },
    156  1.28  pgoyette 	{ SYS_semget, 0, (sy_call_t *)sys_semget },
    157  1.28  pgoyette 	{ SYS_semop, 0, (sy_call_t *)sys_semop },
    158  1.28  pgoyette 	{ SYS_semconfig, 0, (sy_call_t *)sys_semconfig },
    159  1.31  pgoyette #if defined(COMPAT_10) && !defined(_LP64)
    160  1.31  pgoyette 	{ SYS_compat_10_osemsys, 0, (sy_call_t *)compat_10_sys_semsys },
    161  1.31  pgoyette #endif
    162  1.31  pgoyette #if defined(COMPAT_14)
    163  1.31  pgoyette 	{ SYS_compat_14___semctl, 0, (sy_call_t *)compat_14_sys___semctl },
    164  1.29  pgoyette #endif
    165  1.31  pgoyette #if defined(COMPAT_50)
    166  1.31  pgoyette 	{ SYS_compat_50_____semctl13, 0, (sy_call_t *)compat_50_sys_____semctl13 },
    167  1.31  pgoyette #endif
    168  1.31  pgoyette #endif	/* SYSVSEM */
    169  1.31  pgoyette 
    170  1.31  pgoyette #if defined(SYSVMSG)
    171  1.28  pgoyette 	{ SYS___msgctl50, 0, (sy_call_t *)sys___msgctl50 },
    172  1.28  pgoyette 	{ SYS_msgget, 0, (sy_call_t *)sys_msgget },
    173  1.28  pgoyette 	{ SYS_msgsnd, 0, (sy_call_t *)sys_msgsnd },
    174  1.28  pgoyette 	{ SYS_msgrcv, 0, (sy_call_t *)sys_msgrcv },
    175  1.31  pgoyette #if defined(COMPAT_10) && !defined(_LP64)
    176  1.31  pgoyette 	{ SYS_compat_10_omsgsys, 0, (sy_call_t *)compat_10_sys_msgsys },
    177  1.29  pgoyette #endif
    178  1.31  pgoyette #if defined(COMPAT_14)
    179  1.31  pgoyette 	{ SYS_compat_14_msgctl, 0, (sy_call_t *)compat_14_sys_msgctl },
    180  1.31  pgoyette #endif
    181  1.31  pgoyette #if defined(COMPAT_50)
    182  1.31  pgoyette 	{ SYS_compat_50___msgctl13, 0, (sy_call_t *)compat_50_sys___msgctl13 },
    183  1.31  pgoyette #endif
    184  1.31  pgoyette #endif	/* SYSVMSG */
    185  1.28  pgoyette 	{ 0, 0, NULL }
    186  1.28  pgoyette };
    187  1.28  pgoyette 
    188  1.28  pgoyette static int
    189  1.28  pgoyette sysv_ipc_modcmd(modcmd_t cmd, void *arg)
    190  1.28  pgoyette {
    191  1.28  pgoyette 	int error = 0;
    192  1.28  pgoyette 
    193  1.28  pgoyette 	switch (cmd) {
    194  1.28  pgoyette 	case MODULE_CMD_INIT:
    195  1.30  pgoyette 		/* Set up the kauth listener */
    196  1.30  pgoyette 		sysvipcinit();
    197  1.28  pgoyette 
    198  1.28  pgoyette #ifdef _MODULE
    199  1.30  pgoyette 		/* Set up the common sysctl tree */
    200  1.28  pgoyette 		sysctl_ipc_setup(&sysctl_sysvipc_clog);
    201  1.28  pgoyette #endif
    202  1.30  pgoyette 
    203  1.30  pgoyette 		/* Link the system calls */
    204  1.30  pgoyette 		error = syscall_establish(NULL, sysvipc_syscalls);
    205  1.30  pgoyette 		if (error)
    206  1.30  pgoyette 			sysvipcfini();
    207  1.30  pgoyette 
    208  1.30  pgoyette 		/*
    209  1.30  pgoyette 		 * Initialize each sub-component, including their
    210  1.30  pgoyette 		 * sysctl data
    211  1.30  pgoyette 		 */
    212  1.28  pgoyette #ifdef SYSVSHM
    213  1.30  pgoyette 		shminit(&sysctl_sysvipc_clog);
    214  1.28  pgoyette #endif
    215  1.28  pgoyette #ifdef SYSVSEM
    216  1.30  pgoyette 		seminit(&sysctl_sysvipc_clog);
    217  1.28  pgoyette #endif
    218  1.28  pgoyette #ifdef SYSVMSG
    219  1.30  pgoyette 		msginit(&sysctl_sysvipc_clog);
    220  1.28  pgoyette #endif
    221  1.28  pgoyette 		break;
    222  1.28  pgoyette 	case MODULE_CMD_FINI:
    223  1.28  pgoyette 		/*
    224  1.28  pgoyette 		 * Make sure no subcomponents are active.  Each one
    225  1.28  pgoyette 		 * tells us if it is busy, and if it was _not_ busy,
    226  1.28  pgoyette 		 * we assume it has already done its own clean-up.
    227  1.28  pgoyette 		 * So we might need to re-init any components that
    228  1.28  pgoyette 		 * are successfully fini'd if we find one that is
    229  1.28  pgoyette 		 * still busy.
    230  1.28  pgoyette 		 */
    231  1.28  pgoyette #ifdef SYSVSHM
    232  1.28  pgoyette 		if (shmfini()) {
    233  1.28  pgoyette 			return EBUSY;
    234  1.28  pgoyette 		}
    235  1.28  pgoyette #endif
    236  1.28  pgoyette #ifdef SYSVSEM
    237  1.28  pgoyette 		if (semfini()) {
    238  1.28  pgoyette #ifdef SYSVSHM
    239  1.30  pgoyette 			shminit(NULL);
    240  1.28  pgoyette #endif
    241  1.28  pgoyette 			return EBUSY;
    242  1.28  pgoyette 		}
    243  1.28  pgoyette #endif
    244  1.28  pgoyette #ifdef SYSVMSG
    245  1.28  pgoyette 		if (msgfini()) {
    246  1.28  pgoyette #ifdef SYSVSEM
    247  1.30  pgoyette 			seminit(NULL);
    248  1.28  pgoyette #endif
    249  1.28  pgoyette #ifdef SYSVSHM
    250  1.30  pgoyette 			shminit(NULL);
    251  1.28  pgoyette #endif
    252  1.28  pgoyette 			return EBUSY;
    253  1.28  pgoyette 		}
    254  1.26  pgoyette #endif
    255  1.26  pgoyette 
    256  1.28  pgoyette 		/* Unlink the system calls. */
    257  1.28  pgoyette 		error = syscall_disestablish(NULL, sysvipc_syscalls);
    258  1.28  pgoyette 		if (error)
    259  1.28  pgoyette 			return error;
    260  1.28  pgoyette 
    261  1.28  pgoyette #ifdef _MODULE
    262  1.28  pgoyette 		/* Remove the sysctl sub-trees */
    263  1.28  pgoyette 		sysctl_teardown(&sysctl_sysvipc_clog);
    264  1.30  pgoyette #endif
    265  1.28  pgoyette 
    266  1.28  pgoyette 		/* Remove the kauth listener */
    267  1.28  pgoyette 		sysvipcfini();
    268  1.28  pgoyette 		break;
    269  1.28  pgoyette 	default:
    270  1.28  pgoyette 		return ENOTTY;
    271  1.28  pgoyette 	}
    272  1.28  pgoyette 	return error;
    273  1.28  pgoyette }
    274  1.28  pgoyette 
    275  1.24      elad static kauth_listener_t sysvipc_listener = NULL;
    276   1.1       cgd 
    277  1.24      elad static int
    278  1.24      elad sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    279  1.24      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    280   1.1       cgd {
    281  1.12   mycroft 	mode_t mask;
    282  1.17      elad 	int ismember = 0;
    283  1.24      elad 	struct ipc_perm *perm;
    284  1.24      elad 	int mode;
    285  1.24      elad 	enum kauth_system_req req;
    286  1.24      elad 
    287  1.24      elad 	req = (enum kauth_system_req)arg0;
    288  1.24      elad 
    289  1.24      elad 	if (!(action == KAUTH_SYSTEM_SYSVIPC &&
    290  1.24      elad 	      req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
    291  1.24      elad 		return KAUTH_RESULT_DEFER;
    292  1.12   mycroft 
    293  1.24      elad 	perm = arg1;
    294  1.24      elad 	mode = (int)(uintptr_t)arg2;
    295   1.1       cgd 
    296   1.9   mycroft 	if (mode == IPC_M) {
    297  1.17      elad 		if (kauth_cred_geteuid(cred) == perm->uid ||
    298  1.17      elad 		    kauth_cred_geteuid(cred) == perm->cuid)
    299  1.24      elad 			return (KAUTH_RESULT_ALLOW);
    300  1.24      elad 		return (KAUTH_RESULT_DEFER); /* EPERM */
    301   1.1       cgd 	}
    302   1.4   hpeyerl 
    303  1.12   mycroft 	mask = 0;
    304  1.12   mycroft 
    305  1.17      elad 	if (kauth_cred_geteuid(cred) == perm->uid ||
    306  1.17      elad 	    kauth_cred_geteuid(cred) == perm->cuid) {
    307  1.12   mycroft 		if (mode & IPC_R)
    308  1.12   mycroft 			mask |= S_IRUSR;
    309  1.12   mycroft 		if (mode & IPC_W)
    310  1.12   mycroft 			mask |= S_IWUSR;
    311  1.24      elad 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    312  1.12   mycroft 	}
    313  1.12   mycroft 
    314  1.17      elad 	if (kauth_cred_getegid(cred) == perm->gid ||
    315  1.17      elad 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
    316  1.17      elad 	    kauth_cred_getegid(cred) == perm->cgid ||
    317  1.17      elad 	    (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
    318  1.12   mycroft 		if (mode & IPC_R)
    319  1.12   mycroft 			mask |= S_IRGRP;
    320  1.12   mycroft 		if (mode & IPC_W)
    321  1.12   mycroft 			mask |= S_IWGRP;
    322  1.24      elad 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    323  1.12   mycroft 	}
    324  1.12   mycroft 
    325  1.12   mycroft 	if (mode & IPC_R)
    326  1.12   mycroft 		mask |= S_IROTH;
    327  1.12   mycroft 	if (mode & IPC_W)
    328  1.12   mycroft 		mask |= S_IWOTH;
    329  1.24      elad 	return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    330  1.24      elad }
    331  1.24      elad 
    332  1.24      elad /*
    333  1.24      elad  * Check for ipc permission
    334  1.24      elad  */
    335  1.24      elad 
    336  1.24      elad int
    337  1.24      elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
    338  1.24      elad {
    339  1.24      elad 	int error;
    340  1.24      elad 
    341  1.24      elad 	error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
    342  1.24      elad 	    KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
    343  1.24      elad 	if (error == 0)
    344  1.24      elad 		return (0);
    345  1.24      elad 
    346  1.24      elad 	/* Adjust EPERM and EACCES errors until there's a better way to do this. */
    347  1.24      elad 	if (mode != IPC_M)
    348  1.24      elad 		error = EACCES;
    349  1.24      elad 
    350  1.24      elad 	return error;
    351  1.24      elad }
    352  1.24      elad 
    353  1.24      elad void
    354  1.27  pgoyette sysvipcfini(void)
    355  1.27  pgoyette {
    356  1.27  pgoyette 
    357  1.27  pgoyette 	KASSERT(sysvipc_listener != NULL);
    358  1.27  pgoyette 	kauth_unlisten_scope(sysvipc_listener);
    359  1.27  pgoyette }
    360  1.27  pgoyette 
    361  1.27  pgoyette void
    362  1.24      elad sysvipcinit(void)
    363  1.24      elad {
    364  1.24      elad 
    365  1.30  pgoyette 	KASSERT(sysvipc_listener == NULL);
    366  1.24      elad 
    367  1.24      elad 	sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    368  1.24      elad 	    sysvipc_listener_cb, NULL);
    369   1.1       cgd }
    370  1.18  christos 
    371  1.18  christos static int
    372  1.18  christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    373  1.18  christos {
    374  1.18  christos 	void *where = oldp;
    375  1.23     rmind 	size_t sz, *sizep = oldlenp;
    376  1.18  christos #ifdef SYSVMSG
    377  1.18  christos 	struct msg_sysctl_info *msgsi = NULL;
    378  1.18  christos #endif
    379  1.18  christos #ifdef SYSVSEM
    380  1.18  christos 	struct sem_sysctl_info *semsi = NULL;
    381  1.18  christos #endif
    382  1.18  christos #ifdef SYSVSHM
    383  1.18  christos 	struct shm_sysctl_info *shmsi = NULL;
    384  1.18  christos #endif
    385  1.18  christos 	size_t infosize, dssize, tsize, buflen;
    386  1.18  christos 	void *bf = NULL;
    387  1.18  christos 	char *start;
    388  1.18  christos 	int32_t nds;
    389  1.18  christos 	int i, error, ret;
    390  1.18  christos 
    391  1.26  pgoyette /*
    392  1.31  pgoyette  * If present, call the compat sysctl() code.  If it handles the request
    393  1.31  pgoyette  * completely (either success or error), return.  Otherwise fallthrough
    394  1.31  pgoyette  * to the non-compat sysctl code.
    395  1.26  pgoyette  */
    396  1.31  pgoyette 
    397  1.31  pgoyette #if defined(COMPAT_50)
    398  1.31  pgoyette 	error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode));
    399  1.31  pgoyette 	if (error != EPASSTHROUGH)
    400  1.31  pgoyette 		return error;
    401  1.31  pgoyette #endif
    402  1.26  pgoyette 
    403  1.18  christos 	if (namelen != 1)
    404  1.18  christos 		return EINVAL;
    405  1.18  christos 
    406  1.18  christos 	start = where;
    407  1.18  christos 	buflen = *sizep;
    408  1.18  christos 
    409  1.18  christos 	switch (*name) {
    410  1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    411  1.18  christos #ifdef SYSVMSG
    412  1.18  christos 		infosize = sizeof(msgsi->msginfo);
    413  1.18  christos 		nds = msginfo.msgmni;
    414  1.18  christos 		dssize = sizeof(msgsi->msgids[0]);
    415  1.18  christos 		break;
    416  1.18  christos #else
    417  1.18  christos 		return EINVAL;
    418  1.18  christos #endif
    419  1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    420  1.18  christos #ifdef SYSVSEM
    421  1.18  christos 		infosize = sizeof(semsi->seminfo);
    422  1.18  christos 		nds = seminfo.semmni;
    423  1.18  christos 		dssize = sizeof(semsi->semids[0]);
    424  1.18  christos 		break;
    425  1.18  christos #else
    426  1.18  christos 		return EINVAL;
    427  1.18  christos #endif
    428  1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    429  1.18  christos #ifdef SYSVSHM
    430  1.18  christos 		infosize = sizeof(shmsi->shminfo);
    431  1.18  christos 		nds = shminfo.shmmni;
    432  1.18  christos 		dssize = sizeof(shmsi->shmids[0]);
    433  1.18  christos 		break;
    434  1.18  christos #else
    435  1.18  christos 		return EINVAL;
    436  1.18  christos #endif
    437  1.18  christos 	default:
    438  1.18  christos 		return EINVAL;
    439  1.18  christos 	}
    440  1.18  christos 	/*
    441  1.18  christos 	 * Round infosize to 64 bit boundary if requesting more than just
    442  1.18  christos 	 * the info structure or getting the total data size.
    443  1.18  christos 	 */
    444  1.18  christos 	if (where == NULL || *sizep > infosize)
    445  1.18  christos 		infosize = roundup(infosize, sizeof(quad_t));
    446  1.18  christos 	tsize = infosize + nds * dssize;
    447  1.18  christos 
    448  1.18  christos 	/* Return just the total size required. */
    449  1.18  christos 	if (where == NULL) {
    450  1.18  christos 		*sizep = tsize;
    451  1.18  christos 		return 0;
    452  1.18  christos 	}
    453  1.18  christos 
    454  1.18  christos 	/* Not enough room for even the info struct. */
    455  1.18  christos 	if (buflen < infosize) {
    456  1.18  christos 		*sizep = 0;
    457  1.18  christos 		return ENOMEM;
    458  1.18  christos 	}
    459  1.23     rmind 	sz = min(tsize, buflen);
    460  1.23     rmind 	bf = kmem_zalloc(sz, KM_SLEEP);
    461  1.18  christos 
    462  1.18  christos 	switch (*name) {
    463  1.18  christos #ifdef SYSVMSG
    464  1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    465  1.18  christos 		msgsi = (struct msg_sysctl_info *)bf;
    466  1.18  christos 		msgsi->msginfo = msginfo;
    467  1.18  christos 		break;
    468  1.18  christos #endif
    469  1.18  christos #ifdef SYSVSEM
    470  1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    471  1.18  christos 		semsi = (struct sem_sysctl_info *)bf;
    472  1.18  christos 		semsi->seminfo = seminfo;
    473  1.18  christos 		break;
    474  1.18  christos #endif
    475  1.18  christos #ifdef SYSVSHM
    476  1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    477  1.18  christos 		shmsi = (struct shm_sysctl_info *)bf;
    478  1.18  christos 		shmsi->shminfo = shminfo;
    479  1.18  christos 		break;
    480  1.18  christos #endif
    481  1.18  christos 	}
    482  1.18  christos 	buflen -= infosize;
    483  1.18  christos 
    484  1.18  christos 	ret = 0;
    485  1.18  christos 	if (buflen > 0) {
    486  1.18  christos 		/* Fill in the IPC data structures.  */
    487  1.18  christos 		for (i = 0; i < nds; i++) {
    488  1.18  christos 			if (buflen < dssize) {
    489  1.18  christos 				ret = ENOMEM;
    490  1.18  christos 				break;
    491  1.18  christos 			}
    492  1.18  christos 			switch (*name) {
    493  1.18  christos #ifdef SYSVMSG
    494  1.18  christos 			case KERN_SYSVIPC_MSG_INFO:
    495  1.20        ad 				mutex_enter(&msgmutex);
    496  1.22  christos 				SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
    497  1.20        ad 				mutex_exit(&msgmutex);
    498  1.18  christos 				break;
    499  1.18  christos #endif
    500  1.18  christos #ifdef SYSVSEM
    501  1.18  christos 			case KERN_SYSVIPC_SEM_INFO:
    502  1.22  christos 				SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
    503  1.18  christos 				break;
    504  1.18  christos #endif
    505  1.18  christos #ifdef SYSVSHM
    506  1.18  christos 			case KERN_SYSVIPC_SHM_INFO:
    507  1.22  christos 				SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    508  1.18  christos 				break;
    509  1.18  christos #endif
    510  1.18  christos 			}
    511  1.18  christos 			buflen -= dssize;
    512  1.18  christos 		}
    513  1.18  christos 	}
    514  1.18  christos 	*sizep -= buflen;
    515  1.18  christos 	error = copyout(bf, start, *sizep);
    516  1.18  christos 	/* If copyout succeeded, use return code set earlier. */
    517  1.18  christos 	if (error == 0)
    518  1.18  christos 		error = ret;
    519  1.18  christos 	if (bf)
    520  1.23     rmind 		kmem_free(bf, sz);
    521  1.18  christos 	return error;
    522  1.18  christos }
    523  1.18  christos 
    524  1.18  christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
    525  1.18  christos {
    526  1.18  christos 
    527  1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    528  1.18  christos 		CTLFLAG_PERMANENT,
    529  1.18  christos 		CTLTYPE_NODE, "ipc",
    530  1.18  christos 		SYSCTL_DESCR("SysV IPC options"),
    531  1.18  christos 		NULL, 0, NULL, 0,
    532  1.18  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    533  1.18  christos 
    534  1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    535  1.18  christos 		CTLFLAG_PERMANENT,
    536  1.18  christos 		CTLTYPE_STRUCT, "sysvipc_info",
    537  1.18  christos 		SYSCTL_DESCR("System V style IPC information"),
    538  1.18  christos 		sysctl_kern_sysvipc, 0, NULL, 0,
    539  1.18  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
    540  1.18  christos }
    541