Home | History | Annotate | Line # | Download | only in common
sysv_ipc_50.c revision 1.1
      1 /*	$NetBSD: sysv_ipc_50.c,v 1.1 2009/01/19 19:39:41 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sysv_ipc_50.c,v 1.1 2009/01/19 19:39:41 christos Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_sysv.h"
     37 #include "opt_compat_netbsd.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/ipc.h>
     44 #ifdef SYSVMSG
     45 #include <sys/msg.h>
     46 #endif
     47 #ifdef SYSVSEM
     48 #include <sys/sem.h>
     49 #endif
     50 #ifdef SYSVSHM
     51 #include <sys/shm.h>
     52 #endif
     53 #include <sys/systm.h>
     54 #include <sys/malloc.h>
     55 #include <sys/mount.h>
     56 #include <sys/vnode.h>
     57 #include <sys/stat.h>
     58 #include <sys/sysctl.h>
     59 #include <sys/kauth.h>
     60 
     61 #ifdef COMPAT_50
     62 #include <compat/sys/ipc.h>
     63 #ifdef SYSVMSG
     64 #include <compat/sys/msg.h>
     65 #endif
     66 #ifdef SYSVSEM
     67 #include <compat/sys/sem.h>
     68 #endif
     69 #ifdef SYSVSHM
     70 #include <compat/sys/shm.h>
     71 #endif
     72 #endif
     73 
     74 /*
     75  * Check for ipc permission
     76  */
     77 
     78 int
     79 sysctl_kern_sysvipc50(SYSCTLFN_ARGS)
     80 {
     81 	void *where = oldp;
     82 	size_t *sizep = oldlenp;
     83 #ifdef SYSVMSG
     84 	struct msg_sysctl_info50 *msgsi = NULL;
     85 #endif
     86 #ifdef SYSVSEM
     87 	struct sem_sysctl_info50 *semsi = NULL;
     88 #endif
     89 #ifdef SYSVSHM
     90 	struct shm_sysctl_info50 *shmsi = NULL;
     91 #endif
     92 	size_t infosize, dssize, tsize, buflen;
     93 	void *bf = NULL;
     94 	char *start;
     95 	int32_t nds;
     96 	int i, error, ret;
     97 
     98 	if (namelen != 1)
     99 		return EINVAL;
    100 
    101 	start = where;
    102 	buflen = *sizep;
    103 
    104 	switch (*name) {
    105 	case KERN_SYSVIPC_OMSG_INFO:
    106 #ifdef SYSVMSG
    107 		infosize = sizeof(msgsi->msginfo);
    108 		nds = msginfo.msgmni;
    109 		dssize = sizeof(msgsi->msgids[0]);
    110 		break;
    111 #else
    112 		return EINVAL;
    113 #endif
    114 	case KERN_SYSVIPC_OSEM_INFO:
    115 #ifdef SYSVSEM
    116 		infosize = sizeof(semsi->seminfo);
    117 		nds = seminfo.semmni;
    118 		dssize = sizeof(semsi->semids[0]);
    119 		break;
    120 #else
    121 		return EINVAL;
    122 #endif
    123 	case KERN_SYSVIPC_OSHM_INFO:
    124 #ifdef SYSVSHM
    125 		infosize = sizeof(shmsi->shminfo);
    126 		nds = shminfo.shmmni;
    127 		dssize = sizeof(shmsi->shmids[0]);
    128 		break;
    129 #else
    130 		return EINVAL;
    131 #endif
    132 	default:
    133 		return EPASSTHROUGH;
    134 	}
    135 	/*
    136 	 * Round infosize to 64 bit boundary if requesting more than just
    137 	 * the info structure or getting the total data size.
    138 	 */
    139 	if (where == NULL || *sizep > infosize)
    140 		infosize = roundup(infosize, sizeof(quad_t));
    141 	tsize = infosize + nds * dssize;
    142 
    143 	/* Return just the total size required. */
    144 	if (where == NULL) {
    145 		*sizep = tsize;
    146 		return 0;
    147 	}
    148 
    149 	/* Not enough room for even the info struct. */
    150 	if (buflen < infosize) {
    151 		*sizep = 0;
    152 		return ENOMEM;
    153 	}
    154 	bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
    155 
    156 	switch (*name) {
    157 #ifdef SYSVMSG
    158 	case KERN_SYSVIPC_OMSG_INFO:
    159 		msgsi = (struct msg_sysctl_info50 *)bf;
    160 		msgsi->msginfo = msginfo;
    161 		break;
    162 #endif
    163 #ifdef SYSVSEM
    164 	case KERN_SYSVIPC_OSEM_INFO:
    165 		semsi = (struct sem_sysctl_info50 *)bf;
    166 		semsi->seminfo = seminfo;
    167 		break;
    168 #endif
    169 #ifdef SYSVSHM
    170 	case KERN_SYSVIPC_OSHM_INFO:
    171 		shmsi = (struct shm_sysctl_info50 *)bf;
    172 		shmsi->shminfo = shminfo;
    173 		break;
    174 #endif
    175 	}
    176 	buflen -= infosize;
    177 
    178 	ret = 0;
    179 	if (buflen > 0) {
    180 		/* Fill in the IPC data structures.  */
    181 		for (i = 0; i < nds; i++) {
    182 			if (buflen < dssize) {
    183 				ret = ENOMEM;
    184 				break;
    185 			}
    186 			switch (*name) {
    187 #ifdef SYSVMSG
    188 			case KERN_SYSVIPC_OMSG_INFO:
    189 				mutex_enter(&msgmutex);
    190 				SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
    191 				mutex_exit(&msgmutex);
    192 				break;
    193 #endif
    194 #ifdef SYSVSEM
    195 			case KERN_SYSVIPC_OSEM_INFO:
    196 				SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
    197 				break;
    198 #endif
    199 #ifdef SYSVSHM
    200 			case KERN_SYSVIPC_OSHM_INFO:
    201 				SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    202 				break;
    203 #endif
    204 			}
    205 			buflen -= dssize;
    206 		}
    207 	}
    208 	*sizep -= buflen;
    209 	error = copyout(bf, start, *sizep);
    210 	/* If copyout succeeded, use return code set earlier. */
    211 	if (error == 0)
    212 		error = ret;
    213 	if (bf)
    214 		free(bf, M_TEMP);
    215 	return error;
    216 }
    217