xref: /netbsd-src/sys/fs/ptyfs/ptyfs_vfsops.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: ptyfs_vfsops.c,v 1.32 2008/04/29 18:18:08 ad 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.32 2008/04/29 18:18:08 ad 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 
61 #include <fs/ptyfs/ptyfs.h>
62 #include <miscfs/genfs/genfs.h>
63 #include <miscfs/specfs/specdev.h>
64 
65 MALLOC_JUSTDEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
66 MALLOC_JUSTDEFINE(M_PTYFSTMP, "ptyfs temp", "ptyfs temporary structures");
67 
68 VFS_PROTOS(ptyfs);
69 
70 static int ptyfs__allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
71     dev_t, char);
72 static int ptyfs__makename(struct ptm_pty *, struct lwp *, char *, size_t,
73     dev_t, char);
74 static void ptyfs__getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
75 
76 /*
77  * ptm glue: When we mount, we make ptm point to us.
78  */
79 struct ptm_pty *ptyfs_save_ptm;
80 static int ptyfs_count;
81 
82 struct ptm_pty ptm_ptyfspty = {
83 	ptyfs__allocvp,
84 	ptyfs__makename,
85 	ptyfs__getvattr,
86 	NULL
87 };
88 
89 static const char *
90 ptyfs__getpath(struct lwp *l, const struct mount *mp)
91 {
92 #define MAXBUF (sizeof(mp->mnt_stat.f_mntonname) + 32)
93 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
94 	char *buf;
95 	const char *rv;
96 	size_t len;
97 	char *bp;
98 	int error;
99 
100 	rv = mp->mnt_stat.f_mntonname;
101 	if (cwdi->cwdi_rdir == NULL)
102 		return rv;
103 
104 	buf = malloc(MAXBUF, M_TEMP, M_WAITOK);
105 	bp = buf + MAXBUF;
106 	*--bp = '\0';
107 	error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
108 	    buf, MAXBUF / 2, 0, l);
109 	if (error)	/* XXX */
110 		goto out;
111 
112 	len = strlen(bp);
113 	if (len < sizeof(mp->mnt_stat.f_mntonname))	/* XXX */
114 		rv += len;
115 out:
116 	free(buf, M_TEMP);
117 	return rv;
118 }
119 
120 static int
121 ptyfs__makename(struct ptm_pty *pt, struct lwp *l, char *tbuf, size_t bufsiz,
122     dev_t dev, char ms)
123 {
124 	struct mount *mp = pt->arg;
125 	size_t len;
126 
127 	switch (ms) {
128 	case 'p':
129 		/* We don't provide access to the master, should we? */
130 		len = snprintf(tbuf, bufsiz, "/dev/null");
131 		break;
132 	case 't':
133 		len = snprintf(tbuf, bufsiz, "%s/%d", ptyfs__getpath(l, mp),
134 		    minor(dev));
135 		break;
136 	default:
137 		return EINVAL;
138 	}
139 
140 	return len >= bufsiz ? ENOSPC : 0;
141 }
142 
143 
144 static int
145 /*ARGSUSED*/
146 ptyfs__allocvp(struct ptm_pty *pt, struct lwp *l, struct vnode **vpp,
147     dev_t dev, char ms)
148 {
149 	struct mount *mp = pt->arg;
150 	ptyfstype type;
151 
152 	switch (ms) {
153 	case 'p':
154 		type = PTYFSptc;
155 		break;
156 	case 't':
157 		type = PTYFSpts;
158 		break;
159 	default:
160 		return EINVAL;
161 	}
162 
163 	return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
164 }
165 
166 
167 static void
168 ptyfs__getvattr(struct ptm_pty *pt, struct lwp *l, struct vattr *vattr)
169 {
170 	struct mount *mp = pt->arg;
171 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
172 	VATTR_NULL(vattr);
173 	/* get real uid */
174 	vattr->va_uid = kauth_cred_getuid(l->l_cred);
175 	vattr->va_gid = pmnt->pmnt_gid;
176 	vattr->va_mode = pmnt->pmnt_mode;
177 }
178 
179 
180 void
181 ptyfs_init(void)
182 {
183 
184 	malloc_type_attach(M_PTYFSMNT);
185 	malloc_type_attach(M_PTYFSTMP);
186 	ptyfs_hashinit();
187 }
188 
189 void
190 ptyfs_reinit(void)
191 {
192 	ptyfs_hashreinit();
193 }
194 
195 void
196 ptyfs_done(void)
197 {
198 
199 	ptyfs_hashdone();
200 	malloc_type_detach(M_PTYFSTMP);
201 	malloc_type_detach(M_PTYFSMNT);
202 }
203 
204 /*
205  * Mount the Pseudo tty params filesystem
206  */
207 int
208 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
209 {
210 	struct lwp *l = curlwp;
211 	int error = 0;
212 	struct ptyfsmount *pmnt;
213 	struct ptyfs_args *args = data;
214 
215 	if (*data_len < sizeof *args)
216 		return EINVAL;
217 
218 	if (UIO_MX & (UIO_MX - 1)) {
219 		log(LOG_ERR, "ptyfs: invalid directory entry size");
220 		return EINVAL;
221 	}
222 
223 	if (mp->mnt_flag & MNT_GETARGS) {
224 		pmnt = VFSTOPTY(mp);
225 		if (pmnt == NULL)
226 			return EIO;
227 		args->version = PTYFS_ARGSVERSION;
228 		args->mode = pmnt->pmnt_mode;
229 		args->gid = pmnt->pmnt_gid;
230 		*data_len = sizeof *args;
231 		return 0;
232 	}
233 
234 	/* Don't allow more than one mount */
235 	if (ptyfs_count)
236 		return EBUSY;
237 
238 	if (mp->mnt_flag & MNT_UPDATE)
239 		return EOPNOTSUPP;
240 
241 	if (args->version != PTYFS_ARGSVERSION)
242 		return EINVAL;
243 
244 	pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
245 
246 	mp->mnt_data = pmnt;
247 	pmnt->pmnt_gid = args->gid;
248 	pmnt->pmnt_mode = args->mode;
249 	mp->mnt_flag |= MNT_LOCAL;
250 	vfs_getnewfsid(mp);
251 
252 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
253 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
254 		free(pmnt, M_UFSMNT);
255 		return error;
256 	}
257 
258 	/* Point pty access to us */
259 
260 	ptm_ptyfspty.arg = mp;
261 	ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
262 	ptyfs_count++;
263 	return 0;
264 }
265 
266 /*ARGSUSED*/
267 int
268 ptyfs_start(struct mount *mp, int flags)
269 {
270 	return 0;
271 }
272 
273 /*ARGSUSED*/
274 int
275 ptyfs_unmount(struct mount *mp, int mntflags)
276 {
277 	int error;
278 	int flags = 0;
279 
280 	if (mntflags & MNT_FORCE)
281 		flags |= FORCECLOSE;
282 
283 	if ((error = vflush(mp, 0, flags)) != 0)
284 		return (error);
285 
286 	/* Restore where pty access was pointing */
287 	(void)pty_sethandler(ptyfs_save_ptm);
288 	ptyfs_save_ptm = NULL;
289 	ptm_ptyfspty.arg = NULL;
290 
291 	/*
292 	 * Finally, throw away the ptyfsmount structure
293 	 */
294 	free(mp->mnt_data, M_UFSMNT);
295 	mp->mnt_data = 0;
296 	ptyfs_count--;
297 
298 	return 0;
299 }
300 
301 int
302 ptyfs_root(struct mount *mp, struct vnode **vpp)
303 {
304 	/* setup "." */
305 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
306 }
307 
308 /*ARGSUSED*/
309 int
310 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp)
311 {
312 	sbp->f_bsize = DEV_BSIZE;
313 	sbp->f_frsize = DEV_BSIZE;
314 	sbp->f_iosize = DEV_BSIZE;
315 	sbp->f_blocks = 2;		/* 1K to keep df happy */
316 	sbp->f_bfree = 0;
317 	sbp->f_bavail = 0;
318 	sbp->f_bresvd = 0;
319 	sbp->f_files = 1024;	/* XXX lie */
320 	sbp->f_ffree = 128;	/* XXX lie */
321 	sbp->f_favail = 128;	/* XXX lie */
322 	sbp->f_fresvd = 0;
323 	sbp->f_namemax = MAXNAMLEN;
324 	copy_statvfs_info(sbp, mp);
325 	return 0;
326 }
327 
328 /*ARGSUSED*/
329 int
330 ptyfs_sync(struct mount *mp, int waitfor,
331     kauth_cred_t uc)
332 {
333 	return 0;
334 }
335 
336 /*
337  * Kernfs flat namespace lookup.
338  * Currently unsupported.
339  */
340 /*ARGSUSED*/
341 int
342 ptyfs_vget(struct mount *mp, ino_t ino,
343     struct vnode **vpp)
344 {
345 	return EOPNOTSUPP;
346 }
347 
348 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
349 {
350 
351 	sysctl_createv(clog, 0, NULL, NULL,
352 		       CTLFLAG_PERMANENT,
353 		       CTLTYPE_NODE, "vfs", NULL,
354 		       NULL, 0, NULL, 0,
355 		       CTL_VFS, CTL_EOL);
356 	sysctl_createv(clog, 0, NULL, NULL,
357 		       CTLFLAG_PERMANENT,
358 		       CTLTYPE_NODE, "ptyfs",
359 		       SYSCTL_DESCR("Pty file system"),
360 		       NULL, 0, NULL, 0,
361 		       CTL_VFS, 23, CTL_EOL);
362 	/*
363 	 * XXX the "23" above could be dynamic, thereby eliminating
364 	 * one more instance of the "number to vfs" mapping problem,
365 	 * but "23" is the order as taken from sys/mount.h
366 	 */
367 }
368 
369 
370 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
371 
372 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
373 	&ptyfs_vnodeop_opv_desc,
374 	NULL,
375 };
376 
377 struct vfsops ptyfs_vfsops = {
378 	MOUNT_PTYFS,
379 	sizeof (struct ptyfs_args),
380 	ptyfs_mount,
381 	ptyfs_start,
382 	ptyfs_unmount,
383 	ptyfs_root,
384 	(void *)eopnotsupp,		/* vfs_quotactl */
385 	ptyfs_statvfs,
386 	ptyfs_sync,
387 	ptyfs_vget,
388 	(void *)eopnotsupp,		/* vfs_fhtovp */
389 	(void *)eopnotsupp,		/* vfs_vptofp */
390 	ptyfs_init,
391 	ptyfs_reinit,
392 	ptyfs_done,
393 	NULL,				/* vfs_mountroot */
394 	(void *)eopnotsupp,
395 	(void *)eopnotsupp,
396 	(void *)eopnotsupp,		/* vfs_suspendctl */
397 	genfs_renamelock_enter,
398 	genfs_renamelock_exit,
399 	(void *)eopnotsupp,
400 	ptyfs_vnodeopv_descs,
401 	0,
402 	{ NULL, NULL },
403 };
404 VFS_ATTACH(ptyfs_vfsops);
405