2 * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
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 * Detach a shared memory segment.
167 * \param addr The address of the attached segment.
173 int shm_detach(void *addr
)
175 int ret
= shmdt(addr
);
176 return ret
< 0? -ERRNO_TO_PARA_ERROR(errno
) : 1;
179 # if defined __FreeBSD__ || defined __NetBSD__
180 # define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax"
181 # elif defined __APPLE__
182 # define SYSCTL_SHMMAX_VARIABLE "kern.sysv.shmmax"
184 # undef SYSCTL_SHMMAX_VARIABLE
188 * Get the maximal size of a shared memory area.
190 * The value is only computed once when the function is called for the first
191 * time. Subsequent calls return the number which was computed during the
194 * \return A number suitable as an argument to \ref shm_new().
196 size_t shm_get_shmmax(void)
198 static size_t shmmax
;
200 if (shmmax
> 0) /* only dance once */
202 #ifdef __linux__ /* get it from proc fs */
204 int fd
= open("/proc/sys/kernel/shmmax", O_RDONLY
);
207 int ret
= read(fd
, buf
, sizeof(buf
) - 1);
210 shmmax
= strtoul(buf
, NULL
, 10);
215 #elif defined SYSCTL_SHMMAX_VARIABLE
217 size_t len
= sizeof(shmmax
);
218 sysctlbyname(SYSCTL_SHMMAX_VARIABLE
, &shmmax
, &len
, NULL
, 0);
224 PARA_WARNING_LOG("unable to determine shmmax\n");
225 shmmax
= 65535; /* last resort */
227 PARA_INFO_LOG("shmmax: %zu\n", shmmax
);