struct http_client: Use a _pointer_ to the chunk_queue.
[paraslash.git] / ipc.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ipc.c interprocess communication and shared memory helpers */
8
9 #include "para.h"
10 #include "error.h"
11 #include "ipc.h"
12 #include <sys/ipc.h>
13 #include <sys/shm.h>
14 #include <sys/sem.h>
15
16 /** abort if semget() failed that many times */
17 #define MAX_SEMOP_RETRIES 500
18
19 /**
20  * define a new mutex
21  *
22  * \return the identifier for the new mutex on success, \a -E_SEM_GET
23  * on errors.
24  *
25  * \sa semget(2)
26  */
27 int mutex_new(void)
28 {
29         int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
30         return ret < 0?  -E_SEM_GET : ret;
31 }
32
33 /**
34  * destroy a mutex
35  *
36  * \param id the identifier of the mutex to be destroyed
37  *
38  * \return Positive on success, \a -E_SEM_REMOVE on errors.
39  *
40  * \sa semctl(2)
41  */
42 int mutex_destroy(int id)
43 {
44         int ret = semctl(id, 0, IPC_RMID);
45         return ret < 0? -E_SEM_REMOVE : 1;
46 }
47
48 static void para_semop(int id, struct sembuf *sops, unsigned num)
49 {
50         int i;
51
52         for (i = 0; i < MAX_SEMOP_RETRIES; i++)
53                 if (semop(id, sops, num) >= 0)
54                         return;
55         PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES);
56         exit(EXIT_FAILURE);
57 }
58
59 /**
60  * lock the given mutex
61  *
62  * \param id of the shared memory area to lock
63  *
64  * This function either succeeds or aborts.
65  *
66  * \sa semop(2), struct misc_meta_data
67  */
68 void mutex_lock(int id)
69 {
70         struct sembuf sops[2] = {
71                 {
72                         .sem_num = 0,
73                         .sem_op = 0,
74                         .sem_flg = SEM_UNDO
75                 },
76                 {
77                         .sem_num = 0,
78                         .sem_op = 1,
79                         .sem_flg = SEM_UNDO
80                 }
81         };
82         para_semop(id, sops, 2);
83 }
84
85 /**
86  * unlock a mutex
87  *
88  * \param id the identifier of the mutex
89  *
90  * This function either succeeds or aborts.
91  *
92  * \sa semop(2), struct misc_meta_data
93  */
94 void mutex_unlock(int id)
95 {
96         struct sembuf sops[1] = {
97                 {
98                         .sem_num = 0,
99                         .sem_op = -1,
100                         .sem_flg = SEM_UNDO
101                 },
102         };
103         para_semop(id, sops, 1);
104 }
105
106 /**
107  * create a new shared memory area of given size
108  *
109  * \param size the size of the shared memory area to create
110  *
111  * \return The id of the shared memory areay on success, \a -E_SHM_GET on errors.
112  *
113  * \sa shmget(2)
114  */
115 int shm_new(size_t size)
116 {
117         int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
118         return ret < 0 ? -E_SHM_GET : ret;
119 }
120
121 /**
122  * destroy the given shared memory area
123  *
124  * \param id the shared memory id
125  *
126  * \return The return value of the underlying shmctl() call on success,
127  * \a -E_SHM_DESTROY on errors.
128  *
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 area just attached
144  *
145  * \return positive on success, \a -E_SHM_ATTACH on errors.
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  * \return positive on success, \a -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 }