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