aboutsummaryrefslogtreecommitdiff
path: root/sys/sys/sem.h
blob: 184482d7810a7836dc05e1e858a3da52eaa13b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*	$OpenBSD: sem.h,v 1.24 2016/09/01 08:33:40 tom Exp $	*/
/*	$NetBSD: sem.h,v 1.8 1996/02/09 18:25:29 christos Exp $	*/

/*
 * SVID compatible sem.h file
 *
 * Author:  Daniel Boulet
 */

#ifndef _SYS_SEM_H_
#define _SYS_SEM_H_

#ifndef _SYS_IPC_H_
#include <sys/ipc.h>
#endif

#if __BSD_VISIBLE

/* sem-specific sysctl variables corresponding to members of struct seminfo */
#define	KERN_SEMINFO_SEMMNI	1	/* int: # of semaphore identifiers */
#define	KERN_SEMINFO_SEMMNS	2	/* int: # of semaphores in system */
#define	KERN_SEMINFO_SEMMNU	3	/* int: # of undo structures in system */
#define	KERN_SEMINFO_SEMMSL	4	/* int: max semaphores per id */
#define	KERN_SEMINFO_SEMOPM	5	/* int: max operations per semop call */
#define	KERN_SEMINFO_SEMUME	6	/* int: max undo entries per process */
#define	KERN_SEMINFO_SEMUSZ	7	/* int: size in bytes of struct undo */
#define	KERN_SEMINFO_SEMVMX	8	/* int: semaphore maximum value */
#define	KERN_SEMINFO_SEMAEM	9	/* int: adjust on exit max value */
#define	KERN_SEMINFO_MAXID	10	/* number of valid semaphore sysctls */

#define	CTL_KERN_SEMINFO_NAMES { \
	{ 0, 0 }, \
	{ "semmni", CTLTYPE_INT }, \
	{ "semmns", CTLTYPE_INT }, \
	{ "semmnu", CTLTYPE_INT }, \
	{ "semmsl", CTLTYPE_INT }, \
	{ "semopm", CTLTYPE_INT }, \
	{ "semume", CTLTYPE_INT }, \
	{ "semusz", CTLTYPE_INT }, \
	{ "semvmx", CTLTYPE_INT }, \
	{ "semaem", CTLTYPE_INT }, \
}

#endif /* __BSD_VISIBLE */

struct sem {
	unsigned short	semval;		/* semaphore value */
	pid_t		sempid;		/* pid of last operation */
	unsigned short	semncnt;	/* # awaiting semval > cval */
	unsigned short	semzcnt;	/* # awaiting semval = 0 */
};

struct semid_ds {
	struct ipc_perm	sem_perm;	/* operation permission struct */
	struct sem	*sem_base;	/* pointer to first semaphore in set */
	unsigned short	sem_nsems;	/* number of sems in set */
	time_t		sem_otime;	/* last operation time */
	long		sem_pad1;	/* SVABI/386 says I need this here */
	time_t		sem_ctime;	/* last change time */
	    				/* Times measured in secs since */
	    				/* 00:00:00 GMT, Jan. 1, 1970 */
	long		sem_pad2;	/* SVABI/386 says I need this here */
	long		sem_pad3[4];	/* SVABI/386 says I need this here */
};

/*
 * semop's sops parameter structure
 */
struct sembuf {
	unsigned short	sem_num;	/* semaphore # */
	short		sem_op;		/* semaphore operation */
	short		sem_flg;	/* operation flags */
};
#define SEM_UNDO	010000

/*
 * semctl's arg parameter structure
 */
union semun {
	int		val;		/* value for SETVAL */
	struct semid_ds	*buf;		/* buffer for IPC_STAT & IPC_SET */
	unsigned short	*array;		/* array for GETALL & SETALL */
};

/*
 * commands for semctl
 */
#define GETNCNT	3	/* Return the value of semncnt {READ} */
#define GETPID	4	/* Return the value of sempid {READ} */
#define GETVAL	5	/* Return the value of semval {READ} */
#define GETALL	6	/* Return semvals into arg.array {READ} */
#define GETZCNT	7	/* Return the value of semzcnt {READ} */
#define SETVAL	8	/* Set the value of semval to arg.val {ALTER} */
#define SETALL	9	/* Set semvals from arg.array {ALTER} */


/*
 * Permissions
 */
#define SEM_A		0200	/* alter permission */
#define SEM_R		0400	/* read permission */


#ifdef _KERNEL
#include <sys/queue.h>

/*
 * Kernel implementation stuff
 */
#define SEMVMX	32767		/* semaphore maximum value */
#define SEMAEM	16384		/* adjust on exit max value */

/*
 * Undo structure (one per process)
 */
struct sem_undo {
	SLIST_ENTRY(sem_undo) un_next;	/* ptr to next active undo structure */
	struct	process *un_proc;	/* owner of this structure */
	short	un_cnt;			/* # of active entries */
	struct undo {
		short	un_adjval;	/* adjust on exit values */
		short	un_num;		/* semaphore # */
		int	un_id;		/* semid */
	} un_ent[1];			/* undo entries */
};

/*
 * semaphore info struct
 */
struct seminfo {
	int	semmni,		/* # of semaphore identifiers */
		semmns,		/* # of semaphores in system */
		semmnu,		/* # of undo structures in system */
		semmsl,		/* max # of semaphores per id */
		semopm,		/* max # of operations per semop call */
		semume,		/* max # of undo entries per process */
		semusz,		/* size in bytes of undo structure */
		semvmx,		/* semaphore maximum value */
		semaem;		/* adjust on exit max value */
};

struct sem_sysctl_info {
	struct	seminfo seminfo;
	struct	semid_ds semids[1];
};

extern struct seminfo	seminfo;

/*
 * Configuration parameters
 */
#ifndef SEMMNI
#define SEMMNI	10		/* # of semaphore identifiers */
#endif
#ifndef SEMMNS
#define SEMMNS	60		/* # of semaphores in system */
#endif
#ifndef SEMUME
#define SEMUME	10		/* max # of undo entries per process */
#endif
#ifndef SEMMNU
#define SEMMNU	30		/* # of undo structures in system */
#endif

/* shouldn't need tuning */
#ifndef SEMMSL
#define SEMMSL	SEMMNS		/* max # of semaphores per id */
#endif
#ifndef SEMOPM
#define SEMOPM	100		/* max # of operations per semop call */
#endif

/* actual size of an undo structure */
#define SEMUSZ	(sizeof(struct sem_undo)+sizeof(struct undo)*SEMUME)

extern struct	semid_ds **sema;	/* semaphore id list */

struct proc;

void	seminit(void);
void	semexit(struct process *);
int	sysctl_sysvsem(int *, u_int, void *, size_t *, void *, size_t);
int	semctl1(struct proc *, int, int, int, union semun *, register_t *,
	    int (*)(const void *, void *, size_t),
	    int (*)(const void *, void *, size_t));

#endif /* _KERNEL */

#ifndef _KERNEL
__BEGIN_DECLS
int	semctl(int, int, int, ...);
int	__semctl(int, int, int, union semun *);
int	semget(key_t, int, int);
int	semop(int, struct sembuf *, size_t);
__END_DECLS
#endif /* !_KERNEL */

#endif /* !_SYS_SEM_H_ */