make playlist_update_audio_file() static.
[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 /**
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), getpid());
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 }