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