2 * Copyright (c) 2013 Larisa Grigore <larisagrigore@gmail.com>.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Adam Glass and Charles
16 * 4. The names of the authors may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "namespace.h"
32 #include <sys/param.h>
33 #include <sys/queue.h>
44 #include "un-namespace.h"
46 #include "sysvipc_lock.h"
47 #include "sysvipc_ipc.h"
48 #include "sysvipc_sockets.h"
49 #include "sysvipc_shm.h"
50 #include "sysvipc_hash.h"
52 #define SYSV_MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
53 #define SYSV_MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
54 #define SYSV_MUTEX_DESTROY(x) if (__isthreaded) _pthread_mutex_destroy(x)
56 struct hashtable *shmres = NULL;
57 struct hashtable *shmaddrs = NULL;
58 pthread_mutex_t lock_resources = PTHREAD_MUTEX_INITIALIZER;
60 /* File descriptor used to communicate with the daemon. */
62 /* Structure used to save semaphore operation with SEMUNDO flag. */
63 extern struct sem_undo *undos;
73 shmres = _hash_init(MAXSIZE);
77 shmaddrs = _hash_init(MAXSIZE);
84 _hash_destroy(shmres);
95 _hash_destroy(shmres);
96 _hash_destroy(shmaddrs);
97 SYSV_MUTEX_DESTROY(lock_resources);
102 /* Init sysv ipc resources and those used for shared memory. */
108 /* Init sysv resources. */
109 if ((ret = sysvinit()) != 0)
111 /* Init resorces used for shared memory. */
112 if ((ret = shminit()) < 0)
117 /* Check if sysv ipc resources are initialized. */
128 /* OBS: It is used only a rwlock for both hashtables and
129 * socket. I've made that choice because is I considered to
130 * be much expensive to acquire/release more than one especially
131 * as the daemon is not multithreading.
134 /* This function has another parameter apart from shmget.
135 * The parameter has information about the type of sysv
136 * ipc resource (shm, sem, msg, undo).
137 * The undo segment is used for sem ops with UNDO flag set.
140 _shmget(key_t key, size_t size, int shmflg, int type)
142 struct shmget_msg msg;
143 struct shm_data *data;
147 SYSV_MUTEX_LOCK(&lock_resources);
148 if (shmcheck() < 0) {
149 sysv_print_err("init sysv ipc\n");
158 send_message(daemon_fd, type, (char *)&msg, sizeof(msg));
160 /* Accept a file installed by the daemon.
161 * The file is used as shared memory. */
162 fd = receive_fd(daemon_fd);
168 flags = _fcntl(fd, F_GETFD, 0);
169 if (_fcntl(fd, F_SETFD, flags & FD_CLOEXEC) == -1) {
170 sysv_print_err("fcntl error\n");
175 /* Receive the resource id or error. */
176 receive_message(daemon_fd, (char *)&shmid, sizeof(shmid));
184 /* Do we already have an entry for this resource? */
185 data = _hash_lookup(shmres, shmid);
189 /* If not, add necessary data about it. */
190 data = malloc(sizeof(struct shm_data));
197 data->access = 0; /* Used for sems. */
199 /* Insert data in hashtable using the shmid. */
200 _hash_insert(shmres, shmid, data);
202 SYSV_MUTEX_UNLOCK(&lock_resources);
207 sysvipc_shmget(key_t key, size_t size, int shmflg)
209 return (_shmget(key, size, shmflg, SHMGET));
213 sysvipc_shmat(int shmid, const void *shmaddr, int shmflg)
215 struct shmat_msg msg;
220 struct shm_data *data;
222 SYSV_MUTEX_LOCK(&lock_resources);
223 if (!is_shm_started()) {
228 /* Get data using shmid. */
229 data = _hash_lookup(shmres, shmid);
235 size = round_page(data->size);
237 #ifdef VM_PROT_READ_IS_EXEC
238 prot = PROT_READ | PROT_EXECUTE;
242 if ((shmflg & SHM_RDONLY) == 0)
247 if (shmflg & SHM_RND) {
248 addr = (void *)((uintptr_t)shmaddr & ~(SHMLBA-1));
249 } else if (((uintptr_t)shmaddr & (SHMLBA-1)) == 0) {
250 addr = __DECONST(void *, shmaddr);
258 msg.shmaddr = shmaddr;
260 msg.size = data->size; /* For undo segment. */
262 send_message(daemon_fd, SHMAT, (char *)&msg, sizeof(msg));
263 receive_message(daemon_fd, (char *)&error, sizeof(error));
269 addr = mmap(addr, size, prot, flags, data->fd, 0);
271 sysv_print_err("mmap\n");
272 /* Detach ourselves from the segment. */
273 send_message(daemon_fd, SHMDT, (char *)&shmid, sizeof(shmid));
277 /* Necessary for SEMGET, MSGGET, UNDOGET. */
278 data->internal = addr;
280 /* Save the mapped address for munmap call. */
281 _hash_insert(shmaddrs, (u_long)addr, data);
283 SYSV_MUTEX_UNLOCK(&lock_resources);
287 /* Remove a sysv ipc resource. */
289 void shmremove(int shmid)
291 struct shm_data *data;
292 data = _hash_remove(shmres, shmid);
294 //TODO nu trebuie demapat?
301 sysvipc_shmctl(int shmid, int cmd, struct shmid_ds *buf)
304 struct shmctl_msg *msg;
306 /* if (cmd == IPC_SET)
307 size = sizeof(struct shmctl_msg) + sizeof(struct shmid_ds);
309 size = sizeof(struct shmctl_msg);
311 SYSV_MUTEX_LOCK(&lock_resources);
315 if (!is_shm_started()) {
320 size = sizeof(struct shmctl_msg);
328 send_message(daemon_fd, SHMCTL, (char *)msg, sizeof(*msg));
330 receive_message(daemon_fd, (char *)&ret, sizeof(ret));
332 /* Get data in IPC_STAT case. */
333 if (ret == 0 && cmd == IPC_STAT)
334 receive_message(daemon_fd, (char *)buf, sizeof(*buf));
336 /* Free all resources specific to a shmid in IPC_RMID case. */
337 if (ret == 0 && cmd == IPC_RMID)
342 SYSV_MUTEX_UNLOCK(&lock_resources);
343 return (ret == 0 ? 0 : -1);
346 /* Functionality of shmdt with the possibility to inform or not
348 * Inform the daemon when shmdt is called and not when an error
349 * occurs and the daemon doesn't know that the process is attaced.
352 _shmdt(const void *shmaddr, int send_to_daemon)
356 struct shm_data *data;
360 SYSV_MUTEX_LOCK(&lock_resources);
361 if (!is_shm_started()) {
366 /* Verify if shmaddr was returned from a shmat call. */
367 data = _hash_remove(shmaddrs, (u_long)shmaddr);
373 size = round_page(data->size);
375 ret = munmap(__DECONST(void *, shmaddr), size);
380 send_message(daemon_fd, SHMDT, (char *)&data->shmid, sizeof(int));
386 SYSV_MUTEX_UNLOCK(&lock_resources);
391 sysvipc_shmdt(const void *shmaddr)
393 return (_shmdt(shmaddr, 1));
400 struct entries_list *list;
401 struct hashentry *tmp, *ttmp;
402 struct shmat_msg msg;
403 struct shm_data *data;
406 /* OBS: no locking is necessary because this function is called
407 * after the child is created and at that moment only one thread
408 * exists in the process.
410 for (i=0; i<get_hash_size(MAXSIZE); i++) {
411 list = &shmaddrs->entries[i];
412 if (LIST_EMPTY(list))
414 LIST_FOREACH_MUTABLE(tmp, list, entry_link, ttmp) {
415 data = (struct shm_data*)tmp->value;
416 /* Inform daemon that we are attached. */
418 if (data->type == UNDOGET) {
422 msg.shmid = data->shmid;
423 msg.shmaddr = data->internal;
424 msg.shmflg = 0; /* This is enough at this moment. */
425 msg.size = data->size;
426 /* Last field is not necessary because it is used only
430 send_message(daemon_fd, SHMAT, (char *)&msg, sizeof(msg));
431 receive_message(daemon_fd, (char *)&error, sizeof(error));
433 /* If the daemon returned error munmap the region. */
436 _shmdt(data->internal, 0);
437 shmremove(data->shmid);
438 sysv_print_err(" %d shmchild\n", error);
445 /* Remove semundo structures. Those are specific only for the parent.
446 * The child must create for itself a new one.
448 data = _hash_remove(shmaddrs, (u_long)undos);
450 munmap(undos, round_page(data->size));
455 /* Called each time a thread tries to access the sem/msg.
456 * It is used in order to protect data against its removal
460 get_shmdata(int id, int to_remove, int shm_access)
462 struct shm_data *data = NULL;
464 SYSV_MUTEX_LOCK(&lock_resources);
465 if (!is_shm_started()) {
470 data = _hash_lookup(shmres, id);
476 /* If segment was removed by another thread we can't use it. */
478 sysv_print("segment already removed\n");
484 /* Mark for removal. Inform the other threads from the
485 * same address space. */
487 sysv_print("segment is removed\n");
488 data->removed = to_remove; /* 1 if it is removed by
489 the current process and 2 if it was removed by
492 /* No need for any rights check because this is
493 * done by daemon if this is the process that removes
495 * If not, there is no need for any right to clean
496 * internal resources.
501 /* Avoid segmentation fault if the memory zone
502 * is accessed without necessary permissions
503 * (it was mapped according to them).
505 if (!(data->access & shm_access)) {
507 sysv_print("no access rights has %o and wants %o\n",
508 data->access, shm_access);
518 SYSV_MUTEX_UNLOCK(&lock_resources);
522 /* Set the shm_access type (IPC_R, IPC_W) for sem/msg. */
524 set_shmdata_access(int id, int shm_access)
526 struct shm_data *data;
529 SYSV_MUTEX_LOCK(&lock_resources);
530 if (!is_shm_started()) {
535 data = _hash_lookup(shmres, id);
541 /* If segment was removed by another thread we can't use it. */
547 data->access = shm_access;
550 SYSV_MUTEX_UNLOCK(&lock_resources);