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