Remove osx_write.c, this time for real.
[paraslash.git] / ipc.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ipc.c Inter-process communication and shared memory helpers. */
8
9 #include "para.h"
10 #include "error.h"
11 #include "ipc.h"
12 #include <sys/types.h>
13 #include <sys/param.h>
14 #include <sys/sysctl.h>
15
16 #include <sys/ipc.h>
17 #include <sys/shm.h>
18 #include <sys/sem.h>
19
20 /**
21  * Define a new mutex.
22  *
23  * \return The identifier for the new mutex on success, a negative error code
24  * on errors.
25  *
26  * \sa semget(2).
27  */
28 int mutex_new(void)
29 {
30         int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
31         return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
32 }
33
34 /**
35  * Destroy a mutex.
36  *
37  * \param id The identifier of the mutex to be destroyed.
38  *
39  * \return Standard.
40  *
41  * \sa semctl(2)
42  */
43 int mutex_destroy(int id)
44 {
45         int ret = semctl(id, 0, IPC_RMID);
46         return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
47 }
48
49 static void para_semop(int id, struct sembuf *sops, int num)
50 {
51         do {
52                 if (semop(id, sops, num) >= 0)
53                         return;
54         } while (errno == EINTR);
55         if (errno == EIDRM) {
56                 PARA_CRIT_LOG("semaphore set %d was removed\n", id);
57                 return;
58         }
59         PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno),
60                 (int)getpid());
61         exit(EXIT_FAILURE);
62 }
63
64 /**
65  * Lock the given mutex.
66  *
67  * \param id The identifier of the shared memory area to lock.
68  *
69  * This function either succeeds or aborts.
70  *
71  * \sa semop(2), struct misc_meta_data.
72  */
73 void mutex_lock(int id)
74 {
75         struct sembuf sops[2] = {
76                 {
77                         .sem_num = 0,
78                         .sem_op = 0,
79                         .sem_flg = SEM_UNDO
80                 },
81                 {
82                         .sem_num = 0,
83                         .sem_op = 1,
84                         .sem_flg = SEM_UNDO
85                 }
86         };
87         para_semop(id, sops, 2);
88 }
89
90 /**
91  * Unlock a mutex.
92  *
93  * \param id The identifier of the mutex.
94  *
95  * This function either succeeds or aborts.
96  *
97  * \sa semop(2), struct misc_meta_data.
98  */
99 void mutex_unlock(int id)
100 {
101         struct sembuf sops[1] = {
102                 {
103                         .sem_num = 0,
104                         .sem_op = -1,
105                         .sem_flg = SEM_UNDO
106                 },
107         };
108         para_semop(id, sops, 1);
109 }
110
111 /**
112  * Create a new shared memory area of given size.
113  *
114  * \param size The size of the shared memory area to create.
115  *
116  * \return The id of the shared memory array on success, a negative error
117  * code on errors.
118  *
119  * \sa shmget(2).
120  */
121 int shm_new(size_t size)
122 {
123         int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
124         return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
125 }
126
127 /**
128  * Destroy the given shared memory area.
129  *
130  * \param id The shared memory identifier.
131  *
132  * \return The return value of the underlying shmctl() call on success,
133  * a negative error code on errors.
134  *
135  * \sa shmctl(2).
136  */
137 int shm_destroy(int id)
138 {
139         struct shmid_ds shm_desc;
140         int ret = shmctl(id, IPC_RMID, &shm_desc);
141         return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
142 }
143
144 /**
145  * Attach a shared memory segment.
146  *
147  * \param id The identifier of the shared memory segment to attach.
148  * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write).
149  * \param result Points to the attached area just attached on success.
150  *
151  * \return Standard.
152  *
153  * \sa shmat(2).
154  */
155 int shm_attach(int id, enum shm_attach_mode mode, void **result)
156 {
157         if (mode == ATTACH_RW)
158                 *result = shmat(id, NULL, 0);
159         else
160                 *result = shmat(id, NULL, SHM_RDONLY);
161         return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
162 }
163
164 /**
165  * Get the size of a shared memory segment.
166  *
167  * \param id The shared memory segment identifier.
168  * \param result Size in bytes is returned here, zero on errors.
169  *
170  * \return Standard.
171  *
172  * \sa shmctl(2).
173  */
174 int shm_size(int id, size_t *result)
175 {
176         struct shmid_ds ds; /* data structure */
177
178         *result = 0;
179         if (shmctl(id, IPC_STAT, &ds) < 0)
180                 return -ERRNO_TO_PARA_ERROR(errno);
181         *result = ds.shm_segsz;
182         return 1;
183 }
184
185 /**
186  * Detach a shared memory segment.
187  *
188  * \param addr The address of the attached segment.
189  *
190  * \return Standard.
191  *
192  * \sa shmdt(2).
193  */
194 int shm_detach(void *addr)
195 {
196         int ret = shmdt(addr);
197         return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
198 }
199
200 # if defined __FreeBSD__ || defined __NetBSD__
201 #       define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax"
202 # elif defined __APPLE__
203 #       define SYSCTL_SHMMAX_VARIABLE "kern.sysv.shmmax"
204 # else
205 #       undef SYSCTL_SHMMAX_VARIABLE
206 # endif
207
208 /**
209  * Get the maximal size of a shared memory area.
210  *
211  * The value is only computed once when the function is called for the first
212  * time.  Subsequent calls return the number which was computed during the
213  * first call.
214  *
215  * \return A number suitable as an argument to \ref shm_new().
216  */
217 size_t shm_get_shmmax(void)
218 {
219         static size_t shmmax;
220
221         if (shmmax > 0) /* only dance once */
222                 return shmmax;
223 #ifdef __linux__ /* get it from proc fs */
224         {
225                 int fd = open("/proc/sys/kernel/shmmax", O_RDONLY);
226                 if (fd >= 0) {
227                         char buf[100] = "";
228                         int ret = read(fd, buf, sizeof(buf) - 1);
229                         if (ret > 0) {
230                                 buf[ret] = '\0';
231                                 shmmax = strtoul(buf, NULL, 10);
232                         }
233                         close(fd);
234                 }
235         }
236 #elif defined SYSCTL_SHMMAX_VARIABLE
237         {
238                 size_t len = sizeof(shmmax);
239                 sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0);
240         }
241 #elif defined SHMMAX
242         shmmax = SHMMAX;
243 #endif
244         if (shmmax == 0) {
245                 PARA_WARNING_LOG("unable to determine shmmax\n");
246                 shmmax = 65535; /* last resort */
247         }
248         PARA_INFO_LOG("shmmax: %zu\n", shmmax);
249         return shmmax;
250 }