1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file ipc.c Inter-process communication and shared memory helpers. */
18 * \return The identifier for the new mutex on success, a negative error code
25 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
26 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
32 * \param id The identifier of the mutex to be destroyed.
38 int mutex_destroy(int id)
40 int ret = semctl(id, 0, IPC_RMID);
41 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
44 static void para_semop(int id, struct sembuf *sops, int num)
47 if (semop(id, sops, num) >= 0)
49 } while (errno == EINTR);
51 PARA_CRIT_LOG("semaphore set %d was removed\n", id);
54 PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno),
60 * Lock the given mutex.
62 * \param id The identifier of the shared memory area to lock.
64 * This function either succeeds or aborts.
66 * \sa semop(2), struct \ref misc_meta_data.
68 void mutex_lock(int id)
70 struct sembuf sops[2] = {
82 para_semop(id, sops, 2);
88 * \param id The identifier of the mutex.
90 * This function either succeeds or aborts.
92 * \sa semop(2), struct \ref misc_meta_data.
94 void mutex_unlock(int id)
96 struct sembuf sops[1] = {
103 para_semop(id, sops, 1);
107 * Create a new shared memory area of given size.
109 * \param size The size of the shared memory area to create.
111 * \return The id of the shared memory array on success, a negative error
116 int shm_new(size_t size)
118 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
119 return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
123 * Destroy the given shared memory area.
125 * \param id The shared memory identifier.
127 * \return The return value of the underlying shmctl() call on success,
128 * a negative error code on errors.
132 int shm_destroy(int id)
134 struct shmid_ds shm_desc;
135 int ret = shmctl(id, IPC_RMID, &shm_desc);
136 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
140 * Attach a shared memory segment.
142 * \param id The identifier of the shared memory segment to attach.
143 * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write).
144 * \param result Points to the attached area just attached on success.
150 int shm_attach(int id, enum shm_attach_mode mode, void **result)
152 if (mode == ATTACH_RW)
153 *result = shmat(id, NULL, 0);
155 *result = shmat(id, NULL, SHM_RDONLY);
156 return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
160 * Get the size of a shared memory segment.
162 * \param id The shared memory segment identifier.
163 * \param result Size in bytes is returned here, zero on errors.
169 int shm_size(int id, size_t *result)
171 struct shmid_ds ds; /* data structure */
174 if (shmctl(id, IPC_STAT, &ds) < 0)
175 return -ERRNO_TO_PARA_ERROR(errno);
176 *result = ds.shm_segsz;
181 * Detach a shared memory segment.
183 * \param addr The address of the attached segment.
189 int shm_detach(void *addr)
191 int ret = shmdt(addr);
192 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
195 # if defined __FreeBSD__ || defined __NetBSD__
196 #include <sys/sysctl.h>
197 # define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax"
199 # undef SYSCTL_SHMMAX_VARIABLE
203 * Get the maximal size of a shared memory area.
205 * The value is only computed once when the function is called for the first
206 * time. Subsequent calls return the number which was computed during the
209 * \return A number suitable as an argument to \ref shm_new().
211 size_t shm_get_shmmax(void)
213 static size_t shmmax;
215 if (shmmax > 0) /* only dance once */
217 #ifdef __linux__ /* get it from proc fs */
219 int fd = open("/proc/sys/kernel/shmmax", O_RDONLY);
222 int ret = read(fd, buf, sizeof(buf) - 1);
225 shmmax = strtoul(buf, NULL, 10);
230 #elif defined SYSCTL_SHMMAX_VARIABLE
232 size_t len = sizeof(shmmax);
233 sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0);
239 PARA_WARNING_LOG("unable to determine shmmax\n");
240 shmmax = 65535; /* last resort */
242 PARA_INFO_LOG("shmmax: %zu\n", shmmax);