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