xref: /titanic_44/usr/src/lib/libbc/libc/sys/common/shmsys.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1991 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include	<syscall.h>
33 #include	<stdarg.h>
34 #include	<sys/types.h>
35 #include	<sys/ipc.h>
36 #include	<sys/shm.h>
37 #include	<errno.h>
38 
39 
40 /* shmsys dispatch argument */
41 #define	SHMAT	0
42 #define	SHMCTL	1
43 #define	SHMDT	2
44 #define SHMGET	3
45 
46 struct shmid_sv {
47 	struct ipc_perm shm_perm;
48 	int		shm_segsz;
49 	struct anon_map	*shm_amp;
50 	unsigned short	shm_lkcnt;
51 	char		pad[2];
52 	short		shm_lpid;
53 	short		shm_cpid;
54 	unsigned short	shm_nattch;
55 	unsigned short	shm_cnattch;
56 	time_t		shm_atime;
57 	time_t		shm_dtime;
58 	time_t		shm_ctime;
59 };
60 
61 
62 char *
63 shmat(int shmid, char *shmaddr, int shmflg)
64 {
65 	return ((char *)_syscall(SYS_shmsys, SHMAT, shmid, shmaddr, shmflg));
66 }
67 
68 int
69 shmctl(int shmid, int cmd, struct shmid_ds *buf)
70 {
71 	struct shmid_sv n_buf;
72 	int ret;
73 
74 	if (buf == (struct shmid_ds *)-1) {
75 		errno = EFAULT;
76 		return (-1);
77 	}
78 
79 	if (buf == 0) {
80 		ret = _syscall(SYS_shmsys, SHMCTL, shmid, cmd, 0);
81 	} else {
82 		n_buf.shm_perm = buf->shm_perm;
83 		n_buf.shm_segsz = buf->shm_segsz;
84 		n_buf.shm_amp = buf->shm_amp;
85 		n_buf.shm_lpid = buf->shm_lpid;
86 		n_buf.shm_cpid = buf->shm_cpid;
87 		n_buf.shm_nattch = buf->shm_nattch;
88 		n_buf.shm_atime = buf->shm_atime;
89 		n_buf.shm_dtime = buf->shm_dtime;
90 		n_buf.shm_ctime = buf->shm_ctime;
91 		n_buf.shm_lkcnt = 0;
92 		n_buf.shm_cnattch = 0;
93 
94 		ret = _syscall(SYS_shmsys, SHMCTL, shmid, cmd, &n_buf);
95 
96 		buf->shm_perm = n_buf.shm_perm;
97 		buf->shm_segsz = n_buf.shm_segsz;
98 		buf->shm_amp = n_buf.shm_amp;
99 		buf->shm_lpid = n_buf.shm_lpid;
100 		buf->shm_cpid = n_buf.shm_cpid;
101 		buf->shm_nattch = n_buf.shm_nattch;
102 		buf->shm_atime = n_buf.shm_atime;
103 		buf->shm_dtime = n_buf.shm_dtime;
104 		buf->shm_ctime = n_buf.shm_ctime;
105 	}
106 
107 	return (ret);
108 }
109 
110 int
111 shmdt(char *shmaddr)
112 {
113 
114 	return (_syscall(SYS_shmsys, SHMDT, shmaddr));
115 }
116 
117 int
118 shmget(key_t key, int size, int shmflg)
119 {
120 	return (_syscall(SYS_shmsys, SHMGET, key, size, shmflg));
121 }
122 
123 int
124 shmsys(int sysnum, ...)
125 {
126         va_list ap;
127 	int shmid, shmflg, cmd, size;
128 	char *shmaddr;
129 	struct shmid_ds *buf;
130 	key_t key;
131 
132 	va_start(ap, sysnum);
133 	switch (sysnum) {
134 	case SHMAT:
135 			shmid=va_arg(ap, int);
136 			shmaddr=va_arg(ap, char *);
137 			shmflg=va_arg(ap, int);
138 			va_end(ap);
139 			return ((int)shmat(shmid, shmaddr, shmflg));
140 	case SHMCTL:
141 			shmid=va_arg(ap, int);
142 			cmd=va_arg(ap, int);
143 			buf=va_arg(ap, struct shmid_ds *);
144 			va_end(ap);
145 			return (shmctl(shmid, cmd, buf));
146 	case SHMDT:
147 			shmaddr=va_arg(ap, char *);
148 			va_end(ap);
149 			return (shmdt(shmaddr));
150 	case SHMGET:
151 			key=va_arg(ap, key_t);
152 			size=va_arg(ap, int);
153 			shmflg=va_arg(ap, int);
154 			va_end(ap);
155 			return (shmget(key, size, shmflg));
156 	}
157 	va_end(ap);
158 	return (-1);
159 }
160