xref: /minix3/sys/sys/sem.h (revision 4d272e5a970c4e7d6700cabd52e337e37404b70e)
10a6a1f1dSLionel Sambuc /*	$NetBSD: sem.h,v 1.31 2015/05/13 01:16:15 pgoyette Exp $	*/
2f6aac1c3SLionel Sambuc 
3f6aac1c3SLionel Sambuc /*-
4f6aac1c3SLionel Sambuc  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5f6aac1c3SLionel Sambuc  * All rights reserved.
6f6aac1c3SLionel Sambuc  *
7f6aac1c3SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8f6aac1c3SLionel Sambuc  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9f6aac1c3SLionel Sambuc  * NASA Ames Research Center.
10f6aac1c3SLionel Sambuc  *
11f6aac1c3SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
12f6aac1c3SLionel Sambuc  * modification, are permitted provided that the following conditions
13f6aac1c3SLionel Sambuc  * are met:
14f6aac1c3SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16f6aac1c3SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
18f6aac1c3SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
19f6aac1c3SLionel Sambuc  *
20f6aac1c3SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21f6aac1c3SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22f6aac1c3SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23f6aac1c3SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24f6aac1c3SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f6aac1c3SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f6aac1c3SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f6aac1c3SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f6aac1c3SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f6aac1c3SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30f6aac1c3SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
31f6aac1c3SLionel Sambuc  */
32f6aac1c3SLionel Sambuc 
33f6aac1c3SLionel Sambuc /*
34f6aac1c3SLionel Sambuc  * SVID compatible sem.h file
35f6aac1c3SLionel Sambuc  *
36f6aac1c3SLionel Sambuc  * Author: Daniel Boulet
37f6aac1c3SLionel Sambuc  */
38f6aac1c3SLionel Sambuc 
39f6aac1c3SLionel Sambuc #ifndef _SYS_SEM_H_
40f6aac1c3SLionel Sambuc #define _SYS_SEM_H_
41f6aac1c3SLionel Sambuc 
42f6aac1c3SLionel Sambuc #include <sys/featuretest.h>
43f6aac1c3SLionel Sambuc 
44f6aac1c3SLionel Sambuc #include <sys/ipc.h>
45f6aac1c3SLionel Sambuc 
46515ae0a3SBen Gras #ifdef _KERNEL
47515ae0a3SBen Gras struct __sem {
48515ae0a3SBen Gras 	unsigned short	semval;		/* semaphore value */
49515ae0a3SBen Gras 	pid_t		sempid;		/* pid of last operation */
50515ae0a3SBen Gras 	unsigned short	semncnt;	/* # awaiting semval > cval */
51515ae0a3SBen Gras 	unsigned short	semzcnt;	/* # awaiting semval = 0 */
52515ae0a3SBen Gras };
53515ae0a3SBen Gras #endif /* _KERNEL */
54515ae0a3SBen Gras 
55f6aac1c3SLionel Sambuc struct semid_ds {
56515ae0a3SBen Gras 	struct ipc_perm	sem_perm;	/* operation permission structure */
57515ae0a3SBen Gras 	unsigned short	sem_nsems;	/* number of semaphores in set */
58f6aac1c3SLionel Sambuc 	time_t		sem_otime;	/* last semop() time */
59f6aac1c3SLionel Sambuc 	time_t		sem_ctime;	/* last time changed by semctl() */
60515ae0a3SBen Gras 
61515ae0a3SBen Gras 	/*
62515ae0a3SBen Gras 	 * These members are private and used only in the internal
63515ae0a3SBen Gras 	 * implementation of this interface.
64515ae0a3SBen Gras 	 */
65515ae0a3SBen Gras 	struct __sem	*_sem_base;	/* pointer to first semaphore in set */
66f6aac1c3SLionel Sambuc };
67f6aac1c3SLionel Sambuc 
68f6aac1c3SLionel Sambuc /*
69f6aac1c3SLionel Sambuc  * semop's sops parameter structure
70f6aac1c3SLionel Sambuc  */
71f6aac1c3SLionel Sambuc struct sembuf {
72f6aac1c3SLionel Sambuc 	unsigned short	sem_num;	/* semaphore # */
73f6aac1c3SLionel Sambuc 	short		sem_op;		/* semaphore operation */
74f6aac1c3SLionel Sambuc 	short		sem_flg;	/* operation flags */
75f6aac1c3SLionel Sambuc };
76515ae0a3SBen Gras #define SEM_UNDO	010000		/* undo changes on process exit */
77f6aac1c3SLionel Sambuc 
78f6aac1c3SLionel Sambuc /*
79f6aac1c3SLionel Sambuc  * commands for semctl
80f6aac1c3SLionel Sambuc  */
81515ae0a3SBen Gras #define GETNCNT	3	/* Return the value of semncnt {READ} */
82515ae0a3SBen Gras #define GETPID	4	/* Return the value of sempid {READ} */
83515ae0a3SBen Gras #define GETVAL	5	/* Return the value of semval {READ} */
84515ae0a3SBen Gras #define GETALL	6	/* Return semvals into arg.array {READ} */
85515ae0a3SBen Gras #define GETZCNT	7	/* Return the value of semzcnt {READ} */
86515ae0a3SBen Gras #define SETVAL	8	/* Set the value of semval to arg.val {ALTER} */
87515ae0a3SBen Gras #define SETALL	9	/* Set semvals from arg.array {ALTER} */
88f6aac1c3SLionel Sambuc 
89515ae0a3SBen Gras #if defined(_KERNEL) || defined(__minix)
90515ae0a3SBen Gras /*
91515ae0a3SBen Gras  * Kernel implementation stuff
92515ae0a3SBen Gras  */
93515ae0a3SBen Gras #define SEMVMX	32767		/* semaphore maximum value */
94515ae0a3SBen Gras #define SEMAEM	16384		/* adjust on exit max value */
95f6aac1c3SLionel Sambuc 
96515ae0a3SBen Gras /*
97515ae0a3SBen Gras  * Permissions
98515ae0a3SBen Gras  */
99515ae0a3SBen Gras #define SEM_A		0200	/* alter permission */
100515ae0a3SBen Gras #define SEM_R		0400	/* read permission */
101f6aac1c3SLionel Sambuc 
102f6aac1c3SLionel Sambuc /*
103515ae0a3SBen Gras  * Undo structure (one per process)
104515ae0a3SBen Gras  */
1050a6a1f1dSLionel Sambuc struct sem_undo_entry {
1060a6a1f1dSLionel Sambuc 	short	un_adjval;	/* adjust on exit values */
1070a6a1f1dSLionel Sambuc 	short	un_num;		/* semaphore # */
1080a6a1f1dSLionel Sambuc 	int	un_id;		/* semid */
1090a6a1f1dSLionel Sambuc };
1100a6a1f1dSLionel Sambuc 
111515ae0a3SBen Gras struct sem_undo {
112515ae0a3SBen Gras 	struct	sem_undo *un_next;	/* ptr to next active undo structure */
113515ae0a3SBen Gras 	struct	proc *un_proc;		/* owner of this structure */
114515ae0a3SBen Gras 	short	un_cnt;			/* # of active entries */
1150a6a1f1dSLionel Sambuc 	struct	sem_undo_entry un_ent[1];/* undo entries */
116515ae0a3SBen Gras };
117515ae0a3SBen Gras #endif /* _KERNEL */
118515ae0a3SBen Gras 
119515ae0a3SBen Gras #if defined(_NETBSD_SOURCE)
120515ae0a3SBen Gras /*
121f6aac1c3SLionel Sambuc  * semaphore info struct
122f6aac1c3SLionel Sambuc  */
123f6aac1c3SLionel Sambuc struct seminfo {
124f6aac1c3SLionel Sambuc 	int32_t	semmap;		/* # of entries in semaphore map */
125f6aac1c3SLionel Sambuc 	int32_t	semmni;		/* # of semaphore identifiers */
126f6aac1c3SLionel Sambuc 	int32_t	semmns;		/* # of semaphores in system */
127f6aac1c3SLionel Sambuc 	int32_t	semmnu;		/* # of undo structures in system */
128f6aac1c3SLionel Sambuc 	int32_t	semmsl;		/* max # of semaphores per id */
129f6aac1c3SLionel Sambuc 	int32_t	semopm;		/* max # of operations per semop call */
130f6aac1c3SLionel Sambuc 	int32_t	semume;		/* max # of undo entries per process */
131f6aac1c3SLionel Sambuc 	int32_t	semusz;		/* size in bytes of undo structure */
132f6aac1c3SLionel Sambuc 	int32_t	semvmx;		/* semaphore maximum value */
133f6aac1c3SLionel Sambuc 	int32_t	semaem;		/* adjust on exit max value */
134f6aac1c3SLionel Sambuc };
135f6aac1c3SLionel Sambuc 
136515ae0a3SBen Gras /* Warning: 64-bit structure padding is needed here */
137515ae0a3SBen Gras struct semid_ds_sysctl {
138515ae0a3SBen Gras 	struct	ipc_perm_sysctl sem_perm;
139515ae0a3SBen Gras 	int16_t	sem_nsems;
140515ae0a3SBen Gras 	int16_t	pad2;
141515ae0a3SBen Gras 	int32_t	pad3;
142515ae0a3SBen Gras 	time_t	sem_otime;
143515ae0a3SBen Gras 	time_t	sem_ctime;
144515ae0a3SBen Gras };
145515ae0a3SBen Gras struct sem_sysctl_info {
146515ae0a3SBen Gras 	struct	seminfo seminfo;
147515ae0a3SBen Gras 	struct	semid_ds_sysctl semids[1];
148515ae0a3SBen Gras };
149515ae0a3SBen Gras 
150515ae0a3SBen Gras /*
151515ae0a3SBen Gras  * Internal "mode" bits.  The first of these is used by ipcs(1), and so
152515ae0a3SBen Gras  * is defined outside the kernel as well.
153515ae0a3SBen Gras  */
154515ae0a3SBen Gras #define	SEM_ALLOC	01000	/* semaphore is allocated */
155515ae0a3SBen Gras #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
156515ae0a3SBen Gras 
157515ae0a3SBen Gras #if defined(_KERNEL) || defined(__minix)
158515ae0a3SBen Gras #define	SEM_DEST	02000	/* semaphore will be destroyed on last detach */
159f6aac1c3SLionel Sambuc 
160f6aac1c3SLionel Sambuc /*
161f6aac1c3SLionel Sambuc  * Configuration parameters
162f6aac1c3SLionel Sambuc  */
163515ae0a3SBen Gras #ifndef SEMMNI
164515ae0a3SBen Gras #define SEMMNI	10		/* # of semaphore identifiers */
165515ae0a3SBen Gras #endif
166515ae0a3SBen Gras #ifndef SEMMNS
167515ae0a3SBen Gras #define SEMMNS	60		/* # of semaphores in system */
168515ae0a3SBen Gras #endif
169515ae0a3SBen Gras #ifndef SEMUME
170515ae0a3SBen Gras #define SEMUME	10		/* max # of undo entries per process */
171515ae0a3SBen Gras #endif
172515ae0a3SBen Gras #ifndef SEMMNU
173515ae0a3SBen Gras #define SEMMNU	30		/* # of undo structures in system */
174515ae0a3SBen Gras #endif
175f6aac1c3SLionel Sambuc 
176515ae0a3SBen Gras /* shouldn't need tuning */
177515ae0a3SBen Gras #ifndef SEMMAP
178515ae0a3SBen Gras #define SEMMAP	30		/* # of entries in semaphore map */
179515ae0a3SBen Gras #endif
180515ae0a3SBen Gras #ifndef SEMMSL
181515ae0a3SBen Gras #define SEMMSL	SEMMNS		/* max # of semaphores per id */
182515ae0a3SBen Gras #endif
183515ae0a3SBen Gras #ifndef SEMOPM
184515ae0a3SBen Gras #define SEMOPM	100		/* max # of operations per semop call */
185515ae0a3SBen Gras #endif
186f6aac1c3SLionel Sambuc 
187515ae0a3SBen Gras /* actual size of an undo structure */
1880a6a1f1dSLionel Sambuc #define SEMUSZ	(sizeof(struct sem_undo)+sizeof(struct sem_undo_entry)*SEMUME)
189f6aac1c3SLionel Sambuc 
190*4d272e5aSDavid van Moolenbroek #ifndef __minix
191515ae0a3SBen Gras /*
192515ae0a3SBen Gras  * Structures allocated in machdep.c
193515ae0a3SBen Gras  */
194515ae0a3SBen Gras extern struct seminfo seminfo;
195515ae0a3SBen Gras extern struct semid_ds *sema;		/* semaphore id pool */
196*4d272e5aSDavid van Moolenbroek #endif /* !__minix */
197515ae0a3SBen Gras 
198515ae0a3SBen Gras /*
199515ae0a3SBen Gras  * Parameters to the semconfig system call
200515ae0a3SBen Gras  */
201515ae0a3SBen Gras #define	SEM_CONFIG_FREEZE	0	/* Freeze the semaphore facility. */
202515ae0a3SBen Gras #define	SEM_CONFIG_THAW		1	/* Thaw the semaphore facility. */
203515ae0a3SBen Gras 
204515ae0a3SBen Gras #define SYSCTL_FILL_SEM(src, dst) do { \
205515ae0a3SBen Gras 	SYSCTL_FILL_PERM((src).sem_perm, (dst).sem_perm); \
206515ae0a3SBen Gras 	(dst).sem_nsems = (src).sem_nsems; \
207515ae0a3SBen Gras 	(dst).sem_otime = (src).sem_otime; \
208515ae0a3SBen Gras 	(dst).sem_ctime = (src).sem_ctime; \
209515ae0a3SBen Gras } while (/*CONSTCOND*/ 0)
210515ae0a3SBen Gras 
211515ae0a3SBen Gras #endif /* _KERNEL */
212515ae0a3SBen Gras 
21384d9c625SLionel Sambuc #if defined(__minix)
214515ae0a3SBen Gras /* ipcs ctl cmds */
215515ae0a3SBen Gras # define SEM_STAT 18
216515ae0a3SBen Gras # define SEM_INFO 19
21784d9c625SLionel Sambuc #endif /* defined(__minix) */
218515ae0a3SBen Gras 
219515ae0a3SBen Gras #ifndef _KERNEL
220f6aac1c3SLionel Sambuc #include <sys/cdefs.h>
221f6aac1c3SLionel Sambuc 
222f6aac1c3SLionel Sambuc __BEGIN_DECLS
223515ae0a3SBen Gras #ifndef __LIBC12_SOURCE__
224515ae0a3SBen Gras int	semctl(int, int, int, ...) __RENAME(__semctl50);
225515ae0a3SBen Gras #endif
226f6aac1c3SLionel Sambuc int	semget(key_t, int, int);
227f6aac1c3SLionel Sambuc int	semop(int, struct sembuf *, size_t);
228515ae0a3SBen Gras #if defined(_NETBSD_SOURCE)
229515ae0a3SBen Gras int	semconfig(int);
230515ae0a3SBen Gras #endif
231f6aac1c3SLionel Sambuc __END_DECLS
232515ae0a3SBen Gras #else
233515ae0a3SBen Gras void	seminit(void);
2340a6a1f1dSLionel Sambuc int	semfini(void);
235515ae0a3SBen Gras void	semexit(struct proc *, void *);
236515ae0a3SBen Gras 
237515ae0a3SBen Gras int	semctl1(struct lwp *, int, int, int, void *, register_t *);
238515ae0a3SBen Gras #define get_semctl_arg(cmd, sembuf, arg) \
239515ae0a3SBen Gras     ((cmd) == IPC_SET || (cmd) == IPC_STAT ? (void *)sembuf \
240515ae0a3SBen Gras     : (cmd) == GETALL || (cmd) == SETVAL || (cmd) == SETALL ? (void *)arg \
241515ae0a3SBen Gras     : NULL)
242515ae0a3SBen Gras #endif /* !_KERNEL */
243f6aac1c3SLionel Sambuc 
244f6aac1c3SLionel Sambuc #endif /* !_SYS_SEM_H_ */
245