2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file ipc.c interprocess communication and shared memory helpers */
28 /** abort if semget() failed that many times */
29 #define MAX_SEMOP_RETRIES 500
34 * \return the identifier for the new mutex on success, -E_SEM_GET
41 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
42 return ret < 0? -E_SEM_GET : ret;
48 * \param id the identifier of the mutex to be destroyed
50 * \returns Positive on success, -E_SEM_REMOVE on errors.
54 int mutex_destroy(int id)
56 int ret = semctl(id, 0, IPC_RMID);
57 return ret < 0? -E_SEM_REMOVE : 1;
60 static void para_semop(int id, struct sembuf *sops, int num)
64 for (i = 0; i < MAX_SEMOP_RETRIES; i++)
65 if (semop(id, sops, num) >= 0)
67 PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES);
72 * lock the given mutex
74 * This function either succeeds or aborts.
76 * \sa semop(2), struct misc_meta_data
78 void mutex_lock(int id)
80 struct sembuf sops[2] = {
92 para_semop(id, sops, 2);
98 * \param id the identifier of the mutex
100 * This function either succeeds or aborts.
102 * \sa semop(2), struct misc_meta_data
104 void mutex_unlock(int id)
106 struct sembuf sops[1] = {
113 para_semop(id, sops, 1);
117 * create a new shared memory area of given size
121 int shm_new(size_t size)
123 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
124 return ret < 0 ? -E_SHM_GET : ret;
128 * destroy the given shared memory area
131 int shm_destroy(int id)
133 struct shmid_ds shm_desc;
134 int ret = shmctl(id, IPC_RMID, &shm_desc);
135 return ret < 0? -E_SHM_DESTROY : ret;
139 * attach a shared memory segment
141 * \param id the identifier of the shared memory segment to attach
142 * \param mode either ATTACH_RO (read only) or ATTACH_RW (read/write)
143 * \param result points to the attached arer which to the
145 * \returns positive on success, -E_SHM_ATTACH on errrors.
149 int shm_attach(int id, enum shm_attach_mode mode, void **result)
151 if (mode == ATTACH_RW) {
152 *result = shmat(id, NULL, 0);
153 return *result? 1 : -E_SHM_ATTACH;
155 *result = shmat(id, NULL, SHM_RDONLY);
156 return *result? 1 : -E_SHM_ATTACH;
160 * detach a shared memory segment
162 * \param addr the address of the attached segment
164 * \returns positive on success, -E_SHM_DETACH on errors.
168 int shm_detach(void *addr)
170 int ret = shmdt(addr);
171 return ret < 0? -E_SHM_DETACH : 1;