1/*	$NetBSD: sem.h,v 1.35 2024/05/12 10:34:56 rillig 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * SVID compatible sem.h file
35 *
36 * Author: Daniel Boulet
37 */
38
39#ifndef _SYS_SEM_H_
40#define _SYS_SEM_H_
41
42#include <sys/featuretest.h>
43
44#include <sys/ipc.h>
45
46#ifdef _KERNEL
47struct __sem {
48	unsigned short	semval;		/* semaphore value */
49	pid_t		sempid;		/* pid of last operation */
50	unsigned short	semncnt;	/* # awaiting semval > cval */
51	unsigned short	semzcnt;	/* # awaiting semval = 0 */
52};
53#endif /* _KERNEL */
54
55struct semid_ds {
56	struct ipc_perm	sem_perm;	/* operation permission structure */
57	unsigned short	sem_nsems;	/* number of semaphores in set */
58	time_t		sem_otime;	/* last semop() time */
59	time_t		sem_ctime;	/* last time changed by semctl() */
60
61	/*
62	 * These members are private and used only in the internal
63	 * implementation of this interface.
64	 */
65	struct __sem	*_sem_base;	/* pointer to first semaphore in set */
66};
67
68/*
69 * semop's sops parameter structure
70 */
71struct sembuf {
72	unsigned short	sem_num;	/* semaphore # */
73	short		sem_op;		/* semaphore operation */
74	short		sem_flg;	/* operation flags */
75};
76#define SEM_UNDO	010000		/* undo changes on process exit */
77
78/*
79 * commands for semctl
80 */
81#define GETNCNT	3	/* Return the value of semncnt {READ} */
82#define GETPID	4	/* Return the value of sempid {READ} */
83#define GETVAL	5	/* Return the value of semval {READ} */
84#define GETALL	6	/* Return semvals into arg.array {READ} */
85#define GETZCNT	7	/* Return the value of semzcnt {READ} */
86#define SETVAL	8	/* Set the value of semval to arg.val {ALTER} */
87#define SETALL	9	/* Set semvals from arg.array {ALTER} */
88
89#ifdef _KERNEL
90/*
91 * Kernel implementation stuff
92 */
93#define SEMVMX	32767		/* semaphore maximum value */
94#define SEMAEM	16384		/* adjust on exit max value */
95
96/*
97 * Permissions
98 */
99#define SEM_A		0200	/* alter permission */
100#define SEM_R		0400	/* read permission */
101
102/*
103 * Undo structure (one per process)
104 */
105struct sem_undo_entry {
106	short	un_adjval;	/* adjust on exit values */
107	short	un_num;		/* semaphore # */
108	int	un_id;		/* semid */
109};
110
111struct sem_undo {
112	struct	sem_undo *un_next;	/* ptr to next active undo structure */
113	struct	proc *un_proc;		/* owner of this structure */
114	short	un_cnt;			/* # of active entries */
115	struct	sem_undo_entry un_ent[1];/* undo entries */
116};
117#endif /* _KERNEL */
118
119#if defined(_NETBSD_SOURCE)
120/*
121 * semaphore info struct
122 */
123struct seminfo {
124	int32_t	semmap;		/* # of entries in semaphore map */
125	int32_t	semmni;		/* # of semaphore identifiers */
126	int32_t	semmns;		/* # of semaphores in system */
127	int32_t	semmnu;		/* # of undo structures in system */
128	int32_t	semmsl;		/* max # of semaphores per id */
129	int32_t	semopm;		/* max # of operations per semop call */
130	int32_t	semume;		/* max # of undo entries per process */
131	int32_t	semusz;		/* size in bytes of undo structure */
132	int32_t	semvmx;		/* semaphore maximum value */
133	int32_t	semaem;		/* adjust on exit max value */
134};
135
136/* Warning: 64-bit structure padding is needed here */
137struct semid_ds_sysctl {
138	struct	ipc_perm_sysctl sem_perm;
139	int16_t	sem_nsems;
140	int16_t	pad2;
141	int32_t	pad3;
142	time_t	sem_otime;
143	time_t	sem_ctime;
144};
145struct sem_sysctl_info {
146	struct	seminfo seminfo;
147	struct	semid_ds_sysctl semids[1];
148};
149
150/*
151 * Internal "mode" bits.  The first of these is used by ipcs(1), and so
152 * is defined outside the kernel as well.
153 */
154#define	SEM_ALLOC	01000	/* semaphore is allocated */
155#endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
156
157#ifdef _KERNEL
158#define	SEM_DEST	02000	/* semaphore will be destroyed on last detach */
159
160/*
161 * Configuration parameters
162 */
163#ifndef SEMMNI
164#define SEMMNI	10		/* # of semaphore identifiers */
165#endif
166#ifndef SEMMNS
167#define SEMMNS	60		/* # of semaphores in system */
168#endif
169#ifndef SEMUME
170#define SEMUME	10		/* max # of undo entries per process */
171#endif
172#ifndef SEMMNU
173#define SEMMNU	30		/* # of undo structures in system */
174#endif
175
176/* shouldn't need tuning */
177#ifndef SEMMAP
178#define SEMMAP	30		/* # of entries in semaphore map */
179#endif
180#ifndef SEMMSL
181#define SEMMSL	SEMMNS		/* max # of semaphores per id */
182#endif
183#ifndef SEMOPM
184#define SEMOPM	100		/* max # of operations per semop call */
185#endif
186
187/* actual size of an undo structure */
188#define SEMUSZ	(sizeof(struct sem_undo)+sizeof(struct sem_undo_entry)*SEMUME)
189
190/*
191 * Structures allocated in machdep.c
192 */
193extern struct seminfo seminfo;
194extern struct semid_ds *sema;		/* semaphore id pool */
195
196/*
197 * Parameters to the semconfig system call
198 */
199#define	SEM_CONFIG_FREEZE	0	/* Freeze the semaphore facility. */
200#define	SEM_CONFIG_THAW		1	/* Thaw the semaphore facility. */
201
202#define SYSCTL_FILL_SEM(src, dst) do { \
203	SYSCTL_FILL_PERM((src).sem_perm, (dst).sem_perm); \
204	(dst).sem_nsems = (src).sem_nsems; \
205	(dst).sem_otime = (src).sem_otime; \
206	(dst).sem_ctime = (src).sem_ctime; \
207} while (0)
208
209#endif /* _KERNEL */
210
211#ifndef _KERNEL
212#include <sys/cdefs.h>
213
214__BEGIN_DECLS
215#ifndef __LIBC12_SOURCE__
216int	semctl(int, int, int, ...) __RENAME(__semctl50);
217#endif
218int	semget(key_t, int, int);
219int	semop(int, struct sembuf *, size_t);
220#if defined(_NETBSD_SOURCE)
221int	semconfig(int);
222#endif
223__END_DECLS
224#else
225int	seminit(void);
226int	semfini(void);
227void	semexit(struct proc *, void *);
228
229int	semctl1(struct lwp *, int, int, int, void *, register_t *);
230#define get_semctl_arg(cmd, sembuf, arg) \
231    ((cmd) == IPC_SET || (cmd) == IPC_STAT ? (void *)sembuf \
232    : (cmd) == GETALL || (cmd) == SETVAL || (cmd) == SETALL ? (void *)arg \
233    : NULL)
234#endif /* !_KERNEL */
235
236#endif /* !_SYS_SEM_H_ */
237