2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ipc.c Inter-process communication and shared memory helpers. */
12 #include <sys/types.h>
13 #include <sys/param.h>
14 #include <sys/sysctl.h>
23 * \return The identifier for the new mutex on success, a negative error code
30 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
31 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
37 * \param id The identifier of the mutex to be destroyed.
43 int mutex_destroy(int id)
45 int ret = semctl(id, 0, IPC_RMID);
46 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
49 static void para_semop(int id, struct sembuf *sops, int num)
52 if (semop(id, sops, num) >= 0)
54 } while (errno == EINTR);
56 PARA_CRIT_LOG("semaphore set %d was removed\n", id);
59 PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno),
65 * Lock the given mutex.
67 * \param id The identifier of the shared memory area to lock.
69 * This function either succeeds or aborts.
71 * \sa semop(2), struct misc_meta_data.
73 void mutex_lock(int id)
75 struct sembuf sops[2] = {
87 para_semop(id, sops, 2);
93 * \param id The identifier of the mutex.
95 * This function either succeeds or aborts.
97 * \sa semop(2), struct misc_meta_data.
99 void mutex_unlock(int id)
101 struct sembuf sops[1] = {
108 para_semop(id, sops, 1);
112 * Create a new shared memory area of given size.
114 * \param size The size of the shared memory area to create.
116 * \return The id of the shared memory array on success, a negative error
121 int shm_new(size_t size)
123 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
124 return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
128 * Destroy the given shared memory area.
130 * \param id The shared memory identifier.
132 * \return The return value of the underlying shmctl() call on success,
133 * a negative error code on errors.
137 int shm_destroy(int id)
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;
145 * Attach a shared memory segment.
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.
155 int shm_attach(int id, enum shm_attach_mode mode, void **result)
157 if (mode == ATTACH_RW)
158 *result = shmat(id, NULL, 0);
160 *result = shmat(id, NULL, SHM_RDONLY);
161 return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
165 * Get the size of a shared memory segment.
167 * \param id The shared memory segment identifier.
168 * \param result Size in bytes is returned here, zero on errors.
174 int shm_size(int id, size_t *result)
176 struct shmid_ds ds; /* data structure */
179 if (shmctl(id, IPC_STAT, &ds) < 0)
180 return -ERRNO_TO_PARA_ERROR(errno);
181 *result = ds.shm_segsz;
186 * Detach a shared memory segment.
188 * \param addr The address of the attached segment.
194 int shm_detach(void *addr)
196 int ret = shmdt(addr);
197 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
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"
205 # undef SYSCTL_SHMMAX_VARIABLE
209 * Get the maximal size of a shared memory area.
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
215 * \return A number suitable as an argument to \ref shm_new().
217 size_t shm_get_shmmax(void)
219 static size_t shmmax;
221 if (shmmax > 0) /* only dance once */
223 #ifdef __linux__ /* get it from proc fs */
225 int fd = open("/proc/sys/kernel/shmmax", O_RDONLY);
228 int ret = read(fd, buf, sizeof(buf) - 1);
231 shmmax = strtoul(buf, NULL, 10);
236 #elif defined SYSCTL_SHMMAX_VARIABLE
238 size_t len = sizeof(shmmax);
239 sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0);
245 PARA_WARNING_LOG("unable to determine shmmax\n");
246 shmmax = 65535; /* last resort */
248 PARA_INFO_LOG("shmmax: %zu\n", shmmax);