1 /* $NetBSD: sem.h,v 1.13 2002/03/17 22:21:59 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * SVID compatible sem.h file 42 * 43 * Author: Daniel Boulet 44 */ 45 46 #ifndef _SYS_SEM_H_ 47 #define _SYS_SEM_H_ 48 49 #include <sys/featuretest.h> 50 51 #include <sys/ipc.h> 52 53 #ifdef _KERNEL 54 struct __sem { 55 unsigned short semval; /* semaphore value */ 56 pid_t sempid; /* pid of last operation */ 57 unsigned short semncnt; /* # awaiting semval > cval */ 58 unsigned short semzcnt; /* # awaiting semval = 0 */ 59 }; 60 #endif /* _KERNEL */ 61 62 struct semid_ds { 63 struct ipc_perm sem_perm; /* operation permission structure */ 64 unsigned short sem_nsems; /* number of semaphores in set */ 65 time_t sem_otime; /* last semop() time */ 66 time_t sem_ctime; /* last time changed by semctl() */ 67 68 /* 69 * These members are private and used only in the internal 70 * implementation of this interface. 71 */ 72 struct __sem *_sem_base; /* pointer to first semaphore in set */ 73 }; 74 75 #ifdef _KERNEL 76 struct semid_ds14 { 77 struct ipc_perm14 sem_perm; /* operation permission struct */ 78 struct __sem *sem_base; /* pointer to first semaphore in set */ 79 unsigned short sem_nsems; /* number of sems in set */ 80 time_t sem_otime; /* last operation time */ 81 long sem_pad1; /* SVABI/386 says I need this here */ 82 time_t sem_ctime; /* last change time */ 83 /* Times measured in secs since */ 84 /* 00:00:00 GMT, Jan. 1, 1970 */ 85 long sem_pad2; /* SVABI/386 says I need this here */ 86 long sem_pad3[4]; /* SVABI/386 says I need this here */ 87 }; 88 #endif /* _KERNEL */ 89 90 /* 91 * semop's sops parameter structure 92 */ 93 struct sembuf { 94 unsigned short sem_num; /* semaphore # */ 95 short sem_op; /* semaphore operation */ 96 short sem_flg; /* operation flags */ 97 }; 98 #define SEM_UNDO 010000 /* undo changes on process exit */ 99 100 /* 101 * commands for semctl 102 */ 103 #define GETNCNT 3 /* Return the value of semncnt {READ} */ 104 #define GETPID 4 /* Return the value of sempid {READ} */ 105 #define GETVAL 5 /* Return the value of semval {READ} */ 106 #define GETALL 6 /* Return semvals into arg.array {READ} */ 107 #define GETZCNT 7 /* Return the value of semzcnt {READ} */ 108 #define SETVAL 8 /* Set the value of semval to arg.val {ALTER} */ 109 #define SETALL 9 /* Set semvals from arg.array {ALTER} */ 110 111 #ifdef _KERNEL 112 /* 113 * Kernel implementation stuff 114 */ 115 #define SEMVMX 32767 /* semaphore maximum value */ 116 #define SEMAEM 16384 /* adjust on exit max value */ 117 118 #define MAX_SOPS 5 /* maximum # of sembuf's per semop call */ 119 120 /* 121 * Permissions 122 */ 123 #define SEM_A 0200 /* alter permission */ 124 #define SEM_R 0400 /* read permission */ 125 126 /* 127 * Undo structure (one per process) 128 */ 129 struct sem_undo { 130 struct sem_undo *un_next; /* ptr to next active undo structure */ 131 struct proc *un_proc; /* owner of this structure */ 132 short un_cnt; /* # of active entries */ 133 struct undo { 134 short un_adjval; /* adjust on exit values */ 135 short un_num; /* semaphore # */ 136 int un_id; /* semid */ 137 } un_ent[1]; /* undo entries */ 138 }; 139 #endif /* _KERNEL */ 140 141 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) 142 /* 143 * semaphore info struct 144 */ 145 struct seminfo { 146 int32_t semmap; /* # of entries in semaphore map */ 147 int32_t semmni; /* # of semaphore identifiers */ 148 int32_t semmns; /* # of semaphores in system */ 149 int32_t semmnu; /* # of undo structures in system */ 150 int32_t semmsl; /* max # of semaphores per id */ 151 int32_t semopm; /* max # of operations per semop call */ 152 int32_t semume; /* max # of undo entries per process */ 153 int32_t semusz; /* size in bytes of undo structure */ 154 int32_t semvmx; /* semaphore maximum value */ 155 int32_t semaem; /* adjust on exit max value */ 156 }; 157 158 /* Warning: 64-bit structure padding is needed here */ 159 struct semid_ds_sysctl { 160 struct ipc_perm_sysctl sem_perm; 161 int16_t sem_nsems; 162 int16_t pad2; 163 int32_t pad3; 164 time_t sem_otime; 165 time_t sem_ctime; 166 }; 167 struct sem_sysctl_info { 168 struct seminfo seminfo; 169 struct semid_ds_sysctl semids[1]; 170 }; 171 172 /* 173 * Internal "mode" bits. The first of these is used by ipcs(1), and so 174 * is defined outside the kernel as well. 175 */ 176 #define SEM_ALLOC 01000 /* semaphore is allocated */ 177 #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */ 178 179 #ifdef _KERNEL 180 #define SEM_DEST 02000 /* semaphore will be destroyed on last detach */ 181 182 /* 183 * Configuration parameters 184 */ 185 #ifndef SEMMNI 186 #define SEMMNI 10 /* # of semaphore identifiers */ 187 #endif 188 #ifndef SEMMNS 189 #define SEMMNS 60 /* # of semaphores in system */ 190 #endif 191 #ifndef SEMUME 192 #define SEMUME 10 /* max # of undo entries per process */ 193 #endif 194 #ifndef SEMMNU 195 #define SEMMNU 30 /* # of undo structures in system */ 196 #endif 197 198 /* shouldn't need tuning */ 199 #ifndef SEMMAP 200 #define SEMMAP 30 /* # of entries in semaphore map */ 201 #endif 202 #ifndef SEMMSL 203 #define SEMMSL SEMMNS /* max # of semaphores per id */ 204 #endif 205 #ifndef SEMOPM 206 #define SEMOPM 100 /* max # of operations per semop call */ 207 #endif 208 209 /* actual size of an undo structure */ 210 #define SEMUSZ (sizeof(struct sem_undo)+sizeof(struct undo)*SEMUME) 211 212 /* 213 * Structures allocated in machdep.c 214 */ 215 extern struct seminfo seminfo; 216 extern struct semid_ds *sema; /* semaphore id pool */ 217 extern struct __sem *sem; /* semaphore pool */ 218 extern struct map *semmap; /* semaphore allocation map */ 219 extern struct sem_undo *semu_list; /* list of active undo structures */ 220 extern int *semu; /* undo structure pool */ 221 222 /* 223 * Macro to find a particular sem_undo vector 224 */ 225 #define SEMU(ix) ((struct sem_undo *)(((long)semu)+ix * SEMUSZ)) 226 227 /* 228 * Parameters to the semconfig system call 229 */ 230 #define SEM_CONFIG_FREEZE 0 /* Freeze the semaphore facility. */ 231 #define SEM_CONFIG_THAW 1 /* Thaw the semaphore facility. */ 232 #endif /* _KERNEL */ 233 234 #ifndef _KERNEL 235 #include <sys/cdefs.h> 236 237 __BEGIN_DECLS 238 #if defined(__LIBC12_SOURCE__) 239 int semctl __P((int, int, int, union __semun)); 240 int __semctl __P((int, int, int, union __semun *)); 241 int __semctl13 __P((int, int, int, ...)); 242 #else 243 int semctl __P((int, int, int, ...)) __RENAME(__semctl13); 244 #endif 245 int semget __P((key_t, int, int)); 246 int semop __P((int, struct sembuf *, size_t)); 247 #if !defined(_XOPEN_SOURCE) 248 int semconfig __P((int)); 249 #endif 250 __END_DECLS 251 #else 252 void seminit __P((void)); 253 void semexit __P((struct proc *, void *)); 254 255 int semctl1 __P((struct proc *, int, int, int, void *, register_t *)); 256 #endif /* !_KERNEL */ 257 258 #endif /* !_SEM_H_ */ 259