server: Lookup user only once.
[paraslash.git] / ipc.c
1 /*
2 * Copyright (C) 2006-2013 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/types.h>
13 #include <sys/param.h>
14 #include <sys/sysctl.h>
15
16 #include <sys/ipc.h>
17 #include <sys/shm.h>
18 #include <sys/sem.h>
19
20 /**
21 * Define a new mutex.
22 *
23 * \return The identifier for the new mutex on success, a negative error code
24 * on errors.
25 *
26 * \sa semget(2).
27 */
28 int mutex_new(void)
29 {
30 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
31 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
32 }
33
34 /**
35 * Destroy a mutex.
36 *
37 * \param id The identifier of the mutex to be destroyed.
38 *
39 * \return Standard.
40 *
41 * \sa semctl(2)
42 */
43 int mutex_destroy(int id)
44 {
45 int ret = semctl(id, 0, IPC_RMID);
46 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
47 }
48
49 static void para_semop(int id, struct sembuf *sops, int num)
50 {
51 do {
52 if (semop(id, sops, num) >= 0)
53 return;
54 } while (errno == EINTR);
55 if (errno == EIDRM) {
56 PARA_CRIT_LOG("semaphore set %d was removed\n", id);
57 return;
58 }
59 PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno),
60 (int)getpid());
61 exit(EXIT_FAILURE);
62 }
63
64 /**
65 * Lock the given mutex.
66 *
67 * \param id The identifier of the shared memory area to lock.
68 *
69 * This function either succeeds or aborts.
70 *
71 * \sa semop(2), struct misc_meta_data.
72 */
73 void mutex_lock(int id)
74 {
75 struct sembuf sops[2] = {
76 {
77 .sem_num = 0,
78 .sem_op = 0,
79 .sem_flg = SEM_UNDO
80 },
81 {
82 .sem_num = 0,
83 .sem_op = 1,
84 .sem_flg = SEM_UNDO
85 }
86 };
87 para_semop(id, sops, 2);
88 }
89
90 /**
91 * Unlock a mutex.
92 *
93 * \param id The identifier of the mutex.
94 *
95 * This function either succeeds or aborts.
96 *
97 * \sa semop(2), struct misc_meta_data.
98 */
99 void mutex_unlock(int id)
100 {
101 struct sembuf sops[1] = {
102 {
103 .sem_num = 0,
104 .sem_op = -1,
105 .sem_flg = SEM_UNDO
106 },
107 };
108 para_semop(id, sops, 1);
109 }
110
111 /**
112 * Create a new shared memory area of given size.
113 *
114 * \param size The size of the shared memory area to create.
115 *
116 * \return The id of the shared memory array on success, a negative error
117 * code on errors.
118 *
119 * \sa shmget(2).
120 */
121 int shm_new(size_t size)
122 {
123 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
124 return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
125 }
126
127 /**
128 * Destroy the given shared memory area.
129 *
130 * \param id The shared memory identifier.
131 *
132 * \return The return value of the underlying shmctl() call on success,
133 * a negative error code on errors.
134 *
135 * \sa shmctl(2).
136 */
137 int shm_destroy(int id)
138 {
139 struct shmid_ds shm_desc;
140 int ret = shmctl(id, IPC_RMID, &shm_desc);
141 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
142 }
143
144 /**
145 * Attach a shared memory segment.
146 *
147 * \param id The identifier of the shared memory segment to attach.
148 * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write).
149 * \param result Points to the attached area just attached on success.
150 *
151 * \return Standard.
152 *
153 * \sa shmat(2).
154 */
155 int shm_attach(int id, enum shm_attach_mode mode, void **result)
156 {
157 if (mode == ATTACH_RW)
158 *result = shmat(id, NULL, 0);
159 else
160 *result = shmat(id, NULL, SHM_RDONLY);
161 return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
162 }
163
164 /**
165 * Detach a shared memory segment.
166 *
167 * \param addr The address of the attached segment.
168 *
169 * \return Standard.
170 *
171 * \sa shmdt(2).
172 */
173 int shm_detach(void *addr)
174 {
175 int ret = shmdt(addr);
176 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
177 }
178
179 # if defined __FreeBSD__ || defined __NetBSD__
180 # define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax"
181 # elif defined __APPLE__
182 # define SYSCTL_SHMMAX_VARIABLE "kern.sysv.shmmax"
183 # else
184 # undef SYSCTL_SHMMAX_VARIABLE
185 # endif
186
187 /**
188 * Get the maximal size of a shared memory area.
189 *
190 * The value is only computed once when the function is called for the first
191 * time. Subsequent calls return the number which was computed during the
192 * first call.
193 *
194 * \return A number suitable as an argument to \ref shm_new().
195 */
196 size_t shm_get_shmmax(void)
197 {
198 static size_t shmmax;
199
200 if (shmmax > 0) /* only dance once */
201 return shmmax;
202 #ifdef __linux__ /* get it from proc fs */
203 {
204 int fd = open("/proc/sys/kernel/shmmax", O_RDONLY);
205 if (fd >= 0) {
206 char buf[100] = "";
207 int ret = read(fd, buf, sizeof(buf) - 1);
208 if (ret > 0) {
209 buf[ret] = '\0';
210 shmmax = strtoul(buf, NULL, 10);
211 }
212 close(fd);
213 }
214 }
215 #elif defined SYSCTL_SHMMAX_VARIABLE
216 {
217 size_t len = sizeof(shmmax);
218 sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0);
219 }
220 #elif defined SHMMAX
221 shmmax = SHMMAX;
222 #endif
223 if (shmmax == 0) {
224 PARA_WARNING_LOG("unable to determine shmmax\n");
225 shmmax = 65535; /* last resort */
226 }
227 PARA_INFO_LOG("shmmax: %zu\n", shmmax);
228 return shmmax;
229 }