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