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