1.\" $OpenBSD: shmctl.2,v 1.17 2014/12/10 19:19:00 schwarze Exp $ 2.\" $NetBSD: shmctl.2,v 1.3 1997/03/27 08:20:39 mikel Exp $ 3.\" 4.\" Copyright (c) 1995 Frank van der Linden 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed for the NetBSD Project 18.\" by Frank van der Linden 19.\" 4. The name of the author may not be used to endorse or promote products 20.\" derived from this software without specific prior written permission 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.\"/ 33.Dd $Mdocdate: December 10 2014 $ 34.Dt SHMCTL 2 35.Os 36.Sh NAME 37.Nm shmctl 38.Nd shared memory control operations 39.Sh SYNOPSIS 40.In sys/shm.h 41.Ft int 42.Fn shmctl "int shmid" "int cmd" "struct shmid_ds *buf" 43.Sh DESCRIPTION 44The 45.Fn shmctl 46system call performs some control operations on the shared memory area 47specified by 48.Fa shmid . 49.Pp 50Each shared memory segment has a data structure associated with it, 51parts of which may be altered by 52.Fn shmctl 53and parts of which determine the actions of 54.Fn shmctl . 55.Pp 56This structure is defined as follows in 57.In sys/shm.h : 58.Bd -literal 59struct shmid_ds { 60 struct ipc_perm shm_perm; /* operation permissions */ 61 int shm_segsz; /* size of segment in bytes */ 62 pid_t shm_lpid; /* pid of last shm op */ 63 pid_t shm_cpid; /* pid of creator */ 64 short shm_nattch; /* # of current attaches */ 65 time_t shm_atime; /* last shmat() time*/ 66 time_t shm_dtime; /* last shmdt() time */ 67 time_t shm_ctime; /* last change by shmctl() */ 68 void *shm_internal; /* sysv stupidity */ 69}; 70.Ed 71.Pp 72The 73.Bf -literal 74ipc_perm 75.Ef 76structure used inside the 77.Bf -literal 78shmid_ds 79.Ef 80structure is defined in 81.In sys/ipc.h 82and looks like this: 83.Bd -literal 84struct ipc_perm { 85 uid_t cuid; /* creator user id */ 86 gid_t cgid; /* creator group id */ 87 uid_t uid; /* user id */ 88 gid_t gid; /* group id */ 89 mode_t mode; /* r/w permission (see chmod(2)) */ 90 u_short seq; /* sequence # (to generate unique msg/sem/shm id) */ 91 key_t key; /* user specified msg/sem/shm key */ 92}; 93.Ed 94.Pp 95The operation to be performed by 96.Fn shmctl 97is specified in 98.Fa cmd 99and is one of: 100.Bl -tag -width IPC_RMIDX 101.It Dv IPC_STAT 102Gather information about the shared memory segment and place it in the 103structure pointed to by 104.Fa buf . 105.It Dv IPC_SET 106Set the value of the 107.Va shm_perm.uid , 108.Va shm_perm.gid 109and 110.Va shm_perm.mode 111fields in the structure associated with 112.Fa shmid . 113The values are taken from the corresponding fields in the structure 114pointed to by 115.Fa buf . 116This operation can only be executed by the superuser, or a process that 117has an effective user ID equal to either 118.Va shm_perm.cuid 119or 120.Va shm_perm.uid 121in the data structure associated with the shared memory segment. 122.It Dv IPC_RMID 123Mark the shared memory segment specified by 124.Fa shmid 125for removal when it is no longer in use by any process. 126When it is removed, all data associated with it will be destroyed too. 127Only the superuser or a process with an effective UID equal to the 128.Va shm_perm.cuid 129or 130.Va shm_perm.uid 131values in the data structure associated with the queue can do this. 132.El 133.Pp 134The read and write permissions on a shared memory identifier 135are determined by the 136.Va shm_perm.mode 137field in the same way as is 138done with files (see 139.Xr chmod 2 ) , 140but the effective UID can match either the 141.Va shm_perm.cuid 142field or the 143.Va shm_perm.uid 144field, and the 145effective GID can match either 146.Va shm_perm.cgid 147or 148.Va shm_perm.gid . 149.Sh RETURN VALUES 150.Rv -std 151.Sh ERRORS 152.Fn shmctl 153will fail if: 154.Bl -tag -width Er 155.It Bq Er EPERM 156.Fa cmd 157is equal to 158.Dv IPC_SET 159or 160.Dv IPC_RMID 161and the caller is not the superuser, nor does 162the effective UID match either the 163.Va shm_perm.uid 164or 165.Va shm_perm.cuid 166fields of the data structure associated with the shared memory segment. 167.Pp 168An attempt is made to increase the value of 169.Va shm_qbytes 170through 171.Dv IPC_SET 172but the caller is not the superuser. 173.It Bq Er EACCES 174The command is 175.Dv IPC_STAT 176and the caller has no read permission for this shared memory segment. 177.It Bq Er EINVAL 178.Fa shmid 179is not a valid shared memory segment identifier. 180.Pp 181.Va cmd 182is not a valid command. 183.It Bq Er EFAULT 184.Fa buf 185specifies an invalid address. 186.El 187.Sh SEE ALSO 188.Xr shmat 2 , 189.Xr shmget 2 190.Sh STANDARDS 191Segments which are marked for removal (but not yet removed 192since they are still in use) can be attached to by new callers 193using 194.Xr shmat 2 . 195This is permitted as an extension beyond the standards. 196