973a9eaf4ffee1e69eb5a5e5460f7060f654b433
[paraslash.git] / ipc.c
1 #include "para.h"
2 #include "error.h"
3 #include "ipc.h"
4 #include <sys/ipc.h>
5 #include <sys/shm.h>
6
7 #define MAX_SEMOP_RETRIES 500
8
9 int mutex_new(void)
10 {
11 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
12 return ret < 0? -E_SEM_GET : ret;
13 }
14
15 int mutex_destroy(int id)
16 {
17 int ret = semctl(id, 0, IPC_RMID);
18 return ret < 0? -E_SEM_REMOVE : 1;
19 }
20
21 static void para_semop(int id, struct sembuf *sops, int num)
22 {
23 int i;
24
25 for (i = 0; i < MAX_SEMOP_RETRIES; i++)
26 if (semop(id, sops, num) >= 0)
27 return;
28 PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES);
29 exit(EXIT_FAILURE);
30 }
31
32 /**
33 * lock the given mutex
34 *
35 * \sa semop(2), struct misc_meta_data
36 */
37 void mutex_lock(int id)
38 {
39 struct sembuf sops[2] = {
40 {
41 .sem_num = 0,
42 .sem_op = 0,
43 .sem_flg = SEM_UNDO
44 },
45 {
46 .sem_num = 0,
47 .sem_op = 1,
48 .sem_flg = SEM_UNDO
49 }
50 };
51 para_semop(id, sops, 2);
52 }
53
54 /**
55 * unlock a mutex
56 *
57 * \sa semop(2), struct misc_meta_data
58 */
59 void mutex_unlock(int id)
60 {
61 struct sembuf sops[1] = {
62 {
63 .sem_num = 0,
64 .sem_op = -1,
65 .sem_flg = SEM_UNDO
66 },
67 };
68 para_semop(id, sops, 1);
69 }
70
71 /**
72 * create a new shared memory area of given size
73 *
74 * \sa shmget(2)
75 */
76 int shm_new(size_t size)
77 {
78 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
79 return ret < 0 ? -E_SHM_GET : ret;
80 }
81
82 /**
83 * destroy the given shared memory area
84 * \sa shmctl(2)
85 **/
86 int shm_destroy(int id)
87 {
88 struct shmid_ds shm_desc;
89 int ret = shmctl(id, IPC_RMID, &shm_desc);
90 return ret < 0? -E_SHM_DESTROY : ret;
91 }
92
93 /**
94 * attach a shared memory area
95 *
96 * \sa semop(2)
97 */
98 int shm_attach(int id, enum shm_attach_mode mode, void **result)
99 {
100 if (mode == ATTACH_RW) {
101 *result = shmat(id, NULL, 0);
102 return *result? 1 : -E_SHM_ATTACH;
103 }
104 *result = shmat(id, NULL, SHM_RDONLY);
105 return *result? 1 : -E_SHM_ATTACH;
106 }
107
108 int shm_detach(void *addr)
109 {
110 int ret = shmdt(addr);
111 return ret < 0? -E_SHM_DETACH : 1;
112 }