1*2fe8fb19SBen Gras.\" $NetBSD: semctl.2,v 1.20 2010/03/22 19:30:55 joerg Exp $ 2*2fe8fb19SBen Gras.\" 3*2fe8fb19SBen Gras.\" Copyright (c) 1995 Frank van der Linden 4*2fe8fb19SBen Gras.\" All rights reserved. 5*2fe8fb19SBen Gras.\" 6*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 7*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 8*2fe8fb19SBen Gras.\" are met: 9*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 10*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 11*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 12*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 13*2fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 14*2fe8fb19SBen Gras.\" 3. All advertising materials mentioning features or use of this software 15*2fe8fb19SBen Gras.\" must display the following acknowledgement: 16*2fe8fb19SBen Gras.\" This product includes software developed for the NetBSD Project 17*2fe8fb19SBen Gras.\" by Frank van der Linden 18*2fe8fb19SBen Gras.\" 4. The name of the author may not be used to endorse or promote products 19*2fe8fb19SBen Gras.\" derived from this software without specific prior written permission 20*2fe8fb19SBen Gras.\" 21*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23*2fe8fb19SBen Gras.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24*2fe8fb19SBen Gras.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25*2fe8fb19SBen Gras.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26*2fe8fb19SBen Gras.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27*2fe8fb19SBen Gras.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28*2fe8fb19SBen Gras.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29*2fe8fb19SBen Gras.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30*2fe8fb19SBen Gras.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31*2fe8fb19SBen Gras.\" 32*2fe8fb19SBen Gras.Dd August 25, 1999 33*2fe8fb19SBen Gras.Dt SEMCTL 2 34*2fe8fb19SBen Gras.Os 35*2fe8fb19SBen Gras.Sh NAME 36*2fe8fb19SBen Gras.Nm semctl 37*2fe8fb19SBen Gras.Nd semaphore control operations 38*2fe8fb19SBen Gras.Sh LIBRARY 39*2fe8fb19SBen Gras.Lb libc 40*2fe8fb19SBen Gras.Sh SYNOPSIS 41*2fe8fb19SBen Gras.In sys/sem.h 42*2fe8fb19SBen Gras.Ft int 43*2fe8fb19SBen Gras.Fn semctl "int semid" "int semnum" "int cmd" "..." 44*2fe8fb19SBen Gras.Sh DESCRIPTION 45*2fe8fb19SBen GrasThe 46*2fe8fb19SBen Gras.Fn semctl 47*2fe8fb19SBen Grassystem call provides a number of control operations on the semaphore specified 48*2fe8fb19SBen Grasby 49*2fe8fb19SBen Gras.Fa semnum 50*2fe8fb19SBen Grasand 51*2fe8fb19SBen Gras.Fa semid . 52*2fe8fb19SBen GrasThe operation to be performed is specified in 53*2fe8fb19SBen Gras.Fa cmd 54*2fe8fb19SBen Gras(see below). 55*2fe8fb19SBen GrasThe fourth argument is optional and depends upon the operation requested. 56*2fe8fb19SBen GrasIf required, it is a union of the following fields: 57*2fe8fb19SBen Gras.Bd -literal 58*2fe8fb19SBen Gras int val; /* value for SETVAL */ 59*2fe8fb19SBen Gras struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */ 60*2fe8fb19SBen Gras u_short *array; /* array for GETALL \*[Am] SETALL */ 61*2fe8fb19SBen Gras.Ed 62*2fe8fb19SBen Gras.Pp 63*2fe8fb19SBen GrasThe 64*2fe8fb19SBen Gras.Sy semid_ds 65*2fe8fb19SBen Grasstructure used in the 66*2fe8fb19SBen Gras.Dv IPC_SET 67*2fe8fb19SBen Grasand 68*2fe8fb19SBen Gras.Dv IPC_STAT 69*2fe8fb19SBen Grascommands is defined in 70*2fe8fb19SBen Gras.In sys/sem.h 71*2fe8fb19SBen Grasand contains the following members: 72*2fe8fb19SBen Gras.Bd -literal 73*2fe8fb19SBen Gras struct ipc_perm sem_perm; /* operation permissions */ 74*2fe8fb19SBen Gras unsigned short sem_nsems; /* number of sems in set */ 75*2fe8fb19SBen Gras time_t sem_otime; /* last operation time */ 76*2fe8fb19SBen Gras time_t sem_ctime; /* last change time */ 77*2fe8fb19SBen Gras.Ed 78*2fe8fb19SBen Gras.Pp 79*2fe8fb19SBen GrasThe 80*2fe8fb19SBen Gras.Sy ipc_perm 81*2fe8fb19SBen Grasstructure used inside the 82*2fe8fb19SBen Gras.Sy semid_ds 83*2fe8fb19SBen Grasstructure is defined in 84*2fe8fb19SBen Gras.In sys/ipc.h 85*2fe8fb19SBen Grasand contains the following members: 86*2fe8fb19SBen Gras.Bd -literal 87*2fe8fb19SBen Gras uid_t cuid; /* creator user id */ 88*2fe8fb19SBen Gras gid_t cgid; /* creator group id */ 89*2fe8fb19SBen Gras uid_t uid; /* user id */ 90*2fe8fb19SBen Gras gid_t gid; /* group id */ 91*2fe8fb19SBen Gras mode_t mode; /* permission (lower 9 bits) */ 92*2fe8fb19SBen Gras.Ed 93*2fe8fb19SBen Gras.Pp 94*2fe8fb19SBen Gras.Fn semctl 95*2fe8fb19SBen Grasprovides the following operations: 96*2fe8fb19SBen Gras.Bl -tag -width IPC_RMIDX 97*2fe8fb19SBen Gras.It Dv GETVAL 98*2fe8fb19SBen GrasReturn the value of the semaphore. 99*2fe8fb19SBen Gras.It Dv SETVAL 100*2fe8fb19SBen GrasSet the value of the semaphore to 101*2fe8fb19SBen Gras.Fa arg.val , 102*2fe8fb19SBen Graswhere 103*2fe8fb19SBen Gras.Fa arg 104*2fe8fb19SBen Grasis the fourth argument to 105*2fe8fb19SBen Gras.Fn semctl . 106*2fe8fb19SBen Gras.It Dv GETPID 107*2fe8fb19SBen GrasReturn the pid of the last process that did an operation on this semaphore. 108*2fe8fb19SBen Gras.It Dv GETNCNT 109*2fe8fb19SBen GrasReturn the number of processes waiting to acquire the semaphore. 110*2fe8fb19SBen Gras.It Dv GETZCNT 111*2fe8fb19SBen GrasReturn the number of processes waiting for the value of the semaphore to 112*2fe8fb19SBen Grasreach 0. 113*2fe8fb19SBen Gras.It Dv GETALL 114*2fe8fb19SBen GrasReturn the values of all the semaphores associated with 115*2fe8fb19SBen Gras.Fa semid . 116*2fe8fb19SBen Gras.It Dv SETALL 117*2fe8fb19SBen GrasSet the values of all the semaphores that are associated with the semaphore 118*2fe8fb19SBen Grasidentifier 119*2fe8fb19SBen Gras.Fa semid 120*2fe8fb19SBen Grasto the corresponding values in 121*2fe8fb19SBen Gras.Fa arg.array , 122*2fe8fb19SBen Graswhere 123*2fe8fb19SBen Gras.Fa arg 124*2fe8fb19SBen Grasis the fourth argument to 125*2fe8fb19SBen Gras.Fn semctl . 126*2fe8fb19SBen Gras.It Dv IPC_STAT 127*2fe8fb19SBen GrasGather information about a semaphore and place the information in the 128*2fe8fb19SBen Grasstructure pointed to by 129*2fe8fb19SBen Gras.Fa arg.buf , 130*2fe8fb19SBen Graswhere 131*2fe8fb19SBen Gras.Fa arg 132*2fe8fb19SBen Grasis the fourth argument to 133*2fe8fb19SBen Gras.Fn semctl . 134*2fe8fb19SBen Gras.It Dv IPC_SET 135*2fe8fb19SBen GrasSet the value of the 136*2fe8fb19SBen Gras.Va sem_perm.uid , 137*2fe8fb19SBen Gras.Va sem_perm.gid 138*2fe8fb19SBen Grasand 139*2fe8fb19SBen Gras.Va sem_perm.mode 140*2fe8fb19SBen Grasfields in the structure associated with the semaphore. 141*2fe8fb19SBen GrasThe values are taken from the corresponding fields in the structure 142*2fe8fb19SBen Graspointed to by 143*2fe8fb19SBen Gras.Fa arg.buf , 144*2fe8fb19SBen Grasthere 145*2fe8fb19SBen Gras.Fa arg 146*2fe8fb19SBen Grasis the fourth argument to 147*2fe8fb19SBen Gras.Fn semctl . 148*2fe8fb19SBen GrasThis operation can only be executed by the super-user, or a process that 149*2fe8fb19SBen Grashas an effective user id equal to either 150*2fe8fb19SBen Gras.Va sem_perm.cuid 151*2fe8fb19SBen Grasor 152*2fe8fb19SBen Gras.Va sem_perm.uid 153*2fe8fb19SBen Grasin the data structure associated with the semaphore. 154*2fe8fb19SBen Gras.It Dv IPC_RMID 155*2fe8fb19SBen GrasRemove the semaphores associated with 156*2fe8fb19SBen Gras.Fa semid 157*2fe8fb19SBen Grasfrom the system and destroy the data structures associated with it. 158*2fe8fb19SBen GrasOnly the super-user or a process with an effective uid equal to the 159*2fe8fb19SBen Gras.Va sem_perm.cuid 160*2fe8fb19SBen Grasor 161*2fe8fb19SBen Gras.Va sem_perm.uid 162*2fe8fb19SBen Grasvalues in the data structure associated with the semaphore can do this. 163*2fe8fb19SBen Gras.El 164*2fe8fb19SBen Gras.Pp 165*2fe8fb19SBen GrasThe permission to read or change a semaphore (see 166*2fe8fb19SBen Gras.Xr semop 2 ) 167*2fe8fb19SBen Grasis determined by the 168*2fe8fb19SBen Gras.Va sem_perm.mode 169*2fe8fb19SBen Grasfield in the same way as is 170*2fe8fb19SBen Grasdone with files (see 171*2fe8fb19SBen Gras.Xr chmod 2 ) , 172*2fe8fb19SBen Grasbut the effective uid can match either the 173*2fe8fb19SBen Gras.Va sem_perm.cuid 174*2fe8fb19SBen Grasfield or the 175*2fe8fb19SBen Gras.Va sem_perm.uid 176*2fe8fb19SBen Grasfield, and the 177*2fe8fb19SBen Graseffective gid can match either 178*2fe8fb19SBen Gras.Va sem_perm.cgid 179*2fe8fb19SBen Grasor 180*2fe8fb19SBen Gras.Va sem_perm.gid . 181*2fe8fb19SBen Gras.Sh RETURN VALUES 182*2fe8fb19SBen GrasFor the 183*2fe8fb19SBen Gras.Dv GETVAL , 184*2fe8fb19SBen Gras.Dv GETPID , 185*2fe8fb19SBen Gras.Dv GETNCNT , 186*2fe8fb19SBen Grasand 187*2fe8fb19SBen Gras.Dv GETZCNT 188*2fe8fb19SBen Grasoperations, 189*2fe8fb19SBen Gras.Fn semctl 190*2fe8fb19SBen Grasreturns one of the values described above if successful. 191*2fe8fb19SBen GrasAll other operations will make 192*2fe8fb19SBen Gras.Fn semctl 193*2fe8fb19SBen Grasreturn 0 if no errors occur. 194*2fe8fb19SBen GrasOtherwise \-1 is returned and 195*2fe8fb19SBen Gras.Va errno 196*2fe8fb19SBen Grasset to reflect the error. 197*2fe8fb19SBen Gras.Sh ERRORS 198*2fe8fb19SBen Gras.Fn semctl 199*2fe8fb19SBen Graswill fail if: 200*2fe8fb19SBen Gras.Bl -tag -width Er 201*2fe8fb19SBen Gras.It Bq Er EACCES 202*2fe8fb19SBen GrasThe caller has no operation permission for this semaphore. 203*2fe8fb19SBen Gras.It Bq Er EFAULT 204*2fe8fb19SBen Gras.Fa arg.buf 205*2fe8fb19SBen Grasor 206*2fe8fb19SBen Gras.Fa arg.array 207*2fe8fb19SBen Grasspecifies an invalid address. 208*2fe8fb19SBen Gras.It Bq Er EINVAL 209*2fe8fb19SBen Gras.Fa semid 210*2fe8fb19SBen Grasis not a valid message semaphore identifier. 211*2fe8fb19SBen Gras.Pp 212*2fe8fb19SBen Gras.Va cmd 213*2fe8fb19SBen Grasis not a valid command. 214*2fe8fb19SBen Gras.It Bq Er EPERM 215*2fe8fb19SBen Gras.Fa cmd 216*2fe8fb19SBen Grasis equal to 217*2fe8fb19SBen Gras.Dv IPC_SET 218*2fe8fb19SBen Grasor 219*2fe8fb19SBen Gras.Dv IPC_RMID 220*2fe8fb19SBen Grasand the caller is not the super-user, nor does 221*2fe8fb19SBen Grasthe effective uid match either the 222*2fe8fb19SBen Gras.Va sem_perm.uid 223*2fe8fb19SBen Grasor 224*2fe8fb19SBen Gras.Va sem_perm.cuid 225*2fe8fb19SBen Grasfields of the data structure associated with the semaphore. 226*2fe8fb19SBen Gras.It Bq Er ERANGE 227*2fe8fb19SBen Gras.Fa cmd 228*2fe8fb19SBen Grasis equal to 229*2fe8fb19SBen Gras.Dv SETVAL 230*2fe8fb19SBen Grasor 231*2fe8fb19SBen Gras.Dv SETALL 232*2fe8fb19SBen Grasand the value to be set is greater than the system semaphore maximum value. 233*2fe8fb19SBen Gras.El 234*2fe8fb19SBen Gras.Sh SEE ALSO 235*2fe8fb19SBen Gras.Xr semget 2 , 236*2fe8fb19SBen Gras.Xr semop 2 237*2fe8fb19SBen Gras.Sh STANDARDS 238*2fe8fb19SBen GrasThe 239*2fe8fb19SBen Gras.Nm 240*2fe8fb19SBen Grassystem call conforms to 241*2fe8fb19SBen Gras.St -xsh5 . 242*2fe8fb19SBen Gras.Sh HISTORY 243*2fe8fb19SBen GrasSemaphores appeared in the first release of 244*2fe8fb19SBen Gras.At V . 245