summaryrefslogtreecommitdiffstats
path: root/sys/i386/ibcs2/ibcs2_ipc.c
blob: 5ba7e4930589328b2be129b1a0a3ad38e8360352 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*-
 * Copyright (c) 1995 Scott Bartram
 * Copyright (c) 1995 Steven Wallace
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/sysproto.h>

#include <i386/ibcs2/ibcs2_types.h>
#include <i386/ibcs2/ibcs2_signal.h>
#include <i386/ibcs2/ibcs2_proto.h>
#include <i386/ibcs2/ibcs2_util.h>
#include <i386/ibcs2/ibcs2_ipc.h>

#define IBCS2_IPC_RMID	0
#define IBCS2_IPC_SET	1
#define IBCS2_IPC_STAT	2
#define IBCS2_SETVAL	8



static void cvt_msqid2imsqid(struct msqid_ds *, struct ibcs2_msqid_ds *);
static void cvt_imsqid2msqid(struct ibcs2_msqid_ds *, struct msqid_ds *);
#ifdef unused
static void cvt_sem2isem(struct sem *, struct ibcs2_sem *);
static void cvt_isem2sem(struct ibcs2_sem *, struct sem *);
#endif
static void cvt_semid2isemid(struct semid_ds *, struct ibcs2_semid_ds *);
static void cvt_isemid2semid(struct ibcs2_semid_ds *, struct semid_ds *);
static void cvt_shmid2ishmid(struct shmid_ds *, struct ibcs2_shmid_ds *);
static void cvt_ishmid2shmid(struct ibcs2_shmid_ds *, struct shmid_ds *);
static void cvt_perm2iperm(struct ipc_perm *, struct ibcs2_ipc_perm *);
static void cvt_iperm2perm(struct ibcs2_ipc_perm *, struct ipc_perm *);


/*
 * iBCS2 msgsys call
 */

static void
cvt_msqid2imsqid(bp, ibp)
struct msqid_ds *bp;
struct ibcs2_msqid_ds *ibp;
{
	cvt_perm2iperm(&bp->msg_perm, &ibp->msg_perm);
	ibp->msg_first = bp->msg_first;
	ibp->msg_last = bp->msg_last;
	ibp->msg_cbytes = (u_short)bp->msg_cbytes;
	ibp->msg_qnum = (u_short)bp->msg_qnum;
	ibp->msg_qbytes = (u_short)bp->msg_qbytes;
	ibp->msg_lspid = (u_short)bp->msg_lspid;
	ibp->msg_lrpid = (u_short)bp->msg_lrpid;
	ibp->msg_stime = bp->msg_stime;
	ibp->msg_rtime = bp->msg_rtime;
	ibp->msg_ctime = bp->msg_ctime;
	return;
}

static void
cvt_imsqid2msqid(ibp, bp)
struct ibcs2_msqid_ds *ibp;
struct msqid_ds *bp;
{
	cvt_iperm2perm(&ibp->msg_perm, &bp->msg_perm);
	bp->msg_first = ibp->msg_first;
	bp->msg_last = ibp->msg_last;
	bp->msg_cbytes = ibp->msg_cbytes;
	bp->msg_qnum = ibp->msg_qnum;
	bp->msg_qbytes = ibp->msg_qbytes;
	bp->msg_lspid = ibp->msg_lspid;
	bp->msg_lrpid = ibp->msg_lrpid;
	bp->msg_stime = ibp->msg_stime;
	bp->msg_rtime = ibp->msg_rtime;
	bp->msg_ctime = ibp->msg_ctime;
	return;
}

int
ibcs2_msgsys(td, uap)
	struct thread *td;
	struct ibcs2_msgsys_args *uap;
{
	switch (uap->which) {
	case 0:				/* msgget */
		uap->which = 1;
		return msgsys(td, (struct msgsys_args *)uap);
	case 1: {			/* msgctl */
		int error;
		struct msgsys_args margs;
		caddr_t sg = stackgap_init();

		margs.which = 0;
		margs.a2 = uap->a2;
		margs.a4 =
		    (int)stackgap_alloc(&sg, sizeof(struct msqid_ds));
		margs.a3 = uap->a3;
		switch (margs.a3) {
		case IBCS2_IPC_STAT:
			error = msgsys(td, &margs);
			if (!error)
				cvt_msqid2imsqid(
				    (struct msqid_ds *)margs.a4,
				    (struct ibcs2_msqid_ds *)uap->a4);
			return error;
		case IBCS2_IPC_SET:
			cvt_imsqid2msqid((struct ibcs2_msqid_ds *)uap->a4,
					 (struct msqid_ds *)margs.a4);
			return msgsys(td, &margs);
		case IBCS2_IPC_RMID:
			return msgsys(td, &margs);
		}
		return EINVAL;
	}
	case 2:				/* msgrcv */
		uap->which = 3;
		return msgsys(td, (struct msgsys_args *)uap);
	case 3:				/* msgsnd */
		uap->which = 2;
		return msgsys(td, (struct msgsys_args *)uap);
	default:
		return EINVAL;
	}
}

/*
 * iBCS2 semsys call
 */
#ifdef unused
static void
cvt_sem2isem(bp, ibp)
struct sem *bp;
struct ibcs2_sem *ibp;
{
	ibp->semval = bp->semval;
	ibp->sempid = bp->sempid;
	ibp->semncnt = bp->semncnt;
	ibp->semzcnt = bp->semzcnt;
	return;
}

static void
cvt_isem2sem(ibp, bp)
struct ibcs2_sem *ibp;
struct sem *bp;
{
	bp->semval = ibp->semval;
	bp->sempid = ibp->sempid;
	bp->semncnt = ibp->semncnt;
	bp->semzcnt = ibp->semzcnt;
	return;
}
#endif

static void
cvt_iperm2perm(ipp, pp)
struct ibcs2_ipc_perm *ipp;
struct ipc_perm *pp;
{
	pp->uid = ipp->uid;
	pp->gid = ipp->gid;
	pp->cuid = ipp->cuid;
	pp->cgid = ipp->cgid;
	pp->mode = ipp->mode;
	pp->seq = ipp->seq;
	pp->key = ipp->key;
}

static void
cvt_perm2iperm(pp, ipp)
struct ipc_perm *pp;
struct ibcs2_ipc_perm *ipp;
{
	ipp->uid = pp->uid;
	ipp->gid = pp->gid;
	ipp->cuid = pp->cuid;
	ipp->cgid = pp->cgid;
	ipp->mode = pp->mode;
	ipp->seq = pp->seq;
	ipp->key = pp->key;
}

static void
cvt_semid2isemid(bp, ibp)
struct semid_ds *bp;
struct ibcs2_semid_ds *ibp;
{
	cvt_perm2iperm(&bp->sem_perm, &ibp->sem_perm);
	ibp->sem_base = (struct ibcs2_sem *)bp->sem_base;
	ibp->sem_nsems = bp->sem_nsems;
	ibp->sem_otime = bp->sem_otime;
	ibp->sem_ctime = bp->sem_ctime;
	return;
}

static void
cvt_isemid2semid(ibp, bp)
struct ibcs2_semid_ds *ibp;
struct semid_ds *bp;
{
	cvt_iperm2perm(&ibp->sem_perm, &bp->sem_perm);
	bp->sem_base = (struct sem *)ibp->sem_base;
	bp->sem_nsems = ibp->sem_nsems;
	bp->sem_otime = ibp->sem_otime;
	bp->sem_ctime = ibp->sem_ctime;
	return;
}

int
ibcs2_semsys(td, uap)
	struct thread *td;
	struct ibcs2_semsys_args *uap;
{
	int error;

	switch (uap->which) {
	case 0:					/* semctl */
		switch(uap->a4) {
		case IBCS2_IPC_STAT:
		    {
			struct ibcs2_semid_ds *isp;
			struct semid_ds *sp;
			union semun *sup, ssu;
			caddr_t sg = stackgap_init();


			ssu = (union semun) uap->a5;
			sp = stackgap_alloc(&sg, sizeof(struct semid_ds));
			sup = stackgap_alloc(&sg, sizeof(union semun));
			sup->buf = sp;
			uap->a5 = (int)sup;
			error = semsys(td, (struct semsys_args *)uap);
			if (!error) {
				uap->a5 = (int)ssu.buf;
				isp = stackgap_alloc(&sg, sizeof(*isp));
				cvt_semid2isemid(sp, isp);
				error = copyout((caddr_t)isp,
						(caddr_t)ssu.buf,
						sizeof(*isp));
			}
			return error;
		    }
		case IBCS2_IPC_SET:
		    {
			struct ibcs2_semid_ds *isp;
			struct semid_ds *sp;
			caddr_t sg = stackgap_init();

			isp = stackgap_alloc(&sg, sizeof(*isp));
			sp = stackgap_alloc(&sg, sizeof(*sp));
			error = copyin((caddr_t)uap->a5, (caddr_t)isp,
				       sizeof(*isp));
			if (error)
				return error;
			cvt_isemid2semid(isp, sp);
			uap->a5 = (int)sp;
			return semsys(td, (struct semsys_args *)uap);
		    }
		case IBCS2_SETVAL:
		    {
			union semun *sp;
			caddr_t sg = stackgap_init();

			sp = stackgap_alloc(&sg, sizeof(*sp));
			sp->val = (int) uap->a5;
			uap->a5 = (int)sp;
			return semsys(td, (struct semsys_args *)uap);
		    }
		}

		return semsys(td, (struct semsys_args *)uap);

	case 1:				/* semget */
		return semsys(td, (struct semsys_args *)uap);

	case 2:				/* semop */
		return semsys(td, (struct semsys_args *)uap);
	}
	return EINVAL;
}


/*
 * iBCS2 shmsys call
 */

static void
cvt_shmid2ishmid(bp, ibp)
struct shmid_ds *bp;
struct ibcs2_shmid_ds *ibp;
{
	cvt_perm2iperm(&bp->shm_perm, &ibp->shm_perm);
	ibp->shm_segsz = bp->shm_segsz;
	ibp->shm_lpid = bp->shm_lpid;
	ibp->shm_cpid = bp->shm_cpid;
	ibp->shm_nattch = bp->shm_nattch;
	ibp->shm_cnattch = 0;			/* ignored anyway */
	ibp->shm_atime = bp->shm_atime;
	ibp->shm_dtime = bp->shm_dtime;
	ibp->shm_ctime = bp->shm_ctime;
	return;
}

static void
cvt_ishmid2shmid(ibp, bp)
struct ibcs2_shmid_ds *ibp;
struct shmid_ds *bp;
{
	cvt_iperm2perm(&ibp->shm_perm, &bp->shm_perm);
	bp->shm_segsz = ibp->shm_segsz;
	bp->shm_lpid = ibp->shm_lpid;
	bp->shm_cpid = ibp->shm_cpid;
	bp->shm_nattch = ibp->shm_nattch;
	bp->shm_atime = ibp->shm_atime;
	bp->shm_dtime = ibp->shm_dtime;
	bp->shm_ctime = ibp->shm_ctime;
	bp->shm_internal = (void *)0;		/* ignored anyway */
	return;
}

int
ibcs2_shmsys(td, uap)
	struct thread *td;
	struct ibcs2_shmsys_args *uap;
{
	int error;

	switch (uap->which) {
	case 0:						/* shmat */
		return shmsys(td, (struct shmsys_args *)uap);

	case 1:						/* shmctl */
		switch(uap->a3) {
		case IBCS2_IPC_STAT:
		    {
			struct ibcs2_shmid_ds *isp;
			struct shmid_ds *sp;
			caddr_t sg = stackgap_init();

			isp = (struct ibcs2_shmid_ds *)uap->a4;
			sp = stackgap_alloc(&sg, sizeof(*sp));
			uap->a4 = (int)sp;
			error = shmsys(td, (struct shmsys_args *)uap);
			if (!error) {
				uap->a4 = (int)isp;
				isp = stackgap_alloc(&sg, sizeof(*isp));
				cvt_shmid2ishmid(sp, isp);
				error = copyout((caddr_t)isp,
						(caddr_t)uap->a4,
						sizeof(*isp));
			}
			return error;
		    }
		case IBCS2_IPC_SET:
		    {
			struct ibcs2_shmid_ds *isp;
			struct shmid_ds *sp;
			caddr_t sg = stackgap_init();

			isp = stackgap_alloc(&sg, sizeof(*isp));
			sp = stackgap_alloc(&sg, sizeof(*sp));
			error = copyin((caddr_t)uap->a4, (caddr_t)isp,
				       sizeof(*isp));
			if (error)
				return error;
			cvt_ishmid2shmid(isp, sp);
			uap->a4 = (int)sp;
			return shmsys(td, (struct shmsys_args *)uap);
		    }
		}

		return shmsys(td, (struct shmsys_args *)uap);

	case 2:						/* shmdt */
		return shmsys(td, (struct shmsys_args *)uap);

	case 3:						/* shmget */
		return shmsys(td, (struct shmsys_args *)uap);
	}
	return EINVAL;
}

MODULE_DEPEND(ibcs2, sysvmsg, 1, 1, 1);
MODULE_DEPEND(ibcs2, sysvsem, 1, 1, 1);
MODULE_DEPEND(ibcs2, sysvshm, 1, 1, 1);
OpenPOWER on IntegriCloud