Introduce LIST_HEAD macro and use it in audiod_command.
[paraslash.git] / ipc.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file ipc.c Inter-process 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 negative error code
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? -ERRNO_TO_PARA_ERROR(errno) : ret;
28 }
29
30 /**
31 * Destroy a mutex.
32 *
33 * \param id The identifier of the mutex to be destroyed.
34 *
35 * \return Standard.
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? -ERRNO_TO_PARA_ERROR(errno) : 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_CRIT_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 The identifier 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 array on success, a negative error
113 * code on errors.
114 *
115 * \sa shmget(2).
116 */
117 int shm_new(size_t size)
118 {
119 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
120 return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
121 }
122
123 /**
124 * Destroy the given shared memory area.
125 *
126 * \param id The shared memory identifier.
127 *
128 * \return The return value of the underlying shmctl() call on success,
129 * a negative error code on errors.
130 *
131 * \sa shmctl(2).
132 */
133 int shm_destroy(int id)
134 {
135 struct shmid_ds shm_desc;
136 int ret = shmctl(id, IPC_RMID, &shm_desc);
137 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
138 }
139
140 /**
141 * Attach a shared memory segment.
142 *
143 * \param id The identifier of the shared memory segment to attach.
144 * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write).
145 * \param result Points to the attached area just attached on success.
146 *
147 * \return Standard.
148 *
149 * \sa shmat(2).
150 */
151 int shm_attach(int id, enum shm_attach_mode mode, void **result)
152 {
153 if (mode == ATTACH_RW)
154 *result = shmat(id, NULL, 0);
155 else
156 *result = shmat(id, NULL, SHM_RDONLY);
157 return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
158 }
159
160 /**
161 * Detach a shared memory segment.
162 *
163 * \param addr The address of the attached segment.
164 *
165 * \return Standard.
166 *
167 * \sa shmdt(2).
168 */
169 int shm_detach(void *addr)
170 {
171 int ret = shmdt(addr);
172 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
173 }