xref: /openbsd/sys/sys/sem.h (revision 08b61afc)
1 /*	$OpenBSD: sem.h,v 1.25 2022/09/16 15:57:23 mbuhl Exp $	*/
2 /*	$NetBSD: sem.h,v 1.8 1996/02/09 18:25:29 christos Exp $	*/
3 
4 /*
5  * SVID compatible sem.h file
6  *
7  * Author:  Daniel Boulet
8  */
9 
10 #ifndef _SYS_SEM_H_
11 #define _SYS_SEM_H_
12 
13 #ifndef _SYS_IPC_H_
14 #include <sys/ipc.h>
15 #endif
16 
17 #if __BSD_VISIBLE
18 
19 /* sem-specific sysctl variables corresponding to members of struct seminfo */
20 #define	KERN_SEMINFO_SEMMNI	1	/* int: # of semaphore identifiers */
21 #define	KERN_SEMINFO_SEMMNS	2	/* int: # of semaphores in system */
22 #define	KERN_SEMINFO_SEMMNU	3	/* int: # of undo structures in system */
23 #define	KERN_SEMINFO_SEMMSL	4	/* int: max semaphores per id */
24 #define	KERN_SEMINFO_SEMOPM	5	/* int: max operations per semop call */
25 #define	KERN_SEMINFO_SEMUME	6	/* int: max undo entries per process */
26 #define	KERN_SEMINFO_SEMUSZ	7	/* int: size in bytes of struct undo */
27 #define	KERN_SEMINFO_SEMVMX	8	/* int: semaphore maximum value */
28 #define	KERN_SEMINFO_SEMAEM	9	/* int: adjust on exit max value */
29 #define	KERN_SEMINFO_MAXID	10	/* number of valid semaphore sysctls */
30 
31 #define	CTL_KERN_SEMINFO_NAMES { \
32 	{ 0, 0 }, \
33 	{ "semmni", CTLTYPE_INT }, \
34 	{ "semmns", CTLTYPE_INT }, \
35 	{ "semmnu", CTLTYPE_INT }, \
36 	{ "semmsl", CTLTYPE_INT }, \
37 	{ "semopm", CTLTYPE_INT }, \
38 	{ "semume", CTLTYPE_INT }, \
39 	{ "semusz", CTLTYPE_INT }, \
40 	{ "semvmx", CTLTYPE_INT }, \
41 	{ "semaem", CTLTYPE_INT }, \
42 }
43 
44 #endif /* __BSD_VISIBLE */
45 
46 struct sem {
47 	unsigned short	semval;		/* semaphore value */
48 	pid_t		sempid;		/* pid of last operation */
49 	unsigned short	semncnt;	/* # awaiting semval > cval */
50 	unsigned short	semzcnt;	/* # awaiting semval = 0 */
51 };
52 
53 struct semid_ds {
54 	struct ipc_perm	sem_perm;	/* operation permission struct */
55 	struct sem	*sem_base;	/* pointer to first semaphore in set */
56 	unsigned short	sem_nsems;	/* number of sems in set */
57 	time_t		sem_otime;	/* last operation time */
58 	long		sem_pad1;	/* SVABI/386 says I need this here */
59 	time_t		sem_ctime;	/* last change time */
60 	    				/* Times measured in secs since */
61 	    				/* 00:00:00 GMT, Jan. 1, 1970 */
62 	long		sem_pad2;	/* SVABI/386 says I need this here */
63 	long		sem_pad3[4];	/* SVABI/386 says I need this here */
64 };
65 
66 /*
67  * semop's sops parameter structure
68  */
69 struct sembuf {
70 	unsigned short	sem_num;	/* semaphore # */
71 	short		sem_op;		/* semaphore operation */
72 	short		sem_flg;	/* operation flags */
73 };
74 #define SEM_UNDO	010000
75 
76 /*
77  * semctl's arg parameter structure
78  */
79 union semun {
80 	int		val;		/* value for SETVAL */
81 	struct semid_ds	*buf;		/* buffer for IPC_STAT & IPC_SET */
82 	unsigned short	*array;		/* array for GETALL & SETALL */
83 };
84 
85 /*
86  * commands for semctl
87  */
88 #define GETNCNT	3	/* Return the value of semncnt {READ} */
89 #define GETPID	4	/* Return the value of sempid {READ} */
90 #define GETVAL	5	/* Return the value of semval {READ} */
91 #define GETALL	6	/* Return semvals into arg.array {READ} */
92 #define GETZCNT	7	/* Return the value of semzcnt {READ} */
93 #define SETVAL	8	/* Set the value of semval to arg.val {ALTER} */
94 #define SETALL	9	/* Set semvals from arg.array {ALTER} */
95 
96 
97 /*
98  * Permissions
99  */
100 #define SEM_A		0200	/* alter permission */
101 #define SEM_R		0400	/* read permission */
102 
103 
104 #ifdef _KERNEL
105 #include <sys/queue.h>
106 
107 /*
108  * Kernel implementation stuff
109  */
110 #define SEMVMX	32767		/* semaphore maximum value */
111 #define SEMAEM	16384		/* adjust on exit max value */
112 
113 /*
114  * Undo structure (one per process)
115  */
116 struct sem_undo {
117 	SLIST_ENTRY(sem_undo) un_next;	/* ptr to next active undo structure */
118 	struct	process *un_proc;	/* owner of this structure */
119 	short	un_cnt;			/* # of active entries */
120 	struct undo {
121 		short	un_adjval;	/* adjust on exit values */
122 		short	un_num;		/* semaphore # */
123 		int	un_id;		/* semid */
124 	} un_ent[1];			/* undo entries */
125 };
126 
127 /*
128  * semaphore info struct
129  */
130 struct seminfo {
131 	int	semmni,		/* # of semaphore identifiers */
132 		semmns,		/* # of semaphores in system */
133 		semmnu,		/* # of undo structures in system */
134 		semmsl,		/* max # of semaphores per id */
135 		semopm,		/* max # of operations per semop call */
136 		semume,		/* max # of undo entries per process */
137 		semusz,		/* size in bytes of undo structure */
138 		semvmx,		/* semaphore maximum value */
139 		semaem;		/* adjust on exit max value */
140 };
141 
142 struct sem_sysctl_info {
143 	struct	seminfo seminfo;
144 	struct	semid_ds semids[1];
145 };
146 
147 extern struct seminfo	seminfo;
148 
149 /*
150  * Configuration parameters
151  */
152 #ifndef SEMMNI
153 #define SEMMNI	10		/* # of semaphore identifiers */
154 #endif
155 #ifndef SEMMNS
156 #define SEMMNS	60		/* # of semaphores in system */
157 #endif
158 #ifndef SEMUME
159 #define SEMUME	10		/* max # of undo entries per process */
160 #endif
161 #ifndef SEMMNU
162 #define SEMMNU	30		/* # of undo structures in system */
163 #endif
164 
165 /* shouldn't need tuning */
166 #ifndef SEMMSL
167 #define SEMMSL	SEMMNS		/* max # of semaphores per id */
168 #endif
169 #ifndef SEMOPM
170 #define SEMOPM	100		/* max # of operations per semop call */
171 #endif
172 
173 /* actual size of an undo structure */
174 #define SEMUSZ	(sizeof(struct sem_undo)+sizeof(struct undo)*SEMUME)
175 
176 extern struct	semid_ds **sema;	/* semaphore id list */
177 
178 struct proc;
179 
180 void	seminit(void);
181 void	semexit(struct process *);
182 int	sysctl_sysvsem(int *, u_int, void *, size_t *, void *, size_t);
183 #endif /* _KERNEL */
184 
185 #ifndef _KERNEL
186 __BEGIN_DECLS
187 int	semctl(int, int, int, ...);
188 int	__semctl(int, int, int, union semun *);
189 int	semget(key_t, int, int);
190 int	semop(int, struct sembuf *, size_t);
191 __END_DECLS
192 #endif /* !_KERNEL */
193 
194 #endif /* !_SYS_SEM_H_ */
195