1 /* Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    see <http://www.gnu.org/licenses/>.  */
18 
19 /* SHMLBA uses it on most of the archs (not mips) */
20 #define __getpagesize getpagesize
21 
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/shm.h>
25 #include <syscall.h>
26 #include "ipc.h"
27 
28 #if defined(__UCLIBC_USE_TIME64__)
29 union shmun {
30     struct shmid_ds* buff;
31     void *__pad;
32 };
33 #endif
34 
35 #ifdef L_shmat
36 /* Attach the shared memory segment associated with SHMID to the data
37    segment of the calling process.  SHMADDR and SHMFLG determine how
38    and where the segment is attached.  */
39 #if defined(__NR_osf_shmat)
40 # define __NR_shmat  __NR_osf_shmat
41 #endif
42 #ifdef __NR_shmat
_syscall3(void *,shmat,int,shmid,const void *,shmaddr,int,shmflg)43 _syscall3(void *, shmat, int, shmid, const void *,shmaddr, int, shmflg)
44 #else
45 /* psm: don't remove this, else mips will fail */
46 #include <unistd.h>
47 
48 void * shmat (int shmid, const void *shmaddr, int shmflg)
49 {
50     int retval;
51     unsigned long raddr;
52 
53     retval = __syscall_ipc(IPCOP_shmat, shmid, shmflg, (int) &raddr, (void *) shmaddr, 0);
54     return ((unsigned long int) retval > -(unsigned long int) SHMLBA
55 	    ? (void *) retval : (void *) raddr);
56 }
57 #endif
58 #endif
59 
60 #ifdef L_shmctl
61 /* Provide operations to control over shared memory segments.  */
62 #ifdef __NR_shmctl
63 #define __NR___syscall_shmctl __NR_shmctl
64 static __always_inline _syscall3(int, __syscall_shmctl, int, shmid, int, cmd, struct shmid_ds *, buf)
65 #endif
66 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
67 {
68 #ifdef __NR_shmctl
69 	int __ret = __syscall_shmctl(shmid, cmd | __IPC_64, buf);
70 #if (__WORDSIZE == 32) && defined(__mips) && defined(__UCLIBC_USE_TIME64__)
71 	union shmun arg = {.buff = buf};
72         if (arg.__pad != NULL) {
73 		arg.buff->shm_atime = (__time_t)arg.buff->shm_atime_internal_1 | (__time_t)(arg.buff->shm_atime_internal_2) << 32;
74 		arg.buff->shm_dtime = (__time_t)arg.buff->shm_dtime_internal_1 | (__time_t)(arg.buff->shm_dtime_internal_2) << 32;
75 		arg.buff->shm_ctime = (__time_t)arg.buff->shm_ctime_internal_1 | (__time_t)(arg.buff->shm_ctime_internal_2) << 32;
76 	}
77 #endif
78 	return __ret;
79 #else
80     return __syscall_ipc(IPCOP_shmctl, shmid, cmd | __IPC_64, 0, buf, 0);
81 #endif
82 }
83 #endif
84 
85 
86 #ifdef L_shmdt
87 /* Detach shared memory segment starting at address specified by SHMADDR
88    from the caller's data segment.  */
89 #ifdef __NR_shmdt
90 _syscall1(int, shmdt, const void *, shmaddr)
91 #else
92 int shmdt (const void *shmaddr)
93 {
94     return __syscall_ipc(IPCOP_shmdt, 0, 0, 0, (void *) shmaddr, 0);
95 }
96 #endif
97 #endif
98 
99 #ifdef L_shmget
100 /* Return an identifier for an shared memory segment of at least size SIZE
101    which is associated with KEY.  */
102 #ifdef __NR_shmget
103 _syscall3(int, shmget, key_t, key, size_t, size, int, shmflg)
104 #else
105 int shmget (key_t key, size_t size, int shmflg)
106 {
107     return __syscall_ipc(IPCOP_shmget, key, size, shmflg, NULL, 0);
108 }
109 #endif
110 #endif
111