1 | /* $NetBSD: ptyfs_vnops.c,v 1.52 2016/08/20 12:37:07 hannken Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1993, 1995 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
7 | * This code is derived from software contributed 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 | * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 |
35 | */ |
36 | |
37 | /* |
38 | * Copyright (c) 1993 Jan-Simon Pendry |
39 | * |
40 | * This code is derived from software contributed to Berkeley by |
41 | * Jan-Simon Pendry. |
42 | * |
43 | * Redistribution and use in source and binary forms, with or without |
44 | * modification, are permitted provided that the following conditions |
45 | * are met: |
46 | * 1. Redistributions of source code must retain the above copyright |
47 | * notice, this list of conditions and the following disclaimer. |
48 | * 2. Redistributions in binary form must reproduce the above copyright |
49 | * notice, this list of conditions and the following disclaimer in the |
50 | * documentation and/or other materials provided with the distribution. |
51 | * 3. All advertising materials mentioning features or use of this software |
52 | * must display the following acknowledgement: |
53 | * This product includes software developed by the University of |
54 | * California, Berkeley and its contributors. |
55 | * 4. Neither the name of the University nor the names of its contributors |
56 | * may be used to endorse or promote products derived from this software |
57 | * without specific prior written permission. |
58 | * |
59 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
60 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
62 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
63 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
64 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
65 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
66 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
67 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
68 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
69 | * SUCH DAMAGE. |
70 | * |
71 | * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 |
72 | */ |
73 | |
74 | /* |
75 | * ptyfs vnode interface |
76 | */ |
77 | |
78 | #include <sys/cdefs.h> |
79 | __KERNEL_RCSID(0, "$NetBSD: ptyfs_vnops.c,v 1.52 2016/08/20 12:37:07 hannken Exp $" ); |
80 | |
81 | #include <sys/param.h> |
82 | #include <sys/systm.h> |
83 | #include <sys/time.h> |
84 | #include <sys/kernel.h> |
85 | #include <sys/file.h> |
86 | #include <sys/filedesc.h> |
87 | #include <sys/proc.h> |
88 | #include <sys/vnode.h> |
89 | #include <sys/namei.h> |
90 | #include <sys/malloc.h> |
91 | #include <sys/mount.h> |
92 | #include <sys/select.h> |
93 | #include <sys/dirent.h> |
94 | #include <sys/resourcevar.h> |
95 | #include <sys/stat.h> |
96 | #include <sys/conf.h> |
97 | #include <sys/tty.h> |
98 | #include <sys/pty.h> |
99 | #include <sys/kauth.h> |
100 | |
101 | #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ |
102 | |
103 | #include <machine/reg.h> |
104 | |
105 | #include <fs/ptyfs/ptyfs.h> |
106 | #include <miscfs/genfs/genfs.h> |
107 | #include <miscfs/specfs/specdev.h> |
108 | |
109 | MALLOC_DECLARE(M_PTYFSTMP); |
110 | |
111 | /* |
112 | * Vnode Operations. |
113 | * |
114 | */ |
115 | |
116 | int ptyfs_lookup (void *); |
117 | #define ptyfs_create genfs_eopnotsupp |
118 | #define ptyfs_mknod genfs_eopnotsupp |
119 | int ptyfs_open (void *); |
120 | int ptyfs_close (void *); |
121 | int ptyfs_access (void *); |
122 | int ptyfs_getattr (void *); |
123 | int ptyfs_setattr (void *); |
124 | int ptyfs_read (void *); |
125 | int ptyfs_write (void *); |
126 | #define ptyfs_fcntl genfs_fcntl |
127 | int ptyfs_ioctl (void *); |
128 | int ptyfs_poll (void *); |
129 | int ptyfs_kqfilter (void *); |
130 | #define ptyfs_revoke genfs_revoke |
131 | #define ptyfs_mmap genfs_eopnotsupp |
132 | #define ptyfs_fsync genfs_nullop |
133 | #define ptyfs_seek genfs_nullop |
134 | #define ptyfs_remove genfs_eopnotsupp |
135 | #define ptyfs_link genfs_abortop |
136 | #define ptyfs_rename genfs_eopnotsupp |
137 | #define ptyfs_mkdir genfs_eopnotsupp |
138 | #define ptyfs_rmdir genfs_eopnotsupp |
139 | #define ptyfs_symlink genfs_abortop |
140 | int ptyfs_readdir (void *); |
141 | #define ptyfs_readlink genfs_eopnotsupp |
142 | #define ptyfs_abortop genfs_abortop |
143 | int ptyfs_reclaim (void *); |
144 | int ptyfs_inactive (void *); |
145 | #define ptyfs_lock genfs_lock |
146 | #define ptyfs_unlock genfs_unlock |
147 | #define ptyfs_bmap genfs_badop |
148 | #define ptyfs_strategy genfs_badop |
149 | int ptyfs_print (void *); |
150 | int ptyfs_pathconf (void *); |
151 | #define ptyfs_islocked genfs_islocked |
152 | int ptyfs_advlock (void *); |
153 | #define ptyfs_bwrite genfs_eopnotsupp |
154 | #define ptyfs_putpages genfs_null_putpages |
155 | |
156 | static int ptyfs_update(struct vnode *, const struct timespec *, |
157 | const struct timespec *, int); |
158 | static int ptyfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, |
159 | struct lwp *); |
160 | static int ptyfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *); |
161 | static int atoi(const char *, size_t); |
162 | |
163 | /* |
164 | * ptyfs vnode operations. |
165 | */ |
166 | int (**ptyfs_vnodeop_p)(void *); |
167 | const struct vnodeopv_entry_desc ptyfs_vnodeop_entries[] = { |
168 | { &vop_default_desc, vn_default_error }, |
169 | { &vop_lookup_desc, ptyfs_lookup }, /* lookup */ |
170 | { &vop_create_desc, ptyfs_create }, /* create */ |
171 | { &vop_mknod_desc, ptyfs_mknod }, /* mknod */ |
172 | { &vop_open_desc, ptyfs_open }, /* open */ |
173 | { &vop_close_desc, ptyfs_close }, /* close */ |
174 | { &vop_access_desc, ptyfs_access }, /* access */ |
175 | { &vop_getattr_desc, ptyfs_getattr }, /* getattr */ |
176 | { &vop_setattr_desc, ptyfs_setattr }, /* setattr */ |
177 | { &vop_read_desc, ptyfs_read }, /* read */ |
178 | { &vop_write_desc, ptyfs_write }, /* write */ |
179 | { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */ |
180 | { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */ |
181 | { &vop_ioctl_desc, ptyfs_ioctl }, /* ioctl */ |
182 | { &vop_fcntl_desc, ptyfs_fcntl }, /* fcntl */ |
183 | { &vop_poll_desc, ptyfs_poll }, /* poll */ |
184 | { &vop_kqfilter_desc, ptyfs_kqfilter }, /* kqfilter */ |
185 | { &vop_revoke_desc, ptyfs_revoke }, /* revoke */ |
186 | { &vop_mmap_desc, ptyfs_mmap }, /* mmap */ |
187 | { &vop_fsync_desc, ptyfs_fsync }, /* fsync */ |
188 | { &vop_seek_desc, ptyfs_seek }, /* seek */ |
189 | { &vop_remove_desc, ptyfs_remove }, /* remove */ |
190 | { &vop_link_desc, ptyfs_link }, /* link */ |
191 | { &vop_rename_desc, ptyfs_rename }, /* rename */ |
192 | { &vop_mkdir_desc, ptyfs_mkdir }, /* mkdir */ |
193 | { &vop_rmdir_desc, ptyfs_rmdir }, /* rmdir */ |
194 | { &vop_symlink_desc, ptyfs_symlink }, /* symlink */ |
195 | { &vop_readdir_desc, ptyfs_readdir }, /* readdir */ |
196 | { &vop_readlink_desc, ptyfs_readlink }, /* readlink */ |
197 | { &vop_abortop_desc, ptyfs_abortop }, /* abortop */ |
198 | { &vop_inactive_desc, ptyfs_inactive }, /* inactive */ |
199 | { &vop_reclaim_desc, ptyfs_reclaim }, /* reclaim */ |
200 | { &vop_lock_desc, ptyfs_lock }, /* lock */ |
201 | { &vop_unlock_desc, ptyfs_unlock }, /* unlock */ |
202 | { &vop_bmap_desc, ptyfs_bmap }, /* bmap */ |
203 | { &vop_strategy_desc, ptyfs_strategy }, /* strategy */ |
204 | { &vop_print_desc, ptyfs_print }, /* print */ |
205 | { &vop_islocked_desc, ptyfs_islocked }, /* islocked */ |
206 | { &vop_pathconf_desc, ptyfs_pathconf }, /* pathconf */ |
207 | { &vop_advlock_desc, ptyfs_advlock }, /* advlock */ |
208 | { &vop_bwrite_desc, ptyfs_bwrite }, /* bwrite */ |
209 | { &vop_putpages_desc, ptyfs_putpages }, /* putpages */ |
210 | { NULL, NULL } |
211 | }; |
212 | const struct vnodeopv_desc ptyfs_vnodeop_opv_desc = |
213 | { &ptyfs_vnodeop_p, ptyfs_vnodeop_entries }; |
214 | |
215 | /* |
216 | * free any private data and remove the node |
217 | * from any private lists. |
218 | */ |
219 | int |
220 | ptyfs_reclaim(void *v) |
221 | { |
222 | struct vop_reclaim_args /* { |
223 | struct vnode *a_vp; |
224 | } */ *ap = v; |
225 | struct vnode *vp = ap->a_vp; |
226 | |
227 | vp->v_data = NULL; |
228 | return 0; |
229 | } |
230 | |
231 | int |
232 | ptyfs_inactive(void *v) |
233 | { |
234 | struct vop_inactive_args /* { |
235 | struct vnode *a_vp; |
236 | bool *a_recycle; |
237 | } */ *ap = v; |
238 | struct vnode *vp = ap->a_vp; |
239 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
240 | |
241 | if (ptyfs->ptyfs_type == PTYFSptc) |
242 | ptyfs_clr_active(vp->v_mount, ptyfs->ptyfs_pty); |
243 | VOP_UNLOCK(vp); |
244 | return 0; |
245 | } |
246 | |
247 | /* |
248 | * Return POSIX pathconf information applicable to special devices. |
249 | */ |
250 | int |
251 | ptyfs_pathconf(void *v) |
252 | { |
253 | struct vop_pathconf_args /* { |
254 | struct vnode *a_vp; |
255 | int a_name; |
256 | register_t *a_retval; |
257 | } */ *ap = v; |
258 | |
259 | switch (ap->a_name) { |
260 | case _PC_LINK_MAX: |
261 | *ap->a_retval = LINK_MAX; |
262 | return 0; |
263 | case _PC_MAX_CANON: |
264 | *ap->a_retval = MAX_CANON; |
265 | return 0; |
266 | case _PC_MAX_INPUT: |
267 | *ap->a_retval = MAX_INPUT; |
268 | return 0; |
269 | case _PC_PIPE_BUF: |
270 | *ap->a_retval = PIPE_BUF; |
271 | return 0; |
272 | case _PC_CHOWN_RESTRICTED: |
273 | *ap->a_retval = 1; |
274 | return 0; |
275 | case _PC_VDISABLE: |
276 | *ap->a_retval = _POSIX_VDISABLE; |
277 | return 0; |
278 | case _PC_SYNC_IO: |
279 | *ap->a_retval = 1; |
280 | return 0; |
281 | default: |
282 | return EINVAL; |
283 | } |
284 | } |
285 | |
286 | /* |
287 | * _print is used for debugging. |
288 | * just print a readable description |
289 | * of (vp). |
290 | */ |
291 | int |
292 | ptyfs_print(void *v) |
293 | { |
294 | struct vop_print_args /* { |
295 | struct vnode *a_vp; |
296 | } */ *ap = v; |
297 | struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp); |
298 | |
299 | printf("tag VT_PTYFS, type %d, pty %d\n" , |
300 | ptyfs->ptyfs_type, ptyfs->ptyfs_pty); |
301 | return 0; |
302 | } |
303 | |
304 | /* |
305 | * support advisory locking on pty nodes |
306 | */ |
307 | int |
308 | ptyfs_advlock(void *v) |
309 | { |
310 | struct vop_print_args /* { |
311 | struct vnode *a_vp; |
312 | } */ *ap = v; |
313 | struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp); |
314 | |
315 | switch (ptyfs->ptyfs_type) { |
316 | case PTYFSpts: |
317 | case PTYFSptc: |
318 | return spec_advlock(v); |
319 | default: |
320 | return EOPNOTSUPP; |
321 | } |
322 | } |
323 | |
324 | /* |
325 | * Invent attributes for ptyfsnode (vp) and store |
326 | * them in (vap). |
327 | * Directories lengths are returned as zero since |
328 | * any real length would require the genuine size |
329 | * to be computed, and nothing cares anyway. |
330 | * |
331 | * this is relatively minimal for ptyfs. |
332 | */ |
333 | int |
334 | ptyfs_getattr(void *v) |
335 | { |
336 | struct vop_getattr_args /* { |
337 | struct vnode *a_vp; |
338 | struct vattr *a_vap; |
339 | kauth_cred_t a_cred; |
340 | } */ *ap = v; |
341 | struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp); |
342 | struct vattr *vap = ap->a_vap; |
343 | |
344 | PTYFS_ITIMES(ptyfs, NULL, NULL, NULL); |
345 | |
346 | /* start by zeroing out the attributes */ |
347 | vattr_null(vap); |
348 | |
349 | /* next do all the common fields */ |
350 | vap->va_type = ap->a_vp->v_type; |
351 | vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; |
352 | vap->va_fileid = ptyfs->ptyfs_fileno; |
353 | vap->va_gen = 0; |
354 | vap->va_flags = 0; |
355 | vap->va_blocksize = PAGE_SIZE; |
356 | |
357 | vap->va_atime = ptyfs->ptyfs_atime; |
358 | vap->va_mtime = ptyfs->ptyfs_mtime; |
359 | vap->va_ctime = ptyfs->ptyfs_ctime; |
360 | vap->va_birthtime = ptyfs->ptyfs_birthtime; |
361 | vap->va_mode = ptyfs->ptyfs_mode; |
362 | vap->va_flags = ptyfs->ptyfs_flags; |
363 | vap->va_uid = ptyfs->ptyfs_uid; |
364 | vap->va_gid = ptyfs->ptyfs_gid; |
365 | |
366 | switch (ptyfs->ptyfs_type) { |
367 | case PTYFSpts: |
368 | case PTYFSptc: |
369 | if (pty_isfree(ptyfs->ptyfs_pty, 1)) |
370 | return ENOENT; |
371 | vap->va_bytes = vap->va_size = 0; |
372 | vap->va_rdev = ap->a_vp->v_rdev; |
373 | vap->va_nlink = 1; |
374 | break; |
375 | case PTYFSroot: |
376 | vap->va_rdev = 0; |
377 | vap->va_bytes = vap->va_size = DEV_BSIZE; |
378 | vap->va_nlink = 2; |
379 | break; |
380 | default: |
381 | return EOPNOTSUPP; |
382 | } |
383 | |
384 | return 0; |
385 | } |
386 | |
387 | /*ARGSUSED*/ |
388 | int |
389 | ptyfs_setattr(void *v) |
390 | { |
391 | struct vop_setattr_args /* { |
392 | struct vnodeop_desc *a_desc; |
393 | struct vnode *a_vp; |
394 | struct vattr *a_vap; |
395 | kauth_cred_t a_cred; |
396 | } */ *ap = v; |
397 | struct vnode *vp = ap->a_vp; |
398 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
399 | struct vattr *vap = ap->a_vap; |
400 | kauth_cred_t cred = ap->a_cred; |
401 | struct lwp *l = curlwp; |
402 | int error; |
403 | kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS; |
404 | bool changing_sysflags = false; |
405 | |
406 | if (vap->va_size != VNOVAL) { |
407 | switch (ptyfs->ptyfs_type) { |
408 | case PTYFSroot: |
409 | return EISDIR; |
410 | case PTYFSpts: |
411 | case PTYFSptc: |
412 | break; |
413 | default: |
414 | return EINVAL; |
415 | } |
416 | } |
417 | |
418 | if (vap->va_flags != VNOVAL) { |
419 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
420 | return EROFS; |
421 | |
422 | /* Immutable and append-only flags are not supported on ptyfs. */ |
423 | if (vap->va_flags & (IMMUTABLE | APPEND)) |
424 | return EINVAL; |
425 | |
426 | /* Snapshot flag cannot be set or cleared */ |
427 | if ((vap->va_flags & SF_SNAPSHOT) != (ptyfs->ptyfs_flags & SF_SNAPSHOT)) |
428 | return EPERM; |
429 | |
430 | if ((ptyfs->ptyfs_flags & SF_SETTABLE) != (vap->va_flags & SF_SETTABLE)) { |
431 | changing_sysflags = true; |
432 | action |= KAUTH_VNODE_WRITE_SYSFLAGS; |
433 | } |
434 | |
435 | error = kauth_authorize_vnode(cred, action, vp, NULL, |
436 | genfs_can_chflags(cred, vp->v_type, ptyfs->ptyfs_uid, |
437 | changing_sysflags)); |
438 | if (error) |
439 | return error; |
440 | |
441 | if (changing_sysflags) { |
442 | ptyfs->ptyfs_flags = vap->va_flags; |
443 | } else { |
444 | ptyfs->ptyfs_flags &= SF_SETTABLE; |
445 | ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE); |
446 | } |
447 | ptyfs->ptyfs_status |= PTYFS_CHANGE; |
448 | } |
449 | |
450 | /* |
451 | * Go through the fields and update iff not VNOVAL. |
452 | */ |
453 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { |
454 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
455 | return EROFS; |
456 | if (ptyfs->ptyfs_type == PTYFSroot) |
457 | return EPERM; |
458 | error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, l); |
459 | if (error) |
460 | return error; |
461 | } |
462 | |
463 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || |
464 | vap->va_birthtime.tv_sec != VNOVAL) { |
465 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
466 | return EROFS; |
467 | if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0) |
468 | return EPERM; |
469 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, |
470 | NULL, genfs_can_chtimes(vp, vap->va_vaflags, |
471 | ptyfs->ptyfs_uid, cred)); |
472 | if (error) |
473 | return (error); |
474 | if (vap->va_atime.tv_sec != VNOVAL) |
475 | if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) |
476 | ptyfs->ptyfs_status |= PTYFS_ACCESS; |
477 | if (vap->va_mtime.tv_sec != VNOVAL) { |
478 | ptyfs->ptyfs_status |= PTYFS_CHANGE | PTYFS_MODIFY; |
479 | if (vp->v_mount->mnt_flag & MNT_RELATIME) |
480 | ptyfs->ptyfs_status |= PTYFS_ACCESS; |
481 | } |
482 | if (vap->va_birthtime.tv_sec != VNOVAL) |
483 | ptyfs->ptyfs_birthtime = vap->va_birthtime; |
484 | ptyfs->ptyfs_status |= PTYFS_CHANGE; |
485 | error = ptyfs_update(vp, &vap->va_atime, &vap->va_mtime, 0); |
486 | if (error) |
487 | return error; |
488 | } |
489 | if (vap->va_mode != (mode_t)VNOVAL) { |
490 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
491 | return EROFS; |
492 | if (ptyfs->ptyfs_type == PTYFSroot) |
493 | return EPERM; |
494 | if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 && |
495 | (vap->va_mode & |
496 | (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH))) |
497 | return EPERM; |
498 | error = ptyfs_chmod(vp, vap->va_mode, cred, l); |
499 | if (error) |
500 | return error; |
501 | } |
502 | VN_KNOTE(vp, NOTE_ATTRIB); |
503 | return 0; |
504 | } |
505 | |
506 | /* |
507 | * Change the mode on a file. |
508 | * Inode must be locked before calling. |
509 | */ |
510 | static int |
511 | ptyfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l) |
512 | { |
513 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
514 | int error; |
515 | |
516 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp, |
517 | NULL, genfs_can_chmod(vp->v_type, cred, ptyfs->ptyfs_uid, |
518 | ptyfs->ptyfs_gid, mode)); |
519 | if (error) |
520 | return (error); |
521 | |
522 | ptyfs->ptyfs_mode &= ~ALLPERMS; |
523 | ptyfs->ptyfs_mode |= (mode & ALLPERMS); |
524 | return 0; |
525 | } |
526 | |
527 | /* |
528 | * Perform chown operation on inode ip; |
529 | * inode must be locked prior to call. |
530 | */ |
531 | static int |
532 | ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred, |
533 | struct lwp *l) |
534 | { |
535 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
536 | int error; |
537 | |
538 | if (uid == (uid_t)VNOVAL) |
539 | uid = ptyfs->ptyfs_uid; |
540 | if (gid == (gid_t)VNOVAL) |
541 | gid = ptyfs->ptyfs_gid; |
542 | |
543 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp, |
544 | NULL, genfs_can_chown(cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid, |
545 | uid, gid)); |
546 | if (error) |
547 | return (error); |
548 | |
549 | ptyfs->ptyfs_gid = gid; |
550 | ptyfs->ptyfs_uid = uid; |
551 | return 0; |
552 | } |
553 | |
554 | /* |
555 | * implement access checking. |
556 | * |
557 | * actually, the check for super-user is slightly |
558 | * broken since it will allow read access to write-only |
559 | * objects. this doesn't cause any particular trouble |
560 | * but does mean that the i/o entry points need to check |
561 | * that the operation really does make sense. |
562 | */ |
563 | int |
564 | ptyfs_access(void *v) |
565 | { |
566 | struct vop_access_args /* { |
567 | struct vnode *a_vp; |
568 | int a_mode; |
569 | kauth_cred_t a_cred; |
570 | } */ *ap = v; |
571 | struct vattr va; |
572 | int error; |
573 | |
574 | if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0) |
575 | return error; |
576 | |
577 | return kauth_authorize_vnode(ap->a_cred, |
578 | KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode), |
579 | ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode, va.va_uid, |
580 | va.va_gid, ap->a_mode, ap->a_cred)); |
581 | } |
582 | |
583 | /* |
584 | * lookup. this is incredibly complicated in the |
585 | * general case, however for most pseudo-filesystems |
586 | * very little needs to be done. |
587 | * |
588 | * Locking isn't hard here, just poorly documented. |
589 | * |
590 | * If we're looking up ".", just vref the parent & return it. |
591 | * |
592 | * If we're looking up "..", unlock the parent, and lock "..". If everything |
593 | * went ok, try to re-lock the parent. We do this to prevent lock races. |
594 | * |
595 | * For anything else, get the needed node. |
596 | * |
597 | * We try to exit with the parent locked in error cases. |
598 | */ |
599 | int |
600 | ptyfs_lookup(void *v) |
601 | { |
602 | struct vop_lookup_v2_args /* { |
603 | struct vnode * a_dvp; |
604 | struct vnode ** a_vpp; |
605 | struct componentname * a_cnp; |
606 | } */ *ap = v; |
607 | struct componentname *cnp = ap->a_cnp; |
608 | struct vnode **vpp = ap->a_vpp; |
609 | struct vnode *dvp = ap->a_dvp; |
610 | const char *pname = cnp->cn_nameptr; |
611 | struct ptyfsnode *ptyfs; |
612 | int pty, error; |
613 | |
614 | *vpp = NULL; |
615 | |
616 | if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) |
617 | return EROFS; |
618 | |
619 | if (cnp->cn_namelen == 1 && *pname == '.') { |
620 | *vpp = dvp; |
621 | vref(dvp); |
622 | return 0; |
623 | } |
624 | |
625 | ptyfs = VTOPTYFS(dvp); |
626 | switch (ptyfs->ptyfs_type) { |
627 | case PTYFSroot: |
628 | /* |
629 | * Shouldn't get here with .. in the root node. |
630 | */ |
631 | if (cnp->cn_flags & ISDOTDOT) |
632 | return EIO; |
633 | |
634 | pty = atoi(pname, cnp->cn_namelen); |
635 | if (pty < 0 || ptyfs_next_active(dvp->v_mount, pty) != pty) |
636 | break; |
637 | error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty); |
638 | if (error) |
639 | return error; |
640 | if (ptyfs_next_active(dvp->v_mount, pty) != pty) { |
641 | vrele(*vpp); |
642 | *vpp = NULL; |
643 | return ENOENT; |
644 | } |
645 | return 0; |
646 | |
647 | default: |
648 | return ENOTDIR; |
649 | } |
650 | |
651 | return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS; |
652 | } |
653 | |
654 | /* |
655 | * readdir returns directory entries from ptyfsnode (vp). |
656 | * |
657 | * the strategy here with ptyfs is to generate a single |
658 | * directory entry at a time (struct dirent) and then |
659 | * copy that out to userland using uiomove. a more efficent |
660 | * though more complex implementation, would try to minimize |
661 | * the number of calls to uiomove(). for ptyfs, this is |
662 | * hardly worth the added code complexity. |
663 | * |
664 | * this should just be done through read() |
665 | */ |
666 | int |
667 | ptyfs_readdir(void *v) |
668 | { |
669 | struct vop_readdir_args /* { |
670 | struct vnode *a_vp; |
671 | struct uio *a_uio; |
672 | kauth_cred_t a_cred; |
673 | int *a_eofflag; |
674 | off_t **a_cookies; |
675 | int *a_ncookies; |
676 | } */ *ap = v; |
677 | struct uio *uio = ap->a_uio; |
678 | struct dirent *dp; |
679 | struct ptyfsnode *ptyfs; |
680 | off_t i; |
681 | int error; |
682 | off_t *cookies = NULL; |
683 | int ncookies; |
684 | struct vnode *vp; |
685 | int n, nc = 0; |
686 | |
687 | vp = ap->a_vp; |
688 | ptyfs = VTOPTYFS(vp); |
689 | |
690 | if (uio->uio_resid < UIO_MX) |
691 | return EINVAL; |
692 | if (uio->uio_offset < 0) |
693 | return EINVAL; |
694 | |
695 | dp = malloc(sizeof(struct dirent), M_PTYFSTMP, M_WAITOK | M_ZERO); |
696 | |
697 | error = 0; |
698 | i = uio->uio_offset; |
699 | dp->d_reclen = UIO_MX; |
700 | ncookies = uio->uio_resid / UIO_MX; |
701 | |
702 | if (ptyfs->ptyfs_type != PTYFSroot) { |
703 | error = ENOTDIR; |
704 | goto out; |
705 | } |
706 | |
707 | if (i >= npty) |
708 | goto out; |
709 | |
710 | if (ap->a_ncookies) { |
711 | ncookies = min(ncookies, (npty + 2 - i)); |
712 | cookies = malloc(ncookies * sizeof (off_t), |
713 | M_TEMP, M_WAITOK); |
714 | *ap->a_cookies = cookies; |
715 | } |
716 | |
717 | for (; i < 2; i++) { |
718 | /* `.' and/or `..' */ |
719 | dp->d_fileno = PTYFS_FILENO(0, PTYFSroot); |
720 | dp->d_namlen = i + 1; |
721 | (void)memcpy(dp->d_name, ".." , dp->d_namlen); |
722 | dp->d_name[i + 1] = '\0'; |
723 | dp->d_type = DT_DIR; |
724 | if ((error = uiomove(dp, UIO_MX, uio)) != 0) |
725 | goto out; |
726 | if (cookies) |
727 | *cookies++ = i + 1; |
728 | nc++; |
729 | } |
730 | while (uio->uio_resid >= UIO_MX) { |
731 | /* check for used ptys */ |
732 | n = ptyfs_next_active(vp->v_mount, i - 2); |
733 | if (n < 0) |
734 | break; |
735 | dp->d_fileno = PTYFS_FILENO(n, PTYFSpts); |
736 | dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name), |
737 | "%lld" , (long long)(n)); |
738 | dp->d_type = DT_CHR; |
739 | if ((error = uiomove(dp, UIO_MX, uio)) != 0) |
740 | goto out; |
741 | i = n + 3; |
742 | if (cookies) |
743 | *cookies++ = i; |
744 | nc++; |
745 | } |
746 | |
747 | out: |
748 | /* not pertinent in error cases */ |
749 | ncookies = nc; |
750 | |
751 | if (ap->a_ncookies) { |
752 | if (error) { |
753 | if (cookies) |
754 | free(*ap->a_cookies, M_TEMP); |
755 | *ap->a_ncookies = 0; |
756 | *ap->a_cookies = NULL; |
757 | } else |
758 | *ap->a_ncookies = ncookies; |
759 | } |
760 | uio->uio_offset = i; |
761 | free(dp, M_PTYFSTMP); |
762 | return error; |
763 | } |
764 | |
765 | int |
766 | ptyfs_open(void *v) |
767 | { |
768 | struct vop_open_args /* { |
769 | struct vnode *a_vp; |
770 | int a_mode; |
771 | kauth_cred_t a_cred; |
772 | } */ *ap = v; |
773 | struct vnode *vp = ap->a_vp; |
774 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
775 | |
776 | switch (ptyfs->ptyfs_type) { |
777 | case PTYFSpts: |
778 | case PTYFSptc: |
779 | return spec_open(v); |
780 | case PTYFSroot: |
781 | return 0; |
782 | default: |
783 | return EINVAL; |
784 | } |
785 | } |
786 | |
787 | int |
788 | ptyfs_close(void *v) |
789 | { |
790 | struct vop_close_args /* { |
791 | struct vnode *a_vp; |
792 | int a_fflag; |
793 | kauth_cred_t a_cred; |
794 | } */ *ap = v; |
795 | struct vnode *vp = ap->a_vp; |
796 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
797 | |
798 | mutex_enter(vp->v_interlock); |
799 | if (vp->v_usecount > 1) |
800 | PTYFS_ITIMES(ptyfs, NULL, NULL, NULL); |
801 | mutex_exit(vp->v_interlock); |
802 | |
803 | switch (ptyfs->ptyfs_type) { |
804 | case PTYFSpts: |
805 | case PTYFSptc: |
806 | return spec_close(v); |
807 | case PTYFSroot: |
808 | return 0; |
809 | default: |
810 | return EINVAL; |
811 | } |
812 | } |
813 | |
814 | int |
815 | ptyfs_read(void *v) |
816 | { |
817 | struct vop_read_args /* { |
818 | struct vnode *a_vp; |
819 | struct uio *a_uio; |
820 | int a_ioflag; |
821 | kauth_cred_t a_cred; |
822 | } */ *ap = v; |
823 | struct timespec ts; |
824 | struct vnode *vp = ap->a_vp; |
825 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
826 | int error; |
827 | |
828 | if (vp->v_type == VDIR) |
829 | return EISDIR; |
830 | |
831 | ptyfs->ptyfs_status |= PTYFS_ACCESS; |
832 | /* hardclock() resolution is good enough for ptyfs */ |
833 | getnanotime(&ts); |
834 | (void)ptyfs_update(vp, &ts, &ts, 0); |
835 | |
836 | switch (ptyfs->ptyfs_type) { |
837 | case PTYFSpts: |
838 | case PTYFSptc: |
839 | VOP_UNLOCK(vp); |
840 | error = cdev_read(vp->v_rdev, ap->a_uio, ap->a_ioflag); |
841 | vn_lock(vp, LK_RETRY|LK_EXCLUSIVE); |
842 | return error; |
843 | default: |
844 | return EOPNOTSUPP; |
845 | } |
846 | } |
847 | |
848 | int |
849 | ptyfs_write(void *v) |
850 | { |
851 | struct vop_write_args /* { |
852 | struct vnode *a_vp; |
853 | struct uio *a_uio; |
854 | int a_ioflag; |
855 | kauth_cred_t a_cred; |
856 | } */ *ap = v; |
857 | struct timespec ts; |
858 | struct vnode *vp = ap->a_vp; |
859 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
860 | int error; |
861 | |
862 | ptyfs->ptyfs_status |= PTYFS_MODIFY; |
863 | getnanotime(&ts); |
864 | (void)ptyfs_update(vp, &ts, &ts, 0); |
865 | |
866 | switch (ptyfs->ptyfs_type) { |
867 | case PTYFSpts: |
868 | case PTYFSptc: |
869 | VOP_UNLOCK(vp); |
870 | error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag); |
871 | vn_lock(vp, LK_RETRY|LK_EXCLUSIVE); |
872 | return error; |
873 | default: |
874 | return EOPNOTSUPP; |
875 | } |
876 | } |
877 | |
878 | int |
879 | ptyfs_ioctl(void *v) |
880 | { |
881 | struct vop_ioctl_args /* { |
882 | struct vnode *a_vp; |
883 | u_long a_command; |
884 | void *a_data; |
885 | int a_fflag; |
886 | kauth_cred_t a_cred; |
887 | } */ *ap = v; |
888 | struct vnode *vp = ap->a_vp; |
889 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
890 | |
891 | switch (ptyfs->ptyfs_type) { |
892 | case PTYFSpts: |
893 | case PTYFSptc: |
894 | return cdev_ioctl(vp->v_rdev, ap->a_command, |
895 | ap->a_data, ap->a_fflag, curlwp); |
896 | default: |
897 | return EOPNOTSUPP; |
898 | } |
899 | } |
900 | |
901 | int |
902 | ptyfs_poll(void *v) |
903 | { |
904 | struct vop_poll_args /* { |
905 | struct vnode *a_vp; |
906 | int a_events; |
907 | } */ *ap = v; |
908 | struct vnode *vp = ap->a_vp; |
909 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
910 | |
911 | switch (ptyfs->ptyfs_type) { |
912 | case PTYFSpts: |
913 | case PTYFSptc: |
914 | return cdev_poll(vp->v_rdev, ap->a_events, curlwp); |
915 | default: |
916 | return genfs_poll(v); |
917 | } |
918 | } |
919 | |
920 | int |
921 | ptyfs_kqfilter(void *v) |
922 | { |
923 | struct vop_kqfilter_args /* { |
924 | struct vnode *a_vp; |
925 | struct knote *a_kn; |
926 | } */ *ap = v; |
927 | struct vnode *vp = ap->a_vp; |
928 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
929 | |
930 | switch (ptyfs->ptyfs_type) { |
931 | case PTYFSpts: |
932 | case PTYFSptc: |
933 | return cdev_kqfilter(vp->v_rdev, ap->a_kn); |
934 | default: |
935 | return genfs_kqfilter(v); |
936 | } |
937 | } |
938 | |
939 | static int |
940 | ptyfs_update(struct vnode *vp, const struct timespec *acc, |
941 | const struct timespec *mod, int flags) |
942 | { |
943 | struct ptyfsnode *ptyfs = VTOPTYFS(vp); |
944 | |
945 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
946 | return 0; |
947 | |
948 | PTYFS_ITIMES(ptyfs, acc, mod, NULL); |
949 | return 0; |
950 | } |
951 | |
952 | void |
953 | ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc, |
954 | const struct timespec *mod, const struct timespec *cre) |
955 | { |
956 | struct timespec now; |
957 | |
958 | KASSERT(ptyfs->ptyfs_status & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY)); |
959 | |
960 | getnanotime(&now); |
961 | if (ptyfs->ptyfs_status & PTYFS_ACCESS) { |
962 | if (acc == NULL) |
963 | acc = &now; |
964 | ptyfs->ptyfs_atime = *acc; |
965 | } |
966 | if (ptyfs->ptyfs_status & PTYFS_MODIFY) { |
967 | if (mod == NULL) |
968 | mod = &now; |
969 | ptyfs->ptyfs_mtime = *mod; |
970 | } |
971 | if (ptyfs->ptyfs_status & PTYFS_CHANGE) { |
972 | if (cre == NULL) |
973 | cre = &now; |
974 | ptyfs->ptyfs_ctime = *cre; |
975 | } |
976 | ptyfs->ptyfs_status &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY); |
977 | } |
978 | |
979 | /* |
980 | * convert decimal ascii to int |
981 | */ |
982 | static int |
983 | atoi(const char *b, size_t len) |
984 | { |
985 | int p = 0; |
986 | |
987 | while (len--) { |
988 | char c = *b++; |
989 | if (c < '0' || c > '9') |
990 | return -1; |
991 | p = 10 * p + (c - '0'); |
992 | } |
993 | |
994 | return p; |
995 | } |
996 | |