rump.c revision 1.2 1 1.2 pooka /* $NetBSD: rump.c,v 1.2 2007/08/06 22:20:57 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by Google Summer of Code.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.1 pooka #include <sys/param.h>
31 1.1 pooka #include <sys/filedesc.h>
32 1.1 pooka #include <sys/kauth.h>
33 1.1 pooka #include <sys/mount.h>
34 1.1 pooka #include <sys/namei.h>
35 1.1 pooka #include <sys/queue.h>
36 1.1 pooka #include <sys/resourcevar.h>
37 1.1 pooka #include <sys/vnode.h>
38 1.1 pooka
39 1.1 pooka #include <machine/cpu.h>
40 1.1 pooka
41 1.1 pooka #include "rump.h"
42 1.1 pooka #include "rumpuser.h"
43 1.1 pooka
44 1.1 pooka struct proc rump_proc;
45 1.1 pooka struct cwdinfo rump_cwdi;
46 1.1 pooka struct pstats rump_stats;
47 1.1 pooka struct plimit rump_limits;
48 1.1 pooka kauth_cred_t rump_cred;
49 1.1 pooka struct cpu_info rump_cpu;
50 1.1 pooka
51 1.2 pooka struct fakeblk {
52 1.2 pooka char path[MAXPATHLEN];
53 1.2 pooka LIST_ENTRY(fakeblk) entries;
54 1.2 pooka };
55 1.2 pooka
56 1.2 pooka static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
57 1.2 pooka
58 1.1 pooka void
59 1.1 pooka rump_init()
60 1.1 pooka {
61 1.1 pooka extern char hostname[];
62 1.1 pooka extern size_t hostnamelen;
63 1.1 pooka int error;
64 1.1 pooka
65 1.1 pooka curlwp = &lwp0;
66 1.1 pooka rumpvm_init();
67 1.1 pooka
68 1.1 pooka curlwp->l_proc = &rump_proc;
69 1.1 pooka curlwp->l_cred = rump_cred;
70 1.1 pooka rump_proc.p_stats = &rump_stats;
71 1.1 pooka rump_proc.p_cwdi = &rump_cwdi;
72 1.1 pooka rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
73 1.1 pooka rump_proc.p_limit = &rump_limits;
74 1.1 pooka
75 1.1 pooka vfsinit();
76 1.1 pooka
77 1.1 pooka rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
78 1.1 pooka hostnamelen = strlen(hostname);
79 1.1 pooka }
80 1.1 pooka
81 1.1 pooka void
82 1.1 pooka rump_mountinit(struct mount **mpp, struct vfsops *vfsops)
83 1.1 pooka {
84 1.1 pooka struct mount *mp;
85 1.1 pooka
86 1.1 pooka mp = rumpuser_malloc(sizeof(struct mount), 0);
87 1.1 pooka memset(mp, 0, sizeof(struct mount));
88 1.1 pooka
89 1.1 pooka mp->mnt_op = vfsops;
90 1.1 pooka TAILQ_INIT(&mp->mnt_vnodelist);
91 1.1 pooka *mpp = mp;
92 1.1 pooka }
93 1.1 pooka
94 1.1 pooka void
95 1.1 pooka rump_mountdestroy(struct mount *mp)
96 1.1 pooka {
97 1.1 pooka
98 1.1 pooka rumpuser_free(mp);
99 1.1 pooka }
100 1.1 pooka
101 1.1 pooka struct componentname *
102 1.1 pooka rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
103 1.1 pooka struct lwp *l)
104 1.1 pooka {
105 1.1 pooka struct componentname *cnp;
106 1.1 pooka
107 1.1 pooka cnp = rumpuser_malloc(sizeof(struct componentname), 0);
108 1.1 pooka memset(cnp, 0, sizeof(struct componentname));
109 1.1 pooka
110 1.1 pooka cnp->cn_nameiop = nameiop;
111 1.1 pooka cnp->cn_flags = flags;
112 1.1 pooka
113 1.1 pooka cnp->cn_pnbuf = PNBUF_GET();
114 1.1 pooka strcpy(cnp->cn_pnbuf, name);
115 1.1 pooka cnp->cn_nameptr = cnp->cn_pnbuf;
116 1.1 pooka cnp->cn_namelen = namelen;
117 1.1 pooka
118 1.1 pooka cnp->cn_cred = l->l_cred;
119 1.1 pooka cnp->cn_lwp = l;
120 1.1 pooka
121 1.1 pooka return cnp;
122 1.1 pooka }
123 1.1 pooka
124 1.1 pooka void
125 1.1 pooka rump_freecn(struct componentname *cnp, int islookup)
126 1.1 pooka {
127 1.1 pooka
128 1.1 pooka if (cnp->cn_flags & SAVENAME) {
129 1.1 pooka if (islookup || cnp->cn_flags & SAVESTART)
130 1.1 pooka PNBUF_PUT(cnp->cn_pnbuf);
131 1.1 pooka } else {
132 1.1 pooka PNBUF_PUT(cnp->cn_pnbuf);
133 1.1 pooka }
134 1.1 pooka rumpuser_free(cnp);
135 1.1 pooka }
136 1.1 pooka
137 1.1 pooka int
138 1.1 pooka rump_recyclenode(struct vnode *vp)
139 1.1 pooka {
140 1.1 pooka
141 1.1 pooka return vrecycle(vp, NULL, curlwp);
142 1.1 pooka }
143 1.2 pooka
144 1.2 pooka static struct fakeblk *
145 1.2 pooka _rump_fakeblk_find(const char *path)
146 1.2 pooka {
147 1.2 pooka char buf[MAXPATHLEN];
148 1.2 pooka struct fakeblk *fblk;
149 1.2 pooka
150 1.2 pooka rumpuser_realpath(path, buf);
151 1.2 pooka
152 1.2 pooka LIST_FOREACH(fblk, &fakeblks, entries)
153 1.2 pooka if (strcmp(fblk->path, buf) == 0)
154 1.2 pooka return fblk;
155 1.2 pooka
156 1.2 pooka return NULL;
157 1.2 pooka }
158 1.2 pooka
159 1.2 pooka int
160 1.2 pooka rump_fakeblk_register(const char *path)
161 1.2 pooka {
162 1.2 pooka char buf[MAXPATHLEN];
163 1.2 pooka struct fakeblk *fblk;
164 1.2 pooka
165 1.2 pooka if (_rump_fakeblk_find(path))
166 1.2 pooka return EEXIST;
167 1.2 pooka
168 1.2 pooka fblk = rumpuser_malloc(sizeof(struct fakeblk), 1);
169 1.2 pooka if (fblk == NULL)
170 1.2 pooka return ENOMEM;
171 1.2 pooka
172 1.2 pooka strlcpy(fblk->path, rumpuser_realpath(path, buf), MAXPATHLEN);
173 1.2 pooka LIST_INSERT_HEAD(&fakeblks, fblk, entries);
174 1.2 pooka
175 1.2 pooka return 0;
176 1.2 pooka }
177 1.2 pooka
178 1.2 pooka int
179 1.2 pooka rump_fakeblk_find(const char *path)
180 1.2 pooka {
181 1.2 pooka
182 1.2 pooka return _rump_fakeblk_find(path) != NULL;
183 1.2 pooka }
184 1.2 pooka
185 1.2 pooka void
186 1.2 pooka rump_fakeblk_deregister(const char *path)
187 1.2 pooka {
188 1.2 pooka struct fakeblk *fblk;
189 1.2 pooka
190 1.2 pooka fblk = _rump_fakeblk_find(path);
191 1.2 pooka if (fblk == NULL)
192 1.2 pooka panic("%s: invalid path \"%s\"", __func__, path);
193 1.2 pooka
194 1.2 pooka LIST_REMOVE(fblk, entries);
195 1.2 pooka rumpuser_free(fblk);
196 1.2 pooka }
197