summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/sys/common/shmsys.c
blob: 2ae18dbe3015229bb5002789acdeec63b4b2b983 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984 AT&T */
/*	  All Rights Reserved   */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/syscall.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>


/* shmsys dispatch argument */
#define	SHMAT	0
#define	SHMCTL	1
#define	SHMDT	2
#define	SHMGET	3

struct shmid_sv {
	struct ipc_perm shm_perm;
	int		shm_segsz;
	struct anon_map	*shm_amp;
	unsigned short	shm_lkcnt;
	char		pad[2];
	short		shm_lpid;
	short		shm_cpid;
	unsigned short	shm_nattch;
	unsigned short	shm_cnattch;
	time_t		shm_atime;
	time_t		shm_dtime;
	time_t		shm_ctime;
};


char *
shmat(int shmid, char *shmaddr, int shmflg)
{
	return ((char *)_syscall(SYS_shmsys, SHMAT, shmid, shmaddr, shmflg));
}

int
shmctl(int shmid, int cmd, struct shmid_ds *buf)
{
	struct shmid_sv n_buf;
	int ret;

	if (buf == (struct shmid_ds *)-1) {
		errno = EFAULT;
		return (-1);
	}

	if (buf == 0) {
		ret = _syscall(SYS_shmsys, SHMCTL, shmid, cmd, 0);
	} else {
		n_buf.shm_perm = buf->shm_perm;
		n_buf.shm_segsz = buf->shm_segsz;
		n_buf.shm_amp = buf->shm_amp;
		n_buf.shm_lpid = buf->shm_lpid;
		n_buf.shm_cpid = buf->shm_cpid;
		n_buf.shm_nattch = buf->shm_nattch;
		n_buf.shm_atime = buf->shm_atime;
		n_buf.shm_dtime = buf->shm_dtime;
		n_buf.shm_ctime = buf->shm_ctime;
		n_buf.shm_lkcnt = 0;
		n_buf.shm_cnattch = 0;

		ret = _syscall(SYS_shmsys, SHMCTL, shmid, cmd, &n_buf);

		buf->shm_perm = n_buf.shm_perm;
		buf->shm_segsz = n_buf.shm_segsz;
		buf->shm_amp = n_buf.shm_amp;
		buf->shm_lpid = n_buf.shm_lpid;
		buf->shm_cpid = n_buf.shm_cpid;
		buf->shm_nattch = n_buf.shm_nattch;
		buf->shm_atime = n_buf.shm_atime;
		buf->shm_dtime = n_buf.shm_dtime;
		buf->shm_ctime = n_buf.shm_ctime;
	}

	return (ret);
}

int
shmdt(char *shmaddr)
{
	return (_syscall(SYS_shmsys, SHMDT, shmaddr));
}

int
shmget(key_t key, int size, int shmflg)
{
	return (_syscall(SYS_shmsys, SHMGET, key, size, shmflg));
}

int
shmsys(int sysnum, ...)
{
	va_list ap;
	int shmid, shmflg, cmd, size;
	char *shmaddr;
	struct shmid_ds *buf;
	key_t key;

	va_start(ap, sysnum);
	switch (sysnum) {
	case SHMAT:
		shmid = va_arg(ap, int);
		shmaddr = va_arg(ap, char *);
		shmflg = va_arg(ap, int);
		va_end(ap);
		return ((int)shmat(shmid, shmaddr, shmflg));
	case SHMCTL:
		shmid = va_arg(ap, int);
		cmd = va_arg(ap, int);
		buf = va_arg(ap, struct shmid_ds *);
		va_end(ap);
		return (shmctl(shmid, cmd, buf));
	case SHMDT:
		shmaddr = va_arg(ap, char *);
		va_end(ap);
		return (shmdt(shmaddr));
	case SHMGET:
		key = va_arg(ap, key_t);
		size = va_arg(ap, int);
		shmflg = va_arg(ap, int);
		va_end(ap);
		return (shmget(key, size, shmflg));
	}
	va_end(ap);
	return (-1);
}