paraslash 0.2.11
[paraslash.git] / ipc.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file ipc.c interprocess communication and shared memory helpers */
20
21 #include "para.h"
22 #include "error.h"
23 #include "ipc.h"
24 #include <sys/ipc.h>
25 #include <sys/shm.h>
26
27 /** abort if semget() failed that many times */
28 #define MAX_SEMOP_RETRIES 500
29
30 /**
31 * define a new mutex
32 *
33 * \return the identifier for the new mutex on success, -E_SEM_GET
34 * on errors.
35 *
36 * \sa semget(2)
37 */
38 int mutex_new(void)
39 {
40 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
41 return ret < 0? -E_SEM_GET : ret;
42 }
43
44 /**
45 * destroy a mutex
46 *
47 * \param id the identifier of the mutex to be destroyed
48 *
49 * \returns Positive on success, -E_SEM_REMOVE on errors.
50 *
51 * \sa semctl(2)
52 */
53 int mutex_destroy(int id)
54 {
55 int ret = semctl(id, 0, IPC_RMID);
56 return ret < 0? -E_SEM_REMOVE : 1;
57 }
58
59 static void para_semop(int id, struct sembuf *sops, int num)
60 {
61 int i;
62
63 for (i = 0; i < MAX_SEMOP_RETRIES; i++)
64 if (semop(id, sops, num) >= 0)
65 return;
66 PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES);
67 exit(EXIT_FAILURE);
68 }
69
70 /**
71 * lock the given mutex
72 *
73 * This function either succeeds or aborts.
74 *
75 * \sa semop(2), struct misc_meta_data
76 */
77 void mutex_lock(int id)
78 {
79 struct sembuf sops[2] = {
80 {
81 .sem_num = 0,
82 .sem_op = 0,
83 .sem_flg = SEM_UNDO
84 },
85 {
86 .sem_num = 0,
87 .sem_op = 1,
88 .sem_flg = SEM_UNDO
89 }
90 };
91 para_semop(id, sops, 2);
92 }
93
94 /**
95 * unlock a mutex
96 *
97 * \param id the identifier of the mutex
98 *
99 * This function either succeeds or aborts.
100 *
101 * \sa semop(2), struct misc_meta_data
102 */
103 void mutex_unlock(int id)
104 {
105 struct sembuf sops[1] = {
106 {
107 .sem_num = 0,
108 .sem_op = -1,
109 .sem_flg = SEM_UNDO
110 },
111 };
112 para_semop(id, sops, 1);
113 }
114
115 /**
116 * create a new shared memory area of given size
117 *
118 * \sa shmget(2)
119 */
120 int shm_new(size_t size)
121 {
122 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
123 return ret < 0 ? -E_SHM_GET : ret;
124 }
125
126 /**
127 * destroy the given shared memory area
128 * \sa shmctl(2)
129 **/
130 int shm_destroy(int id)
131 {
132 struct shmid_ds shm_desc;
133 int ret = shmctl(id, IPC_RMID, &shm_desc);
134 return ret < 0? -E_SHM_DESTROY : ret;
135 }
136
137 /**
138 * attach a shared memory segment
139 *
140 * \param id the identifier of the shared memory segment to attach
141 * \param mode either ATTACH_RO (read only) or ATTACH_RW (read/write)
142 * \param result points to the attached arer which to the
143 *
144 * \returns positive on success, -E_SHM_ATTACH on errrors.
145 *
146 * \sa shmat(2)
147 */
148 int shm_attach(int id, enum shm_attach_mode mode, void **result)
149 {
150 if (mode == ATTACH_RW) {
151 *result = shmat(id, NULL, 0);
152 return *result? 1 : -E_SHM_ATTACH;
153 }
154 *result = shmat(id, NULL, SHM_RDONLY);
155 return *result? 1 : -E_SHM_ATTACH;
156 }
157
158 /**
159 * detach a shared memory segment
160 *
161 * \param addr the address of the attached segment
162 *
163 * \returns positive on success, -E_SHM_DETACH on errors.
164 *
165 * \sa shmdt(2)
166 */
167 int shm_detach(void *addr)
168 {
169 int ret = shmdt(addr);
170 return ret < 0? -E_SHM_DETACH : 1;
171 }