Home | History | Annotate | Line # | Download | only in libshmif
shmif_user.c revision 1.1
      1  1.1  pooka /*      $NetBSD: shmif_user.c,v 1.1 2014/03/13 01:42:59 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 2009, 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka #ifndef _KERNEL
     28  1.1  pooka #include <sys/types.h>
     29  1.1  pooka #include <sys/mman.h>
     30  1.1  pooka 
     31  1.1  pooka #include <errno.h>
     32  1.1  pooka 
     33  1.1  pooka #include <rump/rumpuser_component.h>
     34  1.1  pooka 
     35  1.1  pooka #include "shmif_user.h"
     36  1.1  pooka 
     37  1.1  pooka #define seterr(_v_) if ((_v_) == -1) *error = errno; else *error = 0;
     38  1.1  pooka 
     39  1.1  pooka /*
     40  1.1  pooka  * On BSD we use kqueue, on Linux we use inotify.  The underlying
     41  1.1  pooka  * interface requirements aren't quite the same, but we have a very
     42  1.1  pooka  * good chance of doing the fd->path mapping on Linux thanks to dcache,
     43  1.1  pooka  * so just keep the existing interfaces for now.
     44  1.1  pooka  */
     45  1.1  pooka #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) \
     46  1.1  pooka     || defined(__OpenBSD__)
     47  1.1  pooka #include <sys/event.h>
     48  1.1  pooka 
     49  1.1  pooka #include <stdlib.h>
     50  1.1  pooka 
     51  1.1  pooka int
     52  1.1  pooka rumpcomp_shmif_watchsetup(int *kqp, int fd)
     53  1.1  pooka {
     54  1.1  pooka 	struct kevent kev;
     55  1.1  pooka 	int rv, kq;
     56  1.1  pooka 
     57  1.1  pooka 	kq = *kqp;
     58  1.1  pooka 	if (kq == -1) {
     59  1.1  pooka 		kq = kqueue();
     60  1.1  pooka 		if (kq == -1) {
     61  1.1  pooka 			rv = errno;
     62  1.1  pooka 			goto out;
     63  1.1  pooka 		}
     64  1.1  pooka 	}
     65  1.1  pooka 
     66  1.1  pooka 	EV_SET(&kev, fd, EVFILT_VNODE, EV_ADD|EV_ENABLE|EV_CLEAR,
     67  1.1  pooka 	    NOTE_WRITE, 0, 0);
     68  1.1  pooka 	if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) {
     69  1.1  pooka 		rv = errno;
     70  1.1  pooka 	} else {
     71  1.1  pooka 		rv = 0;
     72  1.1  pooka 		*kqp = kq;
     73  1.1  pooka 	}
     74  1.1  pooka 
     75  1.1  pooka  out:
     76  1.1  pooka 	return rumpuser_component_errtrans(rv);
     77  1.1  pooka }
     78  1.1  pooka 
     79  1.1  pooka int
     80  1.1  pooka rumpcomp_shmif_watchwait(int kq)
     81  1.1  pooka {
     82  1.1  pooka 	void *cookie;
     83  1.1  pooka 	struct kevent kev;
     84  1.1  pooka 	int rv;
     85  1.1  pooka 
     86  1.1  pooka 	cookie = rumpuser_component_unschedule();
     87  1.1  pooka 	do {
     88  1.1  pooka 		rv = kevent(kq, NULL, 0, &kev, 1, NULL);
     89  1.1  pooka 	} while (rv == -1 && errno == EINTR);
     90  1.1  pooka 	if (rv == -1) {
     91  1.1  pooka 		rv = errno;
     92  1.1  pooka 	} else {
     93  1.1  pooka 		rv = 0;
     94  1.1  pooka 	}
     95  1.1  pooka 	rumpuser_component_schedule(cookie);
     96  1.1  pooka 
     97  1.1  pooka 	return rumpuser_component_errtrans(rv);
     98  1.1  pooka }
     99  1.1  pooka 
    100  1.1  pooka #elif defined(__linux__)
    101  1.1  pooka #include <sys/inotify.h>
    102  1.1  pooka 
    103  1.1  pooka #include <limits.h>
    104  1.1  pooka #include <stdio.h>
    105  1.1  pooka #include <unistd.h>
    106  1.1  pooka 
    107  1.1  pooka int
    108  1.1  pooka rumpcomp_shmif_watchsetup(int *inotifyp, int fd)
    109  1.1  pooka {
    110  1.1  pooka 	char procbuf[PATH_MAX], linkbuf[PATH_MAX];
    111  1.1  pooka 	ssize_t nn;
    112  1.1  pooka 	int inotify, rv;
    113  1.1  pooka 
    114  1.1  pooka 	inotify = *inotifyp;
    115  1.1  pooka 	if (inotify == -1) {
    116  1.1  pooka 		inotify = inotify_init();
    117  1.1  pooka 		if (inotify == -1) {
    118  1.1  pooka 			rv = errno;
    119  1.1  pooka 			goto out;
    120  1.1  pooka 		}
    121  1.1  pooka 	}
    122  1.1  pooka 
    123  1.1  pooka 	/* ok, need to map fd into path for inotify */
    124  1.1  pooka 	snprintf(procbuf, sizeof(procbuf), "/proc/self/fd/%d", fd);
    125  1.1  pooka 	nn = readlink(procbuf, linkbuf, sizeof(linkbuf)-1);
    126  1.1  pooka 	if (nn >= (ssize_t)sizeof(linkbuf)-1) {
    127  1.1  pooka 		nn = -1;
    128  1.1  pooka 		errno = E2BIG; /* pick something */
    129  1.1  pooka 	}
    130  1.1  pooka 	if (nn == -1) {
    131  1.1  pooka 		rv = errno;
    132  1.1  pooka 		close(inotify);
    133  1.1  pooka 		goto out;
    134  1.1  pooka 	}
    135  1.1  pooka 
    136  1.1  pooka 	linkbuf[nn] = '\0';
    137  1.1  pooka 	if (inotify_add_watch(inotify, linkbuf, IN_MODIFY) == -1) {
    138  1.1  pooka 		rv = errno;
    139  1.1  pooka 		close(inotify);
    140  1.1  pooka 		goto out;
    141  1.1  pooka 	}
    142  1.1  pooka 	rv = 0;
    143  1.1  pooka 	*inotifyp = inotify;
    144  1.1  pooka 
    145  1.1  pooka  out:
    146  1.1  pooka 	return rumpuser_component_errtrans(rv);
    147  1.1  pooka }
    148  1.1  pooka 
    149  1.1  pooka int
    150  1.1  pooka rumpcomp_shmif_watchwait(int kq)
    151  1.1  pooka {
    152  1.1  pooka 	struct inotify_event iev;
    153  1.1  pooka 	void *cookie;
    154  1.1  pooka 	ssize_t nn;
    155  1.1  pooka 	int rv;
    156  1.1  pooka 
    157  1.1  pooka 	cookie = rumpuser_component_unschedule();
    158  1.1  pooka 	do {
    159  1.1  pooka 		nn = read(kq, &iev, sizeof(iev));
    160  1.1  pooka 	} while (nn == -1 && errno == EINTR);
    161  1.1  pooka 	if (nn == -1) {
    162  1.1  pooka 		rv = errno;
    163  1.1  pooka 	} else {
    164  1.1  pooka 		rv = 0;
    165  1.1  pooka 	}
    166  1.1  pooka 
    167  1.1  pooka 	rumpuser_component_schedule(cookie);
    168  1.1  pooka 
    169  1.1  pooka 	return rumpuser_component_errtrans(rv);
    170  1.1  pooka }
    171  1.1  pooka 
    172  1.1  pooka #else
    173  1.1  pooka #include <stdio.h>
    174  1.1  pooka #include <unistd.h>
    175  1.1  pooka 
    176  1.1  pooka /* a polling default implementation */
    177  1.1  pooka int
    178  1.1  pooka rumpcomp_shmif_watchsetup(int *nono, int fd)
    179  1.1  pooka {
    180  1.1  pooka 	static int warned = 0;
    181  1.1  pooka 
    182  1.1  pooka 	if (!warned) {
    183  1.1  pooka 		fprintf(stderr, "WARNING: rumpuser writewatchfile routines are "
    184  1.1  pooka 		    "polling-only on this platform\n");
    185  1.1  pooka 		warned = 1;
    186  1.1  pooka 	}
    187  1.1  pooka 
    188  1.1  pooka 	return 0;
    189  1.1  pooka }
    190  1.1  pooka 
    191  1.1  pooka int
    192  1.1  pooka rumpcomp_shmif_watchwait(int kq)
    193  1.1  pooka {
    194  1.1  pooka 	void *cookie;
    195  1.1  pooka 
    196  1.1  pooka 	cookie = rumpuser_component_unschedule();
    197  1.1  pooka 	usleep(10000);
    198  1.1  pooka 	rumpuser_component_schedule(cookie);
    199  1.1  pooka 
    200  1.1  pooka 	return 0;
    201  1.1  pooka }
    202  1.1  pooka #endif
    203  1.1  pooka 
    204  1.1  pooka int
    205  1.1  pooka rumpcomp_shmif_mmap(int fd, size_t len, void **memp)
    206  1.1  pooka {
    207  1.1  pooka 	void *mem;
    208  1.1  pooka 	int rv;
    209  1.1  pooka 
    210  1.1  pooka 	if (ftruncate(fd, len) == -1) {
    211  1.1  pooka 		rv = errno;
    212  1.1  pooka 		goto out;
    213  1.1  pooka 	}
    214  1.1  pooka 
    215  1.1  pooka #if defined(__sun__) && !defined(MAP_FILE)
    216  1.1  pooka #define MAP_FILE 0
    217  1.1  pooka #endif
    218  1.1  pooka 
    219  1.1  pooka 	mem = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
    220  1.1  pooka 	if (mem == MAP_FAILED) {
    221  1.1  pooka 		rv = errno;
    222  1.1  pooka 	} else {
    223  1.1  pooka 		rv = 0;
    224  1.1  pooka 		*memp = mem;
    225  1.1  pooka 	}
    226  1.1  pooka 
    227  1.1  pooka  out:
    228  1.1  pooka 	return rumpuser_component_errtrans(rv);
    229  1.1  pooka }
    230  1.1  pooka #endif
    231