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