/* SPDX-License-Identifier: GPL-2.0 */ /** \file ipc.c Inter-process communication and shared memory helpers. */ #include "para.h" #include "error.h" #include "ipc.h" #include #include #include #include #include /** * Define a new mutex. * * \return The identifier for the new mutex on success, a negative error code * on errors. * * \sa semget(2). */ int mutex_new(void) { int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666); return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret; } /** * Destroy a mutex. * * \param id The identifier of the mutex to be destroyed. * * \sa semctl(2) */ void mutex_destroy(int id) { if (semctl(id, 0, IPC_RMID) < 0) PARA_WARNING_LOG("%s\n", para_strerror(errno)); } static void para_semop(int id, struct sembuf *sops, int num) { do { if (semop(id, sops, num) >= 0) return; } while (errno == EINTR); if (errno == EIDRM) { PARA_CRIT_LOG("semaphore set %d was removed\n", id); return; } PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno), (int)getpid()); exit(EXIT_FAILURE); } /** * Lock the given mutex. * * \param id The identifier of the shared memory area to lock. * * This function either succeeds or aborts. * * \sa semop(2), struct \ref misc_meta_data. */ void mutex_lock(int id) { struct sembuf sops[2] = { { .sem_num = 0, .sem_op = 0, .sem_flg = SEM_UNDO }, { .sem_num = 0, .sem_op = 1, .sem_flg = SEM_UNDO } }; para_semop(id, sops, 2); } /** * Unlock a mutex. * * \param id The identifier of the mutex. * * This function either succeeds or aborts. * * \sa semop(2), struct \ref misc_meta_data. */ void mutex_unlock(int id) { struct sembuf sops[1] = { { .sem_num = 0, .sem_op = -1, .sem_flg = SEM_UNDO }, }; para_semop(id, sops, 1); } /** * Create a new shared memory area of given size. * * \param size The size of the shared memory area to create. * * \return The id of the shared memory array on success, a negative error * code on errors. * * \sa shmget(2). */ int shm_new(size_t size) { int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600); return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret; } /** * Destroy the given shared memory area. * * \param id The shared memory identifier. * * \return The return value of the underlying shmctl() call on success, * a negative error code on errors. * * \sa shmctl(2). */ int shm_destroy(int id) { struct shmid_ds shm_desc; int ret = shmctl(id, IPC_RMID, &shm_desc); return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret; } /** * Attach a shared memory segment. * * \param id The identifier of the shared memory segment to attach. * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write). * \param result Points to the attached area just attached on success. * * \return Standard. * * \sa shmat(2). */ int shm_attach(int id, enum shm_attach_mode mode, void **result) { if (mode == ATTACH_RW) *result = shmat(id, NULL, 0); else *result = shmat(id, NULL, SHM_RDONLY); return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1; } /** * Get the size of a shared memory segment. * * \param id The shared memory segment identifier. * \param result Size in bytes is returned here, zero on errors. * * \return Standard. * * \sa shmctl(2). */ int shm_size(int id, size_t *result) { struct shmid_ds ds; /* data structure */ *result = 0; if (shmctl(id, IPC_STAT, &ds) < 0) return -ERRNO_TO_PARA_ERROR(errno); *result = ds.shm_segsz; return 1; } /** * Detach a shared memory segment. * * \param addr The address of the attached segment. * * \return Standard. * * \sa shmdt(2). */ int shm_detach(void *addr) { int ret = shmdt(addr); return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1; } # if defined __FreeBSD__ || defined __NetBSD__ #include # define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax" # else # undef SYSCTL_SHMMAX_VARIABLE # endif /** * Get the maximal size of a shared memory area. * * The value is only computed once when the function is called for the first * time. Subsequent calls return the number which was computed during the * first call. * * \return A number suitable as an argument to \ref shm_new(). */ size_t shm_get_shmmax(void) { static size_t shmmax; if (shmmax > 0) /* only dance once */ return shmmax; #ifdef __linux__ /* get it from proc fs */ { int fd = open("/proc/sys/kernel/shmmax", O_RDONLY); if (fd >= 0) { char buf[100]; int ret = read(fd, buf, sizeof(buf) - 1); if (ret > 0) { buf[ret] = '\0'; shmmax = strtoul(buf, NULL, 10); } close(fd); } } #elif defined SYSCTL_SHMMAX_VARIABLE { size_t len = sizeof(shmmax); sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0); } #elif defined SHMMAX shmmax = SHMMAX; #endif /* * The value may be too large for allocating memory. In particular, on * 32 bit Linux it is almost 4G. */ shmmax = PARA_MIN((size_t)10 * 1024 * 1024, shmmax); if (shmmax == 0) { PARA_WARNING_LOG("unable to determine shmmax\n"); shmmax = 65535; /* last resort */ } PARA_INFO_LOG("shmmax: %zu\n", shmmax); return shmmax; }