ptyfs_vfsops.c revision 1.48 1 /* $NetBSD: ptyfs_vfsops.c,v 1.48 2014/03/27 17:31:56 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.48 2014/03/27 17:31:56 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 mount *, struct lwp *, struct vnode **,
76 dev_t, char);
77 static int ptyfs__makename(struct mount *, struct lwp *, char *, size_t,
78 dev_t, char);
79 static void ptyfs__getvattr(struct mount *, 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
105 rv = mp->mnt_stat.f_mntonname;
106 if (cwdi->cwdi_rdir == NULL)
107 return rv;
108
109 buf = malloc(MAXBUF, M_TEMP, M_WAITOK);
110 bp = buf + MAXBUF;
111 *--bp = '\0';
112 error = getcwd_common(mp->mnt_vnodecovered, cwdi->cwdi_rdir, &bp,
113 buf, MAXBUF / 2, 0, l);
114 if (error) { /* Mount point is out of rdir */
115 rv = NULL;
116 goto out;
117 }
118
119 len = strlen(bp);
120 if (len < sizeof(mp->mnt_stat.f_mntonname)) /* XXX */
121 rv += strlen(rv) - len;
122 out:
123 free(buf, M_TEMP);
124 return rv;
125 }
126
127 static int
128 ptyfs__makename(struct mount *mp, struct lwp *l, char *tbuf, size_t bufsiz,
129 dev_t dev, char ms)
130 {
131 size_t len;
132 const char *np;
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 np = ptyfs__getpath(l, mp);
141 if (np == NULL)
142 return EOPNOTSUPP;
143 len = snprintf(tbuf, bufsiz, "%s/%llu", np,
144 (unsigned long long)minor(dev));
145 break;
146 default:
147 return EINVAL;
148 }
149
150 return len >= bufsiz ? ENOSPC : 0;
151 }
152
153
154 static int
155 /*ARGSUSED*/
156 ptyfs__allocvp(struct mount *mp, struct lwp *l, struct vnode **vpp,
157 dev_t dev, char ms)
158 {
159 ptyfstype type;
160
161 switch (ms) {
162 case 'p':
163 type = PTYFSptc;
164 break;
165 case 't':
166 type = PTYFSpts;
167 break;
168 default:
169 return EINVAL;
170 }
171
172 return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
173 }
174
175
176 static void
177 ptyfs__getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr)
178 {
179 struct ptyfsmount *pmnt = VFSTOPTY(mp);
180 vattr_null(vattr);
181 /* get real uid */
182 vattr->va_uid = kauth_cred_getuid(l->l_cred);
183 vattr->va_gid = pmnt->pmnt_gid;
184 vattr->va_mode = pmnt->pmnt_mode;
185 }
186
187
188 void
189 ptyfs_init(void)
190 {
191
192 malloc_type_attach(M_PTYFSMNT);
193 malloc_type_attach(M_PTYFSTMP);
194 ptyfs_hashinit();
195 }
196
197 void
198 ptyfs_reinit(void)
199 {
200 ptyfs_hashreinit();
201 }
202
203 void
204 ptyfs_done(void)
205 {
206
207 ptyfs_hashdone();
208 malloc_type_detach(M_PTYFSTMP);
209 malloc_type_detach(M_PTYFSMNT);
210 }
211
212 #define OSIZE sizeof(struct { int f; gid_t g; mode_t m; })
213 /*
214 * Mount the Pseudo tty params filesystem
215 */
216 int
217 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
218 {
219 struct lwp *l = curlwp;
220 int error = 0;
221 struct ptyfsmount *pmnt;
222 struct ptyfs_args *args = data;
223
224 if (*data_len != sizeof *args && *data_len != OSIZE)
225 return EINVAL;
226
227 if (UIO_MX & (UIO_MX - 1)) {
228 log(LOG_ERR, "ptyfs: invalid directory entry size");
229 return EINVAL;
230 }
231
232 if (mp->mnt_flag & MNT_GETARGS) {
233 pmnt = VFSTOPTY(mp);
234 if (pmnt == NULL)
235 return EIO;
236 args->mode = pmnt->pmnt_mode;
237 args->gid = pmnt->pmnt_gid;
238 if (args->version >= PTYFS_ARGSVERSION) {
239 args->flags = pmnt->pmnt_flags;
240 *data_len = sizeof *args;
241 } else {
242 *data_len = OSIZE;
243 }
244 return 0;
245 }
246
247 #if 0
248 /* Don't allow more than one mount */
249 if (ptyfs_count)
250 return EBUSY;
251 #endif
252
253 if (mp->mnt_flag & MNT_UPDATE)
254 return EOPNOTSUPP;
255
256 if (args->version > PTYFS_ARGSVERSION)
257 return EINVAL;
258
259 pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
260
261 mp->mnt_data = pmnt;
262 pmnt->pmnt_gid = args->gid;
263 pmnt->pmnt_mode = args->mode;
264 if (args->version >= PTYFS_ARGSVERSION)
265 pmnt->pmnt_flags = args->flags;
266 else
267 pmnt->pmnt_flags = 0;
268 mp->mnt_flag |= MNT_LOCAL;
269 vfs_getnewfsid(mp);
270
271 if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
272 UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
273 free(pmnt, M_PTYFSMNT);
274 return error;
275 }
276
277 /* Point pty access to us */
278 if (ptyfs_count == 0) {
279 ptm_ptyfspty.arg = mp;
280 ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
281 }
282 ptyfs_count++;
283 return 0;
284 }
285
286 /*ARGSUSED*/
287 int
288 ptyfs_start(struct mount *mp, int flags)
289 {
290 return 0;
291 }
292
293 /*ARGSUSED*/
294 int
295 ptyfs_unmount(struct mount *mp, int mntflags)
296 {
297 int error;
298 int flags = 0;
299
300 if (mntflags & MNT_FORCE)
301 flags |= FORCECLOSE;
302
303 if ((error = vflush(mp, 0, flags)) != 0)
304 return error;
305
306 ptyfs_count--;
307 if (ptyfs_count == 0) {
308 /* Restore where pty access was pointing */
309 (void)pty_sethandler(ptyfs_save_ptm);
310 ptyfs_save_ptm = NULL;
311 ptm_ptyfspty.arg = NULL;
312 }
313
314 /*
315 * Finally, throw away the ptyfsmount structure
316 */
317 free(mp->mnt_data, M_PTYFSMNT);
318 mp->mnt_data = NULL;
319
320 return 0;
321 }
322
323 int
324 ptyfs_root(struct mount *mp, struct vnode **vpp)
325 {
326 /* setup "." */
327 return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
328 }
329
330 /*ARGSUSED*/
331 int
332 ptyfs_sync(struct mount *mp, int waitfor,
333 kauth_cred_t uc)
334 {
335 return 0;
336 }
337
338 /*
339 * Kernfs flat namespace lookup.
340 * Currently unsupported.
341 */
342 /*ARGSUSED*/
343 int
344 ptyfs_vget(struct mount *mp, ino_t ino,
345 struct vnode **vpp)
346 {
347 return EOPNOTSUPP;
348 }
349
350 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
351
352 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
353 &ptyfs_vnodeop_opv_desc,
354 NULL,
355 };
356
357 struct vfsops ptyfs_vfsops = {
358 .vfs_name = MOUNT_PTYFS,
359 .vfs_min_mount_data = sizeof (struct ptyfs_args),
360 .vfs_mount = ptyfs_mount,
361 .vfs_start = ptyfs_start,
362 .vfs_unmount = ptyfs_unmount,
363 .vfs_root = ptyfs_root,
364 .vfs_quotactl = (void *)eopnotsupp,
365 .vfs_statvfs = genfs_statvfs,
366 .vfs_sync = ptyfs_sync,
367 .vfs_vget = ptyfs_vget,
368 .vfs_fhtovp = (void *)eopnotsupp,
369 .vfs_vptofh = (void *)eopnotsupp,
370 .vfs_init = ptyfs_init,
371 .vfs_reinit = ptyfs_reinit,
372 .vfs_done = ptyfs_done,
373 .vfs_snapshot = (void *)eopnotsupp,
374 .vfs_extattrctl = (void *)eopnotsupp,
375 .vfs_suspendctl = (void *)eopnotsupp,
376 .vfs_renamelock_enter = genfs_renamelock_enter,
377 .vfs_renamelock_exit = genfs_renamelock_exit,
378 .vfs_fsync = (void *)eopnotsupp,
379 .vfs_opv_descs = ptyfs_vnodeopv_descs
380 };
381
382 static int
383 ptyfs_modcmd(modcmd_t cmd, void *arg)
384 {
385 int error;
386
387 switch (cmd) {
388 case MODULE_CMD_INIT:
389 error = vfs_attach(&ptyfs_vfsops);
390 if (error != 0)
391 break;
392 sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
393 CTLFLAG_PERMANENT,
394 CTLTYPE_NODE, "ptyfs",
395 SYSCTL_DESCR("Pty file system"),
396 NULL, 0, NULL, 0,
397 CTL_VFS, 23, CTL_EOL);
398 /*
399 * XXX the "23" above could be dynamic, thereby eliminating
400 * one more instance of the "number to vfs" mapping problem,
401 * but "23" is the order as taken from sys/mount.h
402 */
403 break;
404 case MODULE_CMD_FINI:
405 error = vfs_detach(&ptyfs_vfsops);
406 if (error != 0)
407 break;
408 sysctl_teardown(&ptyfs_sysctl_log);
409 break;
410 default:
411 error = ENOTTY;
412 break;
413 }
414
415 return (error);
416 }
417