ptyfs_vfsops.c revision 1.44 1 1.44 christos /* $NetBSD: ptyfs_vfsops.c,v 1.44 2012/09/18 23:42:52 christos Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1992, 1993, 1995
5 1.1 jdolecek * The Regents of the University of California. All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * This code is derived from software donated to Berkeley by
8 1.1 jdolecek * Jan-Simon Pendry.
9 1.1 jdolecek *
10 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.1 jdolecek * modification, are permitted provided that the following conditions
12 1.1 jdolecek * are met:
13 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.1 jdolecek * documentation and/or other materials provided with the distribution.
18 1.1 jdolecek * 3. Neither the name of the University nor the names of its contributors
19 1.1 jdolecek * may be used to endorse or promote products derived from this software
20 1.1 jdolecek * without specific prior written permission.
21 1.1 jdolecek *
22 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 jdolecek * SUCH DAMAGE.
33 1.1 jdolecek *
34 1.1 jdolecek */
35 1.1 jdolecek
36 1.1 jdolecek /*
37 1.1 jdolecek * Pseudo-tty Filesystem
38 1.1 jdolecek */
39 1.1 jdolecek
40 1.1 jdolecek #include <sys/cdefs.h>
41 1.44 christos __KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.44 2012/09/18 23:42:52 christos Exp $");
42 1.1 jdolecek
43 1.1 jdolecek #include <sys/param.h>
44 1.1 jdolecek #include <sys/systm.h>
45 1.1 jdolecek #include <sys/sysctl.h>
46 1.1 jdolecek #include <sys/conf.h>
47 1.1 jdolecek #include <sys/proc.h>
48 1.1 jdolecek #include <sys/vnode.h>
49 1.1 jdolecek #include <sys/mount.h>
50 1.1 jdolecek #include <sys/namei.h>
51 1.3 christos #include <sys/stat.h>
52 1.1 jdolecek #include <sys/dirent.h>
53 1.1 jdolecek #include <sys/malloc.h>
54 1.1 jdolecek #include <sys/syslog.h>
55 1.1 jdolecek #include <sys/select.h>
56 1.13 christos #include <sys/filedesc.h>
57 1.1 jdolecek #include <sys/tty.h>
58 1.1 jdolecek #include <sys/pty.h>
59 1.15 elad #include <sys/kauth.h>
60 1.33 rumble #include <sys/module.h>
61 1.1 jdolecek
62 1.2 jdolecek #include <fs/ptyfs/ptyfs.h>
63 1.31 dholland #include <miscfs/genfs/genfs.h>
64 1.1 jdolecek #include <miscfs/specfs/specdev.h>
65 1.1 jdolecek
66 1.33 rumble MODULE(MODULE_CLASS_VFS, ptyfs, NULL);
67 1.33 rumble
68 1.24 pooka MALLOC_JUSTDEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
69 1.29 rumble MALLOC_JUSTDEFINE(M_PTYFSTMP, "ptyfs temp", "ptyfs temporary structures");
70 1.1 jdolecek
71 1.28 pooka VFS_PROTOS(ptyfs);
72 1.1 jdolecek
73 1.36 rumble static struct sysctllog *ptyfs_sysctl_log;
74 1.36 rumble
75 1.12 christos static int ptyfs__allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
76 1.1 jdolecek dev_t, char);
77 1.13 christos static int ptyfs__makename(struct ptm_pty *, struct lwp *, char *, size_t,
78 1.13 christos dev_t, char);
79 1.18 ad static void ptyfs__getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
80 1.1 jdolecek
81 1.1 jdolecek /*
82 1.1 jdolecek * ptm glue: When we mount, we make ptm point to us.
83 1.1 jdolecek */
84 1.1 jdolecek struct ptm_pty *ptyfs_save_ptm;
85 1.11 christos static int ptyfs_count;
86 1.1 jdolecek
87 1.1 jdolecek struct ptm_pty ptm_ptyfspty = {
88 1.1 jdolecek ptyfs__allocvp,
89 1.1 jdolecek ptyfs__makename,
90 1.3 christos ptyfs__getvattr,
91 1.1 jdolecek NULL
92 1.1 jdolecek };
93 1.1 jdolecek
94 1.39 christos static const char *
95 1.39 christos ptyfs__getpath(struct lwp *l, const struct mount *mp)
96 1.39 christos {
97 1.39 christos #define MAXBUF (sizeof(mp->mnt_stat.f_mntonname) + 32)
98 1.39 christos struct cwdinfo *cwdi = l->l_proc->p_cwdi;
99 1.39 christos char *buf;
100 1.39 christos const char *rv;
101 1.39 christos size_t len;
102 1.39 christos char *bp;
103 1.39 christos int error;
104 1.39 christos
105 1.39 christos rv = mp->mnt_stat.f_mntonname;
106 1.43 christos if (cwdi->cwdi_rdir == NULL)
107 1.39 christos return rv;
108 1.39 christos
109 1.39 christos buf = malloc(MAXBUF, M_TEMP, M_WAITOK);
110 1.39 christos bp = buf + MAXBUF;
111 1.39 christos *--bp = '\0';
112 1.39 christos error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
113 1.39 christos buf, MAXBUF / 2, 0, l);
114 1.39 christos if (error) /* XXX */
115 1.39 christos goto out;
116 1.39 christos
117 1.39 christos len = strlen(bp);
118 1.39 christos if (len < sizeof(mp->mnt_stat.f_mntonname)) /* XXX */
119 1.39 christos rv += len;
120 1.39 christos out:
121 1.39 christos free(buf, M_TEMP);
122 1.39 christos return rv;
123 1.39 christos }
124 1.39 christos
125 1.1 jdolecek static int
126 1.13 christos ptyfs__makename(struct ptm_pty *pt, struct lwp *l, char *tbuf, size_t bufsiz,
127 1.13 christos dev_t dev, char ms)
128 1.1 jdolecek {
129 1.1 jdolecek struct mount *mp = pt->arg;
130 1.1 jdolecek size_t len;
131 1.1 jdolecek
132 1.1 jdolecek switch (ms) {
133 1.1 jdolecek case 'p':
134 1.1 jdolecek /* We don't provide access to the master, should we? */
135 1.8 christos len = snprintf(tbuf, bufsiz, "/dev/null");
136 1.1 jdolecek break;
137 1.1 jdolecek case 't':
138 1.39 christos len = snprintf(tbuf, bufsiz, "%s/%llu", ptyfs__getpath(l, mp),
139 1.38 christos (unsigned long long)minor(dev));
140 1.1 jdolecek break;
141 1.1 jdolecek default:
142 1.1 jdolecek return EINVAL;
143 1.1 jdolecek }
144 1.1 jdolecek
145 1.1 jdolecek return len >= bufsiz ? ENOSPC : 0;
146 1.1 jdolecek }
147 1.1 jdolecek
148 1.3 christos
149 1.1 jdolecek static int
150 1.1 jdolecek /*ARGSUSED*/
151 1.12 christos ptyfs__allocvp(struct ptm_pty *pt, struct lwp *l, struct vnode **vpp,
152 1.1 jdolecek dev_t dev, char ms)
153 1.1 jdolecek {
154 1.1 jdolecek struct mount *mp = pt->arg;
155 1.1 jdolecek ptyfstype type;
156 1.1 jdolecek
157 1.1 jdolecek switch (ms) {
158 1.1 jdolecek case 'p':
159 1.1 jdolecek type = PTYFSptc;
160 1.1 jdolecek break;
161 1.1 jdolecek case 't':
162 1.1 jdolecek type = PTYFSpts;
163 1.1 jdolecek break;
164 1.1 jdolecek default:
165 1.1 jdolecek return EINVAL;
166 1.1 jdolecek }
167 1.1 jdolecek
168 1.12 christos return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
169 1.1 jdolecek }
170 1.1 jdolecek
171 1.3 christos
172 1.3 christos static void
173 1.18 ad ptyfs__getvattr(struct ptm_pty *pt, struct lwp *l, struct vattr *vattr)
174 1.3 christos {
175 1.3 christos struct mount *mp = pt->arg;
176 1.3 christos struct ptyfsmount *pmnt = VFSTOPTY(mp);
177 1.42 pooka vattr_null(vattr);
178 1.3 christos /* get real uid */
179 1.18 ad vattr->va_uid = kauth_cred_getuid(l->l_cred);
180 1.3 christos vattr->va_gid = pmnt->pmnt_gid;
181 1.3 christos vattr->va_mode = pmnt->pmnt_mode;
182 1.3 christos }
183 1.3 christos
184 1.3 christos
185 1.1 jdolecek void
186 1.1 jdolecek ptyfs_init(void)
187 1.1 jdolecek {
188 1.24 pooka
189 1.1 jdolecek malloc_type_attach(M_PTYFSMNT);
190 1.29 rumble malloc_type_attach(M_PTYFSTMP);
191 1.1 jdolecek ptyfs_hashinit();
192 1.1 jdolecek }
193 1.1 jdolecek
194 1.1 jdolecek void
195 1.1 jdolecek ptyfs_reinit(void)
196 1.1 jdolecek {
197 1.1 jdolecek ptyfs_hashreinit();
198 1.1 jdolecek }
199 1.1 jdolecek
200 1.1 jdolecek void
201 1.1 jdolecek ptyfs_done(void)
202 1.1 jdolecek {
203 1.24 pooka
204 1.24 pooka ptyfs_hashdone();
205 1.29 rumble malloc_type_detach(M_PTYFSTMP);
206 1.1 jdolecek malloc_type_detach(M_PTYFSMNT);
207 1.1 jdolecek }
208 1.1 jdolecek
209 1.40 christos #define OSIZE sizeof(struct { int f; gid_t g; mode_t m; })
210 1.1 jdolecek /*
211 1.1 jdolecek * Mount the Pseudo tty params filesystem
212 1.1 jdolecek */
213 1.1 jdolecek int
214 1.30 pooka ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
215 1.1 jdolecek {
216 1.30 pooka struct lwp *l = curlwp;
217 1.1 jdolecek int error = 0;
218 1.3 christos struct ptyfsmount *pmnt;
219 1.25 dsl struct ptyfs_args *args = data;
220 1.25 dsl
221 1.40 christos if (*data_len != sizeof *args && *data_len != OSIZE)
222 1.25 dsl return EINVAL;
223 1.1 jdolecek
224 1.1 jdolecek if (UIO_MX & (UIO_MX - 1)) {
225 1.1 jdolecek log(LOG_ERR, "ptyfs: invalid directory entry size");
226 1.1 jdolecek return EINVAL;
227 1.1 jdolecek }
228 1.1 jdolecek
229 1.3 christos if (mp->mnt_flag & MNT_GETARGS) {
230 1.3 christos pmnt = VFSTOPTY(mp);
231 1.3 christos if (pmnt == NULL)
232 1.3 christos return EIO;
233 1.25 dsl args->mode = pmnt->pmnt_mode;
234 1.25 dsl args->gid = pmnt->pmnt_gid;
235 1.39 christos if (args->version >= PTYFS_ARGSVERSION) {
236 1.39 christos args->flags = pmnt->pmnt_flags;
237 1.39 christos *data_len = sizeof *args;
238 1.39 christos } else {
239 1.40 christos *data_len = OSIZE;
240 1.39 christos }
241 1.25 dsl return 0;
242 1.3 christos }
243 1.3 christos
244 1.44 christos #if 0
245 1.14 christos /* Don't allow more than one mount */
246 1.14 christos if (ptyfs_count)
247 1.14 christos return EBUSY;
248 1.44 christos #endif
249 1.14 christos
250 1.1 jdolecek if (mp->mnt_flag & MNT_UPDATE)
251 1.1 jdolecek return EOPNOTSUPP;
252 1.1 jdolecek
253 1.40 christos if (args->version > PTYFS_ARGSVERSION)
254 1.7 christos return EINVAL;
255 1.3 christos
256 1.29 rumble pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
257 1.3 christos
258 1.3 christos mp->mnt_data = pmnt;
259 1.25 dsl pmnt->pmnt_gid = args->gid;
260 1.25 dsl pmnt->pmnt_mode = args->mode;
261 1.39 christos if (args->version >= PTYFS_ARGSVERSION)
262 1.39 christos pmnt->pmnt_flags = args->flags;
263 1.39 christos else
264 1.39 christos pmnt->pmnt_flags = 0;
265 1.1 jdolecek mp->mnt_flag |= MNT_LOCAL;
266 1.1 jdolecek vfs_getnewfsid(mp);
267 1.1 jdolecek
268 1.1 jdolecek if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
269 1.26 pooka UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
270 1.34 simonb free(pmnt, M_PTYFSMNT);
271 1.1 jdolecek return error;
272 1.1 jdolecek }
273 1.1 jdolecek
274 1.1 jdolecek /* Point pty access to us */
275 1.44 christos if (ptyfs_count == 0) {
276 1.44 christos ptm_ptyfspty.arg = mp;
277 1.44 christos ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
278 1.44 christos }
279 1.11 christos ptyfs_count++;
280 1.1 jdolecek return 0;
281 1.1 jdolecek }
282 1.1 jdolecek
283 1.1 jdolecek /*ARGSUSED*/
284 1.1 jdolecek int
285 1.30 pooka ptyfs_start(struct mount *mp, int flags)
286 1.1 jdolecek {
287 1.1 jdolecek return 0;
288 1.1 jdolecek }
289 1.1 jdolecek
290 1.1 jdolecek /*ARGSUSED*/
291 1.1 jdolecek int
292 1.30 pooka ptyfs_unmount(struct mount *mp, int mntflags)
293 1.1 jdolecek {
294 1.1 jdolecek int error;
295 1.1 jdolecek int flags = 0;
296 1.1 jdolecek
297 1.1 jdolecek if (mntflags & MNT_FORCE)
298 1.1 jdolecek flags |= FORCECLOSE;
299 1.1 jdolecek
300 1.1 jdolecek if ((error = vflush(mp, 0, flags)) != 0)
301 1.44 christos return error;
302 1.1 jdolecek
303 1.44 christos ptyfs_count--;
304 1.44 christos if (ptyfs_count == 0) {
305 1.44 christos /* Restore where pty access was pointing */
306 1.44 christos (void)pty_sethandler(ptyfs_save_ptm);
307 1.44 christos ptyfs_save_ptm = NULL;
308 1.44 christos ptm_ptyfspty.arg = NULL;
309 1.44 christos }
310 1.3 christos
311 1.1 jdolecek /*
312 1.3 christos * Finally, throw away the ptyfsmount structure
313 1.1 jdolecek */
314 1.34 simonb free(mp->mnt_data, M_PTYFSMNT);
315 1.35 simonb mp->mnt_data = NULL;
316 1.3 christos
317 1.1 jdolecek return 0;
318 1.1 jdolecek }
319 1.1 jdolecek
320 1.1 jdolecek int
321 1.1 jdolecek ptyfs_root(struct mount *mp, struct vnode **vpp)
322 1.1 jdolecek {
323 1.1 jdolecek /* setup "." */
324 1.1 jdolecek return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
325 1.1 jdolecek }
326 1.1 jdolecek
327 1.1 jdolecek /*ARGSUSED*/
328 1.1 jdolecek int
329 1.21 christos ptyfs_sync(struct mount *mp, int waitfor,
330 1.30 pooka kauth_cred_t uc)
331 1.1 jdolecek {
332 1.1 jdolecek return 0;
333 1.1 jdolecek }
334 1.1 jdolecek
335 1.1 jdolecek /*
336 1.1 jdolecek * Kernfs flat namespace lookup.
337 1.1 jdolecek * Currently unsupported.
338 1.1 jdolecek */
339 1.1 jdolecek /*ARGSUSED*/
340 1.1 jdolecek int
341 1.21 christos ptyfs_vget(struct mount *mp, ino_t ino,
342 1.21 christos struct vnode **vpp)
343 1.1 jdolecek {
344 1.1 jdolecek return EOPNOTSUPP;
345 1.1 jdolecek }
346 1.1 jdolecek
347 1.1 jdolecek extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
348 1.1 jdolecek
349 1.1 jdolecek const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
350 1.1 jdolecek &ptyfs_vnodeop_opv_desc,
351 1.1 jdolecek NULL,
352 1.1 jdolecek };
353 1.1 jdolecek
354 1.1 jdolecek struct vfsops ptyfs_vfsops = {
355 1.1 jdolecek MOUNT_PTYFS,
356 1.25 dsl sizeof (struct ptyfs_args),
357 1.1 jdolecek ptyfs_mount,
358 1.1 jdolecek ptyfs_start,
359 1.1 jdolecek ptyfs_unmount,
360 1.1 jdolecek ptyfs_root,
361 1.30 pooka (void *)eopnotsupp, /* vfs_quotactl */
362 1.41 pooka genfs_statvfs,
363 1.1 jdolecek ptyfs_sync,
364 1.1 jdolecek ptyfs_vget,
365 1.22 chs (void *)eopnotsupp, /* vfs_fhtovp */
366 1.22 chs (void *)eopnotsupp, /* vfs_vptofp */
367 1.1 jdolecek ptyfs_init,
368 1.1 jdolecek ptyfs_reinit,
369 1.1 jdolecek ptyfs_done,
370 1.1 jdolecek NULL, /* vfs_mountroot */
371 1.30 pooka (void *)eopnotsupp,
372 1.30 pooka (void *)eopnotsupp,
373 1.27 pooka (void *)eopnotsupp, /* vfs_suspendctl */
374 1.31 dholland genfs_renamelock_enter,
375 1.31 dholland genfs_renamelock_exit,
376 1.32 ad (void *)eopnotsupp,
377 1.1 jdolecek ptyfs_vnodeopv_descs,
378 1.19 christos 0,
379 1.19 christos { NULL, NULL },
380 1.1 jdolecek };
381 1.33 rumble
382 1.33 rumble static int
383 1.33 rumble ptyfs_modcmd(modcmd_t cmd, void *arg)
384 1.33 rumble {
385 1.36 rumble int error;
386 1.33 rumble
387 1.33 rumble switch (cmd) {
388 1.33 rumble case MODULE_CMD_INIT:
389 1.36 rumble error = vfs_attach(&ptyfs_vfsops);
390 1.36 rumble if (error != 0)
391 1.36 rumble break;
392 1.36 rumble sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
393 1.36 rumble CTLFLAG_PERMANENT,
394 1.36 rumble CTLTYPE_NODE, "vfs", NULL,
395 1.36 rumble NULL, 0, NULL, 0,
396 1.36 rumble CTL_VFS, CTL_EOL);
397 1.36 rumble sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
398 1.36 rumble CTLFLAG_PERMANENT,
399 1.36 rumble CTLTYPE_NODE, "ptyfs",
400 1.36 rumble SYSCTL_DESCR("Pty file system"),
401 1.36 rumble NULL, 0, NULL, 0,
402 1.36 rumble CTL_VFS, 23, CTL_EOL);
403 1.36 rumble /*
404 1.36 rumble * XXX the "23" above could be dynamic, thereby eliminating
405 1.36 rumble * one more instance of the "number to vfs" mapping problem,
406 1.36 rumble * but "23" is the order as taken from sys/mount.h
407 1.36 rumble */
408 1.36 rumble break;
409 1.33 rumble case MODULE_CMD_FINI:
410 1.36 rumble error = vfs_detach(&ptyfs_vfsops);
411 1.36 rumble if (error != 0)
412 1.36 rumble break;
413 1.36 rumble sysctl_teardown(&ptyfs_sysctl_log);
414 1.36 rumble break;
415 1.33 rumble default:
416 1.36 rumble error = ENOTTY;
417 1.36 rumble break;
418 1.33 rumble }
419 1.36 rumble
420 1.36 rumble return (error);
421 1.33 rumble }
422