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 #include <errno.h>
20 #include <sys/sem.h>
21 #include <stddef.h>
22 #include <stdlib.h> /* for NULL */
23
24 #include "ipc.h"
25
26 #if defined(__UCLIBC_USE_TIME64__)
27 #include "internal/time64_helpers.h"
28 #endif
29
30 #ifdef L_semctl
31 /* Return identifier for array of NSEMS semaphores associated with
32 KEY. */
33 #include <stdarg.h>
34 /* arg for semctl system calls. */
35 union semun {
36 int val; /* value for SETVAL */
37 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
38 unsigned short *array; /* array for GETALL & SETALL */
39 struct seminfo *__buf; /* buffer for IPC_INFO */
40 void *__pad;
41 };
42
43
44 #ifdef __NR_semctl
45 #define __NR___semctl __NR_semctl
_syscall4(int,__semctl,int,semid,int,semnum,int,cmd,void *,arg)46 static __inline__ _syscall4(int, __semctl, int, semid, int, semnum, int, cmd, void *, arg)
47 #endif
48
49 int semctl(int semid, int semnum, int cmd, ...)
50 {
51 union semun arg;
52 va_list ap;
53
54 /* Get the argument. */
55 va_start (ap, cmd);
56 arg = va_arg (ap, union semun);
57 va_end (ap);
58 #ifdef __NR_semctl
59 int __ret = __semctl(semid, semnum, cmd | __IPC_64, arg.__pad);
60 #if (__WORDSIZE == 32) && defined(__UCLIBC_USE_TIME64__)
61 if (arg.__pad != NULL) {
62 arg.buf->sem_otime = (__time_t)arg.buf->__sem_otime_internal_1 | (__time_t)(arg.buf->__sem_otime_internal_2) << 32;
63 arg.buf->sem_ctime = (__time_t)arg.buf->__sem_ctime_internal_1 | (__time_t)(arg.buf->__sem_ctime_internal_2) << 32;
64 }
65 #endif
66 return __ret;
67 #else
68 return __syscall_ipc(IPCOP_semctl, semid, semnum, cmd|__IPC_64, &arg, NULL);
69 #endif
70 }
71 #endif
72
73 #ifdef L_semget
74 #ifdef __NR_semget
_syscall3(int,semget,key_t,key,int,nsems,int,semflg)75 _syscall3(int, semget, key_t, key, int, nsems, int, semflg)
76
77 #else
78 /* Return identifier for array of NSEMS semaphores associated
79 * with KEY. */
80 int semget (key_t key, int nsems, int semflg)
81 {
82 return __syscall_ipc(IPCOP_semget, key, nsems, semflg, NULL, 0);
83 }
84 #endif
85 #endif
86
87 #ifdef L_semop
88
89 #ifdef __NR_semop
90 _syscall3(int, semop, int, semid, struct sembuf *, sops, size_t, nsops)
91
92 #else
93 /* Perform user-defined atomical operation of array of semaphores. */
94 int semop (int semid, struct sembuf *sops, size_t nsops)
95 {
96 return __syscall_ipc(IPCOP_semop, semid, (int) nsops, 0, sops, NULL);
97 }
98 #endif
99 #endif
100
101 #ifdef L_semtimedop
102
103 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_semtimedop_time64)
104 int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout)
105 {
106 return INLINE_SYSCALL(semtimedop_time64, 4, semid, sops, nsops, TO_TS64_P(timeout));
107 }
108
109 #elif defined(__NR_semtimedop)
110 _syscall4(int, semtimedop, int, semid, struct sembuf *, sops, size_t, nsops, const struct timespec *, timeout)
111
112 #else
113
114 int semtimedop(int semid, struct sembuf *sops, size_t nsops,
115 const struct timespec *timeout)
116 {
117 return __syscall_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, (void *) timeout);
118 }
119 #endif
120 #endif
121