1 | #include <machine/rtems-bsd-config.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * (c) UNIX System Laboratories, Inc. |
---|
7 | * All or some portions of this file are derived from material licensed |
---|
8 | * to the University of California by American Telephone and Telegraph |
---|
9 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
---|
10 | * the permission of UNIX System Laboratories, Inc. |
---|
11 | * |
---|
12 | * Redistribution and use in source and binary forms, with or without |
---|
13 | * modification, are permitted provided that the following conditions |
---|
14 | * are met: |
---|
15 | * 1. Redistributions of source code must retain the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer. |
---|
17 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer in the |
---|
19 | * documentation and/or other materials provided with the distribution. |
---|
20 | * 4. Neither the name of the University nor the names of its contributors |
---|
21 | * may be used to endorse or promote products derived from this software |
---|
22 | * without specific prior written permission. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
34 | * SUCH DAMAGE. |
---|
35 | * |
---|
36 | * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 |
---|
37 | */ |
---|
38 | |
---|
39 | #include <sys/cdefs.h> |
---|
40 | __FBSDID("$FreeBSD$"); |
---|
41 | |
---|
42 | #include <rtems/bsd/local/opt_compat.h> |
---|
43 | #include <rtems/bsd/local/opt_ddb.h> |
---|
44 | #include <rtems/bsd/local/opt_ktrace.h> |
---|
45 | |
---|
46 | #include <rtems/bsd/sys/param.h> |
---|
47 | #include <sys/systm.h> |
---|
48 | |
---|
49 | #include <sys/conf.h> |
---|
50 | #include <sys/domain.h> |
---|
51 | #include <sys/fcntl.h> |
---|
52 | #include <sys/file.h> |
---|
53 | #include <sys/filedesc.h> |
---|
54 | #include <sys/filio.h> |
---|
55 | #include <sys/jail.h> |
---|
56 | #include <sys/kernel.h> |
---|
57 | #include <sys/limits.h> |
---|
58 | #include <rtems/bsd/sys/lock.h> |
---|
59 | #include <sys/malloc.h> |
---|
60 | #include <sys/mount.h> |
---|
61 | #include <sys/mqueue.h> |
---|
62 | #include <sys/mutex.h> |
---|
63 | #include <sys/namei.h> |
---|
64 | #include <sys/priv.h> |
---|
65 | #include <sys/proc.h> |
---|
66 | #include <sys/protosw.h> |
---|
67 | #include <sys/resourcevar.h> |
---|
68 | #include <sys/signalvar.h> |
---|
69 | #include <sys/socketvar.h> |
---|
70 | #include <sys/stat.h> |
---|
71 | #include <sys/sx.h> |
---|
72 | #include <sys/syscallsubr.h> |
---|
73 | #include <sys/sysctl.h> |
---|
74 | #include <sys/sysproto.h> |
---|
75 | #include <sys/tty.h> |
---|
76 | #include <rtems/bsd/sys/unistd.h> |
---|
77 | #include <sys/user.h> |
---|
78 | #include <sys/vnode.h> |
---|
79 | #ifdef KTRACE |
---|
80 | #include <sys/ktrace.h> |
---|
81 | #endif |
---|
82 | |
---|
83 | #include <security/audit/audit.h> |
---|
84 | |
---|
85 | #include <vm/uma.h> |
---|
86 | |
---|
87 | #include <ddb/ddb.h> |
---|
88 | |
---|
89 | static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table"); |
---|
90 | static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader", |
---|
91 | "file desc to leader structures"); |
---|
92 | static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures"); |
---|
93 | |
---|
94 | static uma_zone_t file_zone; |
---|
95 | |
---|
96 | |
---|
97 | /* Flags for do_dup() */ |
---|
98 | #define DUP_FIXED 0x1 /* Force fixed allocation */ |
---|
99 | #define DUP_FCNTL 0x2 /* fcntl()-style errors */ |
---|
100 | |
---|
101 | static int do_dup(struct thread *td, int flags, int old, int new, |
---|
102 | register_t *retval); |
---|
103 | static int fd_first_free(struct filedesc *, int, int); |
---|
104 | static int fd_last_used(struct filedesc *, int, int); |
---|
105 | static void fdgrowtable(struct filedesc *, int); |
---|
106 | static void fdunused(struct filedesc *fdp, int fd); |
---|
107 | static void fdused(struct filedesc *fdp, int fd); |
---|
108 | |
---|
109 | /* |
---|
110 | * A process is initially started out with NDFILE descriptors stored within |
---|
111 | * this structure, selected to be enough for typical applications based on |
---|
112 | * the historical limit of 20 open files (and the usage of descriptors by |
---|
113 | * shells). If these descriptors are exhausted, a larger descriptor table |
---|
114 | * may be allocated, up to a process' resource limit; the internal arrays |
---|
115 | * are then unused. |
---|
116 | */ |
---|
117 | #define NDFILE 20 |
---|
118 | #define NDSLOTSIZE sizeof(NDSLOTTYPE) |
---|
119 | #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT) |
---|
120 | #define NDSLOT(x) ((x) / NDENTRIES) |
---|
121 | #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES)) |
---|
122 | #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES) |
---|
123 | |
---|
124 | /* |
---|
125 | * Storage required per open file descriptor. |
---|
126 | */ |
---|
127 | #define OFILESIZE (sizeof(struct file *) + sizeof(char)) |
---|
128 | |
---|
129 | /* |
---|
130 | * Storage to hold unused ofiles that need to be reclaimed. |
---|
131 | */ |
---|
132 | struct freetable { |
---|
133 | struct file **ft_table; |
---|
134 | SLIST_ENTRY(freetable) ft_next; |
---|
135 | }; |
---|
136 | |
---|
137 | /* |
---|
138 | * Basic allocation of descriptors: |
---|
139 | * one of the above, plus arrays for NDFILE descriptors. |
---|
140 | */ |
---|
141 | struct filedesc0 { |
---|
142 | struct filedesc fd_fd; |
---|
143 | /* |
---|
144 | * ofiles which need to be reclaimed on free. |
---|
145 | */ |
---|
146 | SLIST_HEAD(,freetable) fd_free; |
---|
147 | /* |
---|
148 | * These arrays are used when the number of open files is |
---|
149 | * <= NDFILE, and are then pointed to by the pointers above. |
---|
150 | */ |
---|
151 | struct file *fd_dfiles[NDFILE]; |
---|
152 | char fd_dfileflags[NDFILE]; |
---|
153 | NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)]; |
---|
154 | }; |
---|
155 | |
---|
156 | /* |
---|
157 | * Descriptor management. |
---|
158 | */ |
---|
159 | volatile int openfiles; /* actual number of open files */ |
---|
160 | struct mtx sigio_lock; /* mtx to protect pointers to sigio */ |
---|
161 | #ifndef __rtems__ |
---|
162 | void (*mq_fdclose)(struct thread *td, int fd, struct file *fp); |
---|
163 | |
---|
164 | /* A mutex to protect the association between a proc and filedesc. */ |
---|
165 | static struct mtx fdesc_mtx; |
---|
166 | |
---|
167 | /* |
---|
168 | * Find the first zero bit in the given bitmap, starting at low and not |
---|
169 | * exceeding size - 1. |
---|
170 | */ |
---|
171 | static int |
---|
172 | fd_first_free(struct filedesc *fdp, int low, int size) |
---|
173 | { |
---|
174 | NDSLOTTYPE *map = fdp->fd_map; |
---|
175 | NDSLOTTYPE mask; |
---|
176 | int off, maxoff; |
---|
177 | |
---|
178 | if (low >= size) |
---|
179 | return (low); |
---|
180 | |
---|
181 | off = NDSLOT(low); |
---|
182 | if (low % NDENTRIES) { |
---|
183 | mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES))); |
---|
184 | if ((mask &= ~map[off]) != 0UL) |
---|
185 | return (off * NDENTRIES + ffsl(mask) - 1); |
---|
186 | ++off; |
---|
187 | } |
---|
188 | for (maxoff = NDSLOTS(size); off < maxoff; ++off) |
---|
189 | if (map[off] != ~0UL) |
---|
190 | return (off * NDENTRIES + ffsl(~map[off]) - 1); |
---|
191 | return (size); |
---|
192 | } |
---|
193 | |
---|
194 | /* |
---|
195 | * Find the highest non-zero bit in the given bitmap, starting at low and |
---|
196 | * not exceeding size - 1. |
---|
197 | */ |
---|
198 | static int |
---|
199 | fd_last_used(struct filedesc *fdp, int low, int size) |
---|
200 | { |
---|
201 | NDSLOTTYPE *map = fdp->fd_map; |
---|
202 | NDSLOTTYPE mask; |
---|
203 | int off, minoff; |
---|
204 | |
---|
205 | if (low >= size) |
---|
206 | return (-1); |
---|
207 | |
---|
208 | off = NDSLOT(size); |
---|
209 | if (size % NDENTRIES) { |
---|
210 | mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES)); |
---|
211 | if ((mask &= map[off]) != 0) |
---|
212 | return (off * NDENTRIES + flsl(mask) - 1); |
---|
213 | --off; |
---|
214 | } |
---|
215 | for (minoff = NDSLOT(low); off >= minoff; --off) |
---|
216 | if (map[off] != 0) |
---|
217 | return (off * NDENTRIES + flsl(map[off]) - 1); |
---|
218 | return (low - 1); |
---|
219 | } |
---|
220 | |
---|
221 | static int |
---|
222 | fdisused(struct filedesc *fdp, int fd) |
---|
223 | { |
---|
224 | KASSERT(fd >= 0 && fd < fdp->fd_nfiles, |
---|
225 | ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles)); |
---|
226 | return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0); |
---|
227 | } |
---|
228 | |
---|
229 | /* |
---|
230 | * Mark a file descriptor as used. |
---|
231 | */ |
---|
232 | static void |
---|
233 | fdused(struct filedesc *fdp, int fd) |
---|
234 | { |
---|
235 | |
---|
236 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
237 | KASSERT(!fdisused(fdp, fd), |
---|
238 | ("fd already used")); |
---|
239 | |
---|
240 | fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd); |
---|
241 | if (fd > fdp->fd_lastfile) |
---|
242 | fdp->fd_lastfile = fd; |
---|
243 | if (fd == fdp->fd_freefile) |
---|
244 | fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles); |
---|
245 | } |
---|
246 | |
---|
247 | /* |
---|
248 | * Mark a file descriptor as unused. |
---|
249 | */ |
---|
250 | static void |
---|
251 | fdunused(struct filedesc *fdp, int fd) |
---|
252 | { |
---|
253 | |
---|
254 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
255 | KASSERT(fdisused(fdp, fd), |
---|
256 | ("fd is already unused")); |
---|
257 | KASSERT(fdp->fd_ofiles[fd] == NULL, |
---|
258 | ("fd is still in use")); |
---|
259 | |
---|
260 | fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd); |
---|
261 | if (fd < fdp->fd_freefile) |
---|
262 | fdp->fd_freefile = fd; |
---|
263 | if (fd == fdp->fd_lastfile) |
---|
264 | fdp->fd_lastfile = fd_last_used(fdp, 0, fd); |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
268 | * System calls on descriptors. |
---|
269 | */ |
---|
270 | #ifndef _SYS_SYSPROTO_H_ |
---|
271 | struct getdtablesize_args { |
---|
272 | int dummy; |
---|
273 | }; |
---|
274 | #endif |
---|
275 | /* ARGSUSED */ |
---|
276 | int |
---|
277 | getdtablesize(struct thread *td, struct getdtablesize_args *uap) |
---|
278 | { |
---|
279 | struct proc *p = td->td_proc; |
---|
280 | |
---|
281 | PROC_LOCK(p); |
---|
282 | td->td_retval[0] = |
---|
283 | min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
284 | PROC_UNLOCK(p); |
---|
285 | return (0); |
---|
286 | } |
---|
287 | |
---|
288 | /* |
---|
289 | * Duplicate a file descriptor to a particular value. |
---|
290 | * |
---|
291 | * Note: keep in mind that a potential race condition exists when closing |
---|
292 | * descriptors from a shared descriptor table (via rfork). |
---|
293 | */ |
---|
294 | #ifndef _SYS_SYSPROTO_H_ |
---|
295 | struct dup2_args { |
---|
296 | u_int from; |
---|
297 | u_int to; |
---|
298 | }; |
---|
299 | #endif |
---|
300 | /* ARGSUSED */ |
---|
301 | int |
---|
302 | dup2(struct thread *td, struct dup2_args *uap) |
---|
303 | { |
---|
304 | |
---|
305 | return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to, |
---|
306 | td->td_retval)); |
---|
307 | } |
---|
308 | |
---|
309 | /* |
---|
310 | * Duplicate a file descriptor. |
---|
311 | */ |
---|
312 | #ifndef _SYS_SYSPROTO_H_ |
---|
313 | struct dup_args { |
---|
314 | u_int fd; |
---|
315 | }; |
---|
316 | #endif |
---|
317 | /* ARGSUSED */ |
---|
318 | int |
---|
319 | dup(struct thread *td, struct dup_args *uap) |
---|
320 | { |
---|
321 | |
---|
322 | return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval)); |
---|
323 | } |
---|
324 | |
---|
325 | /* |
---|
326 | * The file control system call. |
---|
327 | */ |
---|
328 | #ifndef _SYS_SYSPROTO_H_ |
---|
329 | struct fcntl_args { |
---|
330 | int fd; |
---|
331 | int cmd; |
---|
332 | long arg; |
---|
333 | }; |
---|
334 | #endif |
---|
335 | /* ARGSUSED */ |
---|
336 | int |
---|
337 | fcntl(struct thread *td, struct fcntl_args *uap) |
---|
338 | { |
---|
339 | struct flock fl; |
---|
340 | struct oflock ofl; |
---|
341 | intptr_t arg; |
---|
342 | int error; |
---|
343 | int cmd; |
---|
344 | |
---|
345 | error = 0; |
---|
346 | cmd = uap->cmd; |
---|
347 | switch (uap->cmd) { |
---|
348 | case F_OGETLK: |
---|
349 | case F_OSETLK: |
---|
350 | case F_OSETLKW: |
---|
351 | /* |
---|
352 | * Convert old flock structure to new. |
---|
353 | */ |
---|
354 | error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl)); |
---|
355 | fl.l_start = ofl.l_start; |
---|
356 | fl.l_len = ofl.l_len; |
---|
357 | fl.l_pid = ofl.l_pid; |
---|
358 | fl.l_type = ofl.l_type; |
---|
359 | fl.l_whence = ofl.l_whence; |
---|
360 | fl.l_sysid = 0; |
---|
361 | |
---|
362 | switch (uap->cmd) { |
---|
363 | case F_OGETLK: |
---|
364 | cmd = F_GETLK; |
---|
365 | break; |
---|
366 | case F_OSETLK: |
---|
367 | cmd = F_SETLK; |
---|
368 | break; |
---|
369 | case F_OSETLKW: |
---|
370 | cmd = F_SETLKW; |
---|
371 | break; |
---|
372 | } |
---|
373 | arg = (intptr_t)&fl; |
---|
374 | break; |
---|
375 | case F_GETLK: |
---|
376 | case F_SETLK: |
---|
377 | case F_SETLKW: |
---|
378 | case F_SETLK_REMOTE: |
---|
379 | error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl)); |
---|
380 | arg = (intptr_t)&fl; |
---|
381 | break; |
---|
382 | default: |
---|
383 | arg = uap->arg; |
---|
384 | break; |
---|
385 | } |
---|
386 | if (error) |
---|
387 | return (error); |
---|
388 | error = kern_fcntl(td, uap->fd, cmd, arg); |
---|
389 | if (error) |
---|
390 | return (error); |
---|
391 | if (uap->cmd == F_OGETLK) { |
---|
392 | ofl.l_start = fl.l_start; |
---|
393 | ofl.l_len = fl.l_len; |
---|
394 | ofl.l_pid = fl.l_pid; |
---|
395 | ofl.l_type = fl.l_type; |
---|
396 | ofl.l_whence = fl.l_whence; |
---|
397 | error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl)); |
---|
398 | } else if (uap->cmd == F_GETLK) { |
---|
399 | error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl)); |
---|
400 | } |
---|
401 | return (error); |
---|
402 | } |
---|
403 | |
---|
404 | static inline struct file * |
---|
405 | fdtofp(int fd, struct filedesc *fdp) |
---|
406 | { |
---|
407 | struct file *fp; |
---|
408 | |
---|
409 | FILEDESC_LOCK_ASSERT(fdp); |
---|
410 | if ((unsigned)fd >= fdp->fd_nfiles || |
---|
411 | (fp = fdp->fd_ofiles[fd]) == NULL) |
---|
412 | return (NULL); |
---|
413 | return (fp); |
---|
414 | } |
---|
415 | |
---|
416 | int |
---|
417 | kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg) |
---|
418 | { |
---|
419 | struct filedesc *fdp; |
---|
420 | struct flock *flp; |
---|
421 | struct file *fp; |
---|
422 | struct proc *p; |
---|
423 | char *pop; |
---|
424 | struct vnode *vp; |
---|
425 | int error, flg, tmp; |
---|
426 | int vfslocked; |
---|
427 | u_int old, new; |
---|
428 | uint64_t bsize; |
---|
429 | |
---|
430 | vfslocked = 0; |
---|
431 | error = 0; |
---|
432 | flg = F_POSIX; |
---|
433 | p = td->td_proc; |
---|
434 | fdp = p->p_fd; |
---|
435 | |
---|
436 | switch (cmd) { |
---|
437 | case F_DUPFD: |
---|
438 | tmp = arg; |
---|
439 | error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval); |
---|
440 | break; |
---|
441 | |
---|
442 | case F_DUP2FD: |
---|
443 | tmp = arg; |
---|
444 | error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval); |
---|
445 | break; |
---|
446 | |
---|
447 | case F_GETFD: |
---|
448 | FILEDESC_SLOCK(fdp); |
---|
449 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
450 | FILEDESC_SUNLOCK(fdp); |
---|
451 | error = EBADF; |
---|
452 | break; |
---|
453 | } |
---|
454 | pop = &fdp->fd_ofileflags[fd]; |
---|
455 | td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0; |
---|
456 | FILEDESC_SUNLOCK(fdp); |
---|
457 | break; |
---|
458 | |
---|
459 | case F_SETFD: |
---|
460 | FILEDESC_XLOCK(fdp); |
---|
461 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
462 | FILEDESC_XUNLOCK(fdp); |
---|
463 | error = EBADF; |
---|
464 | break; |
---|
465 | } |
---|
466 | pop = &fdp->fd_ofileflags[fd]; |
---|
467 | *pop = (*pop &~ UF_EXCLOSE) | |
---|
468 | (arg & FD_CLOEXEC ? UF_EXCLOSE : 0); |
---|
469 | FILEDESC_XUNLOCK(fdp); |
---|
470 | break; |
---|
471 | |
---|
472 | case F_GETFL: |
---|
473 | FILEDESC_SLOCK(fdp); |
---|
474 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
475 | FILEDESC_SUNLOCK(fdp); |
---|
476 | error = EBADF; |
---|
477 | break; |
---|
478 | } |
---|
479 | td->td_retval[0] = OFLAGS(fp->f_flag); |
---|
480 | FILEDESC_SUNLOCK(fdp); |
---|
481 | break; |
---|
482 | |
---|
483 | case F_SETFL: |
---|
484 | FILEDESC_SLOCK(fdp); |
---|
485 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
486 | FILEDESC_SUNLOCK(fdp); |
---|
487 | error = EBADF; |
---|
488 | break; |
---|
489 | } |
---|
490 | fhold(fp); |
---|
491 | FILEDESC_SUNLOCK(fdp); |
---|
492 | do { |
---|
493 | tmp = flg = fp->f_flag; |
---|
494 | tmp &= ~FCNTLFLAGS; |
---|
495 | tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS; |
---|
496 | } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); |
---|
497 | tmp = fp->f_flag & FNONBLOCK; |
---|
498 | error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); |
---|
499 | if (error) { |
---|
500 | fdrop(fp, td); |
---|
501 | break; |
---|
502 | } |
---|
503 | tmp = fp->f_flag & FASYNC; |
---|
504 | error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); |
---|
505 | if (error == 0) { |
---|
506 | fdrop(fp, td); |
---|
507 | break; |
---|
508 | } |
---|
509 | atomic_clear_int(&fp->f_flag, FNONBLOCK); |
---|
510 | tmp = 0; |
---|
511 | (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); |
---|
512 | fdrop(fp, td); |
---|
513 | break; |
---|
514 | |
---|
515 | case F_GETOWN: |
---|
516 | FILEDESC_SLOCK(fdp); |
---|
517 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
518 | FILEDESC_SUNLOCK(fdp); |
---|
519 | error = EBADF; |
---|
520 | break; |
---|
521 | } |
---|
522 | fhold(fp); |
---|
523 | FILEDESC_SUNLOCK(fdp); |
---|
524 | error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td); |
---|
525 | if (error == 0) |
---|
526 | td->td_retval[0] = tmp; |
---|
527 | fdrop(fp, td); |
---|
528 | break; |
---|
529 | |
---|
530 | case F_SETOWN: |
---|
531 | FILEDESC_SLOCK(fdp); |
---|
532 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
533 | FILEDESC_SUNLOCK(fdp); |
---|
534 | error = EBADF; |
---|
535 | break; |
---|
536 | } |
---|
537 | fhold(fp); |
---|
538 | FILEDESC_SUNLOCK(fdp); |
---|
539 | tmp = arg; |
---|
540 | error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td); |
---|
541 | fdrop(fp, td); |
---|
542 | break; |
---|
543 | |
---|
544 | case F_SETLK_REMOTE: |
---|
545 | error = priv_check(td, PRIV_NFS_LOCKD); |
---|
546 | if (error) |
---|
547 | return (error); |
---|
548 | flg = F_REMOTE; |
---|
549 | goto do_setlk; |
---|
550 | |
---|
551 | case F_SETLKW: |
---|
552 | flg |= F_WAIT; |
---|
553 | /* FALLTHROUGH F_SETLK */ |
---|
554 | |
---|
555 | case F_SETLK: |
---|
556 | do_setlk: |
---|
557 | FILEDESC_SLOCK(fdp); |
---|
558 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
559 | FILEDESC_SUNLOCK(fdp); |
---|
560 | error = EBADF; |
---|
561 | break; |
---|
562 | } |
---|
563 | if (fp->f_type != DTYPE_VNODE) { |
---|
564 | FILEDESC_SUNLOCK(fdp); |
---|
565 | error = EBADF; |
---|
566 | break; |
---|
567 | } |
---|
568 | flp = (struct flock *)arg; |
---|
569 | if (flp->l_whence == SEEK_CUR) { |
---|
570 | if (fp->f_offset < 0 || |
---|
571 | (flp->l_start > 0 && |
---|
572 | fp->f_offset > OFF_MAX - flp->l_start)) { |
---|
573 | FILEDESC_SUNLOCK(fdp); |
---|
574 | error = EOVERFLOW; |
---|
575 | break; |
---|
576 | } |
---|
577 | flp->l_start += fp->f_offset; |
---|
578 | } |
---|
579 | |
---|
580 | /* |
---|
581 | * VOP_ADVLOCK() may block. |
---|
582 | */ |
---|
583 | fhold(fp); |
---|
584 | FILEDESC_SUNLOCK(fdp); |
---|
585 | vp = fp->f_vnode; |
---|
586 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
587 | switch (flp->l_type) { |
---|
588 | case F_RDLCK: |
---|
589 | if ((fp->f_flag & FREAD) == 0) { |
---|
590 | error = EBADF; |
---|
591 | break; |
---|
592 | } |
---|
593 | PROC_LOCK(p->p_leader); |
---|
594 | p->p_leader->p_flag |= P_ADVLOCK; |
---|
595 | PROC_UNLOCK(p->p_leader); |
---|
596 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, |
---|
597 | flp, flg); |
---|
598 | break; |
---|
599 | case F_WRLCK: |
---|
600 | if ((fp->f_flag & FWRITE) == 0) { |
---|
601 | error = EBADF; |
---|
602 | break; |
---|
603 | } |
---|
604 | PROC_LOCK(p->p_leader); |
---|
605 | p->p_leader->p_flag |= P_ADVLOCK; |
---|
606 | PROC_UNLOCK(p->p_leader); |
---|
607 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, |
---|
608 | flp, flg); |
---|
609 | break; |
---|
610 | case F_UNLCK: |
---|
611 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, |
---|
612 | flp, flg); |
---|
613 | break; |
---|
614 | case F_UNLCKSYS: |
---|
615 | /* |
---|
616 | * Temporary api for testing remote lock |
---|
617 | * infrastructure. |
---|
618 | */ |
---|
619 | if (flg != F_REMOTE) { |
---|
620 | error = EINVAL; |
---|
621 | break; |
---|
622 | } |
---|
623 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, |
---|
624 | F_UNLCKSYS, flp, flg); |
---|
625 | break; |
---|
626 | default: |
---|
627 | error = EINVAL; |
---|
628 | break; |
---|
629 | } |
---|
630 | VFS_UNLOCK_GIANT(vfslocked); |
---|
631 | vfslocked = 0; |
---|
632 | /* Check for race with close */ |
---|
633 | FILEDESC_SLOCK(fdp); |
---|
634 | if ((unsigned) fd >= fdp->fd_nfiles || |
---|
635 | fp != fdp->fd_ofiles[fd]) { |
---|
636 | FILEDESC_SUNLOCK(fdp); |
---|
637 | flp->l_whence = SEEK_SET; |
---|
638 | flp->l_start = 0; |
---|
639 | flp->l_len = 0; |
---|
640 | flp->l_type = F_UNLCK; |
---|
641 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
642 | (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, |
---|
643 | F_UNLCK, flp, F_POSIX); |
---|
644 | VFS_UNLOCK_GIANT(vfslocked); |
---|
645 | vfslocked = 0; |
---|
646 | } else |
---|
647 | FILEDESC_SUNLOCK(fdp); |
---|
648 | fdrop(fp, td); |
---|
649 | break; |
---|
650 | |
---|
651 | case F_GETLK: |
---|
652 | FILEDESC_SLOCK(fdp); |
---|
653 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
654 | FILEDESC_SUNLOCK(fdp); |
---|
655 | error = EBADF; |
---|
656 | break; |
---|
657 | } |
---|
658 | if (fp->f_type != DTYPE_VNODE) { |
---|
659 | FILEDESC_SUNLOCK(fdp); |
---|
660 | error = EBADF; |
---|
661 | break; |
---|
662 | } |
---|
663 | flp = (struct flock *)arg; |
---|
664 | if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK && |
---|
665 | flp->l_type != F_UNLCK) { |
---|
666 | FILEDESC_SUNLOCK(fdp); |
---|
667 | error = EINVAL; |
---|
668 | break; |
---|
669 | } |
---|
670 | if (flp->l_whence == SEEK_CUR) { |
---|
671 | if ((flp->l_start > 0 && |
---|
672 | fp->f_offset > OFF_MAX - flp->l_start) || |
---|
673 | (flp->l_start < 0 && |
---|
674 | fp->f_offset < OFF_MIN - flp->l_start)) { |
---|
675 | FILEDESC_SUNLOCK(fdp); |
---|
676 | error = EOVERFLOW; |
---|
677 | break; |
---|
678 | } |
---|
679 | flp->l_start += fp->f_offset; |
---|
680 | } |
---|
681 | /* |
---|
682 | * VOP_ADVLOCK() may block. |
---|
683 | */ |
---|
684 | fhold(fp); |
---|
685 | FILEDESC_SUNLOCK(fdp); |
---|
686 | vp = fp->f_vnode; |
---|
687 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
688 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp, |
---|
689 | F_POSIX); |
---|
690 | VFS_UNLOCK_GIANT(vfslocked); |
---|
691 | vfslocked = 0; |
---|
692 | fdrop(fp, td); |
---|
693 | break; |
---|
694 | |
---|
695 | case F_RDAHEAD: |
---|
696 | arg = arg ? 128 * 1024: 0; |
---|
697 | /* FALLTHROUGH */ |
---|
698 | case F_READAHEAD: |
---|
699 | FILEDESC_SLOCK(fdp); |
---|
700 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
701 | FILEDESC_SUNLOCK(fdp); |
---|
702 | error = EBADF; |
---|
703 | break; |
---|
704 | } |
---|
705 | if (fp->f_type != DTYPE_VNODE) { |
---|
706 | FILEDESC_SUNLOCK(fdp); |
---|
707 | error = EBADF; |
---|
708 | break; |
---|
709 | } |
---|
710 | fhold(fp); |
---|
711 | FILEDESC_SUNLOCK(fdp); |
---|
712 | if (arg != 0) { |
---|
713 | vp = fp->f_vnode; |
---|
714 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
715 | error = vn_lock(vp, LK_SHARED); |
---|
716 | if (error != 0) |
---|
717 | goto readahead_vnlock_fail; |
---|
718 | bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize; |
---|
719 | VOP_UNLOCK(vp, 0); |
---|
720 | fp->f_seqcount = (arg + bsize - 1) / bsize; |
---|
721 | do { |
---|
722 | new = old = fp->f_flag; |
---|
723 | new |= FRDAHEAD; |
---|
724 | } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); |
---|
725 | readahead_vnlock_fail: |
---|
726 | VFS_UNLOCK_GIANT(vfslocked); |
---|
727 | vfslocked = 0; |
---|
728 | } else { |
---|
729 | do { |
---|
730 | new = old = fp->f_flag; |
---|
731 | new &= ~FRDAHEAD; |
---|
732 | } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); |
---|
733 | } |
---|
734 | fdrop(fp, td); |
---|
735 | break; |
---|
736 | |
---|
737 | default: |
---|
738 | error = EINVAL; |
---|
739 | break; |
---|
740 | } |
---|
741 | VFS_UNLOCK_GIANT(vfslocked); |
---|
742 | return (error); |
---|
743 | } |
---|
744 | |
---|
745 | /* |
---|
746 | * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD). |
---|
747 | */ |
---|
748 | static int |
---|
749 | do_dup(struct thread *td, int flags, int old, int new, |
---|
750 | register_t *retval) |
---|
751 | { |
---|
752 | struct filedesc *fdp; |
---|
753 | struct proc *p; |
---|
754 | struct file *fp; |
---|
755 | struct file *delfp; |
---|
756 | int error, holdleaders, maxfd; |
---|
757 | |
---|
758 | p = td->td_proc; |
---|
759 | fdp = p->p_fd; |
---|
760 | |
---|
761 | /* |
---|
762 | * Verify we have a valid descriptor to dup from and possibly to |
---|
763 | * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should |
---|
764 | * return EINVAL when the new descriptor is out of bounds. |
---|
765 | */ |
---|
766 | if (old < 0) |
---|
767 | return (EBADF); |
---|
768 | if (new < 0) |
---|
769 | return (flags & DUP_FCNTL ? EINVAL : EBADF); |
---|
770 | PROC_LOCK(p); |
---|
771 | maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
772 | PROC_UNLOCK(p); |
---|
773 | if (new >= maxfd) |
---|
774 | return (flags & DUP_FCNTL ? EINVAL : EMFILE); |
---|
775 | |
---|
776 | FILEDESC_XLOCK(fdp); |
---|
777 | if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { |
---|
778 | FILEDESC_XUNLOCK(fdp); |
---|
779 | return (EBADF); |
---|
780 | } |
---|
781 | if (flags & DUP_FIXED && old == new) { |
---|
782 | *retval = new; |
---|
783 | FILEDESC_XUNLOCK(fdp); |
---|
784 | return (0); |
---|
785 | } |
---|
786 | fp = fdp->fd_ofiles[old]; |
---|
787 | fhold(fp); |
---|
788 | |
---|
789 | /* |
---|
790 | * If the caller specified a file descriptor, make sure the file |
---|
791 | * table is large enough to hold it, and grab it. Otherwise, just |
---|
792 | * allocate a new descriptor the usual way. Since the filedesc |
---|
793 | * lock may be temporarily dropped in the process, we have to look |
---|
794 | * out for a race. |
---|
795 | */ |
---|
796 | if (flags & DUP_FIXED) { |
---|
797 | if (new >= fdp->fd_nfiles) |
---|
798 | fdgrowtable(fdp, new + 1); |
---|
799 | if (fdp->fd_ofiles[new] == NULL) |
---|
800 | fdused(fdp, new); |
---|
801 | } else { |
---|
802 | if ((error = fdalloc(td, new, &new)) != 0) { |
---|
803 | FILEDESC_XUNLOCK(fdp); |
---|
804 | fdrop(fp, td); |
---|
805 | return (error); |
---|
806 | } |
---|
807 | } |
---|
808 | |
---|
809 | /* |
---|
810 | * If the old file changed out from under us then treat it as a |
---|
811 | * bad file descriptor. Userland should do its own locking to |
---|
812 | * avoid this case. |
---|
813 | */ |
---|
814 | if (fdp->fd_ofiles[old] != fp) { |
---|
815 | /* we've allocated a descriptor which we won't use */ |
---|
816 | if (fdp->fd_ofiles[new] == NULL) |
---|
817 | fdunused(fdp, new); |
---|
818 | FILEDESC_XUNLOCK(fdp); |
---|
819 | fdrop(fp, td); |
---|
820 | return (EBADF); |
---|
821 | } |
---|
822 | KASSERT(old != new, |
---|
823 | ("new fd is same as old")); |
---|
824 | |
---|
825 | /* |
---|
826 | * Save info on the descriptor being overwritten. We cannot close |
---|
827 | * it without introducing an ownership race for the slot, since we |
---|
828 | * need to drop the filedesc lock to call closef(). |
---|
829 | * |
---|
830 | * XXX this duplicates parts of close(). |
---|
831 | */ |
---|
832 | delfp = fdp->fd_ofiles[new]; |
---|
833 | holdleaders = 0; |
---|
834 | if (delfp != NULL) { |
---|
835 | if (td->td_proc->p_fdtol != NULL) { |
---|
836 | /* |
---|
837 | * Ask fdfree() to sleep to ensure that all relevant |
---|
838 | * process leaders can be traversed in closef(). |
---|
839 | */ |
---|
840 | fdp->fd_holdleaderscount++; |
---|
841 | holdleaders = 1; |
---|
842 | } |
---|
843 | } |
---|
844 | |
---|
845 | /* |
---|
846 | * Duplicate the source descriptor |
---|
847 | */ |
---|
848 | fdp->fd_ofiles[new] = fp; |
---|
849 | fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; |
---|
850 | if (new > fdp->fd_lastfile) |
---|
851 | fdp->fd_lastfile = new; |
---|
852 | *retval = new; |
---|
853 | |
---|
854 | /* |
---|
855 | * If we dup'd over a valid file, we now own the reference to it |
---|
856 | * and must dispose of it using closef() semantics (as if a |
---|
857 | * close() were performed on it). |
---|
858 | * |
---|
859 | * XXX this duplicates parts of close(). |
---|
860 | */ |
---|
861 | if (delfp != NULL) { |
---|
862 | knote_fdclose(td, new); |
---|
863 | if (delfp->f_type == DTYPE_MQUEUE) |
---|
864 | mq_fdclose(td, new, delfp); |
---|
865 | FILEDESC_XUNLOCK(fdp); |
---|
866 | (void) closef(delfp, td); |
---|
867 | if (holdleaders) { |
---|
868 | FILEDESC_XLOCK(fdp); |
---|
869 | fdp->fd_holdleaderscount--; |
---|
870 | if (fdp->fd_holdleaderscount == 0 && |
---|
871 | fdp->fd_holdleaderswakeup != 0) { |
---|
872 | fdp->fd_holdleaderswakeup = 0; |
---|
873 | wakeup(&fdp->fd_holdleaderscount); |
---|
874 | } |
---|
875 | FILEDESC_XUNLOCK(fdp); |
---|
876 | } |
---|
877 | } else { |
---|
878 | FILEDESC_XUNLOCK(fdp); |
---|
879 | } |
---|
880 | return (0); |
---|
881 | } |
---|
882 | |
---|
883 | /* |
---|
884 | * If sigio is on the list associated with a process or process group, |
---|
885 | * disable signalling from the device, remove sigio from the list and |
---|
886 | * free sigio. |
---|
887 | */ |
---|
888 | void |
---|
889 | funsetown(struct sigio **sigiop) |
---|
890 | { |
---|
891 | struct sigio *sigio; |
---|
892 | |
---|
893 | SIGIO_LOCK(); |
---|
894 | sigio = *sigiop; |
---|
895 | if (sigio == NULL) { |
---|
896 | SIGIO_UNLOCK(); |
---|
897 | return; |
---|
898 | } |
---|
899 | *(sigio->sio_myref) = NULL; |
---|
900 | if ((sigio)->sio_pgid < 0) { |
---|
901 | struct pgrp *pg = (sigio)->sio_pgrp; |
---|
902 | PGRP_LOCK(pg); |
---|
903 | SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio, |
---|
904 | sigio, sio_pgsigio); |
---|
905 | PGRP_UNLOCK(pg); |
---|
906 | } else { |
---|
907 | struct proc *p = (sigio)->sio_proc; |
---|
908 | PROC_LOCK(p); |
---|
909 | SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio, |
---|
910 | sigio, sio_pgsigio); |
---|
911 | PROC_UNLOCK(p); |
---|
912 | } |
---|
913 | SIGIO_UNLOCK(); |
---|
914 | crfree(sigio->sio_ucred); |
---|
915 | free(sigio, M_SIGIO); |
---|
916 | } |
---|
917 | |
---|
918 | /* |
---|
919 | * Free a list of sigio structures. |
---|
920 | * We only need to lock the SIGIO_LOCK because we have made ourselves |
---|
921 | * inaccessible to callers of fsetown and therefore do not need to lock |
---|
922 | * the proc or pgrp struct for the list manipulation. |
---|
923 | */ |
---|
924 | void |
---|
925 | funsetownlst(struct sigiolst *sigiolst) |
---|
926 | { |
---|
927 | struct proc *p; |
---|
928 | struct pgrp *pg; |
---|
929 | struct sigio *sigio; |
---|
930 | |
---|
931 | sigio = SLIST_FIRST(sigiolst); |
---|
932 | if (sigio == NULL) |
---|
933 | return; |
---|
934 | p = NULL; |
---|
935 | pg = NULL; |
---|
936 | |
---|
937 | /* |
---|
938 | * Every entry of the list should belong |
---|
939 | * to a single proc or pgrp. |
---|
940 | */ |
---|
941 | if (sigio->sio_pgid < 0) { |
---|
942 | pg = sigio->sio_pgrp; |
---|
943 | PGRP_LOCK_ASSERT(pg, MA_NOTOWNED); |
---|
944 | } else /* if (sigio->sio_pgid > 0) */ { |
---|
945 | p = sigio->sio_proc; |
---|
946 | PROC_LOCK_ASSERT(p, MA_NOTOWNED); |
---|
947 | } |
---|
948 | |
---|
949 | SIGIO_LOCK(); |
---|
950 | while ((sigio = SLIST_FIRST(sigiolst)) != NULL) { |
---|
951 | *(sigio->sio_myref) = NULL; |
---|
952 | if (pg != NULL) { |
---|
953 | KASSERT(sigio->sio_pgid < 0, |
---|
954 | ("Proc sigio in pgrp sigio list")); |
---|
955 | KASSERT(sigio->sio_pgrp == pg, |
---|
956 | ("Bogus pgrp in sigio list")); |
---|
957 | PGRP_LOCK(pg); |
---|
958 | SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, |
---|
959 | sio_pgsigio); |
---|
960 | PGRP_UNLOCK(pg); |
---|
961 | } else /* if (p != NULL) */ { |
---|
962 | KASSERT(sigio->sio_pgid > 0, |
---|
963 | ("Pgrp sigio in proc sigio list")); |
---|
964 | KASSERT(sigio->sio_proc == p, |
---|
965 | ("Bogus proc in sigio list")); |
---|
966 | PROC_LOCK(p); |
---|
967 | SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, |
---|
968 | sio_pgsigio); |
---|
969 | PROC_UNLOCK(p); |
---|
970 | } |
---|
971 | SIGIO_UNLOCK(); |
---|
972 | crfree(sigio->sio_ucred); |
---|
973 | free(sigio, M_SIGIO); |
---|
974 | SIGIO_LOCK(); |
---|
975 | } |
---|
976 | SIGIO_UNLOCK(); |
---|
977 | } |
---|
978 | |
---|
979 | /* |
---|
980 | * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). |
---|
981 | * |
---|
982 | * After permission checking, add a sigio structure to the sigio list for |
---|
983 | * the process or process group. |
---|
984 | */ |
---|
985 | int |
---|
986 | fsetown(pid_t pgid, struct sigio **sigiop) |
---|
987 | { |
---|
988 | struct proc *proc; |
---|
989 | struct pgrp *pgrp; |
---|
990 | struct sigio *sigio; |
---|
991 | int ret; |
---|
992 | |
---|
993 | if (pgid == 0) { |
---|
994 | funsetown(sigiop); |
---|
995 | return (0); |
---|
996 | } |
---|
997 | |
---|
998 | ret = 0; |
---|
999 | |
---|
1000 | /* Allocate and fill in the new sigio out of locks. */ |
---|
1001 | sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK); |
---|
1002 | sigio->sio_pgid = pgid; |
---|
1003 | sigio->sio_ucred = crhold(curthread->td_ucred); |
---|
1004 | sigio->sio_myref = sigiop; |
---|
1005 | |
---|
1006 | sx_slock(&proctree_lock); |
---|
1007 | if (pgid > 0) { |
---|
1008 | proc = pfind(pgid); |
---|
1009 | if (proc == NULL) { |
---|
1010 | ret = ESRCH; |
---|
1011 | goto fail; |
---|
1012 | } |
---|
1013 | |
---|
1014 | /* |
---|
1015 | * Policy - Don't allow a process to FSETOWN a process |
---|
1016 | * in another session. |
---|
1017 | * |
---|
1018 | * Remove this test to allow maximum flexibility or |
---|
1019 | * restrict FSETOWN to the current process or process |
---|
1020 | * group for maximum safety. |
---|
1021 | */ |
---|
1022 | PROC_UNLOCK(proc); |
---|
1023 | if (proc->p_session != curthread->td_proc->p_session) { |
---|
1024 | ret = EPERM; |
---|
1025 | goto fail; |
---|
1026 | } |
---|
1027 | |
---|
1028 | pgrp = NULL; |
---|
1029 | } else /* if (pgid < 0) */ { |
---|
1030 | pgrp = pgfind(-pgid); |
---|
1031 | if (pgrp == NULL) { |
---|
1032 | ret = ESRCH; |
---|
1033 | goto fail; |
---|
1034 | } |
---|
1035 | PGRP_UNLOCK(pgrp); |
---|
1036 | |
---|
1037 | /* |
---|
1038 | * Policy - Don't allow a process to FSETOWN a process |
---|
1039 | * in another session. |
---|
1040 | * |
---|
1041 | * Remove this test to allow maximum flexibility or |
---|
1042 | * restrict FSETOWN to the current process or process |
---|
1043 | * group for maximum safety. |
---|
1044 | */ |
---|
1045 | if (pgrp->pg_session != curthread->td_proc->p_session) { |
---|
1046 | ret = EPERM; |
---|
1047 | goto fail; |
---|
1048 | } |
---|
1049 | |
---|
1050 | proc = NULL; |
---|
1051 | } |
---|
1052 | funsetown(sigiop); |
---|
1053 | if (pgid > 0) { |
---|
1054 | PROC_LOCK(proc); |
---|
1055 | /* |
---|
1056 | * Since funsetownlst() is called without the proctree |
---|
1057 | * locked, we need to check for P_WEXIT. |
---|
1058 | * XXX: is ESRCH correct? |
---|
1059 | */ |
---|
1060 | if ((proc->p_flag & P_WEXIT) != 0) { |
---|
1061 | PROC_UNLOCK(proc); |
---|
1062 | ret = ESRCH; |
---|
1063 | goto fail; |
---|
1064 | } |
---|
1065 | SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio); |
---|
1066 | sigio->sio_proc = proc; |
---|
1067 | PROC_UNLOCK(proc); |
---|
1068 | } else { |
---|
1069 | PGRP_LOCK(pgrp); |
---|
1070 | SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); |
---|
1071 | sigio->sio_pgrp = pgrp; |
---|
1072 | PGRP_UNLOCK(pgrp); |
---|
1073 | } |
---|
1074 | sx_sunlock(&proctree_lock); |
---|
1075 | SIGIO_LOCK(); |
---|
1076 | *sigiop = sigio; |
---|
1077 | SIGIO_UNLOCK(); |
---|
1078 | return (0); |
---|
1079 | |
---|
1080 | fail: |
---|
1081 | sx_sunlock(&proctree_lock); |
---|
1082 | crfree(sigio->sio_ucred); |
---|
1083 | free(sigio, M_SIGIO); |
---|
1084 | return (ret); |
---|
1085 | } |
---|
1086 | |
---|
1087 | /* |
---|
1088 | * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). |
---|
1089 | */ |
---|
1090 | pid_t |
---|
1091 | fgetown(sigiop) |
---|
1092 | struct sigio **sigiop; |
---|
1093 | { |
---|
1094 | pid_t pgid; |
---|
1095 | |
---|
1096 | SIGIO_LOCK(); |
---|
1097 | pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0; |
---|
1098 | SIGIO_UNLOCK(); |
---|
1099 | return (pgid); |
---|
1100 | } |
---|
1101 | |
---|
1102 | /* |
---|
1103 | * Close a file descriptor. |
---|
1104 | */ |
---|
1105 | #ifndef _SYS_SYSPROTO_H_ |
---|
1106 | struct close_args { |
---|
1107 | int fd; |
---|
1108 | }; |
---|
1109 | #endif |
---|
1110 | /* ARGSUSED */ |
---|
1111 | int |
---|
1112 | close(td, uap) |
---|
1113 | struct thread *td; |
---|
1114 | struct close_args *uap; |
---|
1115 | { |
---|
1116 | |
---|
1117 | return (kern_close(td, uap->fd)); |
---|
1118 | } |
---|
1119 | |
---|
1120 | int |
---|
1121 | kern_close(td, fd) |
---|
1122 | struct thread *td; |
---|
1123 | int fd; |
---|
1124 | { |
---|
1125 | struct filedesc *fdp; |
---|
1126 | struct file *fp; |
---|
1127 | int error; |
---|
1128 | int holdleaders; |
---|
1129 | |
---|
1130 | error = 0; |
---|
1131 | holdleaders = 0; |
---|
1132 | fdp = td->td_proc->p_fd; |
---|
1133 | |
---|
1134 | AUDIT_SYSCLOSE(td, fd); |
---|
1135 | |
---|
1136 | FILEDESC_XLOCK(fdp); |
---|
1137 | if ((unsigned)fd >= fdp->fd_nfiles || |
---|
1138 | (fp = fdp->fd_ofiles[fd]) == NULL) { |
---|
1139 | FILEDESC_XUNLOCK(fdp); |
---|
1140 | return (EBADF); |
---|
1141 | } |
---|
1142 | fdp->fd_ofiles[fd] = NULL; |
---|
1143 | fdp->fd_ofileflags[fd] = 0; |
---|
1144 | fdunused(fdp, fd); |
---|
1145 | if (td->td_proc->p_fdtol != NULL) { |
---|
1146 | /* |
---|
1147 | * Ask fdfree() to sleep to ensure that all relevant |
---|
1148 | * process leaders can be traversed in closef(). |
---|
1149 | */ |
---|
1150 | fdp->fd_holdleaderscount++; |
---|
1151 | holdleaders = 1; |
---|
1152 | } |
---|
1153 | |
---|
1154 | /* |
---|
1155 | * We now hold the fp reference that used to be owned by the |
---|
1156 | * descriptor array. We have to unlock the FILEDESC *AFTER* |
---|
1157 | * knote_fdclose to prevent a race of the fd getting opened, a knote |
---|
1158 | * added, and deleteing a knote for the new fd. |
---|
1159 | */ |
---|
1160 | knote_fdclose(td, fd); |
---|
1161 | if (fp->f_type == DTYPE_MQUEUE) |
---|
1162 | mq_fdclose(td, fd, fp); |
---|
1163 | FILEDESC_XUNLOCK(fdp); |
---|
1164 | |
---|
1165 | error = closef(fp, td); |
---|
1166 | if (holdleaders) { |
---|
1167 | FILEDESC_XLOCK(fdp); |
---|
1168 | fdp->fd_holdleaderscount--; |
---|
1169 | if (fdp->fd_holdleaderscount == 0 && |
---|
1170 | fdp->fd_holdleaderswakeup != 0) { |
---|
1171 | fdp->fd_holdleaderswakeup = 0; |
---|
1172 | wakeup(&fdp->fd_holdleaderscount); |
---|
1173 | } |
---|
1174 | FILEDESC_XUNLOCK(fdp); |
---|
1175 | } |
---|
1176 | return (error); |
---|
1177 | } |
---|
1178 | |
---|
1179 | /* |
---|
1180 | * Close open file descriptors. |
---|
1181 | */ |
---|
1182 | #ifndef _SYS_SYSPROTO_H_ |
---|
1183 | struct closefrom_args { |
---|
1184 | int lowfd; |
---|
1185 | }; |
---|
1186 | #endif |
---|
1187 | /* ARGSUSED */ |
---|
1188 | int |
---|
1189 | closefrom(struct thread *td, struct closefrom_args *uap) |
---|
1190 | { |
---|
1191 | struct filedesc *fdp; |
---|
1192 | int fd; |
---|
1193 | |
---|
1194 | fdp = td->td_proc->p_fd; |
---|
1195 | AUDIT_ARG_FD(uap->lowfd); |
---|
1196 | |
---|
1197 | /* |
---|
1198 | * Treat negative starting file descriptor values identical to |
---|
1199 | * closefrom(0) which closes all files. |
---|
1200 | */ |
---|
1201 | if (uap->lowfd < 0) |
---|
1202 | uap->lowfd = 0; |
---|
1203 | FILEDESC_SLOCK(fdp); |
---|
1204 | for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) { |
---|
1205 | if (fdp->fd_ofiles[fd] != NULL) { |
---|
1206 | FILEDESC_SUNLOCK(fdp); |
---|
1207 | (void)kern_close(td, fd); |
---|
1208 | FILEDESC_SLOCK(fdp); |
---|
1209 | } |
---|
1210 | } |
---|
1211 | FILEDESC_SUNLOCK(fdp); |
---|
1212 | return (0); |
---|
1213 | } |
---|
1214 | |
---|
1215 | #if defined(COMPAT_43) |
---|
1216 | /* |
---|
1217 | * Return status information about a file descriptor. |
---|
1218 | */ |
---|
1219 | #ifndef _SYS_SYSPROTO_H_ |
---|
1220 | struct ofstat_args { |
---|
1221 | int fd; |
---|
1222 | struct ostat *sb; |
---|
1223 | }; |
---|
1224 | #endif |
---|
1225 | /* ARGSUSED */ |
---|
1226 | int |
---|
1227 | ofstat(struct thread *td, struct ofstat_args *uap) |
---|
1228 | { |
---|
1229 | struct ostat oub; |
---|
1230 | struct stat ub; |
---|
1231 | int error; |
---|
1232 | |
---|
1233 | error = kern_fstat(td, uap->fd, &ub); |
---|
1234 | if (error == 0) { |
---|
1235 | cvtstat(&ub, &oub); |
---|
1236 | error = copyout(&oub, uap->sb, sizeof(oub)); |
---|
1237 | } |
---|
1238 | return (error); |
---|
1239 | } |
---|
1240 | #endif /* COMPAT_43 */ |
---|
1241 | |
---|
1242 | /* |
---|
1243 | * Return status information about a file descriptor. |
---|
1244 | */ |
---|
1245 | #ifndef _SYS_SYSPROTO_H_ |
---|
1246 | struct fstat_args { |
---|
1247 | int fd; |
---|
1248 | struct stat *sb; |
---|
1249 | }; |
---|
1250 | #endif |
---|
1251 | /* ARGSUSED */ |
---|
1252 | int |
---|
1253 | fstat(struct thread *td, struct fstat_args *uap) |
---|
1254 | { |
---|
1255 | struct stat ub; |
---|
1256 | int error; |
---|
1257 | |
---|
1258 | error = kern_fstat(td, uap->fd, &ub); |
---|
1259 | if (error == 0) |
---|
1260 | error = copyout(&ub, uap->sb, sizeof(ub)); |
---|
1261 | return (error); |
---|
1262 | } |
---|
1263 | |
---|
1264 | int |
---|
1265 | kern_fstat(struct thread *td, int fd, struct stat *sbp) |
---|
1266 | { |
---|
1267 | struct file *fp; |
---|
1268 | int error; |
---|
1269 | |
---|
1270 | AUDIT_ARG_FD(fd); |
---|
1271 | |
---|
1272 | if ((error = fget(td, fd, &fp)) != 0) |
---|
1273 | return (error); |
---|
1274 | |
---|
1275 | AUDIT_ARG_FILE(td->td_proc, fp); |
---|
1276 | |
---|
1277 | error = fo_stat(fp, sbp, td->td_ucred, td); |
---|
1278 | fdrop(fp, td); |
---|
1279 | #ifdef KTRACE |
---|
1280 | if (error == 0 && KTRPOINT(td, KTR_STRUCT)) |
---|
1281 | ktrstat(sbp); |
---|
1282 | #endif |
---|
1283 | return (error); |
---|
1284 | } |
---|
1285 | |
---|
1286 | /* |
---|
1287 | * Return status information about a file descriptor. |
---|
1288 | */ |
---|
1289 | #ifndef _SYS_SYSPROTO_H_ |
---|
1290 | struct nfstat_args { |
---|
1291 | int fd; |
---|
1292 | struct nstat *sb; |
---|
1293 | }; |
---|
1294 | #endif |
---|
1295 | /* ARGSUSED */ |
---|
1296 | int |
---|
1297 | nfstat(struct thread *td, struct nfstat_args *uap) |
---|
1298 | { |
---|
1299 | struct nstat nub; |
---|
1300 | struct stat ub; |
---|
1301 | int error; |
---|
1302 | |
---|
1303 | error = kern_fstat(td, uap->fd, &ub); |
---|
1304 | if (error == 0) { |
---|
1305 | cvtnstat(&ub, &nub); |
---|
1306 | error = copyout(&nub, uap->sb, sizeof(nub)); |
---|
1307 | } |
---|
1308 | return (error); |
---|
1309 | } |
---|
1310 | |
---|
1311 | /* |
---|
1312 | * Return pathconf information about a file descriptor. |
---|
1313 | */ |
---|
1314 | #ifndef _SYS_SYSPROTO_H_ |
---|
1315 | struct fpathconf_args { |
---|
1316 | int fd; |
---|
1317 | int name; |
---|
1318 | }; |
---|
1319 | #endif |
---|
1320 | /* ARGSUSED */ |
---|
1321 | int |
---|
1322 | fpathconf(struct thread *td, struct fpathconf_args *uap) |
---|
1323 | { |
---|
1324 | struct file *fp; |
---|
1325 | struct vnode *vp; |
---|
1326 | int error; |
---|
1327 | |
---|
1328 | if ((error = fget(td, uap->fd, &fp)) != 0) |
---|
1329 | return (error); |
---|
1330 | |
---|
1331 | /* If asynchronous I/O is available, it works for all descriptors. */ |
---|
1332 | if (uap->name == _PC_ASYNC_IO) { |
---|
1333 | td->td_retval[0] = async_io_version; |
---|
1334 | goto out; |
---|
1335 | } |
---|
1336 | vp = fp->f_vnode; |
---|
1337 | if (vp != NULL) { |
---|
1338 | int vfslocked; |
---|
1339 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
1340 | vn_lock(vp, LK_SHARED | LK_RETRY); |
---|
1341 | error = VOP_PATHCONF(vp, uap->name, td->td_retval); |
---|
1342 | VOP_UNLOCK(vp, 0); |
---|
1343 | VFS_UNLOCK_GIANT(vfslocked); |
---|
1344 | } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { |
---|
1345 | if (uap->name != _PC_PIPE_BUF) { |
---|
1346 | error = EINVAL; |
---|
1347 | } else { |
---|
1348 | td->td_retval[0] = PIPE_BUF; |
---|
1349 | error = 0; |
---|
1350 | } |
---|
1351 | } else { |
---|
1352 | error = EOPNOTSUPP; |
---|
1353 | } |
---|
1354 | out: |
---|
1355 | fdrop(fp, td); |
---|
1356 | return (error); |
---|
1357 | } |
---|
1358 | |
---|
1359 | /* |
---|
1360 | * Grow the file table to accomodate (at least) nfd descriptors. This may |
---|
1361 | * block and drop the filedesc lock, but it will reacquire it before |
---|
1362 | * returning. |
---|
1363 | */ |
---|
1364 | static void |
---|
1365 | fdgrowtable(struct filedesc *fdp, int nfd) |
---|
1366 | { |
---|
1367 | struct filedesc0 *fdp0; |
---|
1368 | struct freetable *fo; |
---|
1369 | struct file **ntable; |
---|
1370 | struct file **otable; |
---|
1371 | char *nfileflags; |
---|
1372 | int nnfiles, onfiles; |
---|
1373 | NDSLOTTYPE *nmap; |
---|
1374 | |
---|
1375 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
1376 | |
---|
1377 | KASSERT(fdp->fd_nfiles > 0, |
---|
1378 | ("zero-length file table")); |
---|
1379 | |
---|
1380 | /* compute the size of the new table */ |
---|
1381 | onfiles = fdp->fd_nfiles; |
---|
1382 | nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */ |
---|
1383 | if (nnfiles <= onfiles) |
---|
1384 | /* the table is already large enough */ |
---|
1385 | return; |
---|
1386 | |
---|
1387 | /* allocate a new table and (if required) new bitmaps */ |
---|
1388 | FILEDESC_XUNLOCK(fdp); |
---|
1389 | ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable), |
---|
1390 | M_FILEDESC, M_ZERO | M_WAITOK); |
---|
1391 | nfileflags = (char *)&ntable[nnfiles]; |
---|
1392 | if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) |
---|
1393 | nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, |
---|
1394 | M_FILEDESC, M_ZERO | M_WAITOK); |
---|
1395 | else |
---|
1396 | nmap = NULL; |
---|
1397 | FILEDESC_XLOCK(fdp); |
---|
1398 | |
---|
1399 | /* |
---|
1400 | * We now have new tables ready to go. Since we dropped the |
---|
1401 | * filedesc lock to call malloc(), watch out for a race. |
---|
1402 | */ |
---|
1403 | onfiles = fdp->fd_nfiles; |
---|
1404 | if (onfiles >= nnfiles) { |
---|
1405 | /* we lost the race, but that's OK */ |
---|
1406 | free(ntable, M_FILEDESC); |
---|
1407 | if (nmap != NULL) |
---|
1408 | free(nmap, M_FILEDESC); |
---|
1409 | return; |
---|
1410 | } |
---|
1411 | bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable)); |
---|
1412 | bcopy(fdp->fd_ofileflags, nfileflags, onfiles); |
---|
1413 | otable = fdp->fd_ofiles; |
---|
1414 | fdp->fd_ofileflags = nfileflags; |
---|
1415 | fdp->fd_ofiles = ntable; |
---|
1416 | /* |
---|
1417 | * We must preserve ofiles until the process exits because we can't |
---|
1418 | * be certain that no threads have references to the old table via |
---|
1419 | * _fget(). |
---|
1420 | */ |
---|
1421 | if (onfiles > NDFILE) { |
---|
1422 | fo = (struct freetable *)&otable[onfiles]; |
---|
1423 | fdp0 = (struct filedesc0 *)fdp; |
---|
1424 | fo->ft_table = otable; |
---|
1425 | SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next); |
---|
1426 | } |
---|
1427 | if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { |
---|
1428 | bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap)); |
---|
1429 | if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) |
---|
1430 | free(fdp->fd_map, M_FILEDESC); |
---|
1431 | fdp->fd_map = nmap; |
---|
1432 | } |
---|
1433 | fdp->fd_nfiles = nnfiles; |
---|
1434 | } |
---|
1435 | |
---|
1436 | /* |
---|
1437 | * Allocate a file descriptor for the process. |
---|
1438 | */ |
---|
1439 | int |
---|
1440 | fdalloc(struct thread *td, int minfd, int *result) |
---|
1441 | { |
---|
1442 | struct proc *p = td->td_proc; |
---|
1443 | struct filedesc *fdp = p->p_fd; |
---|
1444 | int fd = -1, maxfd; |
---|
1445 | |
---|
1446 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
1447 | |
---|
1448 | if (fdp->fd_freefile > minfd) |
---|
1449 | minfd = fdp->fd_freefile; |
---|
1450 | |
---|
1451 | PROC_LOCK(p); |
---|
1452 | maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
1453 | PROC_UNLOCK(p); |
---|
1454 | |
---|
1455 | /* |
---|
1456 | * Search the bitmap for a free descriptor. If none is found, try |
---|
1457 | * to grow the file table. Keep at it until we either get a file |
---|
1458 | * descriptor or run into process or system limits; fdgrowtable() |
---|
1459 | * may drop the filedesc lock, so we're in a race. |
---|
1460 | */ |
---|
1461 | for (;;) { |
---|
1462 | fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); |
---|
1463 | if (fd >= maxfd) |
---|
1464 | return (EMFILE); |
---|
1465 | if (fd < fdp->fd_nfiles) |
---|
1466 | break; |
---|
1467 | fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd)); |
---|
1468 | } |
---|
1469 | |
---|
1470 | /* |
---|
1471 | * Perform some sanity checks, then mark the file descriptor as |
---|
1472 | * used and return it to the caller. |
---|
1473 | */ |
---|
1474 | KASSERT(!fdisused(fdp, fd), |
---|
1475 | ("fd_first_free() returned non-free descriptor")); |
---|
1476 | KASSERT(fdp->fd_ofiles[fd] == NULL, |
---|
1477 | ("free descriptor isn't")); |
---|
1478 | fdp->fd_ofileflags[fd] = 0; /* XXX needed? */ |
---|
1479 | fdused(fdp, fd); |
---|
1480 | *result = fd; |
---|
1481 | return (0); |
---|
1482 | } |
---|
1483 | |
---|
1484 | /* |
---|
1485 | * Check to see whether n user file descriptors are available to the process |
---|
1486 | * p. |
---|
1487 | */ |
---|
1488 | int |
---|
1489 | fdavail(struct thread *td, int n) |
---|
1490 | { |
---|
1491 | struct proc *p = td->td_proc; |
---|
1492 | struct filedesc *fdp = td->td_proc->p_fd; |
---|
1493 | struct file **fpp; |
---|
1494 | int i, lim, last; |
---|
1495 | |
---|
1496 | FILEDESC_LOCK_ASSERT(fdp); |
---|
1497 | |
---|
1498 | PROC_LOCK(p); |
---|
1499 | lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
1500 | PROC_UNLOCK(p); |
---|
1501 | if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) |
---|
1502 | return (1); |
---|
1503 | last = min(fdp->fd_nfiles, lim); |
---|
1504 | fpp = &fdp->fd_ofiles[fdp->fd_freefile]; |
---|
1505 | for (i = last - fdp->fd_freefile; --i >= 0; fpp++) { |
---|
1506 | if (*fpp == NULL && --n <= 0) |
---|
1507 | return (1); |
---|
1508 | } |
---|
1509 | return (0); |
---|
1510 | } |
---|
1511 | |
---|
1512 | /* |
---|
1513 | * Create a new open file structure and allocate a file decriptor for the |
---|
1514 | * process that refers to it. We add one reference to the file for the |
---|
1515 | * descriptor table and one reference for resultfp. This is to prevent us |
---|
1516 | * being preempted and the entry in the descriptor table closed after we |
---|
1517 | * release the FILEDESC lock. |
---|
1518 | */ |
---|
1519 | int |
---|
1520 | falloc(struct thread *td, struct file **resultfp, int *resultfd) |
---|
1521 | { |
---|
1522 | struct proc *p = td->td_proc; |
---|
1523 | struct file *fp; |
---|
1524 | int error, i; |
---|
1525 | int maxuserfiles = maxfiles - (maxfiles / 20); |
---|
1526 | static struct timeval lastfail; |
---|
1527 | static int curfail; |
---|
1528 | |
---|
1529 | fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); |
---|
1530 | if ((openfiles >= maxuserfiles && |
---|
1531 | priv_check(td, PRIV_MAXFILES) != 0) || |
---|
1532 | openfiles >= maxfiles) { |
---|
1533 | if (ppsratecheck(&lastfail, &curfail, 1)) { |
---|
1534 | printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", |
---|
1535 | td->td_ucred->cr_ruid); |
---|
1536 | } |
---|
1537 | uma_zfree(file_zone, fp); |
---|
1538 | return (ENFILE); |
---|
1539 | } |
---|
1540 | atomic_add_int(&openfiles, 1); |
---|
1541 | |
---|
1542 | /* |
---|
1543 | * If the process has file descriptor zero open, add the new file |
---|
1544 | * descriptor to the list of open files at that point, otherwise |
---|
1545 | * put it at the front of the list of open files. |
---|
1546 | */ |
---|
1547 | refcount_init(&fp->f_count, 1); |
---|
1548 | if (resultfp) |
---|
1549 | fhold(fp); |
---|
1550 | fp->f_cred = crhold(td->td_ucred); |
---|
1551 | fp->f_ops = &badfileops; |
---|
1552 | fp->f_data = NULL; |
---|
1553 | fp->f_vnode = NULL; |
---|
1554 | FILEDESC_XLOCK(p->p_fd); |
---|
1555 | if ((error = fdalloc(td, 0, &i))) { |
---|
1556 | FILEDESC_XUNLOCK(p->p_fd); |
---|
1557 | |
---|
1558 | fdrop(fp, td); |
---|
1559 | if (resultfp) |
---|
1560 | fdrop(fp, td); |
---|
1561 | return (error); |
---|
1562 | } |
---|
1563 | p->p_fd->fd_ofiles[i] = fp; |
---|
1564 | FILEDESC_XUNLOCK(p->p_fd); |
---|
1565 | if (resultfp) |
---|
1566 | *resultfp = fp; |
---|
1567 | if (resultfd) |
---|
1568 | *resultfd = i; |
---|
1569 | return (0); |
---|
1570 | } |
---|
1571 | |
---|
1572 | /* |
---|
1573 | * Build a new filedesc structure from another. |
---|
1574 | * Copy the current, root, and jail root vnode references. |
---|
1575 | */ |
---|
1576 | struct filedesc * |
---|
1577 | fdinit(struct filedesc *fdp) |
---|
1578 | { |
---|
1579 | struct filedesc0 *newfdp; |
---|
1580 | |
---|
1581 | newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO); |
---|
1582 | FILEDESC_LOCK_INIT(&newfdp->fd_fd); |
---|
1583 | if (fdp != NULL) { |
---|
1584 | FILEDESC_XLOCK(fdp); |
---|
1585 | newfdp->fd_fd.fd_cdir = fdp->fd_cdir; |
---|
1586 | if (newfdp->fd_fd.fd_cdir) |
---|
1587 | VREF(newfdp->fd_fd.fd_cdir); |
---|
1588 | newfdp->fd_fd.fd_rdir = fdp->fd_rdir; |
---|
1589 | if (newfdp->fd_fd.fd_rdir) |
---|
1590 | VREF(newfdp->fd_fd.fd_rdir); |
---|
1591 | newfdp->fd_fd.fd_jdir = fdp->fd_jdir; |
---|
1592 | if (newfdp->fd_fd.fd_jdir) |
---|
1593 | VREF(newfdp->fd_fd.fd_jdir); |
---|
1594 | FILEDESC_XUNLOCK(fdp); |
---|
1595 | } |
---|
1596 | |
---|
1597 | /* Create the file descriptor table. */ |
---|
1598 | newfdp->fd_fd.fd_refcnt = 1; |
---|
1599 | newfdp->fd_fd.fd_holdcnt = 1; |
---|
1600 | newfdp->fd_fd.fd_cmask = CMASK; |
---|
1601 | newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles; |
---|
1602 | newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags; |
---|
1603 | newfdp->fd_fd.fd_nfiles = NDFILE; |
---|
1604 | newfdp->fd_fd.fd_map = newfdp->fd_dmap; |
---|
1605 | newfdp->fd_fd.fd_lastfile = -1; |
---|
1606 | return (&newfdp->fd_fd); |
---|
1607 | } |
---|
1608 | |
---|
1609 | static struct filedesc * |
---|
1610 | fdhold(struct proc *p) |
---|
1611 | { |
---|
1612 | struct filedesc *fdp; |
---|
1613 | |
---|
1614 | mtx_lock(&fdesc_mtx); |
---|
1615 | fdp = p->p_fd; |
---|
1616 | if (fdp != NULL) |
---|
1617 | fdp->fd_holdcnt++; |
---|
1618 | mtx_unlock(&fdesc_mtx); |
---|
1619 | return (fdp); |
---|
1620 | } |
---|
1621 | |
---|
1622 | static void |
---|
1623 | fddrop(struct filedesc *fdp) |
---|
1624 | { |
---|
1625 | struct filedesc0 *fdp0; |
---|
1626 | struct freetable *ft; |
---|
1627 | int i; |
---|
1628 | |
---|
1629 | mtx_lock(&fdesc_mtx); |
---|
1630 | i = --fdp->fd_holdcnt; |
---|
1631 | mtx_unlock(&fdesc_mtx); |
---|
1632 | if (i > 0) |
---|
1633 | return; |
---|
1634 | |
---|
1635 | FILEDESC_LOCK_DESTROY(fdp); |
---|
1636 | fdp0 = (struct filedesc0 *)fdp; |
---|
1637 | while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) { |
---|
1638 | SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next); |
---|
1639 | free(ft->ft_table, M_FILEDESC); |
---|
1640 | } |
---|
1641 | free(fdp, M_FILEDESC); |
---|
1642 | } |
---|
1643 | |
---|
1644 | /* |
---|
1645 | * Share a filedesc structure. |
---|
1646 | */ |
---|
1647 | struct filedesc * |
---|
1648 | fdshare(struct filedesc *fdp) |
---|
1649 | { |
---|
1650 | |
---|
1651 | FILEDESC_XLOCK(fdp); |
---|
1652 | fdp->fd_refcnt++; |
---|
1653 | FILEDESC_XUNLOCK(fdp); |
---|
1654 | return (fdp); |
---|
1655 | } |
---|
1656 | |
---|
1657 | /* |
---|
1658 | * Unshare a filedesc structure, if necessary by making a copy |
---|
1659 | */ |
---|
1660 | void |
---|
1661 | fdunshare(struct proc *p, struct thread *td) |
---|
1662 | { |
---|
1663 | |
---|
1664 | FILEDESC_XLOCK(p->p_fd); |
---|
1665 | if (p->p_fd->fd_refcnt > 1) { |
---|
1666 | struct filedesc *tmp; |
---|
1667 | |
---|
1668 | FILEDESC_XUNLOCK(p->p_fd); |
---|
1669 | tmp = fdcopy(p->p_fd); |
---|
1670 | fdfree(td); |
---|
1671 | p->p_fd = tmp; |
---|
1672 | } else |
---|
1673 | FILEDESC_XUNLOCK(p->p_fd); |
---|
1674 | } |
---|
1675 | |
---|
1676 | /* |
---|
1677 | * Copy a filedesc structure. A NULL pointer in returns a NULL reference, |
---|
1678 | * this is to ease callers, not catch errors. |
---|
1679 | */ |
---|
1680 | struct filedesc * |
---|
1681 | fdcopy(struct filedesc *fdp) |
---|
1682 | { |
---|
1683 | struct filedesc *newfdp; |
---|
1684 | int i; |
---|
1685 | |
---|
1686 | /* Certain daemons might not have file descriptors. */ |
---|
1687 | if (fdp == NULL) |
---|
1688 | return (NULL); |
---|
1689 | |
---|
1690 | newfdp = fdinit(fdp); |
---|
1691 | FILEDESC_SLOCK(fdp); |
---|
1692 | while (fdp->fd_lastfile >= newfdp->fd_nfiles) { |
---|
1693 | FILEDESC_SUNLOCK(fdp); |
---|
1694 | FILEDESC_XLOCK(newfdp); |
---|
1695 | fdgrowtable(newfdp, fdp->fd_lastfile + 1); |
---|
1696 | FILEDESC_XUNLOCK(newfdp); |
---|
1697 | FILEDESC_SLOCK(fdp); |
---|
1698 | } |
---|
1699 | /* copy everything except kqueue descriptors */ |
---|
1700 | newfdp->fd_freefile = -1; |
---|
1701 | for (i = 0; i <= fdp->fd_lastfile; ++i) { |
---|
1702 | if (fdisused(fdp, i) && |
---|
1703 | fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE && |
---|
1704 | fdp->fd_ofiles[i]->f_ops != &badfileops) { |
---|
1705 | newfdp->fd_ofiles[i] = fdp->fd_ofiles[i]; |
---|
1706 | newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i]; |
---|
1707 | fhold(newfdp->fd_ofiles[i]); |
---|
1708 | newfdp->fd_lastfile = i; |
---|
1709 | } else { |
---|
1710 | if (newfdp->fd_freefile == -1) |
---|
1711 | newfdp->fd_freefile = i; |
---|
1712 | } |
---|
1713 | } |
---|
1714 | newfdp->fd_cmask = fdp->fd_cmask; |
---|
1715 | FILEDESC_SUNLOCK(fdp); |
---|
1716 | FILEDESC_XLOCK(newfdp); |
---|
1717 | for (i = 0; i <= newfdp->fd_lastfile; ++i) |
---|
1718 | if (newfdp->fd_ofiles[i] != NULL) |
---|
1719 | fdused(newfdp, i); |
---|
1720 | if (newfdp->fd_freefile == -1) |
---|
1721 | newfdp->fd_freefile = i; |
---|
1722 | FILEDESC_XUNLOCK(newfdp); |
---|
1723 | return (newfdp); |
---|
1724 | } |
---|
1725 | |
---|
1726 | /* |
---|
1727 | * Release a filedesc structure. |
---|
1728 | */ |
---|
1729 | void |
---|
1730 | fdfree(struct thread *td) |
---|
1731 | { |
---|
1732 | struct filedesc *fdp; |
---|
1733 | struct file **fpp; |
---|
1734 | int i, locked; |
---|
1735 | struct filedesc_to_leader *fdtol; |
---|
1736 | struct file *fp; |
---|
1737 | struct vnode *cdir, *jdir, *rdir, *vp; |
---|
1738 | struct flock lf; |
---|
1739 | |
---|
1740 | /* Certain daemons might not have file descriptors. */ |
---|
1741 | fdp = td->td_proc->p_fd; |
---|
1742 | if (fdp == NULL) |
---|
1743 | return; |
---|
1744 | |
---|
1745 | /* Check for special need to clear POSIX style locks */ |
---|
1746 | fdtol = td->td_proc->p_fdtol; |
---|
1747 | if (fdtol != NULL) { |
---|
1748 | FILEDESC_XLOCK(fdp); |
---|
1749 | KASSERT(fdtol->fdl_refcount > 0, |
---|
1750 | ("filedesc_to_refcount botch: fdl_refcount=%d", |
---|
1751 | fdtol->fdl_refcount)); |
---|
1752 | if (fdtol->fdl_refcount == 1 && |
---|
1753 | (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
1754 | for (i = 0, fpp = fdp->fd_ofiles; |
---|
1755 | i <= fdp->fd_lastfile; |
---|
1756 | i++, fpp++) { |
---|
1757 | if (*fpp == NULL || |
---|
1758 | (*fpp)->f_type != DTYPE_VNODE) |
---|
1759 | continue; |
---|
1760 | fp = *fpp; |
---|
1761 | fhold(fp); |
---|
1762 | FILEDESC_XUNLOCK(fdp); |
---|
1763 | lf.l_whence = SEEK_SET; |
---|
1764 | lf.l_start = 0; |
---|
1765 | lf.l_len = 0; |
---|
1766 | lf.l_type = F_UNLCK; |
---|
1767 | vp = fp->f_vnode; |
---|
1768 | locked = VFS_LOCK_GIANT(vp->v_mount); |
---|
1769 | (void) VOP_ADVLOCK(vp, |
---|
1770 | (caddr_t)td->td_proc-> |
---|
1771 | p_leader, |
---|
1772 | F_UNLCK, |
---|
1773 | &lf, |
---|
1774 | F_POSIX); |
---|
1775 | VFS_UNLOCK_GIANT(locked); |
---|
1776 | FILEDESC_XLOCK(fdp); |
---|
1777 | fdrop(fp, td); |
---|
1778 | fpp = fdp->fd_ofiles + i; |
---|
1779 | } |
---|
1780 | } |
---|
1781 | retry: |
---|
1782 | if (fdtol->fdl_refcount == 1) { |
---|
1783 | if (fdp->fd_holdleaderscount > 0 && |
---|
1784 | (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
1785 | /* |
---|
1786 | * close() or do_dup() has cleared a reference |
---|
1787 | * in a shared file descriptor table. |
---|
1788 | */ |
---|
1789 | fdp->fd_holdleaderswakeup = 1; |
---|
1790 | sx_sleep(&fdp->fd_holdleaderscount, |
---|
1791 | FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); |
---|
1792 | goto retry; |
---|
1793 | } |
---|
1794 | if (fdtol->fdl_holdcount > 0) { |
---|
1795 | /* |
---|
1796 | * Ensure that fdtol->fdl_leader remains |
---|
1797 | * valid in closef(). |
---|
1798 | */ |
---|
1799 | fdtol->fdl_wakeup = 1; |
---|
1800 | sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK, |
---|
1801 | "fdlhold", 0); |
---|
1802 | goto retry; |
---|
1803 | } |
---|
1804 | } |
---|
1805 | fdtol->fdl_refcount--; |
---|
1806 | if (fdtol->fdl_refcount == 0 && |
---|
1807 | fdtol->fdl_holdcount == 0) { |
---|
1808 | fdtol->fdl_next->fdl_prev = fdtol->fdl_prev; |
---|
1809 | fdtol->fdl_prev->fdl_next = fdtol->fdl_next; |
---|
1810 | } else |
---|
1811 | fdtol = NULL; |
---|
1812 | td->td_proc->p_fdtol = NULL; |
---|
1813 | FILEDESC_XUNLOCK(fdp); |
---|
1814 | if (fdtol != NULL) |
---|
1815 | free(fdtol, M_FILEDESC_TO_LEADER); |
---|
1816 | } |
---|
1817 | FILEDESC_XLOCK(fdp); |
---|
1818 | i = --fdp->fd_refcnt; |
---|
1819 | FILEDESC_XUNLOCK(fdp); |
---|
1820 | if (i > 0) |
---|
1821 | return; |
---|
1822 | |
---|
1823 | fpp = fdp->fd_ofiles; |
---|
1824 | for (i = fdp->fd_lastfile; i-- >= 0; fpp++) { |
---|
1825 | if (*fpp) { |
---|
1826 | FILEDESC_XLOCK(fdp); |
---|
1827 | fp = *fpp; |
---|
1828 | *fpp = NULL; |
---|
1829 | FILEDESC_XUNLOCK(fdp); |
---|
1830 | (void) closef(fp, td); |
---|
1831 | } |
---|
1832 | } |
---|
1833 | FILEDESC_XLOCK(fdp); |
---|
1834 | |
---|
1835 | /* XXX This should happen earlier. */ |
---|
1836 | mtx_lock(&fdesc_mtx); |
---|
1837 | td->td_proc->p_fd = NULL; |
---|
1838 | mtx_unlock(&fdesc_mtx); |
---|
1839 | |
---|
1840 | if (fdp->fd_nfiles > NDFILE) |
---|
1841 | free(fdp->fd_ofiles, M_FILEDESC); |
---|
1842 | if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) |
---|
1843 | free(fdp->fd_map, M_FILEDESC); |
---|
1844 | |
---|
1845 | fdp->fd_nfiles = 0; |
---|
1846 | |
---|
1847 | cdir = fdp->fd_cdir; |
---|
1848 | fdp->fd_cdir = NULL; |
---|
1849 | rdir = fdp->fd_rdir; |
---|
1850 | fdp->fd_rdir = NULL; |
---|
1851 | jdir = fdp->fd_jdir; |
---|
1852 | fdp->fd_jdir = NULL; |
---|
1853 | FILEDESC_XUNLOCK(fdp); |
---|
1854 | |
---|
1855 | if (cdir) { |
---|
1856 | locked = VFS_LOCK_GIANT(cdir->v_mount); |
---|
1857 | vrele(cdir); |
---|
1858 | VFS_UNLOCK_GIANT(locked); |
---|
1859 | } |
---|
1860 | if (rdir) { |
---|
1861 | locked = VFS_LOCK_GIANT(rdir->v_mount); |
---|
1862 | vrele(rdir); |
---|
1863 | VFS_UNLOCK_GIANT(locked); |
---|
1864 | } |
---|
1865 | if (jdir) { |
---|
1866 | locked = VFS_LOCK_GIANT(jdir->v_mount); |
---|
1867 | vrele(jdir); |
---|
1868 | VFS_UNLOCK_GIANT(locked); |
---|
1869 | } |
---|
1870 | |
---|
1871 | fddrop(fdp); |
---|
1872 | } |
---|
1873 | |
---|
1874 | /* |
---|
1875 | * For setugid programs, we don't want to people to use that setugidness |
---|
1876 | * to generate error messages which write to a file which otherwise would |
---|
1877 | * otherwise be off-limits to the process. We check for filesystems where |
---|
1878 | * the vnode can change out from under us after execve (like [lin]procfs). |
---|
1879 | * |
---|
1880 | * Since setugidsafety calls this only for fd 0, 1 and 2, this check is |
---|
1881 | * sufficient. We also don't check for setugidness since we know we are. |
---|
1882 | */ |
---|
1883 | static int |
---|
1884 | is_unsafe(struct file *fp) |
---|
1885 | { |
---|
1886 | if (fp->f_type == DTYPE_VNODE) { |
---|
1887 | struct vnode *vp = fp->f_vnode; |
---|
1888 | |
---|
1889 | if ((vp->v_vflag & VV_PROCDEP) != 0) |
---|
1890 | return (1); |
---|
1891 | } |
---|
1892 | return (0); |
---|
1893 | } |
---|
1894 | |
---|
1895 | /* |
---|
1896 | * Make this setguid thing safe, if at all possible. |
---|
1897 | */ |
---|
1898 | void |
---|
1899 | setugidsafety(struct thread *td) |
---|
1900 | { |
---|
1901 | struct filedesc *fdp; |
---|
1902 | int i; |
---|
1903 | |
---|
1904 | /* Certain daemons might not have file descriptors. */ |
---|
1905 | fdp = td->td_proc->p_fd; |
---|
1906 | if (fdp == NULL) |
---|
1907 | return; |
---|
1908 | |
---|
1909 | /* |
---|
1910 | * Note: fdp->fd_ofiles may be reallocated out from under us while |
---|
1911 | * we are blocked in a close. Be careful! |
---|
1912 | */ |
---|
1913 | FILEDESC_XLOCK(fdp); |
---|
1914 | for (i = 0; i <= fdp->fd_lastfile; i++) { |
---|
1915 | if (i > 2) |
---|
1916 | break; |
---|
1917 | if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) { |
---|
1918 | struct file *fp; |
---|
1919 | |
---|
1920 | knote_fdclose(td, i); |
---|
1921 | /* |
---|
1922 | * NULL-out descriptor prior to close to avoid |
---|
1923 | * a race while close blocks. |
---|
1924 | */ |
---|
1925 | fp = fdp->fd_ofiles[i]; |
---|
1926 | fdp->fd_ofiles[i] = NULL; |
---|
1927 | fdp->fd_ofileflags[i] = 0; |
---|
1928 | fdunused(fdp, i); |
---|
1929 | FILEDESC_XUNLOCK(fdp); |
---|
1930 | (void) closef(fp, td); |
---|
1931 | FILEDESC_XLOCK(fdp); |
---|
1932 | } |
---|
1933 | } |
---|
1934 | FILEDESC_XUNLOCK(fdp); |
---|
1935 | } |
---|
1936 | |
---|
1937 | /* |
---|
1938 | * If a specific file object occupies a specific file descriptor, close the |
---|
1939 | * file descriptor entry and drop a reference on the file object. This is a |
---|
1940 | * convenience function to handle a subsequent error in a function that calls |
---|
1941 | * falloc() that handles the race that another thread might have closed the |
---|
1942 | * file descriptor out from under the thread creating the file object. |
---|
1943 | */ |
---|
1944 | void |
---|
1945 | fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td) |
---|
1946 | { |
---|
1947 | |
---|
1948 | FILEDESC_XLOCK(fdp); |
---|
1949 | if (fdp->fd_ofiles[idx] == fp) { |
---|
1950 | fdp->fd_ofiles[idx] = NULL; |
---|
1951 | fdunused(fdp, idx); |
---|
1952 | FILEDESC_XUNLOCK(fdp); |
---|
1953 | fdrop(fp, td); |
---|
1954 | } else |
---|
1955 | FILEDESC_XUNLOCK(fdp); |
---|
1956 | } |
---|
1957 | |
---|
1958 | /* |
---|
1959 | * Close any files on exec? |
---|
1960 | */ |
---|
1961 | void |
---|
1962 | fdcloseexec(struct thread *td) |
---|
1963 | { |
---|
1964 | struct filedesc *fdp; |
---|
1965 | int i; |
---|
1966 | |
---|
1967 | /* Certain daemons might not have file descriptors. */ |
---|
1968 | fdp = td->td_proc->p_fd; |
---|
1969 | if (fdp == NULL) |
---|
1970 | return; |
---|
1971 | |
---|
1972 | FILEDESC_XLOCK(fdp); |
---|
1973 | |
---|
1974 | /* |
---|
1975 | * We cannot cache fd_ofiles or fd_ofileflags since operations |
---|
1976 | * may block and rip them out from under us. |
---|
1977 | */ |
---|
1978 | for (i = 0; i <= fdp->fd_lastfile; i++) { |
---|
1979 | if (fdp->fd_ofiles[i] != NULL && |
---|
1980 | (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE || |
---|
1981 | (fdp->fd_ofileflags[i] & UF_EXCLOSE))) { |
---|
1982 | struct file *fp; |
---|
1983 | |
---|
1984 | knote_fdclose(td, i); |
---|
1985 | /* |
---|
1986 | * NULL-out descriptor prior to close to avoid |
---|
1987 | * a race while close blocks. |
---|
1988 | */ |
---|
1989 | fp = fdp->fd_ofiles[i]; |
---|
1990 | fdp->fd_ofiles[i] = NULL; |
---|
1991 | fdp->fd_ofileflags[i] = 0; |
---|
1992 | fdunused(fdp, i); |
---|
1993 | if (fp->f_type == DTYPE_MQUEUE) |
---|
1994 | mq_fdclose(td, i, fp); |
---|
1995 | FILEDESC_XUNLOCK(fdp); |
---|
1996 | (void) closef(fp, td); |
---|
1997 | FILEDESC_XLOCK(fdp); |
---|
1998 | } |
---|
1999 | } |
---|
2000 | FILEDESC_XUNLOCK(fdp); |
---|
2001 | } |
---|
2002 | |
---|
2003 | /* |
---|
2004 | * It is unsafe for set[ug]id processes to be started with file |
---|
2005 | * descriptors 0..2 closed, as these descriptors are given implicit |
---|
2006 | * significance in the Standard C library. fdcheckstd() will create a |
---|
2007 | * descriptor referencing /dev/null for each of stdin, stdout, and |
---|
2008 | * stderr that is not already open. |
---|
2009 | */ |
---|
2010 | int |
---|
2011 | fdcheckstd(struct thread *td) |
---|
2012 | { |
---|
2013 | struct filedesc *fdp; |
---|
2014 | register_t retval, save; |
---|
2015 | int i, error, devnull; |
---|
2016 | |
---|
2017 | fdp = td->td_proc->p_fd; |
---|
2018 | if (fdp == NULL) |
---|
2019 | return (0); |
---|
2020 | KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared")); |
---|
2021 | devnull = -1; |
---|
2022 | error = 0; |
---|
2023 | for (i = 0; i < 3; i++) { |
---|
2024 | if (fdp->fd_ofiles[i] != NULL) |
---|
2025 | continue; |
---|
2026 | if (devnull < 0) { |
---|
2027 | save = td->td_retval[0]; |
---|
2028 | error = kern_open(td, "/dev/null", UIO_SYSSPACE, |
---|
2029 | O_RDWR, 0); |
---|
2030 | devnull = td->td_retval[0]; |
---|
2031 | KASSERT(devnull == i, ("oof, we didn't get our fd")); |
---|
2032 | td->td_retval[0] = save; |
---|
2033 | if (error) |
---|
2034 | break; |
---|
2035 | } else { |
---|
2036 | error = do_dup(td, DUP_FIXED, devnull, i, &retval); |
---|
2037 | if (error != 0) |
---|
2038 | break; |
---|
2039 | } |
---|
2040 | } |
---|
2041 | return (error); |
---|
2042 | } |
---|
2043 | |
---|
2044 | /* |
---|
2045 | * Internal form of close. Decrement reference count on file structure. |
---|
2046 | * Note: td may be NULL when closing a file that was being passed in a |
---|
2047 | * message. |
---|
2048 | * |
---|
2049 | * XXXRW: Giant is not required for the caller, but often will be held; this |
---|
2050 | * makes it moderately likely the Giant will be recursed in the VFS case. |
---|
2051 | */ |
---|
2052 | int |
---|
2053 | closef(struct file *fp, struct thread *td) |
---|
2054 | { |
---|
2055 | struct vnode *vp; |
---|
2056 | struct flock lf; |
---|
2057 | struct filedesc_to_leader *fdtol; |
---|
2058 | struct filedesc *fdp; |
---|
2059 | |
---|
2060 | /* |
---|
2061 | * POSIX record locking dictates that any close releases ALL |
---|
2062 | * locks owned by this process. This is handled by setting |
---|
2063 | * a flag in the unlock to free ONLY locks obeying POSIX |
---|
2064 | * semantics, and not to free BSD-style file locks. |
---|
2065 | * If the descriptor was in a message, POSIX-style locks |
---|
2066 | * aren't passed with the descriptor, and the thread pointer |
---|
2067 | * will be NULL. Callers should be careful only to pass a |
---|
2068 | * NULL thread pointer when there really is no owning |
---|
2069 | * context that might have locks, or the locks will be |
---|
2070 | * leaked. |
---|
2071 | */ |
---|
2072 | if (fp->f_type == DTYPE_VNODE && td != NULL) { |
---|
2073 | int vfslocked; |
---|
2074 | |
---|
2075 | vp = fp->f_vnode; |
---|
2076 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
2077 | if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
2078 | lf.l_whence = SEEK_SET; |
---|
2079 | lf.l_start = 0; |
---|
2080 | lf.l_len = 0; |
---|
2081 | lf.l_type = F_UNLCK; |
---|
2082 | (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, |
---|
2083 | F_UNLCK, &lf, F_POSIX); |
---|
2084 | } |
---|
2085 | fdtol = td->td_proc->p_fdtol; |
---|
2086 | if (fdtol != NULL) { |
---|
2087 | /* |
---|
2088 | * Handle special case where file descriptor table is |
---|
2089 | * shared between multiple process leaders. |
---|
2090 | */ |
---|
2091 | fdp = td->td_proc->p_fd; |
---|
2092 | FILEDESC_XLOCK(fdp); |
---|
2093 | for (fdtol = fdtol->fdl_next; |
---|
2094 | fdtol != td->td_proc->p_fdtol; |
---|
2095 | fdtol = fdtol->fdl_next) { |
---|
2096 | if ((fdtol->fdl_leader->p_flag & |
---|
2097 | P_ADVLOCK) == 0) |
---|
2098 | continue; |
---|
2099 | fdtol->fdl_holdcount++; |
---|
2100 | FILEDESC_XUNLOCK(fdp); |
---|
2101 | lf.l_whence = SEEK_SET; |
---|
2102 | lf.l_start = 0; |
---|
2103 | lf.l_len = 0; |
---|
2104 | lf.l_type = F_UNLCK; |
---|
2105 | vp = fp->f_vnode; |
---|
2106 | (void) VOP_ADVLOCK(vp, |
---|
2107 | (caddr_t)fdtol->fdl_leader, |
---|
2108 | F_UNLCK, &lf, F_POSIX); |
---|
2109 | FILEDESC_XLOCK(fdp); |
---|
2110 | fdtol->fdl_holdcount--; |
---|
2111 | if (fdtol->fdl_holdcount == 0 && |
---|
2112 | fdtol->fdl_wakeup != 0) { |
---|
2113 | fdtol->fdl_wakeup = 0; |
---|
2114 | wakeup(fdtol); |
---|
2115 | } |
---|
2116 | } |
---|
2117 | FILEDESC_XUNLOCK(fdp); |
---|
2118 | } |
---|
2119 | VFS_UNLOCK_GIANT(vfslocked); |
---|
2120 | } |
---|
2121 | return (fdrop(fp, td)); |
---|
2122 | } |
---|
2123 | |
---|
2124 | /* |
---|
2125 | * Initialize the file pointer with the specified properties. |
---|
2126 | * |
---|
2127 | * The ops are set with release semantics to be certain that the flags, type, |
---|
2128 | * and data are visible when ops is. This is to prevent ops methods from being |
---|
2129 | * called with bad data. |
---|
2130 | */ |
---|
2131 | void |
---|
2132 | finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops) |
---|
2133 | { |
---|
2134 | fp->f_data = data; |
---|
2135 | fp->f_flag = flag; |
---|
2136 | fp->f_type = type; |
---|
2137 | atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); |
---|
2138 | } |
---|
2139 | #endif /* __rtems__ */ |
---|
2140 | |
---|
2141 | struct file * |
---|
2142 | fget_unlocked(struct filedesc *fdp, int fd) |
---|
2143 | { |
---|
2144 | struct file *fp; |
---|
2145 | u_int count; |
---|
2146 | |
---|
2147 | if (fd < 0 || fd >= fdp->fd_nfiles) |
---|
2148 | return (NULL); |
---|
2149 | /* |
---|
2150 | * Fetch the descriptor locklessly. We avoid fdrop() races by |
---|
2151 | * never raising a refcount above 0. To accomplish this we have |
---|
2152 | * to use a cmpset loop rather than an atomic_add. The descriptor |
---|
2153 | * must be re-verified once we acquire a reference to be certain |
---|
2154 | * that the identity is still correct and we did not lose a race |
---|
2155 | * due to preemption. |
---|
2156 | */ |
---|
2157 | for (;;) { |
---|
2158 | fp = fdp->fd_ofiles[fd]; |
---|
2159 | if (fp == NULL) |
---|
2160 | break; |
---|
2161 | count = fp->f_count; |
---|
2162 | if (count == 0) |
---|
2163 | continue; |
---|
2164 | /* |
---|
2165 | * Use an acquire barrier to prevent caching of fd_ofiles |
---|
2166 | * so it is refreshed for verification. |
---|
2167 | */ |
---|
2168 | if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1) |
---|
2169 | continue; |
---|
2170 | if (fp == fdp->fd_ofiles[fd]) |
---|
2171 | break; |
---|
2172 | fdrop(fp, curthread); |
---|
2173 | } |
---|
2174 | |
---|
2175 | return (fp); |
---|
2176 | } |
---|
2177 | |
---|
2178 | /* |
---|
2179 | * Extract the file pointer associated with the specified descriptor for the |
---|
2180 | * current user process. |
---|
2181 | * |
---|
2182 | * If the descriptor doesn't exist or doesn't match 'flags', EBADF is |
---|
2183 | * returned. |
---|
2184 | * |
---|
2185 | * If an error occured the non-zero error is returned and *fpp is set to |
---|
2186 | * NULL. Otherwise *fpp is held and set and zero is returned. Caller is |
---|
2187 | * responsible for fdrop(). |
---|
2188 | */ |
---|
2189 | static __inline int |
---|
2190 | _fget(struct thread *td, int fd, struct file **fpp, int flags) |
---|
2191 | { |
---|
2192 | struct filedesc *fdp; |
---|
2193 | struct file *fp; |
---|
2194 | |
---|
2195 | *fpp = NULL; |
---|
2196 | if (td == NULL || (fdp = td->td_proc->p_fd) == NULL) |
---|
2197 | return (EBADF); |
---|
2198 | if ((fp = fget_unlocked(fdp, fd)) == NULL) |
---|
2199 | return (EBADF); |
---|
2200 | if (fp->f_ops == &badfileops) { |
---|
2201 | fdrop(fp, td); |
---|
2202 | return (EBADF); |
---|
2203 | } |
---|
2204 | /* |
---|
2205 | * FREAD and FWRITE failure return EBADF as per POSIX. |
---|
2206 | * |
---|
2207 | * Only one flag, or 0, may be specified. |
---|
2208 | */ |
---|
2209 | if ((flags == FREAD && (fp->f_flag & FREAD) == 0) || |
---|
2210 | (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) { |
---|
2211 | fdrop(fp, td); |
---|
2212 | return (EBADF); |
---|
2213 | } |
---|
2214 | *fpp = fp; |
---|
2215 | return (0); |
---|
2216 | } |
---|
2217 | |
---|
2218 | int |
---|
2219 | fget(struct thread *td, int fd, struct file **fpp) |
---|
2220 | { |
---|
2221 | |
---|
2222 | return(_fget(td, fd, fpp, 0)); |
---|
2223 | } |
---|
2224 | |
---|
2225 | int |
---|
2226 | fget_read(struct thread *td, int fd, struct file **fpp) |
---|
2227 | { |
---|
2228 | |
---|
2229 | return(_fget(td, fd, fpp, FREAD)); |
---|
2230 | } |
---|
2231 | |
---|
2232 | #ifndef __rtems__ |
---|
2233 | int |
---|
2234 | fget_write(struct thread *td, int fd, struct file **fpp) |
---|
2235 | { |
---|
2236 | |
---|
2237 | return(_fget(td, fd, fpp, FWRITE)); |
---|
2238 | } |
---|
2239 | |
---|
2240 | /* |
---|
2241 | * Like fget() but loads the underlying vnode, or returns an error if the |
---|
2242 | * descriptor does not represent a vnode. Note that pipes use vnodes but |
---|
2243 | * never have VM objects. The returned vnode will be vref()'d. |
---|
2244 | * |
---|
2245 | * XXX: what about the unused flags ? |
---|
2246 | */ |
---|
2247 | static __inline int |
---|
2248 | _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags) |
---|
2249 | { |
---|
2250 | struct file *fp; |
---|
2251 | int error; |
---|
2252 | |
---|
2253 | *vpp = NULL; |
---|
2254 | if ((error = _fget(td, fd, &fp, flags)) != 0) |
---|
2255 | return (error); |
---|
2256 | if (fp->f_vnode == NULL) { |
---|
2257 | error = EINVAL; |
---|
2258 | } else { |
---|
2259 | *vpp = fp->f_vnode; |
---|
2260 | vref(*vpp); |
---|
2261 | } |
---|
2262 | fdrop(fp, td); |
---|
2263 | |
---|
2264 | return (error); |
---|
2265 | } |
---|
2266 | |
---|
2267 | int |
---|
2268 | fgetvp(struct thread *td, int fd, struct vnode **vpp) |
---|
2269 | { |
---|
2270 | |
---|
2271 | return (_fgetvp(td, fd, vpp, 0)); |
---|
2272 | } |
---|
2273 | |
---|
2274 | int |
---|
2275 | fgetvp_read(struct thread *td, int fd, struct vnode **vpp) |
---|
2276 | { |
---|
2277 | |
---|
2278 | return (_fgetvp(td, fd, vpp, FREAD)); |
---|
2279 | } |
---|
2280 | |
---|
2281 | #ifdef notyet |
---|
2282 | int |
---|
2283 | fgetvp_write(struct thread *td, int fd, struct vnode **vpp) |
---|
2284 | { |
---|
2285 | |
---|
2286 | return (_fgetvp(td, fd, vpp, FWRITE)); |
---|
2287 | } |
---|
2288 | #endif |
---|
2289 | |
---|
2290 | /* |
---|
2291 | * Like fget() but loads the underlying socket, or returns an error if the |
---|
2292 | * descriptor does not represent a socket. |
---|
2293 | * |
---|
2294 | * We bump the ref count on the returned socket. XXX Also obtain the SX lock |
---|
2295 | * in the future. |
---|
2296 | * |
---|
2297 | * Note: fgetsock() and fputsock() are deprecated, as consumers should rely |
---|
2298 | * on their file descriptor reference to prevent the socket from being free'd |
---|
2299 | * during use. |
---|
2300 | */ |
---|
2301 | int |
---|
2302 | fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp) |
---|
2303 | { |
---|
2304 | struct file *fp; |
---|
2305 | int error; |
---|
2306 | |
---|
2307 | *spp = NULL; |
---|
2308 | if (fflagp != NULL) |
---|
2309 | *fflagp = 0; |
---|
2310 | if ((error = _fget(td, fd, &fp, 0)) != 0) |
---|
2311 | return (error); |
---|
2312 | if (fp->f_type != DTYPE_SOCKET) { |
---|
2313 | error = ENOTSOCK; |
---|
2314 | } else { |
---|
2315 | *spp = fp->f_data; |
---|
2316 | if (fflagp) |
---|
2317 | *fflagp = fp->f_flag; |
---|
2318 | SOCK_LOCK(*spp); |
---|
2319 | soref(*spp); |
---|
2320 | SOCK_UNLOCK(*spp); |
---|
2321 | } |
---|
2322 | fdrop(fp, td); |
---|
2323 | |
---|
2324 | return (error); |
---|
2325 | } |
---|
2326 | |
---|
2327 | /* |
---|
2328 | * Drop the reference count on the socket and XXX release the SX lock in the |
---|
2329 | * future. The last reference closes the socket. |
---|
2330 | * |
---|
2331 | * Note: fputsock() is deprecated, see comment for fgetsock(). |
---|
2332 | */ |
---|
2333 | void |
---|
2334 | fputsock(struct socket *so) |
---|
2335 | { |
---|
2336 | |
---|
2337 | ACCEPT_LOCK(); |
---|
2338 | SOCK_LOCK(so); |
---|
2339 | sorele(so); |
---|
2340 | } |
---|
2341 | #endif /* __rtems__ */ |
---|
2342 | |
---|
2343 | /* |
---|
2344 | * Handle the last reference to a file being closed. |
---|
2345 | */ |
---|
2346 | int |
---|
2347 | _fdrop(struct file *fp, struct thread *td) |
---|
2348 | { |
---|
2349 | #ifdef __rtems__ |
---|
2350 | panic("fdrop: RTEMS unsupported"); |
---|
2351 | |
---|
2352 | #else /* __rtems__ */ |
---|
2353 | int error; |
---|
2354 | |
---|
2355 | error = 0; |
---|
2356 | if (fp->f_count != 0) |
---|
2357 | panic("fdrop: count %d", fp->f_count); |
---|
2358 | if (fp->f_ops != &badfileops) |
---|
2359 | error = fo_close(fp, td); |
---|
2360 | /* |
---|
2361 | * The f_cdevpriv cannot be assigned non-NULL value while we |
---|
2362 | * are destroying the file. |
---|
2363 | */ |
---|
2364 | if (fp->f_cdevpriv != NULL) |
---|
2365 | devfs_fpdrop(fp); |
---|
2366 | atomic_subtract_int(&openfiles, 1); |
---|
2367 | crfree(fp->f_cred); |
---|
2368 | uma_zfree(file_zone, fp); |
---|
2369 | |
---|
2370 | return (error); |
---|
2371 | #endif /* __rtems__ */ |
---|
2372 | } |
---|
2373 | |
---|
2374 | #ifndef __rtems__ |
---|
2375 | /* |
---|
2376 | * Apply an advisory lock on a file descriptor. |
---|
2377 | * |
---|
2378 | * Just attempt to get a record lock of the requested type on the entire file |
---|
2379 | * (l_whence = SEEK_SET, l_start = 0, l_len = 0). |
---|
2380 | */ |
---|
2381 | #ifndef _SYS_SYSPROTO_H_ |
---|
2382 | struct flock_args { |
---|
2383 | int fd; |
---|
2384 | int how; |
---|
2385 | }; |
---|
2386 | #endif |
---|
2387 | /* ARGSUSED */ |
---|
2388 | int |
---|
2389 | flock(struct thread *td, struct flock_args *uap) |
---|
2390 | { |
---|
2391 | struct file *fp; |
---|
2392 | struct vnode *vp; |
---|
2393 | struct flock lf; |
---|
2394 | int vfslocked; |
---|
2395 | int error; |
---|
2396 | |
---|
2397 | if ((error = fget(td, uap->fd, &fp)) != 0) |
---|
2398 | return (error); |
---|
2399 | if (fp->f_type != DTYPE_VNODE) { |
---|
2400 | fdrop(fp, td); |
---|
2401 | return (EOPNOTSUPP); |
---|
2402 | } |
---|
2403 | |
---|
2404 | vp = fp->f_vnode; |
---|
2405 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
2406 | lf.l_whence = SEEK_SET; |
---|
2407 | lf.l_start = 0; |
---|
2408 | lf.l_len = 0; |
---|
2409 | if (uap->how & LOCK_UN) { |
---|
2410 | lf.l_type = F_UNLCK; |
---|
2411 | atomic_clear_int(&fp->f_flag, FHASLOCK); |
---|
2412 | error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); |
---|
2413 | goto done2; |
---|
2414 | } |
---|
2415 | if (uap->how & LOCK_EX) |
---|
2416 | lf.l_type = F_WRLCK; |
---|
2417 | else if (uap->how & LOCK_SH) |
---|
2418 | lf.l_type = F_RDLCK; |
---|
2419 | else { |
---|
2420 | error = EBADF; |
---|
2421 | goto done2; |
---|
2422 | } |
---|
2423 | atomic_set_int(&fp->f_flag, FHASLOCK); |
---|
2424 | error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, |
---|
2425 | (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); |
---|
2426 | done2: |
---|
2427 | fdrop(fp, td); |
---|
2428 | VFS_UNLOCK_GIANT(vfslocked); |
---|
2429 | return (error); |
---|
2430 | } |
---|
2431 | /* |
---|
2432 | * Duplicate the specified descriptor to a free descriptor. |
---|
2433 | */ |
---|
2434 | int |
---|
2435 | dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error) |
---|
2436 | { |
---|
2437 | struct file *wfp; |
---|
2438 | struct file *fp; |
---|
2439 | |
---|
2440 | /* |
---|
2441 | * If the to-be-dup'd fd number is greater than the allowed number |
---|
2442 | * of file descriptors, or the fd to be dup'd has already been |
---|
2443 | * closed, then reject. |
---|
2444 | */ |
---|
2445 | FILEDESC_XLOCK(fdp); |
---|
2446 | if (dfd < 0 || dfd >= fdp->fd_nfiles || |
---|
2447 | (wfp = fdp->fd_ofiles[dfd]) == NULL) { |
---|
2448 | FILEDESC_XUNLOCK(fdp); |
---|
2449 | return (EBADF); |
---|
2450 | } |
---|
2451 | |
---|
2452 | /* |
---|
2453 | * There are two cases of interest here. |
---|
2454 | * |
---|
2455 | * For ENODEV simply dup (dfd) to file descriptor (indx) and return. |
---|
2456 | * |
---|
2457 | * For ENXIO steal away the file structure from (dfd) and store it in |
---|
2458 | * (indx). (dfd) is effectively closed by this operation. |
---|
2459 | * |
---|
2460 | * Any other error code is just returned. |
---|
2461 | */ |
---|
2462 | switch (error) { |
---|
2463 | case ENODEV: |
---|
2464 | /* |
---|
2465 | * Check that the mode the file is being opened for is a |
---|
2466 | * subset of the mode of the existing descriptor. |
---|
2467 | */ |
---|
2468 | if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { |
---|
2469 | FILEDESC_XUNLOCK(fdp); |
---|
2470 | return (EACCES); |
---|
2471 | } |
---|
2472 | fp = fdp->fd_ofiles[indx]; |
---|
2473 | fdp->fd_ofiles[indx] = wfp; |
---|
2474 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; |
---|
2475 | if (fp == NULL) |
---|
2476 | fdused(fdp, indx); |
---|
2477 | fhold(wfp); |
---|
2478 | FILEDESC_XUNLOCK(fdp); |
---|
2479 | if (fp != NULL) |
---|
2480 | /* |
---|
2481 | * We now own the reference to fp that the ofiles[] |
---|
2482 | * array used to own. Release it. |
---|
2483 | */ |
---|
2484 | fdrop(fp, td); |
---|
2485 | return (0); |
---|
2486 | |
---|
2487 | case ENXIO: |
---|
2488 | /* |
---|
2489 | * Steal away the file pointer from dfd and stuff it into indx. |
---|
2490 | */ |
---|
2491 | fp = fdp->fd_ofiles[indx]; |
---|
2492 | fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; |
---|
2493 | fdp->fd_ofiles[dfd] = NULL; |
---|
2494 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; |
---|
2495 | fdp->fd_ofileflags[dfd] = 0; |
---|
2496 | fdunused(fdp, dfd); |
---|
2497 | if (fp == NULL) |
---|
2498 | fdused(fdp, indx); |
---|
2499 | FILEDESC_XUNLOCK(fdp); |
---|
2500 | |
---|
2501 | /* |
---|
2502 | * We now own the reference to fp that the ofiles[] array |
---|
2503 | * used to own. Release it. |
---|
2504 | */ |
---|
2505 | if (fp != NULL) |
---|
2506 | fdrop(fp, td); |
---|
2507 | return (0); |
---|
2508 | |
---|
2509 | default: |
---|
2510 | FILEDESC_XUNLOCK(fdp); |
---|
2511 | return (error); |
---|
2512 | } |
---|
2513 | /* NOTREACHED */ |
---|
2514 | } |
---|
2515 | |
---|
2516 | /* |
---|
2517 | * Scan all active processes and prisons to see if any of them have a current |
---|
2518 | * or root directory of `olddp'. If so, replace them with the new mount point. |
---|
2519 | */ |
---|
2520 | void |
---|
2521 | mountcheckdirs(struct vnode *olddp, struct vnode *newdp) |
---|
2522 | { |
---|
2523 | struct filedesc *fdp; |
---|
2524 | struct prison *pr; |
---|
2525 | struct proc *p; |
---|
2526 | int nrele; |
---|
2527 | |
---|
2528 | if (vrefcnt(olddp) == 1) |
---|
2529 | return; |
---|
2530 | nrele = 0; |
---|
2531 | sx_slock(&allproc_lock); |
---|
2532 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
2533 | fdp = fdhold(p); |
---|
2534 | if (fdp == NULL) |
---|
2535 | continue; |
---|
2536 | FILEDESC_XLOCK(fdp); |
---|
2537 | if (fdp->fd_cdir == olddp) { |
---|
2538 | vref(newdp); |
---|
2539 | fdp->fd_cdir = newdp; |
---|
2540 | nrele++; |
---|
2541 | } |
---|
2542 | if (fdp->fd_rdir == olddp) { |
---|
2543 | vref(newdp); |
---|
2544 | fdp->fd_rdir = newdp; |
---|
2545 | nrele++; |
---|
2546 | } |
---|
2547 | if (fdp->fd_jdir == olddp) { |
---|
2548 | vref(newdp); |
---|
2549 | fdp->fd_jdir = newdp; |
---|
2550 | nrele++; |
---|
2551 | } |
---|
2552 | FILEDESC_XUNLOCK(fdp); |
---|
2553 | fddrop(fdp); |
---|
2554 | } |
---|
2555 | sx_sunlock(&allproc_lock); |
---|
2556 | if (rootvnode == olddp) { |
---|
2557 | vref(newdp); |
---|
2558 | rootvnode = newdp; |
---|
2559 | nrele++; |
---|
2560 | } |
---|
2561 | mtx_lock(&prison0.pr_mtx); |
---|
2562 | if (prison0.pr_root == olddp) { |
---|
2563 | vref(newdp); |
---|
2564 | prison0.pr_root = newdp; |
---|
2565 | nrele++; |
---|
2566 | } |
---|
2567 | mtx_unlock(&prison0.pr_mtx); |
---|
2568 | sx_slock(&allprison_lock); |
---|
2569 | TAILQ_FOREACH(pr, &allprison, pr_list) { |
---|
2570 | mtx_lock(&pr->pr_mtx); |
---|
2571 | if (pr->pr_root == olddp) { |
---|
2572 | vref(newdp); |
---|
2573 | pr->pr_root = newdp; |
---|
2574 | nrele++; |
---|
2575 | } |
---|
2576 | mtx_unlock(&pr->pr_mtx); |
---|
2577 | } |
---|
2578 | sx_sunlock(&allprison_lock); |
---|
2579 | while (nrele--) |
---|
2580 | vrele(olddp); |
---|
2581 | } |
---|
2582 | |
---|
2583 | struct filedesc_to_leader * |
---|
2584 | filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader) |
---|
2585 | { |
---|
2586 | struct filedesc_to_leader *fdtol; |
---|
2587 | |
---|
2588 | fdtol = malloc(sizeof(struct filedesc_to_leader), |
---|
2589 | M_FILEDESC_TO_LEADER, |
---|
2590 | M_WAITOK); |
---|
2591 | fdtol->fdl_refcount = 1; |
---|
2592 | fdtol->fdl_holdcount = 0; |
---|
2593 | fdtol->fdl_wakeup = 0; |
---|
2594 | fdtol->fdl_leader = leader; |
---|
2595 | if (old != NULL) { |
---|
2596 | FILEDESC_XLOCK(fdp); |
---|
2597 | fdtol->fdl_next = old->fdl_next; |
---|
2598 | fdtol->fdl_prev = old; |
---|
2599 | old->fdl_next = fdtol; |
---|
2600 | fdtol->fdl_next->fdl_prev = fdtol; |
---|
2601 | FILEDESC_XUNLOCK(fdp); |
---|
2602 | } else { |
---|
2603 | fdtol->fdl_next = fdtol; |
---|
2604 | fdtol->fdl_prev = fdtol; |
---|
2605 | } |
---|
2606 | return (fdtol); |
---|
2607 | } |
---|
2608 | |
---|
2609 | /* |
---|
2610 | * Get file structures globally. |
---|
2611 | */ |
---|
2612 | static int |
---|
2613 | sysctl_kern_file(SYSCTL_HANDLER_ARGS) |
---|
2614 | { |
---|
2615 | struct xfile xf; |
---|
2616 | struct filedesc *fdp; |
---|
2617 | struct file *fp; |
---|
2618 | struct proc *p; |
---|
2619 | int error, n; |
---|
2620 | |
---|
2621 | error = sysctl_wire_old_buffer(req, 0); |
---|
2622 | if (error != 0) |
---|
2623 | return (error); |
---|
2624 | if (req->oldptr == NULL) { |
---|
2625 | n = 0; |
---|
2626 | sx_slock(&allproc_lock); |
---|
2627 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
2628 | if (p->p_state == PRS_NEW) |
---|
2629 | continue; |
---|
2630 | fdp = fdhold(p); |
---|
2631 | if (fdp == NULL) |
---|
2632 | continue; |
---|
2633 | /* overestimates sparse tables. */ |
---|
2634 | if (fdp->fd_lastfile > 0) |
---|
2635 | n += fdp->fd_lastfile; |
---|
2636 | fddrop(fdp); |
---|
2637 | } |
---|
2638 | sx_sunlock(&allproc_lock); |
---|
2639 | return (SYSCTL_OUT(req, 0, n * sizeof(xf))); |
---|
2640 | } |
---|
2641 | error = 0; |
---|
2642 | bzero(&xf, sizeof(xf)); |
---|
2643 | xf.xf_size = sizeof(xf); |
---|
2644 | sx_slock(&allproc_lock); |
---|
2645 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
2646 | if (p->p_state == PRS_NEW) |
---|
2647 | continue; |
---|
2648 | PROC_LOCK(p); |
---|
2649 | if (p_cansee(req->td, p) != 0) { |
---|
2650 | PROC_UNLOCK(p); |
---|
2651 | continue; |
---|
2652 | } |
---|
2653 | xf.xf_pid = p->p_pid; |
---|
2654 | xf.xf_uid = p->p_ucred->cr_uid; |
---|
2655 | PROC_UNLOCK(p); |
---|
2656 | fdp = fdhold(p); |
---|
2657 | if (fdp == NULL) |
---|
2658 | continue; |
---|
2659 | FILEDESC_SLOCK(fdp); |
---|
2660 | for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) { |
---|
2661 | if ((fp = fdp->fd_ofiles[n]) == NULL) |
---|
2662 | continue; |
---|
2663 | xf.xf_fd = n; |
---|
2664 | xf.xf_file = fp; |
---|
2665 | xf.xf_data = fp->f_data; |
---|
2666 | xf.xf_vnode = fp->f_vnode; |
---|
2667 | xf.xf_type = fp->f_type; |
---|
2668 | xf.xf_count = fp->f_count; |
---|
2669 | xf.xf_msgcount = 0; |
---|
2670 | xf.xf_offset = fp->f_offset; |
---|
2671 | xf.xf_flag = fp->f_flag; |
---|
2672 | error = SYSCTL_OUT(req, &xf, sizeof(xf)); |
---|
2673 | if (error) |
---|
2674 | break; |
---|
2675 | } |
---|
2676 | FILEDESC_SUNLOCK(fdp); |
---|
2677 | fddrop(fdp); |
---|
2678 | if (error) |
---|
2679 | break; |
---|
2680 | } |
---|
2681 | sx_sunlock(&allproc_lock); |
---|
2682 | return (error); |
---|
2683 | } |
---|
2684 | |
---|
2685 | SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, |
---|
2686 | 0, 0, sysctl_kern_file, "S,xfile", "Entire file table"); |
---|
2687 | |
---|
2688 | #ifdef KINFO_OFILE_SIZE |
---|
2689 | CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE); |
---|
2690 | #endif |
---|
2691 | |
---|
2692 | #ifdef COMPAT_FREEBSD7 |
---|
2693 | static int |
---|
2694 | export_vnode_for_osysctl(struct vnode *vp, int type, |
---|
2695 | struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req) |
---|
2696 | { |
---|
2697 | int error; |
---|
2698 | char *fullpath, *freepath; |
---|
2699 | int vfslocked; |
---|
2700 | |
---|
2701 | bzero(kif, sizeof(*kif)); |
---|
2702 | kif->kf_structsize = sizeof(*kif); |
---|
2703 | |
---|
2704 | vref(vp); |
---|
2705 | kif->kf_fd = type; |
---|
2706 | kif->kf_type = KF_TYPE_VNODE; |
---|
2707 | /* This function only handles directories. */ |
---|
2708 | if (vp->v_type != VDIR) { |
---|
2709 | vrele(vp); |
---|
2710 | return (ENOTDIR); |
---|
2711 | } |
---|
2712 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
2713 | |
---|
2714 | /* |
---|
2715 | * This is not a true file descriptor, so we set a bogus refcount |
---|
2716 | * and offset to indicate these fields should be ignored. |
---|
2717 | */ |
---|
2718 | kif->kf_ref_count = -1; |
---|
2719 | kif->kf_offset = -1; |
---|
2720 | |
---|
2721 | freepath = NULL; |
---|
2722 | fullpath = "-"; |
---|
2723 | FILEDESC_SUNLOCK(fdp); |
---|
2724 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
2725 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
2726 | vrele(vp); |
---|
2727 | VFS_UNLOCK_GIANT(vfslocked); |
---|
2728 | strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); |
---|
2729 | if (freepath != NULL) |
---|
2730 | free(freepath, M_TEMP); |
---|
2731 | error = SYSCTL_OUT(req, kif, sizeof(*kif)); |
---|
2732 | FILEDESC_SLOCK(fdp); |
---|
2733 | return (error); |
---|
2734 | } |
---|
2735 | |
---|
2736 | /* |
---|
2737 | * Get per-process file descriptors for use by procstat(1), et al. |
---|
2738 | */ |
---|
2739 | static int |
---|
2740 | sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS) |
---|
2741 | { |
---|
2742 | char *fullpath, *freepath; |
---|
2743 | struct kinfo_ofile *kif; |
---|
2744 | struct filedesc *fdp; |
---|
2745 | int error, i, *name; |
---|
2746 | struct socket *so; |
---|
2747 | struct vnode *vp; |
---|
2748 | struct file *fp; |
---|
2749 | struct proc *p; |
---|
2750 | struct tty *tp; |
---|
2751 | int vfslocked; |
---|
2752 | |
---|
2753 | name = (int *)arg1; |
---|
2754 | if ((p = pfind((pid_t)name[0])) == NULL) |
---|
2755 | return (ESRCH); |
---|
2756 | if ((error = p_candebug(curthread, p))) { |
---|
2757 | PROC_UNLOCK(p); |
---|
2758 | return (error); |
---|
2759 | } |
---|
2760 | fdp = fdhold(p); |
---|
2761 | PROC_UNLOCK(p); |
---|
2762 | if (fdp == NULL) |
---|
2763 | return (ENOENT); |
---|
2764 | kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); |
---|
2765 | FILEDESC_SLOCK(fdp); |
---|
2766 | if (fdp->fd_cdir != NULL) |
---|
2767 | export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, |
---|
2768 | fdp, req); |
---|
2769 | if (fdp->fd_rdir != NULL) |
---|
2770 | export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, |
---|
2771 | fdp, req); |
---|
2772 | if (fdp->fd_jdir != NULL) |
---|
2773 | export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, |
---|
2774 | fdp, req); |
---|
2775 | for (i = 0; i < fdp->fd_nfiles; i++) { |
---|
2776 | if ((fp = fdp->fd_ofiles[i]) == NULL) |
---|
2777 | continue; |
---|
2778 | bzero(kif, sizeof(*kif)); |
---|
2779 | kif->kf_structsize = sizeof(*kif); |
---|
2780 | vp = NULL; |
---|
2781 | so = NULL; |
---|
2782 | tp = NULL; |
---|
2783 | kif->kf_fd = i; |
---|
2784 | switch (fp->f_type) { |
---|
2785 | case DTYPE_VNODE: |
---|
2786 | kif->kf_type = KF_TYPE_VNODE; |
---|
2787 | vp = fp->f_vnode; |
---|
2788 | break; |
---|
2789 | |
---|
2790 | case DTYPE_SOCKET: |
---|
2791 | kif->kf_type = KF_TYPE_SOCKET; |
---|
2792 | so = fp->f_data; |
---|
2793 | break; |
---|
2794 | |
---|
2795 | case DTYPE_PIPE: |
---|
2796 | kif->kf_type = KF_TYPE_PIPE; |
---|
2797 | break; |
---|
2798 | |
---|
2799 | case DTYPE_FIFO: |
---|
2800 | kif->kf_type = KF_TYPE_FIFO; |
---|
2801 | vp = fp->f_vnode; |
---|
2802 | break; |
---|
2803 | |
---|
2804 | case DTYPE_KQUEUE: |
---|
2805 | kif->kf_type = KF_TYPE_KQUEUE; |
---|
2806 | break; |
---|
2807 | |
---|
2808 | case DTYPE_CRYPTO: |
---|
2809 | kif->kf_type = KF_TYPE_CRYPTO; |
---|
2810 | break; |
---|
2811 | |
---|
2812 | case DTYPE_MQUEUE: |
---|
2813 | kif->kf_type = KF_TYPE_MQUEUE; |
---|
2814 | break; |
---|
2815 | |
---|
2816 | case DTYPE_SHM: |
---|
2817 | kif->kf_type = KF_TYPE_SHM; |
---|
2818 | break; |
---|
2819 | |
---|
2820 | case DTYPE_SEM: |
---|
2821 | kif->kf_type = KF_TYPE_SEM; |
---|
2822 | break; |
---|
2823 | |
---|
2824 | case DTYPE_PTS: |
---|
2825 | kif->kf_type = KF_TYPE_PTS; |
---|
2826 | tp = fp->f_data; |
---|
2827 | break; |
---|
2828 | |
---|
2829 | default: |
---|
2830 | kif->kf_type = KF_TYPE_UNKNOWN; |
---|
2831 | break; |
---|
2832 | } |
---|
2833 | kif->kf_ref_count = fp->f_count; |
---|
2834 | if (fp->f_flag & FREAD) |
---|
2835 | kif->kf_flags |= KF_FLAG_READ; |
---|
2836 | if (fp->f_flag & FWRITE) |
---|
2837 | kif->kf_flags |= KF_FLAG_WRITE; |
---|
2838 | if (fp->f_flag & FAPPEND) |
---|
2839 | kif->kf_flags |= KF_FLAG_APPEND; |
---|
2840 | if (fp->f_flag & FASYNC) |
---|
2841 | kif->kf_flags |= KF_FLAG_ASYNC; |
---|
2842 | if (fp->f_flag & FFSYNC) |
---|
2843 | kif->kf_flags |= KF_FLAG_FSYNC; |
---|
2844 | if (fp->f_flag & FNONBLOCK) |
---|
2845 | kif->kf_flags |= KF_FLAG_NONBLOCK; |
---|
2846 | if (fp->f_flag & O_DIRECT) |
---|
2847 | kif->kf_flags |= KF_FLAG_DIRECT; |
---|
2848 | if (fp->f_flag & FHASLOCK) |
---|
2849 | kif->kf_flags |= KF_FLAG_HASLOCK; |
---|
2850 | kif->kf_offset = fp->f_offset; |
---|
2851 | if (vp != NULL) { |
---|
2852 | vref(vp); |
---|
2853 | switch (vp->v_type) { |
---|
2854 | case VNON: |
---|
2855 | kif->kf_vnode_type = KF_VTYPE_VNON; |
---|
2856 | break; |
---|
2857 | case VREG: |
---|
2858 | kif->kf_vnode_type = KF_VTYPE_VREG; |
---|
2859 | break; |
---|
2860 | case VDIR: |
---|
2861 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
2862 | break; |
---|
2863 | case VBLK: |
---|
2864 | kif->kf_vnode_type = KF_VTYPE_VBLK; |
---|
2865 | break; |
---|
2866 | case VCHR: |
---|
2867 | kif->kf_vnode_type = KF_VTYPE_VCHR; |
---|
2868 | break; |
---|
2869 | case VLNK: |
---|
2870 | kif->kf_vnode_type = KF_VTYPE_VLNK; |
---|
2871 | break; |
---|
2872 | case VSOCK: |
---|
2873 | kif->kf_vnode_type = KF_VTYPE_VSOCK; |
---|
2874 | break; |
---|
2875 | case VFIFO: |
---|
2876 | kif->kf_vnode_type = KF_VTYPE_VFIFO; |
---|
2877 | break; |
---|
2878 | case VBAD: |
---|
2879 | kif->kf_vnode_type = KF_VTYPE_VBAD; |
---|
2880 | break; |
---|
2881 | default: |
---|
2882 | kif->kf_vnode_type = KF_VTYPE_UNKNOWN; |
---|
2883 | break; |
---|
2884 | } |
---|
2885 | /* |
---|
2886 | * It is OK to drop the filedesc lock here as we will |
---|
2887 | * re-validate and re-evaluate its properties when |
---|
2888 | * the loop continues. |
---|
2889 | */ |
---|
2890 | freepath = NULL; |
---|
2891 | fullpath = "-"; |
---|
2892 | FILEDESC_SUNLOCK(fdp); |
---|
2893 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
2894 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
2895 | vrele(vp); |
---|
2896 | VFS_UNLOCK_GIANT(vfslocked); |
---|
2897 | strlcpy(kif->kf_path, fullpath, |
---|
2898 | sizeof(kif->kf_path)); |
---|
2899 | if (freepath != NULL) |
---|
2900 | free(freepath, M_TEMP); |
---|
2901 | FILEDESC_SLOCK(fdp); |
---|
2902 | } |
---|
2903 | if (so != NULL) { |
---|
2904 | struct sockaddr *sa; |
---|
2905 | |
---|
2906 | if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) |
---|
2907 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { |
---|
2908 | bcopy(sa, &kif->kf_sa_local, sa->sa_len); |
---|
2909 | free(sa, M_SONAME); |
---|
2910 | } |
---|
2911 | if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) |
---|
2912 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { |
---|
2913 | bcopy(sa, &kif->kf_sa_peer, sa->sa_len); |
---|
2914 | free(sa, M_SONAME); |
---|
2915 | } |
---|
2916 | kif->kf_sock_domain = |
---|
2917 | so->so_proto->pr_domain->dom_family; |
---|
2918 | kif->kf_sock_type = so->so_type; |
---|
2919 | kif->kf_sock_protocol = so->so_proto->pr_protocol; |
---|
2920 | } |
---|
2921 | if (tp != NULL) { |
---|
2922 | strlcpy(kif->kf_path, tty_devname(tp), |
---|
2923 | sizeof(kif->kf_path)); |
---|
2924 | } |
---|
2925 | error = SYSCTL_OUT(req, kif, sizeof(*kif)); |
---|
2926 | if (error) |
---|
2927 | break; |
---|
2928 | } |
---|
2929 | FILEDESC_SUNLOCK(fdp); |
---|
2930 | fddrop(fdp); |
---|
2931 | free(kif, M_TEMP); |
---|
2932 | return (0); |
---|
2933 | } |
---|
2934 | |
---|
2935 | static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD, |
---|
2936 | sysctl_kern_proc_ofiledesc, "Process ofiledesc entries"); |
---|
2937 | #endif /* COMPAT_FREEBSD7 */ |
---|
2938 | |
---|
2939 | #ifdef KINFO_FILE_SIZE |
---|
2940 | CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); |
---|
2941 | #endif |
---|
2942 | |
---|
2943 | static int |
---|
2944 | export_vnode_for_sysctl(struct vnode *vp, int type, |
---|
2945 | struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req) |
---|
2946 | { |
---|
2947 | int error; |
---|
2948 | char *fullpath, *freepath; |
---|
2949 | int vfslocked; |
---|
2950 | |
---|
2951 | bzero(kif, sizeof(*kif)); |
---|
2952 | |
---|
2953 | vref(vp); |
---|
2954 | kif->kf_fd = type; |
---|
2955 | kif->kf_type = KF_TYPE_VNODE; |
---|
2956 | /* This function only handles directories. */ |
---|
2957 | if (vp->v_type != VDIR) { |
---|
2958 | vrele(vp); |
---|
2959 | return (ENOTDIR); |
---|
2960 | } |
---|
2961 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
2962 | |
---|
2963 | /* |
---|
2964 | * This is not a true file descriptor, so we set a bogus refcount |
---|
2965 | * and offset to indicate these fields should be ignored. |
---|
2966 | */ |
---|
2967 | kif->kf_ref_count = -1; |
---|
2968 | kif->kf_offset = -1; |
---|
2969 | |
---|
2970 | freepath = NULL; |
---|
2971 | fullpath = "-"; |
---|
2972 | FILEDESC_SUNLOCK(fdp); |
---|
2973 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
2974 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
2975 | vrele(vp); |
---|
2976 | VFS_UNLOCK_GIANT(vfslocked); |
---|
2977 | strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); |
---|
2978 | if (freepath != NULL) |
---|
2979 | free(freepath, M_TEMP); |
---|
2980 | /* Pack record size down */ |
---|
2981 | kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + |
---|
2982 | strlen(kif->kf_path) + 1; |
---|
2983 | kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t)); |
---|
2984 | error = SYSCTL_OUT(req, kif, kif->kf_structsize); |
---|
2985 | FILEDESC_SLOCK(fdp); |
---|
2986 | return (error); |
---|
2987 | } |
---|
2988 | |
---|
2989 | /* |
---|
2990 | * Get per-process file descriptors for use by procstat(1), et al. |
---|
2991 | */ |
---|
2992 | static int |
---|
2993 | sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS) |
---|
2994 | { |
---|
2995 | char *fullpath, *freepath; |
---|
2996 | struct kinfo_file *kif; |
---|
2997 | struct filedesc *fdp; |
---|
2998 | int error, i, *name; |
---|
2999 | struct socket *so; |
---|
3000 | struct vnode *vp; |
---|
3001 | struct file *fp; |
---|
3002 | struct proc *p; |
---|
3003 | struct tty *tp; |
---|
3004 | int vfslocked; |
---|
3005 | size_t oldidx; |
---|
3006 | |
---|
3007 | name = (int *)arg1; |
---|
3008 | if ((p = pfind((pid_t)name[0])) == NULL) |
---|
3009 | return (ESRCH); |
---|
3010 | if ((error = p_candebug(curthread, p))) { |
---|
3011 | PROC_UNLOCK(p); |
---|
3012 | return (error); |
---|
3013 | } |
---|
3014 | fdp = fdhold(p); |
---|
3015 | PROC_UNLOCK(p); |
---|
3016 | if (fdp == NULL) |
---|
3017 | return (ENOENT); |
---|
3018 | kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); |
---|
3019 | FILEDESC_SLOCK(fdp); |
---|
3020 | if (fdp->fd_cdir != NULL) |
---|
3021 | export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, |
---|
3022 | fdp, req); |
---|
3023 | if (fdp->fd_rdir != NULL) |
---|
3024 | export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, |
---|
3025 | fdp, req); |
---|
3026 | if (fdp->fd_jdir != NULL) |
---|
3027 | export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, |
---|
3028 | fdp, req); |
---|
3029 | for (i = 0; i < fdp->fd_nfiles; i++) { |
---|
3030 | if ((fp = fdp->fd_ofiles[i]) == NULL) |
---|
3031 | continue; |
---|
3032 | bzero(kif, sizeof(*kif)); |
---|
3033 | vp = NULL; |
---|
3034 | so = NULL; |
---|
3035 | tp = NULL; |
---|
3036 | kif->kf_fd = i; |
---|
3037 | switch (fp->f_type) { |
---|
3038 | case DTYPE_VNODE: |
---|
3039 | kif->kf_type = KF_TYPE_VNODE; |
---|
3040 | vp = fp->f_vnode; |
---|
3041 | break; |
---|
3042 | |
---|
3043 | case DTYPE_SOCKET: |
---|
3044 | kif->kf_type = KF_TYPE_SOCKET; |
---|
3045 | so = fp->f_data; |
---|
3046 | break; |
---|
3047 | |
---|
3048 | case DTYPE_PIPE: |
---|
3049 | kif->kf_type = KF_TYPE_PIPE; |
---|
3050 | break; |
---|
3051 | |
---|
3052 | case DTYPE_FIFO: |
---|
3053 | kif->kf_type = KF_TYPE_FIFO; |
---|
3054 | vp = fp->f_vnode; |
---|
3055 | break; |
---|
3056 | |
---|
3057 | case DTYPE_KQUEUE: |
---|
3058 | kif->kf_type = KF_TYPE_KQUEUE; |
---|
3059 | break; |
---|
3060 | |
---|
3061 | case DTYPE_CRYPTO: |
---|
3062 | kif->kf_type = KF_TYPE_CRYPTO; |
---|
3063 | break; |
---|
3064 | |
---|
3065 | case DTYPE_MQUEUE: |
---|
3066 | kif->kf_type = KF_TYPE_MQUEUE; |
---|
3067 | break; |
---|
3068 | |
---|
3069 | case DTYPE_SHM: |
---|
3070 | kif->kf_type = KF_TYPE_SHM; |
---|
3071 | break; |
---|
3072 | |
---|
3073 | case DTYPE_SEM: |
---|
3074 | kif->kf_type = KF_TYPE_SEM; |
---|
3075 | break; |
---|
3076 | |
---|
3077 | case DTYPE_PTS: |
---|
3078 | kif->kf_type = KF_TYPE_PTS; |
---|
3079 | tp = fp->f_data; |
---|
3080 | break; |
---|
3081 | |
---|
3082 | default: |
---|
3083 | kif->kf_type = KF_TYPE_UNKNOWN; |
---|
3084 | break; |
---|
3085 | } |
---|
3086 | kif->kf_ref_count = fp->f_count; |
---|
3087 | if (fp->f_flag & FREAD) |
---|
3088 | kif->kf_flags |= KF_FLAG_READ; |
---|
3089 | if (fp->f_flag & FWRITE) |
---|
3090 | kif->kf_flags |= KF_FLAG_WRITE; |
---|
3091 | if (fp->f_flag & FAPPEND) |
---|
3092 | kif->kf_flags |= KF_FLAG_APPEND; |
---|
3093 | if (fp->f_flag & FASYNC) |
---|
3094 | kif->kf_flags |= KF_FLAG_ASYNC; |
---|
3095 | if (fp->f_flag & FFSYNC) |
---|
3096 | kif->kf_flags |= KF_FLAG_FSYNC; |
---|
3097 | if (fp->f_flag & FNONBLOCK) |
---|
3098 | kif->kf_flags |= KF_FLAG_NONBLOCK; |
---|
3099 | if (fp->f_flag & O_DIRECT) |
---|
3100 | kif->kf_flags |= KF_FLAG_DIRECT; |
---|
3101 | if (fp->f_flag & FHASLOCK) |
---|
3102 | kif->kf_flags |= KF_FLAG_HASLOCK; |
---|
3103 | kif->kf_offset = fp->f_offset; |
---|
3104 | if (vp != NULL) { |
---|
3105 | vref(vp); |
---|
3106 | switch (vp->v_type) { |
---|
3107 | case VNON: |
---|
3108 | kif->kf_vnode_type = KF_VTYPE_VNON; |
---|
3109 | break; |
---|
3110 | case VREG: |
---|
3111 | kif->kf_vnode_type = KF_VTYPE_VREG; |
---|
3112 | break; |
---|
3113 | case VDIR: |
---|
3114 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
3115 | break; |
---|
3116 | case VBLK: |
---|
3117 | kif->kf_vnode_type = KF_VTYPE_VBLK; |
---|
3118 | break; |
---|
3119 | case VCHR: |
---|
3120 | kif->kf_vnode_type = KF_VTYPE_VCHR; |
---|
3121 | break; |
---|
3122 | case VLNK: |
---|
3123 | kif->kf_vnode_type = KF_VTYPE_VLNK; |
---|
3124 | break; |
---|
3125 | case VSOCK: |
---|
3126 | kif->kf_vnode_type = KF_VTYPE_VSOCK; |
---|
3127 | break; |
---|
3128 | case VFIFO: |
---|
3129 | kif->kf_vnode_type = KF_VTYPE_VFIFO; |
---|
3130 | break; |
---|
3131 | case VBAD: |
---|
3132 | kif->kf_vnode_type = KF_VTYPE_VBAD; |
---|
3133 | break; |
---|
3134 | default: |
---|
3135 | kif->kf_vnode_type = KF_VTYPE_UNKNOWN; |
---|
3136 | break; |
---|
3137 | } |
---|
3138 | /* |
---|
3139 | * It is OK to drop the filedesc lock here as we will |
---|
3140 | * re-validate and re-evaluate its properties when |
---|
3141 | * the loop continues. |
---|
3142 | */ |
---|
3143 | freepath = NULL; |
---|
3144 | fullpath = "-"; |
---|
3145 | FILEDESC_SUNLOCK(fdp); |
---|
3146 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
3147 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
3148 | vrele(vp); |
---|
3149 | VFS_UNLOCK_GIANT(vfslocked); |
---|
3150 | strlcpy(kif->kf_path, fullpath, |
---|
3151 | sizeof(kif->kf_path)); |
---|
3152 | if (freepath != NULL) |
---|
3153 | free(freepath, M_TEMP); |
---|
3154 | FILEDESC_SLOCK(fdp); |
---|
3155 | } |
---|
3156 | if (so != NULL) { |
---|
3157 | struct sockaddr *sa; |
---|
3158 | |
---|
3159 | if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) |
---|
3160 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { |
---|
3161 | bcopy(sa, &kif->kf_sa_local, sa->sa_len); |
---|
3162 | free(sa, M_SONAME); |
---|
3163 | } |
---|
3164 | if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) |
---|
3165 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { |
---|
3166 | bcopy(sa, &kif->kf_sa_peer, sa->sa_len); |
---|
3167 | free(sa, M_SONAME); |
---|
3168 | } |
---|
3169 | kif->kf_sock_domain = |
---|
3170 | so->so_proto->pr_domain->dom_family; |
---|
3171 | kif->kf_sock_type = so->so_type; |
---|
3172 | kif->kf_sock_protocol = so->so_proto->pr_protocol; |
---|
3173 | } |
---|
3174 | if (tp != NULL) { |
---|
3175 | strlcpy(kif->kf_path, tty_devname(tp), |
---|
3176 | sizeof(kif->kf_path)); |
---|
3177 | } |
---|
3178 | /* Pack record size down */ |
---|
3179 | kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + |
---|
3180 | strlen(kif->kf_path) + 1; |
---|
3181 | kif->kf_structsize = roundup(kif->kf_structsize, |
---|
3182 | sizeof(uint64_t)); |
---|
3183 | oldidx = req->oldidx; |
---|
3184 | error = SYSCTL_OUT(req, kif, kif->kf_structsize); |
---|
3185 | if (error) { |
---|
3186 | if (error == ENOMEM) { |
---|
3187 | /* |
---|
3188 | * The hack to keep the ABI of sysctl |
---|
3189 | * kern.proc.filedesc intact, but not |
---|
3190 | * to account a partially copied |
---|
3191 | * kinfo_file into the oldidx. |
---|
3192 | */ |
---|
3193 | req->oldidx = oldidx; |
---|
3194 | error = 0; |
---|
3195 | } |
---|
3196 | break; |
---|
3197 | } |
---|
3198 | } |
---|
3199 | FILEDESC_SUNLOCK(fdp); |
---|
3200 | fddrop(fdp); |
---|
3201 | free(kif, M_TEMP); |
---|
3202 | return (error); |
---|
3203 | } |
---|
3204 | |
---|
3205 | static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, |
---|
3206 | sysctl_kern_proc_filedesc, "Process filedesc entries"); |
---|
3207 | |
---|
3208 | #ifdef DDB |
---|
3209 | /* |
---|
3210 | * For the purposes of debugging, generate a human-readable string for the |
---|
3211 | * file type. |
---|
3212 | */ |
---|
3213 | static const char * |
---|
3214 | file_type_to_name(short type) |
---|
3215 | { |
---|
3216 | |
---|
3217 | switch (type) { |
---|
3218 | case 0: |
---|
3219 | return ("zero"); |
---|
3220 | case DTYPE_VNODE: |
---|
3221 | return ("vnod"); |
---|
3222 | case DTYPE_SOCKET: |
---|
3223 | return ("sock"); |
---|
3224 | case DTYPE_PIPE: |
---|
3225 | return ("pipe"); |
---|
3226 | case DTYPE_FIFO: |
---|
3227 | return ("fifo"); |
---|
3228 | case DTYPE_KQUEUE: |
---|
3229 | return ("kque"); |
---|
3230 | case DTYPE_CRYPTO: |
---|
3231 | return ("crpt"); |
---|
3232 | case DTYPE_MQUEUE: |
---|
3233 | return ("mque"); |
---|
3234 | case DTYPE_SHM: |
---|
3235 | return ("shm"); |
---|
3236 | case DTYPE_SEM: |
---|
3237 | return ("ksem"); |
---|
3238 | default: |
---|
3239 | return ("unkn"); |
---|
3240 | } |
---|
3241 | } |
---|
3242 | |
---|
3243 | /* |
---|
3244 | * For the purposes of debugging, identify a process (if any, perhaps one of |
---|
3245 | * many) that references the passed file in its file descriptor array. Return |
---|
3246 | * NULL if none. |
---|
3247 | */ |
---|
3248 | static struct proc * |
---|
3249 | file_to_first_proc(struct file *fp) |
---|
3250 | { |
---|
3251 | struct filedesc *fdp; |
---|
3252 | struct proc *p; |
---|
3253 | int n; |
---|
3254 | |
---|
3255 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
3256 | if (p->p_state == PRS_NEW) |
---|
3257 | continue; |
---|
3258 | fdp = p->p_fd; |
---|
3259 | if (fdp == NULL) |
---|
3260 | continue; |
---|
3261 | for (n = 0; n < fdp->fd_nfiles; n++) { |
---|
3262 | if (fp == fdp->fd_ofiles[n]) |
---|
3263 | return (p); |
---|
3264 | } |
---|
3265 | } |
---|
3266 | return (NULL); |
---|
3267 | } |
---|
3268 | |
---|
3269 | static void |
---|
3270 | db_print_file(struct file *fp, int header) |
---|
3271 | { |
---|
3272 | struct proc *p; |
---|
3273 | |
---|
3274 | if (header) |
---|
3275 | db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n", |
---|
3276 | "File", "Type", "Data", "Flag", "GCFl", "Count", |
---|
3277 | "MCount", "Vnode", "FPID", "FCmd"); |
---|
3278 | p = file_to_first_proc(fp); |
---|
3279 | db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp, |
---|
3280 | file_type_to_name(fp->f_type), fp->f_data, fp->f_flag, |
---|
3281 | 0, fp->f_count, 0, fp->f_vnode, |
---|
3282 | p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-"); |
---|
3283 | } |
---|
3284 | |
---|
3285 | DB_SHOW_COMMAND(file, db_show_file) |
---|
3286 | { |
---|
3287 | struct file *fp; |
---|
3288 | |
---|
3289 | if (!have_addr) { |
---|
3290 | db_printf("usage: show file <addr>\n"); |
---|
3291 | return; |
---|
3292 | } |
---|
3293 | fp = (struct file *)addr; |
---|
3294 | db_print_file(fp, 1); |
---|
3295 | } |
---|
3296 | |
---|
3297 | DB_SHOW_COMMAND(files, db_show_files) |
---|
3298 | { |
---|
3299 | struct filedesc *fdp; |
---|
3300 | struct file *fp; |
---|
3301 | struct proc *p; |
---|
3302 | int header; |
---|
3303 | int n; |
---|
3304 | |
---|
3305 | header = 1; |
---|
3306 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
3307 | if (p->p_state == PRS_NEW) |
---|
3308 | continue; |
---|
3309 | if ((fdp = p->p_fd) == NULL) |
---|
3310 | continue; |
---|
3311 | for (n = 0; n < fdp->fd_nfiles; ++n) { |
---|
3312 | if ((fp = fdp->fd_ofiles[n]) == NULL) |
---|
3313 | continue; |
---|
3314 | db_print_file(fp, header); |
---|
3315 | header = 0; |
---|
3316 | } |
---|
3317 | } |
---|
3318 | } |
---|
3319 | #endif |
---|
3320 | |
---|
3321 | SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, |
---|
3322 | &maxfilesperproc, 0, "Maximum files allowed open per process"); |
---|
3323 | |
---|
3324 | SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, |
---|
3325 | &maxfiles, 0, "Maximum number of files"); |
---|
3326 | |
---|
3327 | SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, |
---|
3328 | __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files"); |
---|
3329 | |
---|
3330 | /* ARGSUSED*/ |
---|
3331 | static void |
---|
3332 | filelistinit(void *dummy) |
---|
3333 | { |
---|
3334 | |
---|
3335 | file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, |
---|
3336 | NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); |
---|
3337 | mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); |
---|
3338 | mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF); |
---|
3339 | } |
---|
3340 | SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL); |
---|
3341 | #endif /* __rtems__ */ |
---|
3342 | |
---|
3343 | /*-------------------------------------------------------------------*/ |
---|
3344 | |
---|
3345 | static int |
---|
3346 | badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td) |
---|
3347 | { |
---|
3348 | |
---|
3349 | return (EBADF); |
---|
3350 | } |
---|
3351 | |
---|
3352 | static int |
---|
3353 | badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td) |
---|
3354 | { |
---|
3355 | |
---|
3356 | return (EINVAL); |
---|
3357 | } |
---|
3358 | |
---|
3359 | static int |
---|
3360 | badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td) |
---|
3361 | { |
---|
3362 | |
---|
3363 | return (EBADF); |
---|
3364 | } |
---|
3365 | |
---|
3366 | static int |
---|
3367 | badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td) |
---|
3368 | { |
---|
3369 | |
---|
3370 | return (0); |
---|
3371 | } |
---|
3372 | |
---|
3373 | static int |
---|
3374 | badfo_kqfilter(struct file *fp, struct knote *kn) |
---|
3375 | { |
---|
3376 | |
---|
3377 | return (EBADF); |
---|
3378 | } |
---|
3379 | |
---|
3380 | static int |
---|
3381 | badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td) |
---|
3382 | { |
---|
3383 | |
---|
3384 | return (EBADF); |
---|
3385 | } |
---|
3386 | |
---|
3387 | static int |
---|
3388 | badfo_close(struct file *fp, struct thread *td) |
---|
3389 | { |
---|
3390 | |
---|
3391 | return (EBADF); |
---|
3392 | } |
---|
3393 | |
---|
3394 | struct fileops badfileops = { |
---|
3395 | .fo_read = badfo_readwrite, |
---|
3396 | .fo_write = badfo_readwrite, |
---|
3397 | .fo_truncate = badfo_truncate, |
---|
3398 | .fo_ioctl = badfo_ioctl, |
---|
3399 | .fo_poll = badfo_poll, |
---|
3400 | .fo_kqfilter = badfo_kqfilter, |
---|
3401 | .fo_stat = badfo_stat, |
---|
3402 | .fo_close = badfo_close, |
---|
3403 | }; |
---|
3404 | |
---|
3405 | #ifndef __rtems__ |
---|
3406 | /*-------------------------------------------------------------------*/ |
---|
3407 | |
---|
3408 | /* |
---|
3409 | * File Descriptor pseudo-device driver (/dev/fd/). |
---|
3410 | * |
---|
3411 | * Opening minor device N dup()s the file (if any) connected to file |
---|
3412 | * descriptor N belonging to the calling process. Note that this driver |
---|
3413 | * consists of only the ``open()'' routine, because all subsequent |
---|
3414 | * references to this file will be direct to the other driver. |
---|
3415 | * |
---|
3416 | * XXX: we could give this one a cloning event handler if necessary. |
---|
3417 | */ |
---|
3418 | |
---|
3419 | /* ARGSUSED */ |
---|
3420 | static int |
---|
3421 | fdopen(struct cdev *dev, int mode, int type, struct thread *td) |
---|
3422 | { |
---|
3423 | |
---|
3424 | /* |
---|
3425 | /*- |
---|
3426 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 |
---|
3427 | * The Regents of the University of California. All rights reserved. |
---|
3428 | * (c) UNIX System Laboratories, Inc. |
---|
3429 | * All or some portions of this file are derived from material licensed |
---|
3430 | * to the University of California by American Telephone and Telegraph |
---|
3431 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
---|
3432 | * the permission of UNIX System Laboratories, Inc. |
---|
3433 | * |
---|
3434 | * Redistribution and use in source and binary forms, with or without |
---|
3435 | * modification, are permitted provided that the following conditions |
---|
3436 | * are met: |
---|
3437 | * 1. Redistributions of source code must retain the above copyright |
---|
3438 | * notice, this list of conditions and the following disclaimer. |
---|
3439 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
3440 | * notice, this list of conditions and the following disclaimer in the |
---|
3441 | * documentation and/or other materials provided with the distribution. |
---|
3442 | * 4. Neither the name of the University nor the names of its contributors |
---|
3443 | * may be used to endorse or promote products derived from this software |
---|
3444 | * without specific prior written permission. |
---|
3445 | * |
---|
3446 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
3447 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
3448 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
3449 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
3450 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
3451 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
3452 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
3453 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
3454 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
3455 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
3456 | * SUCH DAMAGE. |
---|
3457 | * |
---|
3458 | * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 |
---|
3459 | */ |
---|
3460 | |
---|
3461 | #include <sys/cdefs.h> |
---|
3462 | __FBSDID("$FreeBSD$"); |
---|
3463 | |
---|
3464 | #include <rtems/bsd/local/opt_compat.h> |
---|
3465 | #include <rtems/bsd/local/opt_ddb.h> |
---|
3466 | #include <rtems/bsd/local/opt_ktrace.h> |
---|
3467 | |
---|
3468 | #include <rtems/bsd/sys/param.h> |
---|
3469 | #include <sys/systm.h> |
---|
3470 | |
---|
3471 | #include <sys/conf.h> |
---|
3472 | #include <sys/domain.h> |
---|
3473 | #include <sys/fcntl.h> |
---|
3474 | #include <sys/file.h> |
---|
3475 | #include <sys/filedesc.h> |
---|
3476 | #include <sys/filio.h> |
---|
3477 | #include <sys/jail.h> |
---|
3478 | #include <sys/kernel.h> |
---|
3479 | #include <sys/limits.h> |
---|
3480 | #include <rtems/bsd/sys/lock.h> |
---|
3481 | #include <sys/malloc.h> |
---|
3482 | #include <sys/mount.h> |
---|
3483 | #include <sys/mqueue.h> |
---|
3484 | #include <sys/mutex.h> |
---|
3485 | #include <sys/namei.h> |
---|
3486 | #include <sys/priv.h> |
---|
3487 | #include <sys/proc.h> |
---|
3488 | #include <sys/protosw.h> |
---|
3489 | #include <sys/resourcevar.h> |
---|
3490 | #include <sys/signalvar.h> |
---|
3491 | #include <sys/socketvar.h> |
---|
3492 | #include <sys/stat.h> |
---|
3493 | #include <sys/sx.h> |
---|
3494 | #include <sys/syscallsubr.h> |
---|
3495 | #include <sys/sysctl.h> |
---|
3496 | #include <sys/sysproto.h> |
---|
3497 | #include <sys/tty.h> |
---|
3498 | #include <rtems/bsd/sys/unistd.h> |
---|
3499 | #include <sys/user.h> |
---|
3500 | #include <sys/vnode.h> |
---|
3501 | #ifdef KTRACE |
---|
3502 | #include <sys/ktrace.h> |
---|
3503 | #endif |
---|
3504 | |
---|
3505 | #include <security/audit/audit.h> |
---|
3506 | |
---|
3507 | #include <vm/uma.h> |
---|
3508 | |
---|
3509 | #include <ddb/ddb.h> |
---|
3510 | |
---|
3511 | static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table"); |
---|
3512 | static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader", |
---|
3513 | "file desc to leader structures"); |
---|
3514 | static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures"); |
---|
3515 | |
---|
3516 | static uma_zone_t file_zone; |
---|
3517 | |
---|
3518 | |
---|
3519 | /* Flags for do_dup() */ |
---|
3520 | #define DUP_FIXED 0x1 /* Force fixed allocation */ |
---|
3521 | #define DUP_FCNTL 0x2 /* fcntl()-style errors */ |
---|
3522 | |
---|
3523 | static int do_dup(struct thread *td, int flags, int old, int new, |
---|
3524 | register_t *retval); |
---|
3525 | static int fd_first_free(struct filedesc *, int, int); |
---|
3526 | static int fd_last_used(struct filedesc *, int, int); |
---|
3527 | static void fdgrowtable(struct filedesc *, int); |
---|
3528 | static void fdunused(struct filedesc *fdp, int fd); |
---|
3529 | static void fdused(struct filedesc *fdp, int fd); |
---|
3530 | |
---|
3531 | /* |
---|
3532 | * A process is initially started out with NDFILE descriptors stored within |
---|
3533 | * this structure, selected to be enough for typical applications based on |
---|
3534 | * the historical limit of 20 open files (and the usage of descriptors by |
---|
3535 | * shells). If these descriptors are exhausted, a larger descriptor table |
---|
3536 | * may be allocated, up to a process' resource limit; the internal arrays |
---|
3537 | * are then unused. |
---|
3538 | */ |
---|
3539 | #define NDFILE 20 |
---|
3540 | #define NDSLOTSIZE sizeof(NDSLOTTYPE) |
---|
3541 | #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT) |
---|
3542 | #define NDSLOT(x) ((x) / NDENTRIES) |
---|
3543 | #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES)) |
---|
3544 | #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES) |
---|
3545 | |
---|
3546 | /* |
---|
3547 | * Storage required per open file descriptor. |
---|
3548 | */ |
---|
3549 | #define OFILESIZE (sizeof(struct file *) + sizeof(char)) |
---|
3550 | |
---|
3551 | /* |
---|
3552 | * Storage to hold unused ofiles that need to be reclaimed. |
---|
3553 | */ |
---|
3554 | struct freetable { |
---|
3555 | struct file **ft_table; |
---|
3556 | SLIST_ENTRY(freetable) ft_next; |
---|
3557 | }; |
---|
3558 | |
---|
3559 | /* |
---|
3560 | * Basic allocation of descriptors: |
---|
3561 | * one of the above, plus arrays for NDFILE descriptors. |
---|
3562 | */ |
---|
3563 | struct filedesc0 { |
---|
3564 | struct filedesc fd_fd; |
---|
3565 | /* |
---|
3566 | * ofiles which need to be reclaimed on free. |
---|
3567 | */ |
---|
3568 | SLIST_HEAD(,freetable) fd_free; |
---|
3569 | /* |
---|
3570 | * These arrays are used when the number of open files is |
---|
3571 | * <= NDFILE, and are then pointed to by the pointers above. |
---|
3572 | */ |
---|
3573 | struct file *fd_dfiles[NDFILE]; |
---|
3574 | char fd_dfileflags[NDFILE]; |
---|
3575 | NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)]; |
---|
3576 | }; |
---|
3577 | |
---|
3578 | /* |
---|
3579 | * Descriptor management. |
---|
3580 | */ |
---|
3581 | volatile int openfiles; /* actual number of open files */ |
---|
3582 | struct mtx sigio_lock; /* mtx to protect pointers to sigio */ |
---|
3583 | #ifndef __rtems__ |
---|
3584 | void (*mq_fdclose)(struct thread *td, int fd, struct file *fp); |
---|
3585 | |
---|
3586 | /* A mutex to protect the association between a proc and filedesc. */ |
---|
3587 | static struct mtx fdesc_mtx; |
---|
3588 | |
---|
3589 | /* |
---|
3590 | * Find the first zero bit in the given bitmap, starting at low and not |
---|
3591 | * exceeding size - 1. |
---|
3592 | */ |
---|
3593 | static int |
---|
3594 | fd_first_free(struct filedesc *fdp, int low, int size) |
---|
3595 | { |
---|
3596 | NDSLOTTYPE *map = fdp->fd_map; |
---|
3597 | NDSLOTTYPE mask; |
---|
3598 | int off, maxoff; |
---|
3599 | |
---|
3600 | if (low >= size) |
---|
3601 | return (low); |
---|
3602 | |
---|
3603 | off = NDSLOT(low); |
---|
3604 | if (low % NDENTRIES) { |
---|
3605 | mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES))); |
---|
3606 | if ((mask &= ~map[off]) != 0UL) |
---|
3607 | return (off * NDENTRIES + ffsl(mask) - 1); |
---|
3608 | ++off; |
---|
3609 | } |
---|
3610 | for (maxoff = NDSLOTS(size); off < maxoff; ++off) |
---|
3611 | if (map[off] != ~0UL) |
---|
3612 | return (off * NDENTRIES + ffsl(~map[off]) - 1); |
---|
3613 | return (size); |
---|
3614 | } |
---|
3615 | |
---|
3616 | /* |
---|
3617 | * Find the highest non-zero bit in the given bitmap, starting at low and |
---|
3618 | * not exceeding size - 1. |
---|
3619 | */ |
---|
3620 | static int |
---|
3621 | fd_last_used(struct filedesc *fdp, int low, int size) |
---|
3622 | { |
---|
3623 | NDSLOTTYPE *map = fdp->fd_map; |
---|
3624 | NDSLOTTYPE mask; |
---|
3625 | int off, minoff; |
---|
3626 | |
---|
3627 | if (low >= size) |
---|
3628 | return (-1); |
---|
3629 | |
---|
3630 | off = NDSLOT(size); |
---|
3631 | if (size % NDENTRIES) { |
---|
3632 | mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES)); |
---|
3633 | if ((mask &= map[off]) != 0) |
---|
3634 | return (off * NDENTRIES + flsl(mask) - 1); |
---|
3635 | --off; |
---|
3636 | } |
---|
3637 | for (minoff = NDSLOT(low); off >= minoff; --off) |
---|
3638 | if (map[off] != 0) |
---|
3639 | return (off * NDENTRIES + flsl(map[off]) - 1); |
---|
3640 | return (low - 1); |
---|
3641 | } |
---|
3642 | |
---|
3643 | static int |
---|
3644 | fdisused(struct filedesc *fdp, int fd) |
---|
3645 | { |
---|
3646 | KASSERT(fd >= 0 && fd < fdp->fd_nfiles, |
---|
3647 | ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles)); |
---|
3648 | return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0); |
---|
3649 | } |
---|
3650 | |
---|
3651 | /* |
---|
3652 | * Mark a file descriptor as used. |
---|
3653 | */ |
---|
3654 | static void |
---|
3655 | fdused(struct filedesc *fdp, int fd) |
---|
3656 | { |
---|
3657 | |
---|
3658 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
3659 | KASSERT(!fdisused(fdp, fd), |
---|
3660 | ("fd already used")); |
---|
3661 | |
---|
3662 | fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd); |
---|
3663 | if (fd > fdp->fd_lastfile) |
---|
3664 | fdp->fd_lastfile = fd; |
---|
3665 | if (fd == fdp->fd_freefile) |
---|
3666 | fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles); |
---|
3667 | } |
---|
3668 | |
---|
3669 | /* |
---|
3670 | * Mark a file descriptor as unused. |
---|
3671 | */ |
---|
3672 | static void |
---|
3673 | fdunused(struct filedesc *fdp, int fd) |
---|
3674 | { |
---|
3675 | |
---|
3676 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
3677 | KASSERT(fdisused(fdp, fd), |
---|
3678 | ("fd is already unused")); |
---|
3679 | KASSERT(fdp->fd_ofiles[fd] == NULL, |
---|
3680 | ("fd is still in use")); |
---|
3681 | |
---|
3682 | fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd); |
---|
3683 | if (fd < fdp->fd_freefile) |
---|
3684 | fdp->fd_freefile = fd; |
---|
3685 | if (fd == fdp->fd_lastfile) |
---|
3686 | fdp->fd_lastfile = fd_last_used(fdp, 0, fd); |
---|
3687 | } |
---|
3688 | |
---|
3689 | /* |
---|
3690 | * System calls on descriptors. |
---|
3691 | */ |
---|
3692 | #ifndef _SYS_SYSPROTO_H_ |
---|
3693 | struct getdtablesize_args { |
---|
3694 | int dummy; |
---|
3695 | }; |
---|
3696 | #endif |
---|
3697 | /* ARGSUSED */ |
---|
3698 | int |
---|
3699 | getdtablesize(struct thread *td, struct getdtablesize_args *uap) |
---|
3700 | { |
---|
3701 | struct proc *p = td->td_proc; |
---|
3702 | |
---|
3703 | PROC_LOCK(p); |
---|
3704 | td->td_retval[0] = |
---|
3705 | min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
3706 | PROC_UNLOCK(p); |
---|
3707 | return (0); |
---|
3708 | } |
---|
3709 | |
---|
3710 | /* |
---|
3711 | * Duplicate a file descriptor to a particular value. |
---|
3712 | * |
---|
3713 | * Note: keep in mind that a potential race condition exists when closing |
---|
3714 | * descriptors from a shared descriptor table (via rfork). |
---|
3715 | */ |
---|
3716 | #ifndef _SYS_SYSPROTO_H_ |
---|
3717 | struct dup2_args { |
---|
3718 | u_int from; |
---|
3719 | u_int to; |
---|
3720 | }; |
---|
3721 | #endif |
---|
3722 | /* ARGSUSED */ |
---|
3723 | int |
---|
3724 | dup2(struct thread *td, struct dup2_args *uap) |
---|
3725 | { |
---|
3726 | |
---|
3727 | return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to, |
---|
3728 | td->td_retval)); |
---|
3729 | } |
---|
3730 | |
---|
3731 | /* |
---|
3732 | * Duplicate a file descriptor. |
---|
3733 | */ |
---|
3734 | #ifndef _SYS_SYSPROTO_H_ |
---|
3735 | struct dup_args { |
---|
3736 | u_int fd; |
---|
3737 | }; |
---|
3738 | #endif |
---|
3739 | /* ARGSUSED */ |
---|
3740 | int |
---|
3741 | dup(struct thread *td, struct dup_args *uap) |
---|
3742 | { |
---|
3743 | |
---|
3744 | return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval)); |
---|
3745 | } |
---|
3746 | |
---|
3747 | /* |
---|
3748 | * The file control system call. |
---|
3749 | */ |
---|
3750 | #ifndef _SYS_SYSPROTO_H_ |
---|
3751 | struct fcntl_args { |
---|
3752 | int fd; |
---|
3753 | int cmd; |
---|
3754 | long arg; |
---|
3755 | }; |
---|
3756 | #endif |
---|
3757 | /* ARGSUSED */ |
---|
3758 | int |
---|
3759 | fcntl(struct thread *td, struct fcntl_args *uap) |
---|
3760 | { |
---|
3761 | struct flock fl; |
---|
3762 | struct oflock ofl; |
---|
3763 | intptr_t arg; |
---|
3764 | int error; |
---|
3765 | int cmd; |
---|
3766 | |
---|
3767 | error = 0; |
---|
3768 | cmd = uap->cmd; |
---|
3769 | switch (uap->cmd) { |
---|
3770 | case F_OGETLK: |
---|
3771 | case F_OSETLK: |
---|
3772 | case F_OSETLKW: |
---|
3773 | /* |
---|
3774 | * Convert old flock structure to new. |
---|
3775 | */ |
---|
3776 | error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl)); |
---|
3777 | fl.l_start = ofl.l_start; |
---|
3778 | fl.l_len = ofl.l_len; |
---|
3779 | fl.l_pid = ofl.l_pid; |
---|
3780 | fl.l_type = ofl.l_type; |
---|
3781 | fl.l_whence = ofl.l_whence; |
---|
3782 | fl.l_sysid = 0; |
---|
3783 | |
---|
3784 | switch (uap->cmd) { |
---|
3785 | case F_OGETLK: |
---|
3786 | cmd = F_GETLK; |
---|
3787 | break; |
---|
3788 | case F_OSETLK: |
---|
3789 | cmd = F_SETLK; |
---|
3790 | break; |
---|
3791 | case F_OSETLKW: |
---|
3792 | cmd = F_SETLKW; |
---|
3793 | break; |
---|
3794 | } |
---|
3795 | arg = (intptr_t)&fl; |
---|
3796 | break; |
---|
3797 | case F_GETLK: |
---|
3798 | case F_SETLK: |
---|
3799 | case F_SETLKW: |
---|
3800 | case F_SETLK_REMOTE: |
---|
3801 | error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl)); |
---|
3802 | arg = (intptr_t)&fl; |
---|
3803 | break; |
---|
3804 | default: |
---|
3805 | arg = uap->arg; |
---|
3806 | break; |
---|
3807 | } |
---|
3808 | if (error) |
---|
3809 | return (error); |
---|
3810 | error = kern_fcntl(td, uap->fd, cmd, arg); |
---|
3811 | if (error) |
---|
3812 | return (error); |
---|
3813 | if (uap->cmd == F_OGETLK) { |
---|
3814 | ofl.l_start = fl.l_start; |
---|
3815 | ofl.l_len = fl.l_len; |
---|
3816 | ofl.l_pid = fl.l_pid; |
---|
3817 | ofl.l_type = fl.l_type; |
---|
3818 | ofl.l_whence = fl.l_whence; |
---|
3819 | error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl)); |
---|
3820 | } else if (uap->cmd == F_GETLK) { |
---|
3821 | error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl)); |
---|
3822 | } |
---|
3823 | return (error); |
---|
3824 | } |
---|
3825 | |
---|
3826 | static inline struct file * |
---|
3827 | fdtofp(int fd, struct filedesc *fdp) |
---|
3828 | { |
---|
3829 | struct file *fp; |
---|
3830 | |
---|
3831 | FILEDESC_LOCK_ASSERT(fdp); |
---|
3832 | if ((unsigned)fd >= fdp->fd_nfiles || |
---|
3833 | (fp = fdp->fd_ofiles[fd]) == NULL) |
---|
3834 | return (NULL); |
---|
3835 | return (fp); |
---|
3836 | } |
---|
3837 | |
---|
3838 | int |
---|
3839 | kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg) |
---|
3840 | { |
---|
3841 | struct filedesc *fdp; |
---|
3842 | struct flock *flp; |
---|
3843 | struct file *fp; |
---|
3844 | struct proc *p; |
---|
3845 | char *pop; |
---|
3846 | struct vnode *vp; |
---|
3847 | int error, flg, tmp; |
---|
3848 | int vfslocked; |
---|
3849 | u_int old, new; |
---|
3850 | uint64_t bsize; |
---|
3851 | |
---|
3852 | vfslocked = 0; |
---|
3853 | error = 0; |
---|
3854 | flg = F_POSIX; |
---|
3855 | p = td->td_proc; |
---|
3856 | fdp = p->p_fd; |
---|
3857 | |
---|
3858 | switch (cmd) { |
---|
3859 | case F_DUPFD: |
---|
3860 | tmp = arg; |
---|
3861 | error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval); |
---|
3862 | break; |
---|
3863 | |
---|
3864 | case F_DUP2FD: |
---|
3865 | tmp = arg; |
---|
3866 | error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval); |
---|
3867 | break; |
---|
3868 | |
---|
3869 | case F_GETFD: |
---|
3870 | FILEDESC_SLOCK(fdp); |
---|
3871 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3872 | FILEDESC_SUNLOCK(fdp); |
---|
3873 | error = EBADF; |
---|
3874 | break; |
---|
3875 | } |
---|
3876 | pop = &fdp->fd_ofileflags[fd]; |
---|
3877 | td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0; |
---|
3878 | FILEDESC_SUNLOCK(fdp); |
---|
3879 | break; |
---|
3880 | |
---|
3881 | case F_SETFD: |
---|
3882 | FILEDESC_XLOCK(fdp); |
---|
3883 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3884 | FILEDESC_XUNLOCK(fdp); |
---|
3885 | error = EBADF; |
---|
3886 | break; |
---|
3887 | } |
---|
3888 | pop = &fdp->fd_ofileflags[fd]; |
---|
3889 | *pop = (*pop &~ UF_EXCLOSE) | |
---|
3890 | (arg & FD_CLOEXEC ? UF_EXCLOSE : 0); |
---|
3891 | FILEDESC_XUNLOCK(fdp); |
---|
3892 | break; |
---|
3893 | |
---|
3894 | case F_GETFL: |
---|
3895 | FILEDESC_SLOCK(fdp); |
---|
3896 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3897 | FILEDESC_SUNLOCK(fdp); |
---|
3898 | error = EBADF; |
---|
3899 | break; |
---|
3900 | } |
---|
3901 | td->td_retval[0] = OFLAGS(fp->f_flag); |
---|
3902 | FILEDESC_SUNLOCK(fdp); |
---|
3903 | break; |
---|
3904 | |
---|
3905 | case F_SETFL: |
---|
3906 | FILEDESC_SLOCK(fdp); |
---|
3907 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3908 | FILEDESC_SUNLOCK(fdp); |
---|
3909 | error = EBADF; |
---|
3910 | break; |
---|
3911 | } |
---|
3912 | fhold(fp); |
---|
3913 | FILEDESC_SUNLOCK(fdp); |
---|
3914 | do { |
---|
3915 | tmp = flg = fp->f_flag; |
---|
3916 | tmp &= ~FCNTLFLAGS; |
---|
3917 | tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS; |
---|
3918 | } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); |
---|
3919 | tmp = fp->f_flag & FNONBLOCK; |
---|
3920 | error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); |
---|
3921 | if (error) { |
---|
3922 | fdrop(fp, td); |
---|
3923 | break; |
---|
3924 | } |
---|
3925 | tmp = fp->f_flag & FASYNC; |
---|
3926 | error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); |
---|
3927 | if (error == 0) { |
---|
3928 | fdrop(fp, td); |
---|
3929 | break; |
---|
3930 | } |
---|
3931 | atomic_clear_int(&fp->f_flag, FNONBLOCK); |
---|
3932 | tmp = 0; |
---|
3933 | (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); |
---|
3934 | fdrop(fp, td); |
---|
3935 | break; |
---|
3936 | |
---|
3937 | case F_GETOWN: |
---|
3938 | FILEDESC_SLOCK(fdp); |
---|
3939 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3940 | FILEDESC_SUNLOCK(fdp); |
---|
3941 | error = EBADF; |
---|
3942 | break; |
---|
3943 | } |
---|
3944 | fhold(fp); |
---|
3945 | FILEDESC_SUNLOCK(fdp); |
---|
3946 | error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td); |
---|
3947 | if (error == 0) |
---|
3948 | td->td_retval[0] = tmp; |
---|
3949 | fdrop(fp, td); |
---|
3950 | break; |
---|
3951 | |
---|
3952 | case F_SETOWN: |
---|
3953 | FILEDESC_SLOCK(fdp); |
---|
3954 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3955 | FILEDESC_SUNLOCK(fdp); |
---|
3956 | error = EBADF; |
---|
3957 | break; |
---|
3958 | } |
---|
3959 | fhold(fp); |
---|
3960 | FILEDESC_SUNLOCK(fdp); |
---|
3961 | tmp = arg; |
---|
3962 | error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td); |
---|
3963 | fdrop(fp, td); |
---|
3964 | break; |
---|
3965 | |
---|
3966 | case F_SETLK_REMOTE: |
---|
3967 | error = priv_check(td, PRIV_NFS_LOCKD); |
---|
3968 | if (error) |
---|
3969 | return (error); |
---|
3970 | flg = F_REMOTE; |
---|
3971 | goto do_setlk; |
---|
3972 | |
---|
3973 | case F_SETLKW: |
---|
3974 | flg |= F_WAIT; |
---|
3975 | /* FALLTHROUGH F_SETLK */ |
---|
3976 | |
---|
3977 | case F_SETLK: |
---|
3978 | do_setlk: |
---|
3979 | FILEDESC_SLOCK(fdp); |
---|
3980 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
3981 | FILEDESC_SUNLOCK(fdp); |
---|
3982 | error = EBADF; |
---|
3983 | break; |
---|
3984 | } |
---|
3985 | if (fp->f_type != DTYPE_VNODE) { |
---|
3986 | FILEDESC_SUNLOCK(fdp); |
---|
3987 | error = EBADF; |
---|
3988 | break; |
---|
3989 | } |
---|
3990 | flp = (struct flock *)arg; |
---|
3991 | if (flp->l_whence == SEEK_CUR) { |
---|
3992 | if (fp->f_offset < 0 || |
---|
3993 | (flp->l_start > 0 && |
---|
3994 | fp->f_offset > OFF_MAX - flp->l_start)) { |
---|
3995 | FILEDESC_SUNLOCK(fdp); |
---|
3996 | error = EOVERFLOW; |
---|
3997 | break; |
---|
3998 | } |
---|
3999 | flp->l_start += fp->f_offset; |
---|
4000 | } |
---|
4001 | |
---|
4002 | /* |
---|
4003 | * VOP_ADVLOCK() may block. |
---|
4004 | */ |
---|
4005 | fhold(fp); |
---|
4006 | FILEDESC_SUNLOCK(fdp); |
---|
4007 | vp = fp->f_vnode; |
---|
4008 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
4009 | switch (flp->l_type) { |
---|
4010 | case F_RDLCK: |
---|
4011 | if ((fp->f_flag & FREAD) == 0) { |
---|
4012 | error = EBADF; |
---|
4013 | break; |
---|
4014 | } |
---|
4015 | PROC_LOCK(p->p_leader); |
---|
4016 | p->p_leader->p_flag |= P_ADVLOCK; |
---|
4017 | PROC_UNLOCK(p->p_leader); |
---|
4018 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, |
---|
4019 | flp, flg); |
---|
4020 | break; |
---|
4021 | case F_WRLCK: |
---|
4022 | if ((fp->f_flag & FWRITE) == 0) { |
---|
4023 | error = EBADF; |
---|
4024 | break; |
---|
4025 | } |
---|
4026 | PROC_LOCK(p->p_leader); |
---|
4027 | p->p_leader->p_flag |= P_ADVLOCK; |
---|
4028 | PROC_UNLOCK(p->p_leader); |
---|
4029 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, |
---|
4030 | flp, flg); |
---|
4031 | break; |
---|
4032 | case F_UNLCK: |
---|
4033 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, |
---|
4034 | flp, flg); |
---|
4035 | break; |
---|
4036 | case F_UNLCKSYS: |
---|
4037 | /* |
---|
4038 | * Temporary api for testing remote lock |
---|
4039 | * infrastructure. |
---|
4040 | */ |
---|
4041 | if (flg != F_REMOTE) { |
---|
4042 | error = EINVAL; |
---|
4043 | break; |
---|
4044 | } |
---|
4045 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, |
---|
4046 | F_UNLCKSYS, flp, flg); |
---|
4047 | break; |
---|
4048 | default: |
---|
4049 | error = EINVAL; |
---|
4050 | break; |
---|
4051 | } |
---|
4052 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4053 | vfslocked = 0; |
---|
4054 | /* Check for race with close */ |
---|
4055 | FILEDESC_SLOCK(fdp); |
---|
4056 | if ((unsigned) fd >= fdp->fd_nfiles || |
---|
4057 | fp != fdp->fd_ofiles[fd]) { |
---|
4058 | FILEDESC_SUNLOCK(fdp); |
---|
4059 | flp->l_whence = SEEK_SET; |
---|
4060 | flp->l_start = 0; |
---|
4061 | flp->l_len = 0; |
---|
4062 | flp->l_type = F_UNLCK; |
---|
4063 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
4064 | (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, |
---|
4065 | F_UNLCK, flp, F_POSIX); |
---|
4066 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4067 | vfslocked = 0; |
---|
4068 | } else |
---|
4069 | FILEDESC_SUNLOCK(fdp); |
---|
4070 | fdrop(fp, td); |
---|
4071 | break; |
---|
4072 | |
---|
4073 | case F_GETLK: |
---|
4074 | FILEDESC_SLOCK(fdp); |
---|
4075 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
4076 | FILEDESC_SUNLOCK(fdp); |
---|
4077 | error = EBADF; |
---|
4078 | break; |
---|
4079 | } |
---|
4080 | if (fp->f_type != DTYPE_VNODE) { |
---|
4081 | FILEDESC_SUNLOCK(fdp); |
---|
4082 | error = EBADF; |
---|
4083 | break; |
---|
4084 | } |
---|
4085 | flp = (struct flock *)arg; |
---|
4086 | if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK && |
---|
4087 | flp->l_type != F_UNLCK) { |
---|
4088 | FILEDESC_SUNLOCK(fdp); |
---|
4089 | error = EINVAL; |
---|
4090 | break; |
---|
4091 | } |
---|
4092 | if (flp->l_whence == SEEK_CUR) { |
---|
4093 | if ((flp->l_start > 0 && |
---|
4094 | fp->f_offset > OFF_MAX - flp->l_start) || |
---|
4095 | (flp->l_start < 0 && |
---|
4096 | fp->f_offset < OFF_MIN - flp->l_start)) { |
---|
4097 | FILEDESC_SUNLOCK(fdp); |
---|
4098 | error = EOVERFLOW; |
---|
4099 | break; |
---|
4100 | } |
---|
4101 | flp->l_start += fp->f_offset; |
---|
4102 | } |
---|
4103 | /* |
---|
4104 | * VOP_ADVLOCK() may block. |
---|
4105 | */ |
---|
4106 | fhold(fp); |
---|
4107 | FILEDESC_SUNLOCK(fdp); |
---|
4108 | vp = fp->f_vnode; |
---|
4109 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
4110 | error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp, |
---|
4111 | F_POSIX); |
---|
4112 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4113 | vfslocked = 0; |
---|
4114 | fdrop(fp, td); |
---|
4115 | break; |
---|
4116 | |
---|
4117 | case F_RDAHEAD: |
---|
4118 | arg = arg ? 128 * 1024: 0; |
---|
4119 | /* FALLTHROUGH */ |
---|
4120 | case F_READAHEAD: |
---|
4121 | FILEDESC_SLOCK(fdp); |
---|
4122 | if ((fp = fdtofp(fd, fdp)) == NULL) { |
---|
4123 | FILEDESC_SUNLOCK(fdp); |
---|
4124 | error = EBADF; |
---|
4125 | break; |
---|
4126 | } |
---|
4127 | if (fp->f_type != DTYPE_VNODE) { |
---|
4128 | FILEDESC_SUNLOCK(fdp); |
---|
4129 | error = EBADF; |
---|
4130 | break; |
---|
4131 | } |
---|
4132 | fhold(fp); |
---|
4133 | FILEDESC_SUNLOCK(fdp); |
---|
4134 | if (arg != 0) { |
---|
4135 | vp = fp->f_vnode; |
---|
4136 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
4137 | error = vn_lock(vp, LK_SHARED); |
---|
4138 | if (error != 0) |
---|
4139 | goto readahead_vnlock_fail; |
---|
4140 | bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize; |
---|
4141 | VOP_UNLOCK(vp, 0); |
---|
4142 | fp->f_seqcount = (arg + bsize - 1) / bsize; |
---|
4143 | do { |
---|
4144 | new = old = fp->f_flag; |
---|
4145 | new |= FRDAHEAD; |
---|
4146 | } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); |
---|
4147 | readahead_vnlock_fail: |
---|
4148 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4149 | vfslocked = 0; |
---|
4150 | } else { |
---|
4151 | do { |
---|
4152 | new = old = fp->f_flag; |
---|
4153 | new &= ~FRDAHEAD; |
---|
4154 | } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); |
---|
4155 | } |
---|
4156 | fdrop(fp, td); |
---|
4157 | break; |
---|
4158 | |
---|
4159 | default: |
---|
4160 | error = EINVAL; |
---|
4161 | break; |
---|
4162 | } |
---|
4163 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4164 | return (error); |
---|
4165 | } |
---|
4166 | |
---|
4167 | /* |
---|
4168 | * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD). |
---|
4169 | */ |
---|
4170 | static int |
---|
4171 | do_dup(struct thread *td, int flags, int old, int new, |
---|
4172 | register_t *retval) |
---|
4173 | { |
---|
4174 | struct filedesc *fdp; |
---|
4175 | struct proc *p; |
---|
4176 | struct file *fp; |
---|
4177 | struct file *delfp; |
---|
4178 | int error, holdleaders, maxfd; |
---|
4179 | |
---|
4180 | p = td->td_proc; |
---|
4181 | fdp = p->p_fd; |
---|
4182 | |
---|
4183 | /* |
---|
4184 | * Verify we have a valid descriptor to dup from and possibly to |
---|
4185 | * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should |
---|
4186 | * return EINVAL when the new descriptor is out of bounds. |
---|
4187 | */ |
---|
4188 | if (old < 0) |
---|
4189 | return (EBADF); |
---|
4190 | if (new < 0) |
---|
4191 | return (flags & DUP_FCNTL ? EINVAL : EBADF); |
---|
4192 | PROC_LOCK(p); |
---|
4193 | maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
4194 | PROC_UNLOCK(p); |
---|
4195 | if (new >= maxfd) |
---|
4196 | return (flags & DUP_FCNTL ? EINVAL : EMFILE); |
---|
4197 | |
---|
4198 | FILEDESC_XLOCK(fdp); |
---|
4199 | if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { |
---|
4200 | FILEDESC_XUNLOCK(fdp); |
---|
4201 | return (EBADF); |
---|
4202 | } |
---|
4203 | if (flags & DUP_FIXED && old == new) { |
---|
4204 | *retval = new; |
---|
4205 | FILEDESC_XUNLOCK(fdp); |
---|
4206 | return (0); |
---|
4207 | } |
---|
4208 | fp = fdp->fd_ofiles[old]; |
---|
4209 | fhold(fp); |
---|
4210 | |
---|
4211 | /* |
---|
4212 | * If the caller specified a file descriptor, make sure the file |
---|
4213 | * table is large enough to hold it, and grab it. Otherwise, just |
---|
4214 | * allocate a new descriptor the usual way. Since the filedesc |
---|
4215 | * lock may be temporarily dropped in the process, we have to look |
---|
4216 | * out for a race. |
---|
4217 | */ |
---|
4218 | if (flags & DUP_FIXED) { |
---|
4219 | if (new >= fdp->fd_nfiles) |
---|
4220 | fdgrowtable(fdp, new + 1); |
---|
4221 | if (fdp->fd_ofiles[new] == NULL) |
---|
4222 | fdused(fdp, new); |
---|
4223 | } else { |
---|
4224 | if ((error = fdalloc(td, new, &new)) != 0) { |
---|
4225 | FILEDESC_XUNLOCK(fdp); |
---|
4226 | fdrop(fp, td); |
---|
4227 | return (error); |
---|
4228 | } |
---|
4229 | } |
---|
4230 | |
---|
4231 | /* |
---|
4232 | * If the old file changed out from under us then treat it as a |
---|
4233 | * bad file descriptor. Userland should do its own locking to |
---|
4234 | * avoid this case. |
---|
4235 | */ |
---|
4236 | if (fdp->fd_ofiles[old] != fp) { |
---|
4237 | /* we've allocated a descriptor which we won't use */ |
---|
4238 | if (fdp->fd_ofiles[new] == NULL) |
---|
4239 | fdunused(fdp, new); |
---|
4240 | FILEDESC_XUNLOCK(fdp); |
---|
4241 | fdrop(fp, td); |
---|
4242 | return (EBADF); |
---|
4243 | } |
---|
4244 | KASSERT(old != new, |
---|
4245 | ("new fd is same as old")); |
---|
4246 | |
---|
4247 | /* |
---|
4248 | * Save info on the descriptor being overwritten. We cannot close |
---|
4249 | * it without introducing an ownership race for the slot, since we |
---|
4250 | * need to drop the filedesc lock to call closef(). |
---|
4251 | * |
---|
4252 | * XXX this duplicates parts of close(). |
---|
4253 | */ |
---|
4254 | delfp = fdp->fd_ofiles[new]; |
---|
4255 | holdleaders = 0; |
---|
4256 | if (delfp != NULL) { |
---|
4257 | if (td->td_proc->p_fdtol != NULL) { |
---|
4258 | /* |
---|
4259 | * Ask fdfree() to sleep to ensure that all relevant |
---|
4260 | * process leaders can be traversed in closef(). |
---|
4261 | */ |
---|
4262 | fdp->fd_holdleaderscount++; |
---|
4263 | holdleaders = 1; |
---|
4264 | } |
---|
4265 | } |
---|
4266 | |
---|
4267 | /* |
---|
4268 | * Duplicate the source descriptor |
---|
4269 | */ |
---|
4270 | fdp->fd_ofiles[new] = fp; |
---|
4271 | fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; |
---|
4272 | if (new > fdp->fd_lastfile) |
---|
4273 | fdp->fd_lastfile = new; |
---|
4274 | *retval = new; |
---|
4275 | |
---|
4276 | /* |
---|
4277 | * If we dup'd over a valid file, we now own the reference to it |
---|
4278 | * and must dispose of it using closef() semantics (as if a |
---|
4279 | * close() were performed on it). |
---|
4280 | * |
---|
4281 | * XXX this duplicates parts of close(). |
---|
4282 | */ |
---|
4283 | if (delfp != NULL) { |
---|
4284 | knote_fdclose(td, new); |
---|
4285 | if (delfp->f_type == DTYPE_MQUEUE) |
---|
4286 | mq_fdclose(td, new, delfp); |
---|
4287 | FILEDESC_XUNLOCK(fdp); |
---|
4288 | (void) closef(delfp, td); |
---|
4289 | if (holdleaders) { |
---|
4290 | FILEDESC_XLOCK(fdp); |
---|
4291 | fdp->fd_holdleaderscount--; |
---|
4292 | if (fdp->fd_holdleaderscount == 0 && |
---|
4293 | fdp->fd_holdleaderswakeup != 0) { |
---|
4294 | fdp->fd_holdleaderswakeup = 0; |
---|
4295 | wakeup(&fdp->fd_holdleaderscount); |
---|
4296 | } |
---|
4297 | FILEDESC_XUNLOCK(fdp); |
---|
4298 | } |
---|
4299 | } else { |
---|
4300 | FILEDESC_XUNLOCK(fdp); |
---|
4301 | } |
---|
4302 | return (0); |
---|
4303 | } |
---|
4304 | |
---|
4305 | /* |
---|
4306 | * If sigio is on the list associated with a process or process group, |
---|
4307 | * disable signalling from the device, remove sigio from the list and |
---|
4308 | * free sigio. |
---|
4309 | */ |
---|
4310 | void |
---|
4311 | funsetown(struct sigio **sigiop) |
---|
4312 | { |
---|
4313 | struct sigio *sigio; |
---|
4314 | |
---|
4315 | SIGIO_LOCK(); |
---|
4316 | sigio = *sigiop; |
---|
4317 | if (sigio == NULL) { |
---|
4318 | SIGIO_UNLOCK(); |
---|
4319 | return; |
---|
4320 | } |
---|
4321 | *(sigio->sio_myref) = NULL; |
---|
4322 | if ((sigio)->sio_pgid < 0) { |
---|
4323 | struct pgrp *pg = (sigio)->sio_pgrp; |
---|
4324 | PGRP_LOCK(pg); |
---|
4325 | SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio, |
---|
4326 | sigio, sio_pgsigio); |
---|
4327 | PGRP_UNLOCK(pg); |
---|
4328 | } else { |
---|
4329 | struct proc *p = (sigio)->sio_proc; |
---|
4330 | PROC_LOCK(p); |
---|
4331 | SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio, |
---|
4332 | sigio, sio_pgsigio); |
---|
4333 | PROC_UNLOCK(p); |
---|
4334 | } |
---|
4335 | SIGIO_UNLOCK(); |
---|
4336 | crfree(sigio->sio_ucred); |
---|
4337 | free(sigio, M_SIGIO); |
---|
4338 | } |
---|
4339 | |
---|
4340 | /* |
---|
4341 | * Free a list of sigio structures. |
---|
4342 | * We only need to lock the SIGIO_LOCK because we have made ourselves |
---|
4343 | * inaccessible to callers of fsetown and therefore do not need to lock |
---|
4344 | * the proc or pgrp struct for the list manipulation. |
---|
4345 | */ |
---|
4346 | void |
---|
4347 | funsetownlst(struct sigiolst *sigiolst) |
---|
4348 | { |
---|
4349 | struct proc *p; |
---|
4350 | struct pgrp *pg; |
---|
4351 | struct sigio *sigio; |
---|
4352 | |
---|
4353 | sigio = SLIST_FIRST(sigiolst); |
---|
4354 | if (sigio == NULL) |
---|
4355 | return; |
---|
4356 | p = NULL; |
---|
4357 | pg = NULL; |
---|
4358 | |
---|
4359 | /* |
---|
4360 | * Every entry of the list should belong |
---|
4361 | * to a single proc or pgrp. |
---|
4362 | */ |
---|
4363 | if (sigio->sio_pgid < 0) { |
---|
4364 | pg = sigio->sio_pgrp; |
---|
4365 | PGRP_LOCK_ASSERT(pg, MA_NOTOWNED); |
---|
4366 | } else /* if (sigio->sio_pgid > 0) */ { |
---|
4367 | p = sigio->sio_proc; |
---|
4368 | PROC_LOCK_ASSERT(p, MA_NOTOWNED); |
---|
4369 | } |
---|
4370 | |
---|
4371 | SIGIO_LOCK(); |
---|
4372 | while ((sigio = SLIST_FIRST(sigiolst)) != NULL) { |
---|
4373 | *(sigio->sio_myref) = NULL; |
---|
4374 | if (pg != NULL) { |
---|
4375 | KASSERT(sigio->sio_pgid < 0, |
---|
4376 | ("Proc sigio in pgrp sigio list")); |
---|
4377 | KASSERT(sigio->sio_pgrp == pg, |
---|
4378 | ("Bogus pgrp in sigio list")); |
---|
4379 | PGRP_LOCK(pg); |
---|
4380 | SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, |
---|
4381 | sio_pgsigio); |
---|
4382 | PGRP_UNLOCK(pg); |
---|
4383 | } else /* if (p != NULL) */ { |
---|
4384 | KASSERT(sigio->sio_pgid > 0, |
---|
4385 | ("Pgrp sigio in proc sigio list")); |
---|
4386 | KASSERT(sigio->sio_proc == p, |
---|
4387 | ("Bogus proc in sigio list")); |
---|
4388 | PROC_LOCK(p); |
---|
4389 | SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, |
---|
4390 | sio_pgsigio); |
---|
4391 | PROC_UNLOCK(p); |
---|
4392 | } |
---|
4393 | SIGIO_UNLOCK(); |
---|
4394 | crfree(sigio->sio_ucred); |
---|
4395 | free(sigio, M_SIGIO); |
---|
4396 | SIGIO_LOCK(); |
---|
4397 | } |
---|
4398 | SIGIO_UNLOCK(); |
---|
4399 | } |
---|
4400 | |
---|
4401 | /* |
---|
4402 | * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). |
---|
4403 | * |
---|
4404 | * After permission checking, add a sigio structure to the sigio list for |
---|
4405 | * the process or process group. |
---|
4406 | */ |
---|
4407 | int |
---|
4408 | fsetown(pid_t pgid, struct sigio **sigiop) |
---|
4409 | { |
---|
4410 | struct proc *proc; |
---|
4411 | struct pgrp *pgrp; |
---|
4412 | struct sigio *sigio; |
---|
4413 | int ret; |
---|
4414 | |
---|
4415 | if (pgid == 0) { |
---|
4416 | funsetown(sigiop); |
---|
4417 | return (0); |
---|
4418 | } |
---|
4419 | |
---|
4420 | ret = 0; |
---|
4421 | |
---|
4422 | /* Allocate and fill in the new sigio out of locks. */ |
---|
4423 | sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK); |
---|
4424 | sigio->sio_pgid = pgid; |
---|
4425 | sigio->sio_ucred = crhold(curthread->td_ucred); |
---|
4426 | sigio->sio_myref = sigiop; |
---|
4427 | |
---|
4428 | sx_slock(&proctree_lock); |
---|
4429 | if (pgid > 0) { |
---|
4430 | proc = pfind(pgid); |
---|
4431 | if (proc == NULL) { |
---|
4432 | ret = ESRCH; |
---|
4433 | goto fail; |
---|
4434 | } |
---|
4435 | |
---|
4436 | /* |
---|
4437 | * Policy - Don't allow a process to FSETOWN a process |
---|
4438 | * in another session. |
---|
4439 | * |
---|
4440 | * Remove this test to allow maximum flexibility or |
---|
4441 | * restrict FSETOWN to the current process or process |
---|
4442 | * group for maximum safety. |
---|
4443 | */ |
---|
4444 | PROC_UNLOCK(proc); |
---|
4445 | if (proc->p_session != curthread->td_proc->p_session) { |
---|
4446 | ret = EPERM; |
---|
4447 | goto fail; |
---|
4448 | } |
---|
4449 | |
---|
4450 | pgrp = NULL; |
---|
4451 | } else /* if (pgid < 0) */ { |
---|
4452 | pgrp = pgfind(-pgid); |
---|
4453 | if (pgrp == NULL) { |
---|
4454 | ret = ESRCH; |
---|
4455 | goto fail; |
---|
4456 | } |
---|
4457 | PGRP_UNLOCK(pgrp); |
---|
4458 | |
---|
4459 | /* |
---|
4460 | * Policy - Don't allow a process to FSETOWN a process |
---|
4461 | * in another session. |
---|
4462 | * |
---|
4463 | * Remove this test to allow maximum flexibility or |
---|
4464 | * restrict FSETOWN to the current process or process |
---|
4465 | * group for maximum safety. |
---|
4466 | */ |
---|
4467 | if (pgrp->pg_session != curthread->td_proc->p_session) { |
---|
4468 | ret = EPERM; |
---|
4469 | goto fail; |
---|
4470 | } |
---|
4471 | |
---|
4472 | proc = NULL; |
---|
4473 | } |
---|
4474 | funsetown(sigiop); |
---|
4475 | if (pgid > 0) { |
---|
4476 | PROC_LOCK(proc); |
---|
4477 | /* |
---|
4478 | * Since funsetownlst() is called without the proctree |
---|
4479 | * locked, we need to check for P_WEXIT. |
---|
4480 | * XXX: is ESRCH correct? |
---|
4481 | */ |
---|
4482 | if ((proc->p_flag & P_WEXIT) != 0) { |
---|
4483 | PROC_UNLOCK(proc); |
---|
4484 | ret = ESRCH; |
---|
4485 | goto fail; |
---|
4486 | } |
---|
4487 | SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio); |
---|
4488 | sigio->sio_proc = proc; |
---|
4489 | PROC_UNLOCK(proc); |
---|
4490 | } else { |
---|
4491 | PGRP_LOCK(pgrp); |
---|
4492 | SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); |
---|
4493 | sigio->sio_pgrp = pgrp; |
---|
4494 | PGRP_UNLOCK(pgrp); |
---|
4495 | } |
---|
4496 | sx_sunlock(&proctree_lock); |
---|
4497 | SIGIO_LOCK(); |
---|
4498 | *sigiop = sigio; |
---|
4499 | SIGIO_UNLOCK(); |
---|
4500 | return (0); |
---|
4501 | |
---|
4502 | fail: |
---|
4503 | sx_sunlock(&proctree_lock); |
---|
4504 | crfree(sigio->sio_ucred); |
---|
4505 | free(sigio, M_SIGIO); |
---|
4506 | return (ret); |
---|
4507 | } |
---|
4508 | |
---|
4509 | /* |
---|
4510 | * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). |
---|
4511 | */ |
---|
4512 | pid_t |
---|
4513 | fgetown(sigiop) |
---|
4514 | struct sigio **sigiop; |
---|
4515 | { |
---|
4516 | pid_t pgid; |
---|
4517 | |
---|
4518 | SIGIO_LOCK(); |
---|
4519 | pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0; |
---|
4520 | SIGIO_UNLOCK(); |
---|
4521 | return (pgid); |
---|
4522 | } |
---|
4523 | |
---|
4524 | /* |
---|
4525 | * Close a file descriptor. |
---|
4526 | */ |
---|
4527 | #ifndef _SYS_SYSPROTO_H_ |
---|
4528 | struct close_args { |
---|
4529 | int fd; |
---|
4530 | }; |
---|
4531 | #endif |
---|
4532 | /* ARGSUSED */ |
---|
4533 | int |
---|
4534 | close(td, uap) |
---|
4535 | struct thread *td; |
---|
4536 | struct close_args *uap; |
---|
4537 | { |
---|
4538 | |
---|
4539 | return (kern_close(td, uap->fd)); |
---|
4540 | } |
---|
4541 | |
---|
4542 | int |
---|
4543 | kern_close(td, fd) |
---|
4544 | struct thread *td; |
---|
4545 | int fd; |
---|
4546 | { |
---|
4547 | struct filedesc *fdp; |
---|
4548 | struct file *fp; |
---|
4549 | int error; |
---|
4550 | int holdleaders; |
---|
4551 | |
---|
4552 | error = 0; |
---|
4553 | holdleaders = 0; |
---|
4554 | fdp = td->td_proc->p_fd; |
---|
4555 | |
---|
4556 | AUDIT_SYSCLOSE(td, fd); |
---|
4557 | |
---|
4558 | FILEDESC_XLOCK(fdp); |
---|
4559 | if ((unsigned)fd >= fdp->fd_nfiles || |
---|
4560 | (fp = fdp->fd_ofiles[fd]) == NULL) { |
---|
4561 | FILEDESC_XUNLOCK(fdp); |
---|
4562 | return (EBADF); |
---|
4563 | } |
---|
4564 | fdp->fd_ofiles[fd] = NULL; |
---|
4565 | fdp->fd_ofileflags[fd] = 0; |
---|
4566 | fdunused(fdp, fd); |
---|
4567 | if (td->td_proc->p_fdtol != NULL) { |
---|
4568 | /* |
---|
4569 | * Ask fdfree() to sleep to ensure that all relevant |
---|
4570 | * process leaders can be traversed in closef(). |
---|
4571 | */ |
---|
4572 | fdp->fd_holdleaderscount++; |
---|
4573 | holdleaders = 1; |
---|
4574 | } |
---|
4575 | |
---|
4576 | /* |
---|
4577 | * We now hold the fp reference that used to be owned by the |
---|
4578 | * descriptor array. We have to unlock the FILEDESC *AFTER* |
---|
4579 | * knote_fdclose to prevent a race of the fd getting opened, a knote |
---|
4580 | * added, and deleteing a knote for the new fd. |
---|
4581 | */ |
---|
4582 | knote_fdclose(td, fd); |
---|
4583 | if (fp->f_type == DTYPE_MQUEUE) |
---|
4584 | mq_fdclose(td, fd, fp); |
---|
4585 | FILEDESC_XUNLOCK(fdp); |
---|
4586 | |
---|
4587 | error = closef(fp, td); |
---|
4588 | if (holdleaders) { |
---|
4589 | FILEDESC_XLOCK(fdp); |
---|
4590 | fdp->fd_holdleaderscount--; |
---|
4591 | if (fdp->fd_holdleaderscount == 0 && |
---|
4592 | fdp->fd_holdleaderswakeup != 0) { |
---|
4593 | fdp->fd_holdleaderswakeup = 0; |
---|
4594 | wakeup(&fdp->fd_holdleaderscount); |
---|
4595 | } |
---|
4596 | FILEDESC_XUNLOCK(fdp); |
---|
4597 | } |
---|
4598 | return (error); |
---|
4599 | } |
---|
4600 | |
---|
4601 | /* |
---|
4602 | * Close open file descriptors. |
---|
4603 | */ |
---|
4604 | #ifndef _SYS_SYSPROTO_H_ |
---|
4605 | struct closefrom_args { |
---|
4606 | int lowfd; |
---|
4607 | }; |
---|
4608 | #endif |
---|
4609 | /* ARGSUSED */ |
---|
4610 | int |
---|
4611 | closefrom(struct thread *td, struct closefrom_args *uap) |
---|
4612 | { |
---|
4613 | struct filedesc *fdp; |
---|
4614 | int fd; |
---|
4615 | |
---|
4616 | fdp = td->td_proc->p_fd; |
---|
4617 | AUDIT_ARG_FD(uap->lowfd); |
---|
4618 | |
---|
4619 | /* |
---|
4620 | * Treat negative starting file descriptor values identical to |
---|
4621 | * closefrom(0) which closes all files. |
---|
4622 | */ |
---|
4623 | if (uap->lowfd < 0) |
---|
4624 | uap->lowfd = 0; |
---|
4625 | FILEDESC_SLOCK(fdp); |
---|
4626 | for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) { |
---|
4627 | if (fdp->fd_ofiles[fd] != NULL) { |
---|
4628 | FILEDESC_SUNLOCK(fdp); |
---|
4629 | (void)kern_close(td, fd); |
---|
4630 | FILEDESC_SLOCK(fdp); |
---|
4631 | } |
---|
4632 | } |
---|
4633 | FILEDESC_SUNLOCK(fdp); |
---|
4634 | return (0); |
---|
4635 | } |
---|
4636 | |
---|
4637 | #if defined(COMPAT_43) |
---|
4638 | /* |
---|
4639 | * Return status information about a file descriptor. |
---|
4640 | */ |
---|
4641 | #ifndef _SYS_SYSPROTO_H_ |
---|
4642 | struct ofstat_args { |
---|
4643 | int fd; |
---|
4644 | struct ostat *sb; |
---|
4645 | }; |
---|
4646 | #endif |
---|
4647 | /* ARGSUSED */ |
---|
4648 | int |
---|
4649 | ofstat(struct thread *td, struct ofstat_args *uap) |
---|
4650 | { |
---|
4651 | struct ostat oub; |
---|
4652 | struct stat ub; |
---|
4653 | int error; |
---|
4654 | |
---|
4655 | error = kern_fstat(td, uap->fd, &ub); |
---|
4656 | if (error == 0) { |
---|
4657 | cvtstat(&ub, &oub); |
---|
4658 | error = copyout(&oub, uap->sb, sizeof(oub)); |
---|
4659 | } |
---|
4660 | return (error); |
---|
4661 | } |
---|
4662 | #endif /* COMPAT_43 */ |
---|
4663 | |
---|
4664 | /* |
---|
4665 | * Return status information about a file descriptor. |
---|
4666 | */ |
---|
4667 | #ifndef _SYS_SYSPROTO_H_ |
---|
4668 | struct fstat_args { |
---|
4669 | int fd; |
---|
4670 | struct stat *sb; |
---|
4671 | }; |
---|
4672 | #endif |
---|
4673 | /* ARGSUSED */ |
---|
4674 | int |
---|
4675 | fstat(struct thread *td, struct fstat_args *uap) |
---|
4676 | { |
---|
4677 | struct stat ub; |
---|
4678 | int error; |
---|
4679 | |
---|
4680 | error = kern_fstat(td, uap->fd, &ub); |
---|
4681 | if (error == 0) |
---|
4682 | error = copyout(&ub, uap->sb, sizeof(ub)); |
---|
4683 | return (error); |
---|
4684 | } |
---|
4685 | |
---|
4686 | int |
---|
4687 | kern_fstat(struct thread *td, int fd, struct stat *sbp) |
---|
4688 | { |
---|
4689 | struct file *fp; |
---|
4690 | int error; |
---|
4691 | |
---|
4692 | AUDIT_ARG_FD(fd); |
---|
4693 | |
---|
4694 | if ((error = fget(td, fd, &fp)) != 0) |
---|
4695 | return (error); |
---|
4696 | |
---|
4697 | AUDIT_ARG_FILE(td->td_proc, fp); |
---|
4698 | |
---|
4699 | error = fo_stat(fp, sbp, td->td_ucred, td); |
---|
4700 | fdrop(fp, td); |
---|
4701 | #ifdef KTRACE |
---|
4702 | if (error == 0 && KTRPOINT(td, KTR_STRUCT)) |
---|
4703 | ktrstat(sbp); |
---|
4704 | #endif |
---|
4705 | return (error); |
---|
4706 | } |
---|
4707 | |
---|
4708 | /* |
---|
4709 | * Return status information about a file descriptor. |
---|
4710 | */ |
---|
4711 | #ifndef _SYS_SYSPROTO_H_ |
---|
4712 | struct nfstat_args { |
---|
4713 | int fd; |
---|
4714 | struct nstat *sb; |
---|
4715 | }; |
---|
4716 | #endif |
---|
4717 | /* ARGSUSED */ |
---|
4718 | int |
---|
4719 | nfstat(struct thread *td, struct nfstat_args *uap) |
---|
4720 | { |
---|
4721 | struct nstat nub; |
---|
4722 | struct stat ub; |
---|
4723 | int error; |
---|
4724 | |
---|
4725 | error = kern_fstat(td, uap->fd, &ub); |
---|
4726 | if (error == 0) { |
---|
4727 | cvtnstat(&ub, &nub); |
---|
4728 | error = copyout(&nub, uap->sb, sizeof(nub)); |
---|
4729 | } |
---|
4730 | return (error); |
---|
4731 | } |
---|
4732 | |
---|
4733 | /* |
---|
4734 | * Return pathconf information about a file descriptor. |
---|
4735 | */ |
---|
4736 | #ifndef _SYS_SYSPROTO_H_ |
---|
4737 | struct fpathconf_args { |
---|
4738 | int fd; |
---|
4739 | int name; |
---|
4740 | }; |
---|
4741 | #endif |
---|
4742 | /* ARGSUSED */ |
---|
4743 | int |
---|
4744 | fpathconf(struct thread *td, struct fpathconf_args *uap) |
---|
4745 | { |
---|
4746 | struct file *fp; |
---|
4747 | struct vnode *vp; |
---|
4748 | int error; |
---|
4749 | |
---|
4750 | if ((error = fget(td, uap->fd, &fp)) != 0) |
---|
4751 | return (error); |
---|
4752 | |
---|
4753 | /* If asynchronous I/O is available, it works for all descriptors. */ |
---|
4754 | if (uap->name == _PC_ASYNC_IO) { |
---|
4755 | td->td_retval[0] = async_io_version; |
---|
4756 | goto out; |
---|
4757 | } |
---|
4758 | vp = fp->f_vnode; |
---|
4759 | if (vp != NULL) { |
---|
4760 | int vfslocked; |
---|
4761 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
4762 | vn_lock(vp, LK_SHARED | LK_RETRY); |
---|
4763 | error = VOP_PATHCONF(vp, uap->name, td->td_retval); |
---|
4764 | VOP_UNLOCK(vp, 0); |
---|
4765 | VFS_UNLOCK_GIANT(vfslocked); |
---|
4766 | } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { |
---|
4767 | if (uap->name != _PC_PIPE_BUF) { |
---|
4768 | error = EINVAL; |
---|
4769 | } else { |
---|
4770 | td->td_retval[0] = PIPE_BUF; |
---|
4771 | error = 0; |
---|
4772 | } |
---|
4773 | } else { |
---|
4774 | error = EOPNOTSUPP; |
---|
4775 | } |
---|
4776 | out: |
---|
4777 | fdrop(fp, td); |
---|
4778 | return (error); |
---|
4779 | } |
---|
4780 | |
---|
4781 | /* |
---|
4782 | * Grow the file table to accomodate (at least) nfd descriptors. This may |
---|
4783 | * block and drop the filedesc lock, but it will reacquire it before |
---|
4784 | * returning. |
---|
4785 | */ |
---|
4786 | static void |
---|
4787 | fdgrowtable(struct filedesc *fdp, int nfd) |
---|
4788 | { |
---|
4789 | struct filedesc0 *fdp0; |
---|
4790 | struct freetable *fo; |
---|
4791 | struct file **ntable; |
---|
4792 | struct file **otable; |
---|
4793 | char *nfileflags; |
---|
4794 | int nnfiles, onfiles; |
---|
4795 | NDSLOTTYPE *nmap; |
---|
4796 | |
---|
4797 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
4798 | |
---|
4799 | KASSERT(fdp->fd_nfiles > 0, |
---|
4800 | ("zero-length file table")); |
---|
4801 | |
---|
4802 | /* compute the size of the new table */ |
---|
4803 | onfiles = fdp->fd_nfiles; |
---|
4804 | nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */ |
---|
4805 | if (nnfiles <= onfiles) |
---|
4806 | /* the table is already large enough */ |
---|
4807 | return; |
---|
4808 | |
---|
4809 | /* allocate a new table and (if required) new bitmaps */ |
---|
4810 | FILEDESC_XUNLOCK(fdp); |
---|
4811 | ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable), |
---|
4812 | M_FILEDESC, M_ZERO | M_WAITOK); |
---|
4813 | nfileflags = (char *)&ntable[nnfiles]; |
---|
4814 | if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) |
---|
4815 | nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, |
---|
4816 | M_FILEDESC, M_ZERO | M_WAITOK); |
---|
4817 | else |
---|
4818 | nmap = NULL; |
---|
4819 | FILEDESC_XLOCK(fdp); |
---|
4820 | |
---|
4821 | /* |
---|
4822 | * We now have new tables ready to go. Since we dropped the |
---|
4823 | * filedesc lock to call malloc(), watch out for a race. |
---|
4824 | */ |
---|
4825 | onfiles = fdp->fd_nfiles; |
---|
4826 | if (onfiles >= nnfiles) { |
---|
4827 | /* we lost the race, but that's OK */ |
---|
4828 | free(ntable, M_FILEDESC); |
---|
4829 | if (nmap != NULL) |
---|
4830 | free(nmap, M_FILEDESC); |
---|
4831 | return; |
---|
4832 | } |
---|
4833 | bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable)); |
---|
4834 | bcopy(fdp->fd_ofileflags, nfileflags, onfiles); |
---|
4835 | otable = fdp->fd_ofiles; |
---|
4836 | fdp->fd_ofileflags = nfileflags; |
---|
4837 | fdp->fd_ofiles = ntable; |
---|
4838 | /* |
---|
4839 | * We must preserve ofiles until the process exits because we can't |
---|
4840 | * be certain that no threads have references to the old table via |
---|
4841 | * _fget(). |
---|
4842 | */ |
---|
4843 | if (onfiles > NDFILE) { |
---|
4844 | fo = (struct freetable *)&otable[onfiles]; |
---|
4845 | fdp0 = (struct filedesc0 *)fdp; |
---|
4846 | fo->ft_table = otable; |
---|
4847 | SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next); |
---|
4848 | } |
---|
4849 | if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { |
---|
4850 | bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap)); |
---|
4851 | if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) |
---|
4852 | free(fdp->fd_map, M_FILEDESC); |
---|
4853 | fdp->fd_map = nmap; |
---|
4854 | } |
---|
4855 | fdp->fd_nfiles = nnfiles; |
---|
4856 | } |
---|
4857 | |
---|
4858 | /* |
---|
4859 | * Allocate a file descriptor for the process. |
---|
4860 | */ |
---|
4861 | int |
---|
4862 | fdalloc(struct thread *td, int minfd, int *result) |
---|
4863 | { |
---|
4864 | struct proc *p = td->td_proc; |
---|
4865 | struct filedesc *fdp = p->p_fd; |
---|
4866 | int fd = -1, maxfd; |
---|
4867 | |
---|
4868 | FILEDESC_XLOCK_ASSERT(fdp); |
---|
4869 | |
---|
4870 | if (fdp->fd_freefile > minfd) |
---|
4871 | minfd = fdp->fd_freefile; |
---|
4872 | |
---|
4873 | PROC_LOCK(p); |
---|
4874 | maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
4875 | PROC_UNLOCK(p); |
---|
4876 | |
---|
4877 | /* |
---|
4878 | * Search the bitmap for a free descriptor. If none is found, try |
---|
4879 | * to grow the file table. Keep at it until we either get a file |
---|
4880 | * descriptor or run into process or system limits; fdgrowtable() |
---|
4881 | * may drop the filedesc lock, so we're in a race. |
---|
4882 | */ |
---|
4883 | for (;;) { |
---|
4884 | fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); |
---|
4885 | if (fd >= maxfd) |
---|
4886 | return (EMFILE); |
---|
4887 | if (fd < fdp->fd_nfiles) |
---|
4888 | break; |
---|
4889 | fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd)); |
---|
4890 | } |
---|
4891 | |
---|
4892 | /* |
---|
4893 | * Perform some sanity checks, then mark the file descriptor as |
---|
4894 | * used and return it to the caller. |
---|
4895 | */ |
---|
4896 | KASSERT(!fdisused(fdp, fd), |
---|
4897 | ("fd_first_free() returned non-free descriptor")); |
---|
4898 | KASSERT(fdp->fd_ofiles[fd] == NULL, |
---|
4899 | ("free descriptor isn't")); |
---|
4900 | fdp->fd_ofileflags[fd] = 0; /* XXX needed? */ |
---|
4901 | fdused(fdp, fd); |
---|
4902 | *result = fd; |
---|
4903 | return (0); |
---|
4904 | } |
---|
4905 | |
---|
4906 | /* |
---|
4907 | * Check to see whether n user file descriptors are available to the process |
---|
4908 | * p. |
---|
4909 | */ |
---|
4910 | int |
---|
4911 | fdavail(struct thread *td, int n) |
---|
4912 | { |
---|
4913 | struct proc *p = td->td_proc; |
---|
4914 | struct filedesc *fdp = td->td_proc->p_fd; |
---|
4915 | struct file **fpp; |
---|
4916 | int i, lim, last; |
---|
4917 | |
---|
4918 | FILEDESC_LOCK_ASSERT(fdp); |
---|
4919 | |
---|
4920 | PROC_LOCK(p); |
---|
4921 | lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); |
---|
4922 | PROC_UNLOCK(p); |
---|
4923 | if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) |
---|
4924 | return (1); |
---|
4925 | last = min(fdp->fd_nfiles, lim); |
---|
4926 | fpp = &fdp->fd_ofiles[fdp->fd_freefile]; |
---|
4927 | for (i = last - fdp->fd_freefile; --i >= 0; fpp++) { |
---|
4928 | if (*fpp == NULL && --n <= 0) |
---|
4929 | return (1); |
---|
4930 | } |
---|
4931 | return (0); |
---|
4932 | } |
---|
4933 | |
---|
4934 | /* |
---|
4935 | * Create a new open file structure and allocate a file decriptor for the |
---|
4936 | * process that refers to it. We add one reference to the file for the |
---|
4937 | * descriptor table and one reference for resultfp. This is to prevent us |
---|
4938 | * being preempted and the entry in the descriptor table closed after we |
---|
4939 | * release the FILEDESC lock. |
---|
4940 | */ |
---|
4941 | int |
---|
4942 | falloc(struct thread *td, struct file **resultfp, int *resultfd) |
---|
4943 | { |
---|
4944 | struct proc *p = td->td_proc; |
---|
4945 | struct file *fp; |
---|
4946 | int error, i; |
---|
4947 | int maxuserfiles = maxfiles - (maxfiles / 20); |
---|
4948 | static struct timeval lastfail; |
---|
4949 | static int curfail; |
---|
4950 | |
---|
4951 | fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); |
---|
4952 | if ((openfiles >= maxuserfiles && |
---|
4953 | priv_check(td, PRIV_MAXFILES) != 0) || |
---|
4954 | openfiles >= maxfiles) { |
---|
4955 | if (ppsratecheck(&lastfail, &curfail, 1)) { |
---|
4956 | printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", |
---|
4957 | td->td_ucred->cr_ruid); |
---|
4958 | } |
---|
4959 | uma_zfree(file_zone, fp); |
---|
4960 | return (ENFILE); |
---|
4961 | } |
---|
4962 | atomic_add_int(&openfiles, 1); |
---|
4963 | |
---|
4964 | /* |
---|
4965 | * If the process has file descriptor zero open, add the new file |
---|
4966 | * descriptor to the list of open files at that point, otherwise |
---|
4967 | * put it at the front of the list of open files. |
---|
4968 | */ |
---|
4969 | refcount_init(&fp->f_count, 1); |
---|
4970 | if (resultfp) |
---|
4971 | fhold(fp); |
---|
4972 | fp->f_cred = crhold(td->td_ucred); |
---|
4973 | fp->f_ops = &badfileops; |
---|
4974 | fp->f_data = NULL; |
---|
4975 | fp->f_vnode = NULL; |
---|
4976 | FILEDESC_XLOCK(p->p_fd); |
---|
4977 | if ((error = fdalloc(td, 0, &i))) { |
---|
4978 | FILEDESC_XUNLOCK(p->p_fd); |
---|
4979 | |
---|
4980 | fdrop(fp, td); |
---|
4981 | if (resultfp) |
---|
4982 | fdrop(fp, td); |
---|
4983 | return (error); |
---|
4984 | } |
---|
4985 | p->p_fd->fd_ofiles[i] = fp; |
---|
4986 | FILEDESC_XUNLOCK(p->p_fd); |
---|
4987 | if (resultfp) |
---|
4988 | *resultfp = fp; |
---|
4989 | if (resultfd) |
---|
4990 | *resultfd = i; |
---|
4991 | return (0); |
---|
4992 | } |
---|
4993 | |
---|
4994 | /* |
---|
4995 | * Build a new filedesc structure from another. |
---|
4996 | * Copy the current, root, and jail root vnode references. |
---|
4997 | */ |
---|
4998 | struct filedesc * |
---|
4999 | fdinit(struct filedesc *fdp) |
---|
5000 | { |
---|
5001 | struct filedesc0 *newfdp; |
---|
5002 | |
---|
5003 | newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO); |
---|
5004 | FILEDESC_LOCK_INIT(&newfdp->fd_fd); |
---|
5005 | if (fdp != NULL) { |
---|
5006 | FILEDESC_XLOCK(fdp); |
---|
5007 | newfdp->fd_fd.fd_cdir = fdp->fd_cdir; |
---|
5008 | if (newfdp->fd_fd.fd_cdir) |
---|
5009 | VREF(newfdp->fd_fd.fd_cdir); |
---|
5010 | newfdp->fd_fd.fd_rdir = fdp->fd_rdir; |
---|
5011 | if (newfdp->fd_fd.fd_rdir) |
---|
5012 | VREF(newfdp->fd_fd.fd_rdir); |
---|
5013 | newfdp->fd_fd.fd_jdir = fdp->fd_jdir; |
---|
5014 | if (newfdp->fd_fd.fd_jdir) |
---|
5015 | VREF(newfdp->fd_fd.fd_jdir); |
---|
5016 | FILEDESC_XUNLOCK(fdp); |
---|
5017 | } |
---|
5018 | |
---|
5019 | /* Create the file descriptor table. */ |
---|
5020 | newfdp->fd_fd.fd_refcnt = 1; |
---|
5021 | newfdp->fd_fd.fd_holdcnt = 1; |
---|
5022 | newfdp->fd_fd.fd_cmask = CMASK; |
---|
5023 | newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles; |
---|
5024 | newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags; |
---|
5025 | newfdp->fd_fd.fd_nfiles = NDFILE; |
---|
5026 | newfdp->fd_fd.fd_map = newfdp->fd_dmap; |
---|
5027 | newfdp->fd_fd.fd_lastfile = -1; |
---|
5028 | return (&newfdp->fd_fd); |
---|
5029 | } |
---|
5030 | |
---|
5031 | static struct filedesc * |
---|
5032 | fdhold(struct proc *p) |
---|
5033 | { |
---|
5034 | struct filedesc *fdp; |
---|
5035 | |
---|
5036 | mtx_lock(&fdesc_mtx); |
---|
5037 | fdp = p->p_fd; |
---|
5038 | if (fdp != NULL) |
---|
5039 | fdp->fd_holdcnt++; |
---|
5040 | mtx_unlock(&fdesc_mtx); |
---|
5041 | return (fdp); |
---|
5042 | } |
---|
5043 | |
---|
5044 | static void |
---|
5045 | fddrop(struct filedesc *fdp) |
---|
5046 | { |
---|
5047 | struct filedesc0 *fdp0; |
---|
5048 | struct freetable *ft; |
---|
5049 | int i; |
---|
5050 | |
---|
5051 | mtx_lock(&fdesc_mtx); |
---|
5052 | i = --fdp->fd_holdcnt; |
---|
5053 | mtx_unlock(&fdesc_mtx); |
---|
5054 | if (i > 0) |
---|
5055 | return; |
---|
5056 | |
---|
5057 | FILEDESC_LOCK_DESTROY(fdp); |
---|
5058 | fdp0 = (struct filedesc0 *)fdp; |
---|
5059 | while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) { |
---|
5060 | SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next); |
---|
5061 | free(ft->ft_table, M_FILEDESC); |
---|
5062 | } |
---|
5063 | free(fdp, M_FILEDESC); |
---|
5064 | } |
---|
5065 | |
---|
5066 | /* |
---|
5067 | * Share a filedesc structure. |
---|
5068 | */ |
---|
5069 | struct filedesc * |
---|
5070 | fdshare(struct filedesc *fdp) |
---|
5071 | { |
---|
5072 | |
---|
5073 | FILEDESC_XLOCK(fdp); |
---|
5074 | fdp->fd_refcnt++; |
---|
5075 | FILEDESC_XUNLOCK(fdp); |
---|
5076 | return (fdp); |
---|
5077 | } |
---|
5078 | |
---|
5079 | /* |
---|
5080 | * Unshare a filedesc structure, if necessary by making a copy |
---|
5081 | */ |
---|
5082 | void |
---|
5083 | fdunshare(struct proc *p, struct thread *td) |
---|
5084 | { |
---|
5085 | |
---|
5086 | FILEDESC_XLOCK(p->p_fd); |
---|
5087 | if (p->p_fd->fd_refcnt > 1) { |
---|
5088 | struct filedesc *tmp; |
---|
5089 | |
---|
5090 | FILEDESC_XUNLOCK(p->p_fd); |
---|
5091 | tmp = fdcopy(p->p_fd); |
---|
5092 | fdfree(td); |
---|
5093 | p->p_fd = tmp; |
---|
5094 | } else |
---|
5095 | FILEDESC_XUNLOCK(p->p_fd); |
---|
5096 | } |
---|
5097 | |
---|
5098 | /* |
---|
5099 | * Copy a filedesc structure. A NULL pointer in returns a NULL reference, |
---|
5100 | * this is to ease callers, not catch errors. |
---|
5101 | */ |
---|
5102 | struct filedesc * |
---|
5103 | fdcopy(struct filedesc *fdp) |
---|
5104 | { |
---|
5105 | struct filedesc *newfdp; |
---|
5106 | int i; |
---|
5107 | |
---|
5108 | /* Certain daemons might not have file descriptors. */ |
---|
5109 | if (fdp == NULL) |
---|
5110 | return (NULL); |
---|
5111 | |
---|
5112 | newfdp = fdinit(fdp); |
---|
5113 | FILEDESC_SLOCK(fdp); |
---|
5114 | while (fdp->fd_lastfile >= newfdp->fd_nfiles) { |
---|
5115 | FILEDESC_SUNLOCK(fdp); |
---|
5116 | FILEDESC_XLOCK(newfdp); |
---|
5117 | fdgrowtable(newfdp, fdp->fd_lastfile + 1); |
---|
5118 | FILEDESC_XUNLOCK(newfdp); |
---|
5119 | FILEDESC_SLOCK(fdp); |
---|
5120 | } |
---|
5121 | /* copy everything except kqueue descriptors */ |
---|
5122 | newfdp->fd_freefile = -1; |
---|
5123 | for (i = 0; i <= fdp->fd_lastfile; ++i) { |
---|
5124 | if (fdisused(fdp, i) && |
---|
5125 | fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE && |
---|
5126 | fdp->fd_ofiles[i]->f_ops != &badfileops) { |
---|
5127 | newfdp->fd_ofiles[i] = fdp->fd_ofiles[i]; |
---|
5128 | newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i]; |
---|
5129 | fhold(newfdp->fd_ofiles[i]); |
---|
5130 | newfdp->fd_lastfile = i; |
---|
5131 | } else { |
---|
5132 | if (newfdp->fd_freefile == -1) |
---|
5133 | newfdp->fd_freefile = i; |
---|
5134 | } |
---|
5135 | } |
---|
5136 | newfdp->fd_cmask = fdp->fd_cmask; |
---|
5137 | FILEDESC_SUNLOCK(fdp); |
---|
5138 | FILEDESC_XLOCK(newfdp); |
---|
5139 | for (i = 0; i <= newfdp->fd_lastfile; ++i) |
---|
5140 | if (newfdp->fd_ofiles[i] != NULL) |
---|
5141 | fdused(newfdp, i); |
---|
5142 | if (newfdp->fd_freefile == -1) |
---|
5143 | newfdp->fd_freefile = i; |
---|
5144 | FILEDESC_XUNLOCK(newfdp); |
---|
5145 | return (newfdp); |
---|
5146 | } |
---|
5147 | |
---|
5148 | /* |
---|
5149 | * Release a filedesc structure. |
---|
5150 | */ |
---|
5151 | void |
---|
5152 | fdfree(struct thread *td) |
---|
5153 | { |
---|
5154 | struct filedesc *fdp; |
---|
5155 | struct file **fpp; |
---|
5156 | int i, locked; |
---|
5157 | struct filedesc_to_leader *fdtol; |
---|
5158 | struct file *fp; |
---|
5159 | struct vnode *cdir, *jdir, *rdir, *vp; |
---|
5160 | struct flock lf; |
---|
5161 | |
---|
5162 | /* Certain daemons might not have file descriptors. */ |
---|
5163 | fdp = td->td_proc->p_fd; |
---|
5164 | if (fdp == NULL) |
---|
5165 | return; |
---|
5166 | |
---|
5167 | /* Check for special need to clear POSIX style locks */ |
---|
5168 | fdtol = td->td_proc->p_fdtol; |
---|
5169 | if (fdtol != NULL) { |
---|
5170 | FILEDESC_XLOCK(fdp); |
---|
5171 | KASSERT(fdtol->fdl_refcount > 0, |
---|
5172 | ("filedesc_to_refcount botch: fdl_refcount=%d", |
---|
5173 | fdtol->fdl_refcount)); |
---|
5174 | if (fdtol->fdl_refcount == 1 && |
---|
5175 | (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
5176 | for (i = 0, fpp = fdp->fd_ofiles; |
---|
5177 | i <= fdp->fd_lastfile; |
---|
5178 | i++, fpp++) { |
---|
5179 | if (*fpp == NULL || |
---|
5180 | (*fpp)->f_type != DTYPE_VNODE) |
---|
5181 | continue; |
---|
5182 | fp = *fpp; |
---|
5183 | fhold(fp); |
---|
5184 | FILEDESC_XUNLOCK(fdp); |
---|
5185 | lf.l_whence = SEEK_SET; |
---|
5186 | lf.l_start = 0; |
---|
5187 | lf.l_len = 0; |
---|
5188 | lf.l_type = F_UNLCK; |
---|
5189 | vp = fp->f_vnode; |
---|
5190 | locked = VFS_LOCK_GIANT(vp->v_mount); |
---|
5191 | (void) VOP_ADVLOCK(vp, |
---|
5192 | (caddr_t)td->td_proc-> |
---|
5193 | p_leader, |
---|
5194 | F_UNLCK, |
---|
5195 | &lf, |
---|
5196 | F_POSIX); |
---|
5197 | VFS_UNLOCK_GIANT(locked); |
---|
5198 | FILEDESC_XLOCK(fdp); |
---|
5199 | fdrop(fp, td); |
---|
5200 | fpp = fdp->fd_ofiles + i; |
---|
5201 | } |
---|
5202 | } |
---|
5203 | retry: |
---|
5204 | if (fdtol->fdl_refcount == 1) { |
---|
5205 | if (fdp->fd_holdleaderscount > 0 && |
---|
5206 | (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
5207 | /* |
---|
5208 | * close() or do_dup() has cleared a reference |
---|
5209 | * in a shared file descriptor table. |
---|
5210 | */ |
---|
5211 | fdp->fd_holdleaderswakeup = 1; |
---|
5212 | sx_sleep(&fdp->fd_holdleaderscount, |
---|
5213 | FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); |
---|
5214 | goto retry; |
---|
5215 | } |
---|
5216 | if (fdtol->fdl_holdcount > 0) { |
---|
5217 | /* |
---|
5218 | * Ensure that fdtol->fdl_leader remains |
---|
5219 | * valid in closef(). |
---|
5220 | */ |
---|
5221 | fdtol->fdl_wakeup = 1; |
---|
5222 | sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK, |
---|
5223 | "fdlhold", 0); |
---|
5224 | goto retry; |
---|
5225 | } |
---|
5226 | } |
---|
5227 | fdtol->fdl_refcount--; |
---|
5228 | if (fdtol->fdl_refcount == 0 && |
---|
5229 | fdtol->fdl_holdcount == 0) { |
---|
5230 | fdtol->fdl_next->fdl_prev = fdtol->fdl_prev; |
---|
5231 | fdtol->fdl_prev->fdl_next = fdtol->fdl_next; |
---|
5232 | } else |
---|
5233 | fdtol = NULL; |
---|
5234 | td->td_proc->p_fdtol = NULL; |
---|
5235 | FILEDESC_XUNLOCK(fdp); |
---|
5236 | if (fdtol != NULL) |
---|
5237 | free(fdtol, M_FILEDESC_TO_LEADER); |
---|
5238 | } |
---|
5239 | FILEDESC_XLOCK(fdp); |
---|
5240 | i = --fdp->fd_refcnt; |
---|
5241 | FILEDESC_XUNLOCK(fdp); |
---|
5242 | if (i > 0) |
---|
5243 | return; |
---|
5244 | |
---|
5245 | fpp = fdp->fd_ofiles; |
---|
5246 | for (i = fdp->fd_lastfile; i-- >= 0; fpp++) { |
---|
5247 | if (*fpp) { |
---|
5248 | FILEDESC_XLOCK(fdp); |
---|
5249 | fp = *fpp; |
---|
5250 | *fpp = NULL; |
---|
5251 | FILEDESC_XUNLOCK(fdp); |
---|
5252 | (void) closef(fp, td); |
---|
5253 | } |
---|
5254 | } |
---|
5255 | FILEDESC_XLOCK(fdp); |
---|
5256 | |
---|
5257 | /* XXX This should happen earlier. */ |
---|
5258 | mtx_lock(&fdesc_mtx); |
---|
5259 | td->td_proc->p_fd = NULL; |
---|
5260 | mtx_unlock(&fdesc_mtx); |
---|
5261 | |
---|
5262 | if (fdp->fd_nfiles > NDFILE) |
---|
5263 | free(fdp->fd_ofiles, M_FILEDESC); |
---|
5264 | if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) |
---|
5265 | free(fdp->fd_map, M_FILEDESC); |
---|
5266 | |
---|
5267 | fdp->fd_nfiles = 0; |
---|
5268 | |
---|
5269 | cdir = fdp->fd_cdir; |
---|
5270 | fdp->fd_cdir = NULL; |
---|
5271 | rdir = fdp->fd_rdir; |
---|
5272 | fdp->fd_rdir = NULL; |
---|
5273 | jdir = fdp->fd_jdir; |
---|
5274 | fdp->fd_jdir = NULL; |
---|
5275 | FILEDESC_XUNLOCK(fdp); |
---|
5276 | |
---|
5277 | if (cdir) { |
---|
5278 | locked = VFS_LOCK_GIANT(cdir->v_mount); |
---|
5279 | vrele(cdir); |
---|
5280 | VFS_UNLOCK_GIANT(locked); |
---|
5281 | } |
---|
5282 | if (rdir) { |
---|
5283 | locked = VFS_LOCK_GIANT(rdir->v_mount); |
---|
5284 | vrele(rdir); |
---|
5285 | VFS_UNLOCK_GIANT(locked); |
---|
5286 | } |
---|
5287 | if (jdir) { |
---|
5288 | locked = VFS_LOCK_GIANT(jdir->v_mount); |
---|
5289 | vrele(jdir); |
---|
5290 | VFS_UNLOCK_GIANT(locked); |
---|
5291 | } |
---|
5292 | |
---|
5293 | fddrop(fdp); |
---|
5294 | } |
---|
5295 | |
---|
5296 | /* |
---|
5297 | * For setugid programs, we don't want to people to use that setugidness |
---|
5298 | * to generate error messages which write to a file which otherwise would |
---|
5299 | * otherwise be off-limits to the process. We check for filesystems where |
---|
5300 | * the vnode can change out from under us after execve (like [lin]procfs). |
---|
5301 | * |
---|
5302 | * Since setugidsafety calls this only for fd 0, 1 and 2, this check is |
---|
5303 | * sufficient. We also don't check for setugidness since we know we are. |
---|
5304 | */ |
---|
5305 | static int |
---|
5306 | is_unsafe(struct file *fp) |
---|
5307 | { |
---|
5308 | if (fp->f_type == DTYPE_VNODE) { |
---|
5309 | struct vnode *vp = fp->f_vnode; |
---|
5310 | |
---|
5311 | if ((vp->v_vflag & VV_PROCDEP) != 0) |
---|
5312 | return (1); |
---|
5313 | } |
---|
5314 | return (0); |
---|
5315 | } |
---|
5316 | |
---|
5317 | /* |
---|
5318 | * Make this setguid thing safe, if at all possible. |
---|
5319 | */ |
---|
5320 | void |
---|
5321 | setugidsafety(struct thread *td) |
---|
5322 | { |
---|
5323 | struct filedesc *fdp; |
---|
5324 | int i; |
---|
5325 | |
---|
5326 | /* Certain daemons might not have file descriptors. */ |
---|
5327 | fdp = td->td_proc->p_fd; |
---|
5328 | if (fdp == NULL) |
---|
5329 | return; |
---|
5330 | |
---|
5331 | /* |
---|
5332 | * Note: fdp->fd_ofiles may be reallocated out from under us while |
---|
5333 | * we are blocked in a close. Be careful! |
---|
5334 | */ |
---|
5335 | FILEDESC_XLOCK(fdp); |
---|
5336 | for (i = 0; i <= fdp->fd_lastfile; i++) { |
---|
5337 | if (i > 2) |
---|
5338 | break; |
---|
5339 | if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) { |
---|
5340 | struct file *fp; |
---|
5341 | |
---|
5342 | knote_fdclose(td, i); |
---|
5343 | /* |
---|
5344 | * NULL-out descriptor prior to close to avoid |
---|
5345 | * a race while close blocks. |
---|
5346 | */ |
---|
5347 | fp = fdp->fd_ofiles[i]; |
---|
5348 | fdp->fd_ofiles[i] = NULL; |
---|
5349 | fdp->fd_ofileflags[i] = 0; |
---|
5350 | fdunused(fdp, i); |
---|
5351 | FILEDESC_XUNLOCK(fdp); |
---|
5352 | (void) closef(fp, td); |
---|
5353 | FILEDESC_XLOCK(fdp); |
---|
5354 | } |
---|
5355 | } |
---|
5356 | FILEDESC_XUNLOCK(fdp); |
---|
5357 | } |
---|
5358 | |
---|
5359 | /* |
---|
5360 | * If a specific file object occupies a specific file descriptor, close the |
---|
5361 | * file descriptor entry and drop a reference on the file object. This is a |
---|
5362 | * convenience function to handle a subsequent error in a function that calls |
---|
5363 | * falloc() that handles the race that another thread might have closed the |
---|
5364 | * file descriptor out from under the thread creating the file object. |
---|
5365 | */ |
---|
5366 | void |
---|
5367 | fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td) |
---|
5368 | { |
---|
5369 | |
---|
5370 | FILEDESC_XLOCK(fdp); |
---|
5371 | if (fdp->fd_ofiles[idx] == fp) { |
---|
5372 | fdp->fd_ofiles[idx] = NULL; |
---|
5373 | fdunused(fdp, idx); |
---|
5374 | FILEDESC_XUNLOCK(fdp); |
---|
5375 | fdrop(fp, td); |
---|
5376 | } else |
---|
5377 | FILEDESC_XUNLOCK(fdp); |
---|
5378 | } |
---|
5379 | |
---|
5380 | /* |
---|
5381 | * Close any files on exec? |
---|
5382 | */ |
---|
5383 | void |
---|
5384 | fdcloseexec(struct thread *td) |
---|
5385 | { |
---|
5386 | struct filedesc *fdp; |
---|
5387 | int i; |
---|
5388 | |
---|
5389 | /* Certain daemons might not have file descriptors. */ |
---|
5390 | fdp = td->td_proc->p_fd; |
---|
5391 | if (fdp == NULL) |
---|
5392 | return; |
---|
5393 | |
---|
5394 | FILEDESC_XLOCK(fdp); |
---|
5395 | |
---|
5396 | /* |
---|
5397 | * We cannot cache fd_ofiles or fd_ofileflags since operations |
---|
5398 | * may block and rip them out from under us. |
---|
5399 | */ |
---|
5400 | for (i = 0; i <= fdp->fd_lastfile; i++) { |
---|
5401 | if (fdp->fd_ofiles[i] != NULL && |
---|
5402 | (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE || |
---|
5403 | (fdp->fd_ofileflags[i] & UF_EXCLOSE))) { |
---|
5404 | struct file *fp; |
---|
5405 | |
---|
5406 | knote_fdclose(td, i); |
---|
5407 | /* |
---|
5408 | * NULL-out descriptor prior to close to avoid |
---|
5409 | * a race while close blocks. |
---|
5410 | */ |
---|
5411 | fp = fdp->fd_ofiles[i]; |
---|
5412 | fdp->fd_ofiles[i] = NULL; |
---|
5413 | fdp->fd_ofileflags[i] = 0; |
---|
5414 | fdunused(fdp, i); |
---|
5415 | if (fp->f_type == DTYPE_MQUEUE) |
---|
5416 | mq_fdclose(td, i, fp); |
---|
5417 | FILEDESC_XUNLOCK(fdp); |
---|
5418 | (void) closef(fp, td); |
---|
5419 | FILEDESC_XLOCK(fdp); |
---|
5420 | } |
---|
5421 | } |
---|
5422 | FILEDESC_XUNLOCK(fdp); |
---|
5423 | } |
---|
5424 | |
---|
5425 | /* |
---|
5426 | * It is unsafe for set[ug]id processes to be started with file |
---|
5427 | * descriptors 0..2 closed, as these descriptors are given implicit |
---|
5428 | * significance in the Standard C library. fdcheckstd() will create a |
---|
5429 | * descriptor referencing /dev/null for each of stdin, stdout, and |
---|
5430 | * stderr that is not already open. |
---|
5431 | */ |
---|
5432 | int |
---|
5433 | fdcheckstd(struct thread *td) |
---|
5434 | { |
---|
5435 | struct filedesc *fdp; |
---|
5436 | register_t retval, save; |
---|
5437 | int i, error, devnull; |
---|
5438 | |
---|
5439 | fdp = td->td_proc->p_fd; |
---|
5440 | if (fdp == NULL) |
---|
5441 | return (0); |
---|
5442 | KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared")); |
---|
5443 | devnull = -1; |
---|
5444 | error = 0; |
---|
5445 | for (i = 0; i < 3; i++) { |
---|
5446 | if (fdp->fd_ofiles[i] != NULL) |
---|
5447 | continue; |
---|
5448 | if (devnull < 0) { |
---|
5449 | save = td->td_retval[0]; |
---|
5450 | error = kern_open(td, "/dev/null", UIO_SYSSPACE, |
---|
5451 | O_RDWR, 0); |
---|
5452 | devnull = td->td_retval[0]; |
---|
5453 | KASSERT(devnull == i, ("oof, we didn't get our fd")); |
---|
5454 | td->td_retval[0] = save; |
---|
5455 | if (error) |
---|
5456 | break; |
---|
5457 | } else { |
---|
5458 | error = do_dup(td, DUP_FIXED, devnull, i, &retval); |
---|
5459 | if (error != 0) |
---|
5460 | break; |
---|
5461 | } |
---|
5462 | } |
---|
5463 | return (error); |
---|
5464 | } |
---|
5465 | |
---|
5466 | /* |
---|
5467 | * Internal form of close. Decrement reference count on file structure. |
---|
5468 | * Note: td may be NULL when closing a file that was being passed in a |
---|
5469 | * message. |
---|
5470 | * |
---|
5471 | * XXXRW: Giant is not required for the caller, but often will be held; this |
---|
5472 | * makes it moderately likely the Giant will be recursed in the VFS case. |
---|
5473 | */ |
---|
5474 | int |
---|
5475 | closef(struct file *fp, struct thread *td) |
---|
5476 | { |
---|
5477 | struct vnode *vp; |
---|
5478 | struct flock lf; |
---|
5479 | struct filedesc_to_leader *fdtol; |
---|
5480 | struct filedesc *fdp; |
---|
5481 | |
---|
5482 | /* |
---|
5483 | * POSIX record locking dictates that any close releases ALL |
---|
5484 | * locks owned by this process. This is handled by setting |
---|
5485 | * a flag in the unlock to free ONLY locks obeying POSIX |
---|
5486 | * semantics, and not to free BSD-style file locks. |
---|
5487 | * If the descriptor was in a message, POSIX-style locks |
---|
5488 | * aren't passed with the descriptor, and the thread pointer |
---|
5489 | * will be NULL. Callers should be careful only to pass a |
---|
5490 | * NULL thread pointer when there really is no owning |
---|
5491 | * context that might have locks, or the locks will be |
---|
5492 | * leaked. |
---|
5493 | */ |
---|
5494 | if (fp->f_type == DTYPE_VNODE && td != NULL) { |
---|
5495 | int vfslocked; |
---|
5496 | |
---|
5497 | vp = fp->f_vnode; |
---|
5498 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
5499 | if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { |
---|
5500 | lf.l_whence = SEEK_SET; |
---|
5501 | lf.l_start = 0; |
---|
5502 | lf.l_len = 0; |
---|
5503 | lf.l_type = F_UNLCK; |
---|
5504 | (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, |
---|
5505 | F_UNLCK, &lf, F_POSIX); |
---|
5506 | } |
---|
5507 | fdtol = td->td_proc->p_fdtol; |
---|
5508 | if (fdtol != NULL) { |
---|
5509 | /* |
---|
5510 | * Handle special case where file descriptor table is |
---|
5511 | * shared between multiple process leaders. |
---|
5512 | */ |
---|
5513 | fdp = td->td_proc->p_fd; |
---|
5514 | FILEDESC_XLOCK(fdp); |
---|
5515 | for (fdtol = fdtol->fdl_next; |
---|
5516 | fdtol != td->td_proc->p_fdtol; |
---|
5517 | fdtol = fdtol->fdl_next) { |
---|
5518 | if ((fdtol->fdl_leader->p_flag & |
---|
5519 | P_ADVLOCK) == 0) |
---|
5520 | continue; |
---|
5521 | fdtol->fdl_holdcount++; |
---|
5522 | FILEDESC_XUNLOCK(fdp); |
---|
5523 | lf.l_whence = SEEK_SET; |
---|
5524 | lf.l_start = 0; |
---|
5525 | lf.l_len = 0; |
---|
5526 | lf.l_type = F_UNLCK; |
---|
5527 | vp = fp->f_vnode; |
---|
5528 | (void) VOP_ADVLOCK(vp, |
---|
5529 | (caddr_t)fdtol->fdl_leader, |
---|
5530 | F_UNLCK, &lf, F_POSIX); |
---|
5531 | FILEDESC_XLOCK(fdp); |
---|
5532 | fdtol->fdl_holdcount--; |
---|
5533 | if (fdtol->fdl_holdcount == 0 && |
---|
5534 | fdtol->fdl_wakeup != 0) { |
---|
5535 | fdtol->fdl_wakeup = 0; |
---|
5536 | wakeup(fdtol); |
---|
5537 | } |
---|
5538 | } |
---|
5539 | FILEDESC_XUNLOCK(fdp); |
---|
5540 | } |
---|
5541 | VFS_UNLOCK_GIANT(vfslocked); |
---|
5542 | } |
---|
5543 | return (fdrop(fp, td)); |
---|
5544 | } |
---|
5545 | |
---|
5546 | /* |
---|
5547 | * Initialize the file pointer with the specified properties. |
---|
5548 | * |
---|
5549 | * The ops are set with release semantics to be certain that the flags, type, |
---|
5550 | * and data are visible when ops is. This is to prevent ops methods from being |
---|
5551 | * called with bad data. |
---|
5552 | */ |
---|
5553 | void |
---|
5554 | finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops) |
---|
5555 | { |
---|
5556 | fp->f_data = data; |
---|
5557 | fp->f_flag = flag; |
---|
5558 | fp->f_type = type; |
---|
5559 | atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); |
---|
5560 | } |
---|
5561 | #endif /* __rtems__ */ |
---|
5562 | |
---|
5563 | struct file * |
---|
5564 | fget_unlocked(struct filedesc *fdp, int fd) |
---|
5565 | { |
---|
5566 | struct file *fp; |
---|
5567 | u_int count; |
---|
5568 | |
---|
5569 | if (fd < 0 || fd >= fdp->fd_nfiles) |
---|
5570 | return (NULL); |
---|
5571 | /* |
---|
5572 | * Fetch the descriptor locklessly. We avoid fdrop() races by |
---|
5573 | * never raising a refcount above 0. To accomplish this we have |
---|
5574 | * to use a cmpset loop rather than an atomic_add. The descriptor |
---|
5575 | * must be re-verified once we acquire a reference to be certain |
---|
5576 | * that the identity is still correct and we did not lose a race |
---|
5577 | * due to preemption. |
---|
5578 | */ |
---|
5579 | for (;;) { |
---|
5580 | fp = fdp->fd_ofiles[fd]; |
---|
5581 | if (fp == NULL) |
---|
5582 | break; |
---|
5583 | count = fp->f_count; |
---|
5584 | if (count == 0) |
---|
5585 | continue; |
---|
5586 | /* |
---|
5587 | * Use an acquire barrier to prevent caching of fd_ofiles |
---|
5588 | * so it is refreshed for verification. |
---|
5589 | */ |
---|
5590 | if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1) |
---|
5591 | continue; |
---|
5592 | if (fp == fdp->fd_ofiles[fd]) |
---|
5593 | break; |
---|
5594 | fdrop(fp, curthread); |
---|
5595 | } |
---|
5596 | |
---|
5597 | return (fp); |
---|
5598 | } |
---|
5599 | |
---|
5600 | /* |
---|
5601 | * Extract the file pointer associated with the specified descriptor for the |
---|
5602 | * current user process. |
---|
5603 | * |
---|
5604 | * If the descriptor doesn't exist or doesn't match 'flags', EBADF is |
---|
5605 | * returned. |
---|
5606 | * |
---|
5607 | * If an error occured the non-zero error is returned and *fpp is set to |
---|
5608 | * NULL. Otherwise *fpp is held and set and zero is returned. Caller is |
---|
5609 | * responsible for fdrop(). |
---|
5610 | */ |
---|
5611 | static __inline int |
---|
5612 | _fget(struct thread *td, int fd, struct file **fpp, int flags) |
---|
5613 | { |
---|
5614 | struct filedesc *fdp; |
---|
5615 | struct file *fp; |
---|
5616 | |
---|
5617 | *fpp = NULL; |
---|
5618 | if (td == NULL || (fdp = td->td_proc->p_fd) == NULL) |
---|
5619 | return (EBADF); |
---|
5620 | if ((fp = fget_unlocked(fdp, fd)) == NULL) |
---|
5621 | return (EBADF); |
---|
5622 | if (fp->f_ops == &badfileops) { |
---|
5623 | fdrop(fp, td); |
---|
5624 | return (EBADF); |
---|
5625 | } |
---|
5626 | /* |
---|
5627 | * FREAD and FWRITE failure return EBADF as per POSIX. |
---|
5628 | * |
---|
5629 | * Only one flag, or 0, may be specified. |
---|
5630 | */ |
---|
5631 | if ((flags == FREAD && (fp->f_flag & FREAD) == 0) || |
---|
5632 | (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) { |
---|
5633 | fdrop(fp, td); |
---|
5634 | return (EBADF); |
---|
5635 | } |
---|
5636 | *fpp = fp; |
---|
5637 | return (0); |
---|
5638 | } |
---|
5639 | |
---|
5640 | int |
---|
5641 | fget(struct thread *td, int fd, struct file **fpp) |
---|
5642 | { |
---|
5643 | |
---|
5644 | return(_fget(td, fd, fpp, 0)); |
---|
5645 | } |
---|
5646 | |
---|
5647 | int |
---|
5648 | fget_read(struct thread *td, int fd, struct file **fpp) |
---|
5649 | { |
---|
5650 | |
---|
5651 | return(_fget(td, fd, fpp, FREAD)); |
---|
5652 | } |
---|
5653 | |
---|
5654 | #ifndef __rtems__ |
---|
5655 | int |
---|
5656 | fget_write(struct thread *td, int fd, struct file **fpp) |
---|
5657 | { |
---|
5658 | |
---|
5659 | return(_fget(td, fd, fpp, FWRITE)); |
---|
5660 | } |
---|
5661 | |
---|
5662 | /* |
---|
5663 | * Like fget() but loads the underlying vnode, or returns an error if the |
---|
5664 | * descriptor does not represent a vnode. Note that pipes use vnodes but |
---|
5665 | * never have VM objects. The returned vnode will be vref()'d. |
---|
5666 | * |
---|
5667 | * XXX: what about the unused flags ? |
---|
5668 | */ |
---|
5669 | static __inline int |
---|
5670 | _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags) |
---|
5671 | { |
---|
5672 | struct file *fp; |
---|
5673 | int error; |
---|
5674 | |
---|
5675 | *vpp = NULL; |
---|
5676 | if ((error = _fget(td, fd, &fp, flags)) != 0) |
---|
5677 | return (error); |
---|
5678 | if (fp->f_vnode == NULL) { |
---|
5679 | error = EINVAL; |
---|
5680 | } else { |
---|
5681 | *vpp = fp->f_vnode; |
---|
5682 | vref(*vpp); |
---|
5683 | } |
---|
5684 | fdrop(fp, td); |
---|
5685 | |
---|
5686 | return (error); |
---|
5687 | } |
---|
5688 | |
---|
5689 | int |
---|
5690 | fgetvp(struct thread *td, int fd, struct vnode **vpp) |
---|
5691 | { |
---|
5692 | |
---|
5693 | return (_fgetvp(td, fd, vpp, 0)); |
---|
5694 | } |
---|
5695 | |
---|
5696 | int |
---|
5697 | fgetvp_read(struct thread *td, int fd, struct vnode **vpp) |
---|
5698 | { |
---|
5699 | |
---|
5700 | return (_fgetvp(td, fd, vpp, FREAD)); |
---|
5701 | } |
---|
5702 | |
---|
5703 | #ifdef notyet |
---|
5704 | int |
---|
5705 | fgetvp_write(struct thread *td, int fd, struct vnode **vpp) |
---|
5706 | { |
---|
5707 | |
---|
5708 | return (_fgetvp(td, fd, vpp, FWRITE)); |
---|
5709 | } |
---|
5710 | #endif |
---|
5711 | |
---|
5712 | /* |
---|
5713 | * Like fget() but loads the underlying socket, or returns an error if the |
---|
5714 | * descriptor does not represent a socket. |
---|
5715 | * |
---|
5716 | * We bump the ref count on the returned socket. XXX Also obtain the SX lock |
---|
5717 | * in the future. |
---|
5718 | * |
---|
5719 | * Note: fgetsock() and fputsock() are deprecated, as consumers should rely |
---|
5720 | * on their file descriptor reference to prevent the socket from being free'd |
---|
5721 | * during use. |
---|
5722 | */ |
---|
5723 | int |
---|
5724 | fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp) |
---|
5725 | { |
---|
5726 | struct file *fp; |
---|
5727 | int error; |
---|
5728 | |
---|
5729 | *spp = NULL; |
---|
5730 | if (fflagp != NULL) |
---|
5731 | *fflagp = 0; |
---|
5732 | if ((error = _fget(td, fd, &fp, 0)) != 0) |
---|
5733 | return (error); |
---|
5734 | if (fp->f_type != DTYPE_SOCKET) { |
---|
5735 | error = ENOTSOCK; |
---|
5736 | } else { |
---|
5737 | *spp = fp->f_data; |
---|
5738 | if (fflagp) |
---|
5739 | *fflagp = fp->f_flag; |
---|
5740 | SOCK_LOCK(*spp); |
---|
5741 | soref(*spp); |
---|
5742 | SOCK_UNLOCK(*spp); |
---|
5743 | } |
---|
5744 | fdrop(fp, td); |
---|
5745 | |
---|
5746 | return (error); |
---|
5747 | } |
---|
5748 | |
---|
5749 | /* |
---|
5750 | * Drop the reference count on the socket and XXX release the SX lock in the |
---|
5751 | * future. The last reference closes the socket. |
---|
5752 | * |
---|
5753 | * Note: fputsock() is deprecated, see comment for fgetsock(). |
---|
5754 | */ |
---|
5755 | void |
---|
5756 | fputsock(struct socket *so) |
---|
5757 | { |
---|
5758 | |
---|
5759 | ACCEPT_LOCK(); |
---|
5760 | SOCK_LOCK(so); |
---|
5761 | sorele(so); |
---|
5762 | } |
---|
5763 | #endif /* __rtems__ */ |
---|
5764 | |
---|
5765 | /* |
---|
5766 | * Handle the last reference to a file being closed. |
---|
5767 | */ |
---|
5768 | int |
---|
5769 | _fdrop(struct file *fp, struct thread *td) |
---|
5770 | { |
---|
5771 | #ifdef __rtems__ |
---|
5772 | panic("fdrop: RTEMS unsupported"); |
---|
5773 | |
---|
5774 | #else /* __rtems__ */ |
---|
5775 | int error; |
---|
5776 | |
---|
5777 | error = 0; |
---|
5778 | if (fp->f_count != 0) |
---|
5779 | panic("fdrop: count %d", fp->f_count); |
---|
5780 | if (fp->f_ops != &badfileops) |
---|
5781 | error = fo_close(fp, td); |
---|
5782 | /* |
---|
5783 | * The f_cdevpriv cannot be assigned non-NULL value while we |
---|
5784 | * are destroying the file. |
---|
5785 | */ |
---|
5786 | if (fp->f_cdevpriv != NULL) |
---|
5787 | devfs_fpdrop(fp); |
---|
5788 | atomic_subtract_int(&openfiles, 1); |
---|
5789 | crfree(fp->f_cred); |
---|
5790 | uma_zfree(file_zone, fp); |
---|
5791 | |
---|
5792 | return (error); |
---|
5793 | #endif /* __rtems__ */ |
---|
5794 | } |
---|
5795 | |
---|
5796 | #ifndef __rtems__ |
---|
5797 | /* |
---|
5798 | * Apply an advisory lock on a file descriptor. |
---|
5799 | * |
---|
5800 | * Just attempt to get a record lock of the requested type on the entire file |
---|
5801 | * (l_whence = SEEK_SET, l_start = 0, l_len = 0). |
---|
5802 | */ |
---|
5803 | #ifndef _SYS_SYSPROTO_H_ |
---|
5804 | struct flock_args { |
---|
5805 | int fd; |
---|
5806 | int how; |
---|
5807 | }; |
---|
5808 | #endif |
---|
5809 | /* ARGSUSED */ |
---|
5810 | int |
---|
5811 | flock(struct thread *td, struct flock_args *uap) |
---|
5812 | { |
---|
5813 | struct file *fp; |
---|
5814 | struct vnode *vp; |
---|
5815 | struct flock lf; |
---|
5816 | int vfslocked; |
---|
5817 | int error; |
---|
5818 | |
---|
5819 | if ((error = fget(td, uap->fd, &fp)) != 0) |
---|
5820 | return (error); |
---|
5821 | if (fp->f_type != DTYPE_VNODE) { |
---|
5822 | fdrop(fp, td); |
---|
5823 | return (EOPNOTSUPP); |
---|
5824 | } |
---|
5825 | |
---|
5826 | vp = fp->f_vnode; |
---|
5827 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
5828 | lf.l_whence = SEEK_SET; |
---|
5829 | lf.l_start = 0; |
---|
5830 | lf.l_len = 0; |
---|
5831 | if (uap->how & LOCK_UN) { |
---|
5832 | lf.l_type = F_UNLCK; |
---|
5833 | atomic_clear_int(&fp->f_flag, FHASLOCK); |
---|
5834 | error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); |
---|
5835 | goto done2; |
---|
5836 | } |
---|
5837 | if (uap->how & LOCK_EX) |
---|
5838 | lf.l_type = F_WRLCK; |
---|
5839 | else if (uap->how & LOCK_SH) |
---|
5840 | lf.l_type = F_RDLCK; |
---|
5841 | else { |
---|
5842 | error = EBADF; |
---|
5843 | goto done2; |
---|
5844 | } |
---|
5845 | atomic_set_int(&fp->f_flag, FHASLOCK); |
---|
5846 | error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, |
---|
5847 | (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); |
---|
5848 | done2: |
---|
5849 | fdrop(fp, td); |
---|
5850 | VFS_UNLOCK_GIANT(vfslocked); |
---|
5851 | return (error); |
---|
5852 | } |
---|
5853 | /* |
---|
5854 | * Duplicate the specified descriptor to a free descriptor. |
---|
5855 | */ |
---|
5856 | int |
---|
5857 | dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error) |
---|
5858 | { |
---|
5859 | struct file *wfp; |
---|
5860 | struct file *fp; |
---|
5861 | |
---|
5862 | /* |
---|
5863 | * If the to-be-dup'd fd number is greater than the allowed number |
---|
5864 | * of file descriptors, or the fd to be dup'd has already been |
---|
5865 | * closed, then reject. |
---|
5866 | */ |
---|
5867 | FILEDESC_XLOCK(fdp); |
---|
5868 | if (dfd < 0 || dfd >= fdp->fd_nfiles || |
---|
5869 | (wfp = fdp->fd_ofiles[dfd]) == NULL) { |
---|
5870 | FILEDESC_XUNLOCK(fdp); |
---|
5871 | return (EBADF); |
---|
5872 | } |
---|
5873 | |
---|
5874 | /* |
---|
5875 | * There are two cases of interest here. |
---|
5876 | * |
---|
5877 | * For ENODEV simply dup (dfd) to file descriptor (indx) and return. |
---|
5878 | * |
---|
5879 | * For ENXIO steal away the file structure from (dfd) and store it in |
---|
5880 | * (indx). (dfd) is effectively closed by this operation. |
---|
5881 | * |
---|
5882 | * Any other error code is just returned. |
---|
5883 | */ |
---|
5884 | switch (error) { |
---|
5885 | case ENODEV: |
---|
5886 | /* |
---|
5887 | * Check that the mode the file is being opened for is a |
---|
5888 | * subset of the mode of the existing descriptor. |
---|
5889 | */ |
---|
5890 | if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { |
---|
5891 | FILEDESC_XUNLOCK(fdp); |
---|
5892 | return (EACCES); |
---|
5893 | } |
---|
5894 | fp = fdp->fd_ofiles[indx]; |
---|
5895 | fdp->fd_ofiles[indx] = wfp; |
---|
5896 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; |
---|
5897 | if (fp == NULL) |
---|
5898 | fdused(fdp, indx); |
---|
5899 | fhold(wfp); |
---|
5900 | FILEDESC_XUNLOCK(fdp); |
---|
5901 | if (fp != NULL) |
---|
5902 | /* |
---|
5903 | * We now own the reference to fp that the ofiles[] |
---|
5904 | * array used to own. Release it. |
---|
5905 | */ |
---|
5906 | fdrop(fp, td); |
---|
5907 | return (0); |
---|
5908 | |
---|
5909 | case ENXIO: |
---|
5910 | /* |
---|
5911 | * Steal away the file pointer from dfd and stuff it into indx. |
---|
5912 | */ |
---|
5913 | fp = fdp->fd_ofiles[indx]; |
---|
5914 | fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; |
---|
5915 | fdp->fd_ofiles[dfd] = NULL; |
---|
5916 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; |
---|
5917 | fdp->fd_ofileflags[dfd] = 0; |
---|
5918 | fdunused(fdp, dfd); |
---|
5919 | if (fp == NULL) |
---|
5920 | fdused(fdp, indx); |
---|
5921 | FILEDESC_XUNLOCK(fdp); |
---|
5922 | |
---|
5923 | /* |
---|
5924 | * We now own the reference to fp that the ofiles[] array |
---|
5925 | * used to own. Release it. |
---|
5926 | */ |
---|
5927 | if (fp != NULL) |
---|
5928 | fdrop(fp, td); |
---|
5929 | return (0); |
---|
5930 | |
---|
5931 | default: |
---|
5932 | FILEDESC_XUNLOCK(fdp); |
---|
5933 | return (error); |
---|
5934 | } |
---|
5935 | /* NOTREACHED */ |
---|
5936 | } |
---|
5937 | |
---|
5938 | /* |
---|
5939 | * Scan all active processes and prisons to see if any of them have a current |
---|
5940 | * or root directory of `olddp'. If so, replace them with the new mount point. |
---|
5941 | */ |
---|
5942 | void |
---|
5943 | mountcheckdirs(struct vnode *olddp, struct vnode *newdp) |
---|
5944 | { |
---|
5945 | struct filedesc *fdp; |
---|
5946 | struct prison *pr; |
---|
5947 | struct proc *p; |
---|
5948 | int nrele; |
---|
5949 | |
---|
5950 | if (vrefcnt(olddp) == 1) |
---|
5951 | return; |
---|
5952 | nrele = 0; |
---|
5953 | sx_slock(&allproc_lock); |
---|
5954 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
5955 | fdp = fdhold(p); |
---|
5956 | if (fdp == NULL) |
---|
5957 | continue; |
---|
5958 | FILEDESC_XLOCK(fdp); |
---|
5959 | if (fdp->fd_cdir == olddp) { |
---|
5960 | vref(newdp); |
---|
5961 | fdp->fd_cdir = newdp; |
---|
5962 | nrele++; |
---|
5963 | } |
---|
5964 | if (fdp->fd_rdir == olddp) { |
---|
5965 | vref(newdp); |
---|
5966 | fdp->fd_rdir = newdp; |
---|
5967 | nrele++; |
---|
5968 | } |
---|
5969 | if (fdp->fd_jdir == olddp) { |
---|
5970 | vref(newdp); |
---|
5971 | fdp->fd_jdir = newdp; |
---|
5972 | nrele++; |
---|
5973 | } |
---|
5974 | FILEDESC_XUNLOCK(fdp); |
---|
5975 | fddrop(fdp); |
---|
5976 | } |
---|
5977 | sx_sunlock(&allproc_lock); |
---|
5978 | if (rootvnode == olddp) { |
---|
5979 | vref(newdp); |
---|
5980 | rootvnode = newdp; |
---|
5981 | nrele++; |
---|
5982 | } |
---|
5983 | mtx_lock(&prison0.pr_mtx); |
---|
5984 | if (prison0.pr_root == olddp) { |
---|
5985 | vref(newdp); |
---|
5986 | prison0.pr_root = newdp; |
---|
5987 | nrele++; |
---|
5988 | } |
---|
5989 | mtx_unlock(&prison0.pr_mtx); |
---|
5990 | sx_slock(&allprison_lock); |
---|
5991 | TAILQ_FOREACH(pr, &allprison, pr_list) { |
---|
5992 | mtx_lock(&pr->pr_mtx); |
---|
5993 | if (pr->pr_root == olddp) { |
---|
5994 | vref(newdp); |
---|
5995 | pr->pr_root = newdp; |
---|
5996 | nrele++; |
---|
5997 | } |
---|
5998 | mtx_unlock(&pr->pr_mtx); |
---|
5999 | } |
---|
6000 | sx_sunlock(&allprison_lock); |
---|
6001 | while (nrele--) |
---|
6002 | vrele(olddp); |
---|
6003 | } |
---|
6004 | |
---|
6005 | struct filedesc_to_leader * |
---|
6006 | filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader) |
---|
6007 | { |
---|
6008 | struct filedesc_to_leader *fdtol; |
---|
6009 | |
---|
6010 | fdtol = malloc(sizeof(struct filedesc_to_leader), |
---|
6011 | M_FILEDESC_TO_LEADER, |
---|
6012 | M_WAITOK); |
---|
6013 | fdtol->fdl_refcount = 1; |
---|
6014 | fdtol->fdl_holdcount = 0; |
---|
6015 | fdtol->fdl_wakeup = 0; |
---|
6016 | fdtol->fdl_leader = leader; |
---|
6017 | if (old != NULL) { |
---|
6018 | FILEDESC_XLOCK(fdp); |
---|
6019 | fdtol->fdl_next = old->fdl_next; |
---|
6020 | fdtol->fdl_prev = old; |
---|
6021 | old->fdl_next = fdtol; |
---|
6022 | fdtol->fdl_next->fdl_prev = fdtol; |
---|
6023 | FILEDESC_XUNLOCK(fdp); |
---|
6024 | } else { |
---|
6025 | fdtol->fdl_next = fdtol; |
---|
6026 | fdtol->fdl_prev = fdtol; |
---|
6027 | } |
---|
6028 | return (fdtol); |
---|
6029 | } |
---|
6030 | |
---|
6031 | /* |
---|
6032 | * Get file structures globally. |
---|
6033 | */ |
---|
6034 | static int |
---|
6035 | sysctl_kern_file(SYSCTL_HANDLER_ARGS) |
---|
6036 | { |
---|
6037 | struct xfile xf; |
---|
6038 | struct filedesc *fdp; |
---|
6039 | struct file *fp; |
---|
6040 | struct proc *p; |
---|
6041 | int error, n; |
---|
6042 | |
---|
6043 | error = sysctl_wire_old_buffer(req, 0); |
---|
6044 | if (error != 0) |
---|
6045 | return (error); |
---|
6046 | if (req->oldptr == NULL) { |
---|
6047 | n = 0; |
---|
6048 | sx_slock(&allproc_lock); |
---|
6049 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
6050 | if (p->p_state == PRS_NEW) |
---|
6051 | continue; |
---|
6052 | fdp = fdhold(p); |
---|
6053 | if (fdp == NULL) |
---|
6054 | continue; |
---|
6055 | /* overestimates sparse tables. */ |
---|
6056 | if (fdp->fd_lastfile > 0) |
---|
6057 | n += fdp->fd_lastfile; |
---|
6058 | fddrop(fdp); |
---|
6059 | } |
---|
6060 | sx_sunlock(&allproc_lock); |
---|
6061 | return (SYSCTL_OUT(req, 0, n * sizeof(xf))); |
---|
6062 | } |
---|
6063 | error = 0; |
---|
6064 | bzero(&xf, sizeof(xf)); |
---|
6065 | xf.xf_size = sizeof(xf); |
---|
6066 | sx_slock(&allproc_lock); |
---|
6067 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
6068 | if (p->p_state == PRS_NEW) |
---|
6069 | continue; |
---|
6070 | PROC_LOCK(p); |
---|
6071 | if (p_cansee(req->td, p) != 0) { |
---|
6072 | PROC_UNLOCK(p); |
---|
6073 | continue; |
---|
6074 | } |
---|
6075 | xf.xf_pid = p->p_pid; |
---|
6076 | xf.xf_uid = p->p_ucred->cr_uid; |
---|
6077 | PROC_UNLOCK(p); |
---|
6078 | fdp = fdhold(p); |
---|
6079 | if (fdp == NULL) |
---|
6080 | continue; |
---|
6081 | FILEDESC_SLOCK(fdp); |
---|
6082 | for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) { |
---|
6083 | if ((fp = fdp->fd_ofiles[n]) == NULL) |
---|
6084 | continue; |
---|
6085 | xf.xf_fd = n; |
---|
6086 | xf.xf_file = fp; |
---|
6087 | xf.xf_data = fp->f_data; |
---|
6088 | xf.xf_vnode = fp->f_vnode; |
---|
6089 | xf.xf_type = fp->f_type; |
---|
6090 | xf.xf_count = fp->f_count; |
---|
6091 | xf.xf_msgcount = 0; |
---|
6092 | xf.xf_offset = fp->f_offset; |
---|
6093 | xf.xf_flag = fp->f_flag; |
---|
6094 | error = SYSCTL_OUT(req, &xf, sizeof(xf)); |
---|
6095 | if (error) |
---|
6096 | break; |
---|
6097 | } |
---|
6098 | FILEDESC_SUNLOCK(fdp); |
---|
6099 | fddrop(fdp); |
---|
6100 | if (error) |
---|
6101 | break; |
---|
6102 | } |
---|
6103 | sx_sunlock(&allproc_lock); |
---|
6104 | return (error); |
---|
6105 | } |
---|
6106 | |
---|
6107 | SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, |
---|
6108 | 0, 0, sysctl_kern_file, "S,xfile", "Entire file table"); |
---|
6109 | |
---|
6110 | #ifdef KINFO_OFILE_SIZE |
---|
6111 | CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE); |
---|
6112 | #endif |
---|
6113 | |
---|
6114 | #ifdef COMPAT_FREEBSD7 |
---|
6115 | static int |
---|
6116 | export_vnode_for_osysctl(struct vnode *vp, int type, |
---|
6117 | struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req) |
---|
6118 | { |
---|
6119 | int error; |
---|
6120 | char *fullpath, *freepath; |
---|
6121 | int vfslocked; |
---|
6122 | |
---|
6123 | bzero(kif, sizeof(*kif)); |
---|
6124 | kif->kf_structsize = sizeof(*kif); |
---|
6125 | |
---|
6126 | vref(vp); |
---|
6127 | kif->kf_fd = type; |
---|
6128 | kif->kf_type = KF_TYPE_VNODE; |
---|
6129 | /* This function only handles directories. */ |
---|
6130 | if (vp->v_type != VDIR) { |
---|
6131 | vrele(vp); |
---|
6132 | return (ENOTDIR); |
---|
6133 | } |
---|
6134 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
6135 | |
---|
6136 | /* |
---|
6137 | * This is not a true file descriptor, so we set a bogus refcount |
---|
6138 | * and offset to indicate these fields should be ignored. |
---|
6139 | */ |
---|
6140 | kif->kf_ref_count = -1; |
---|
6141 | kif->kf_offset = -1; |
---|
6142 | |
---|
6143 | freepath = NULL; |
---|
6144 | fullpath = "-"; |
---|
6145 | FILEDESC_SUNLOCK(fdp); |
---|
6146 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
6147 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
6148 | vrele(vp); |
---|
6149 | VFS_UNLOCK_GIANT(vfslocked); |
---|
6150 | strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); |
---|
6151 | if (freepath != NULL) |
---|
6152 | free(freepath, M_TEMP); |
---|
6153 | error = SYSCTL_OUT(req, kif, sizeof(*kif)); |
---|
6154 | FILEDESC_SLOCK(fdp); |
---|
6155 | return (error); |
---|
6156 | } |
---|
6157 | |
---|
6158 | /* |
---|
6159 | * Get per-process file descriptors for use by procstat(1), et al. |
---|
6160 | */ |
---|
6161 | static int |
---|
6162 | sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS) |
---|
6163 | { |
---|
6164 | char *fullpath, *freepath; |
---|
6165 | struct kinfo_ofile *kif; |
---|
6166 | struct filedesc *fdp; |
---|
6167 | int error, i, *name; |
---|
6168 | struct socket *so; |
---|
6169 | struct vnode *vp; |
---|
6170 | struct file *fp; |
---|
6171 | struct proc *p; |
---|
6172 | struct tty *tp; |
---|
6173 | int vfslocked; |
---|
6174 | |
---|
6175 | name = (int *)arg1; |
---|
6176 | if ((p = pfind((pid_t)name[0])) == NULL) |
---|
6177 | return (ESRCH); |
---|
6178 | if ((error = p_candebug(curthread, p))) { |
---|
6179 | PROC_UNLOCK(p); |
---|
6180 | return (error); |
---|
6181 | } |
---|
6182 | fdp = fdhold(p); |
---|
6183 | PROC_UNLOCK(p); |
---|
6184 | if (fdp == NULL) |
---|
6185 | return (ENOENT); |
---|
6186 | kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); |
---|
6187 | FILEDESC_SLOCK(fdp); |
---|
6188 | if (fdp->fd_cdir != NULL) |
---|
6189 | export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, |
---|
6190 | fdp, req); |
---|
6191 | if (fdp->fd_rdir != NULL) |
---|
6192 | export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, |
---|
6193 | fdp, req); |
---|
6194 | if (fdp->fd_jdir != NULL) |
---|
6195 | export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, |
---|
6196 | fdp, req); |
---|
6197 | for (i = 0; i < fdp->fd_nfiles; i++) { |
---|
6198 | if ((fp = fdp->fd_ofiles[i]) == NULL) |
---|
6199 | continue; |
---|
6200 | bzero(kif, sizeof(*kif)); |
---|
6201 | kif->kf_structsize = sizeof(*kif); |
---|
6202 | vp = NULL; |
---|
6203 | so = NULL; |
---|
6204 | tp = NULL; |
---|
6205 | kif->kf_fd = i; |
---|
6206 | switch (fp->f_type) { |
---|
6207 | case DTYPE_VNODE: |
---|
6208 | kif->kf_type = KF_TYPE_VNODE; |
---|
6209 | vp = fp->f_vnode; |
---|
6210 | break; |
---|
6211 | |
---|
6212 | case DTYPE_SOCKET: |
---|
6213 | kif->kf_type = KF_TYPE_SOCKET; |
---|
6214 | so = fp->f_data; |
---|
6215 | break; |
---|
6216 | |
---|
6217 | case DTYPE_PIPE: |
---|
6218 | kif->kf_type = KF_TYPE_PIPE; |
---|
6219 | break; |
---|
6220 | |
---|
6221 | case DTYPE_FIFO: |
---|
6222 | kif->kf_type = KF_TYPE_FIFO; |
---|
6223 | vp = fp->f_vnode; |
---|
6224 | break; |
---|
6225 | |
---|
6226 | case DTYPE_KQUEUE: |
---|
6227 | kif->kf_type = KF_TYPE_KQUEUE; |
---|
6228 | break; |
---|
6229 | |
---|
6230 | case DTYPE_CRYPTO: |
---|
6231 | kif->kf_type = KF_TYPE_CRYPTO; |
---|
6232 | break; |
---|
6233 | |
---|
6234 | case DTYPE_MQUEUE: |
---|
6235 | kif->kf_type = KF_TYPE_MQUEUE; |
---|
6236 | break; |
---|
6237 | |
---|
6238 | case DTYPE_SHM: |
---|
6239 | kif->kf_type = KF_TYPE_SHM; |
---|
6240 | break; |
---|
6241 | |
---|
6242 | case DTYPE_SEM: |
---|
6243 | kif->kf_type = KF_TYPE_SEM; |
---|
6244 | break; |
---|
6245 | |
---|
6246 | case DTYPE_PTS: |
---|
6247 | kif->kf_type = KF_TYPE_PTS; |
---|
6248 | tp = fp->f_data; |
---|
6249 | break; |
---|
6250 | |
---|
6251 | default: |
---|
6252 | kif->kf_type = KF_TYPE_UNKNOWN; |
---|
6253 | break; |
---|
6254 | } |
---|
6255 | kif->kf_ref_count = fp->f_count; |
---|
6256 | if (fp->f_flag & FREAD) |
---|
6257 | kif->kf_flags |= KF_FLAG_READ; |
---|
6258 | if (fp->f_flag & FWRITE) |
---|
6259 | kif->kf_flags |= KF_FLAG_WRITE; |
---|
6260 | if (fp->f_flag & FAPPEND) |
---|
6261 | kif->kf_flags |= KF_FLAG_APPEND; |
---|
6262 | if (fp->f_flag & FASYNC) |
---|
6263 | kif->kf_flags |= KF_FLAG_ASYNC; |
---|
6264 | if (fp->f_flag & FFSYNC) |
---|
6265 | kif->kf_flags |= KF_FLAG_FSYNC; |
---|
6266 | if (fp->f_flag & FNONBLOCK) |
---|
6267 | kif->kf_flags |= KF_FLAG_NONBLOCK; |
---|
6268 | if (fp->f_flag & O_DIRECT) |
---|
6269 | kif->kf_flags |= KF_FLAG_DIRECT; |
---|
6270 | if (fp->f_flag & FHASLOCK) |
---|
6271 | kif->kf_flags |= KF_FLAG_HASLOCK; |
---|
6272 | kif->kf_offset = fp->f_offset; |
---|
6273 | if (vp != NULL) { |
---|
6274 | vref(vp); |
---|
6275 | switch (vp->v_type) { |
---|
6276 | case VNON: |
---|
6277 | kif->kf_vnode_type = KF_VTYPE_VNON; |
---|
6278 | break; |
---|
6279 | case VREG: |
---|
6280 | kif->kf_vnode_type = KF_VTYPE_VREG; |
---|
6281 | break; |
---|
6282 | case VDIR: |
---|
6283 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
6284 | break; |
---|
6285 | case VBLK: |
---|
6286 | kif->kf_vnode_type = KF_VTYPE_VBLK; |
---|
6287 | break; |
---|
6288 | case VCHR: |
---|
6289 | kif->kf_vnode_type = KF_VTYPE_VCHR; |
---|
6290 | break; |
---|
6291 | case VLNK: |
---|
6292 | kif->kf_vnode_type = KF_VTYPE_VLNK; |
---|
6293 | break; |
---|
6294 | case VSOCK: |
---|
6295 | kif->kf_vnode_type = KF_VTYPE_VSOCK; |
---|
6296 | break; |
---|
6297 | case VFIFO: |
---|
6298 | kif->kf_vnode_type = KF_VTYPE_VFIFO; |
---|
6299 | break; |
---|
6300 | case VBAD: |
---|
6301 | kif->kf_vnode_type = KF_VTYPE_VBAD; |
---|
6302 | break; |
---|
6303 | default: |
---|
6304 | kif->kf_vnode_type = KF_VTYPE_UNKNOWN; |
---|
6305 | break; |
---|
6306 | } |
---|
6307 | /* |
---|
6308 | * It is OK to drop the filedesc lock here as we will |
---|
6309 | * re-validate and re-evaluate its properties when |
---|
6310 | * the loop continues. |
---|
6311 | */ |
---|
6312 | freepath = NULL; |
---|
6313 | fullpath = "-"; |
---|
6314 | FILEDESC_SUNLOCK(fdp); |
---|
6315 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
6316 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
6317 | vrele(vp); |
---|
6318 | VFS_UNLOCK_GIANT(vfslocked); |
---|
6319 | strlcpy(kif->kf_path, fullpath, |
---|
6320 | sizeof(kif->kf_path)); |
---|
6321 | if (freepath != NULL) |
---|
6322 | free(freepath, M_TEMP); |
---|
6323 | FILEDESC_SLOCK(fdp); |
---|
6324 | } |
---|
6325 | if (so != NULL) { |
---|
6326 | struct sockaddr *sa; |
---|
6327 | |
---|
6328 | if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) |
---|
6329 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { |
---|
6330 | bcopy(sa, &kif->kf_sa_local, sa->sa_len); |
---|
6331 | free(sa, M_SONAME); |
---|
6332 | } |
---|
6333 | if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) |
---|
6334 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { |
---|
6335 | bcopy(sa, &kif->kf_sa_peer, sa->sa_len); |
---|
6336 | free(sa, M_SONAME); |
---|
6337 | } |
---|
6338 | kif->kf_sock_domain = |
---|
6339 | so->so_proto->pr_domain->dom_family; |
---|
6340 | kif->kf_sock_type = so->so_type; |
---|
6341 | kif->kf_sock_protocol = so->so_proto->pr_protocol; |
---|
6342 | } |
---|
6343 | if (tp != NULL) { |
---|
6344 | strlcpy(kif->kf_path, tty_devname(tp), |
---|
6345 | sizeof(kif->kf_path)); |
---|
6346 | } |
---|
6347 | error = SYSCTL_OUT(req, kif, sizeof(*kif)); |
---|
6348 | if (error) |
---|
6349 | break; |
---|
6350 | } |
---|
6351 | FILEDESC_SUNLOCK(fdp); |
---|
6352 | fddrop(fdp); |
---|
6353 | free(kif, M_TEMP); |
---|
6354 | return (0); |
---|
6355 | } |
---|
6356 | |
---|
6357 | static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD, |
---|
6358 | sysctl_kern_proc_ofiledesc, "Process ofiledesc entries"); |
---|
6359 | #endif /* COMPAT_FREEBSD7 */ |
---|
6360 | |
---|
6361 | #ifdef KINFO_FILE_SIZE |
---|
6362 | CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); |
---|
6363 | #endif |
---|
6364 | |
---|
6365 | static int |
---|
6366 | export_vnode_for_sysctl(struct vnode *vp, int type, |
---|
6367 | struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req) |
---|
6368 | { |
---|
6369 | int error; |
---|
6370 | char *fullpath, *freepath; |
---|
6371 | int vfslocked; |
---|
6372 | |
---|
6373 | bzero(kif, sizeof(*kif)); |
---|
6374 | |
---|
6375 | vref(vp); |
---|
6376 | kif->kf_fd = type; |
---|
6377 | kif->kf_type = KF_TYPE_VNODE; |
---|
6378 | /* This function only handles directories. */ |
---|
6379 | if (vp->v_type != VDIR) { |
---|
6380 | vrele(vp); |
---|
6381 | return (ENOTDIR); |
---|
6382 | } |
---|
6383 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
6384 | |
---|
6385 | /* |
---|
6386 | * This is not a true file descriptor, so we set a bogus refcount |
---|
6387 | * and offset to indicate these fields should be ignored. |
---|
6388 | */ |
---|
6389 | kif->kf_ref_count = -1; |
---|
6390 | kif->kf_offset = -1; |
---|
6391 | |
---|
6392 | freepath = NULL; |
---|
6393 | fullpath = "-"; |
---|
6394 | FILEDESC_SUNLOCK(fdp); |
---|
6395 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
6396 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
6397 | vrele(vp); |
---|
6398 | VFS_UNLOCK_GIANT(vfslocked); |
---|
6399 | strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); |
---|
6400 | if (freepath != NULL) |
---|
6401 | free(freepath, M_TEMP); |
---|
6402 | /* Pack record size down */ |
---|
6403 | kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + |
---|
6404 | strlen(kif->kf_path) + 1; |
---|
6405 | kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t)); |
---|
6406 | error = SYSCTL_OUT(req, kif, kif->kf_structsize); |
---|
6407 | FILEDESC_SLOCK(fdp); |
---|
6408 | return (error); |
---|
6409 | } |
---|
6410 | |
---|
6411 | /* |
---|
6412 | * Get per-process file descriptors for use by procstat(1), et al. |
---|
6413 | */ |
---|
6414 | static int |
---|
6415 | sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS) |
---|
6416 | { |
---|
6417 | char *fullpath, *freepath; |
---|
6418 | struct kinfo_file *kif; |
---|
6419 | struct filedesc *fdp; |
---|
6420 | int error, i, *name; |
---|
6421 | struct socket *so; |
---|
6422 | struct vnode *vp; |
---|
6423 | struct file *fp; |
---|
6424 | struct proc *p; |
---|
6425 | struct tty *tp; |
---|
6426 | int vfslocked; |
---|
6427 | size_t oldidx; |
---|
6428 | |
---|
6429 | name = (int *)arg1; |
---|
6430 | if ((p = pfind((pid_t)name[0])) == NULL) |
---|
6431 | return (ESRCH); |
---|
6432 | if ((error = p_candebug(curthread, p))) { |
---|
6433 | PROC_UNLOCK(p); |
---|
6434 | return (error); |
---|
6435 | } |
---|
6436 | fdp = fdhold(p); |
---|
6437 | PROC_UNLOCK(p); |
---|
6438 | if (fdp == NULL) |
---|
6439 | return (ENOENT); |
---|
6440 | kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); |
---|
6441 | FILEDESC_SLOCK(fdp); |
---|
6442 | if (fdp->fd_cdir != NULL) |
---|
6443 | export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, |
---|
6444 | fdp, req); |
---|
6445 | if (fdp->fd_rdir != NULL) |
---|
6446 | export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, |
---|
6447 | fdp, req); |
---|
6448 | if (fdp->fd_jdir != NULL) |
---|
6449 | export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, |
---|
6450 | fdp, req); |
---|
6451 | for (i = 0; i < fdp->fd_nfiles; i++) { |
---|
6452 | if ((fp = fdp->fd_ofiles[i]) == NULL) |
---|
6453 | continue; |
---|
6454 | bzero(kif, sizeof(*kif)); |
---|
6455 | vp = NULL; |
---|
6456 | so = NULL; |
---|
6457 | tp = NULL; |
---|
6458 | kif->kf_fd = i; |
---|
6459 | switch (fp->f_type) { |
---|
6460 | case DTYPE_VNODE: |
---|
6461 | kif->kf_type = KF_TYPE_VNODE; |
---|
6462 | vp = fp->f_vnode; |
---|
6463 | break; |
---|
6464 | |
---|
6465 | case DTYPE_SOCKET: |
---|
6466 | kif->kf_type = KF_TYPE_SOCKET; |
---|
6467 | so = fp->f_data; |
---|
6468 | break; |
---|
6469 | |
---|
6470 | case DTYPE_PIPE: |
---|
6471 | kif->kf_type = KF_TYPE_PIPE; |
---|
6472 | break; |
---|
6473 | |
---|
6474 | case DTYPE_FIFO: |
---|
6475 | kif->kf_type = KF_TYPE_FIFO; |
---|
6476 | vp = fp->f_vnode; |
---|
6477 | break; |
---|
6478 | |
---|
6479 | case DTYPE_KQUEUE: |
---|
6480 | kif->kf_type = KF_TYPE_KQUEUE; |
---|
6481 | break; |
---|
6482 | |
---|
6483 | case DTYPE_CRYPTO: |
---|
6484 | kif->kf_type = KF_TYPE_CRYPTO; |
---|
6485 | break; |
---|
6486 | |
---|
6487 | case DTYPE_MQUEUE: |
---|
6488 | kif->kf_type = KF_TYPE_MQUEUE; |
---|
6489 | break; |
---|
6490 | |
---|
6491 | case DTYPE_SHM: |
---|
6492 | kif->kf_type = KF_TYPE_SHM; |
---|
6493 | break; |
---|
6494 | |
---|
6495 | case DTYPE_SEM: |
---|
6496 | kif->kf_type = KF_TYPE_SEM; |
---|
6497 | break; |
---|
6498 | |
---|
6499 | case DTYPE_PTS: |
---|
6500 | kif->kf_type = KF_TYPE_PTS; |
---|
6501 | tp = fp->f_data; |
---|
6502 | break; |
---|
6503 | |
---|
6504 | default: |
---|
6505 | kif->kf_type = KF_TYPE_UNKNOWN; |
---|
6506 | break; |
---|
6507 | } |
---|
6508 | kif->kf_ref_count = fp->f_count; |
---|
6509 | if (fp->f_flag & FREAD) |
---|
6510 | kif->kf_flags |= KF_FLAG_READ; |
---|
6511 | if (fp->f_flag & FWRITE) |
---|
6512 | kif->kf_flags |= KF_FLAG_WRITE; |
---|
6513 | if (fp->f_flag & FAPPEND) |
---|
6514 | kif->kf_flags |= KF_FLAG_APPEND; |
---|
6515 | if (fp->f_flag & FASYNC) |
---|
6516 | kif->kf_flags |= KF_FLAG_ASYNC; |
---|
6517 | if (fp->f_flag & FFSYNC) |
---|
6518 | kif->kf_flags |= KF_FLAG_FSYNC; |
---|
6519 | if (fp->f_flag & FNONBLOCK) |
---|
6520 | kif->kf_flags |= KF_FLAG_NONBLOCK; |
---|
6521 | if (fp->f_flag & O_DIRECT) |
---|
6522 | kif->kf_flags |= KF_FLAG_DIRECT; |
---|
6523 | if (fp->f_flag & FHASLOCK) |
---|
6524 | kif->kf_flags |= KF_FLAG_HASLOCK; |
---|
6525 | kif->kf_offset = fp->f_offset; |
---|
6526 | if (vp != NULL) { |
---|
6527 | vref(vp); |
---|
6528 | switch (vp->v_type) { |
---|
6529 | case VNON: |
---|
6530 | kif->kf_vnode_type = KF_VTYPE_VNON; |
---|
6531 | break; |
---|
6532 | case VREG: |
---|
6533 | kif->kf_vnode_type = KF_VTYPE_VREG; |
---|
6534 | break; |
---|
6535 | case VDIR: |
---|
6536 | kif->kf_vnode_type = KF_VTYPE_VDIR; |
---|
6537 | break; |
---|
6538 | case VBLK: |
---|
6539 | kif->kf_vnode_type = KF_VTYPE_VBLK; |
---|
6540 | break; |
---|
6541 | case VCHR: |
---|
6542 | kif->kf_vnode_type = KF_VTYPE_VCHR; |
---|
6543 | break; |
---|
6544 | case VLNK: |
---|
6545 | kif->kf_vnode_type = KF_VTYPE_VLNK; |
---|
6546 | break; |
---|
6547 | case VSOCK: |
---|
6548 | kif->kf_vnode_type = KF_VTYPE_VSOCK; |
---|
6549 | break; |
---|
6550 | case VFIFO: |
---|
6551 | kif->kf_vnode_type = KF_VTYPE_VFIFO; |
---|
6552 | break; |
---|
6553 | case VBAD: |
---|
6554 | kif->kf_vnode_type = KF_VTYPE_VBAD; |
---|
6555 | break; |
---|
6556 | default: |
---|
6557 | kif->kf_vnode_type = KF_VTYPE_UNKNOWN; |
---|
6558 | break; |
---|
6559 | } |
---|
6560 | /* |
---|
6561 | * It is OK to drop the filedesc lock here as we will |
---|
6562 | * re-validate and re-evaluate its properties when |
---|
6563 | * the loop continues. |
---|
6564 | */ |
---|
6565 | freepath = NULL; |
---|
6566 | fullpath = "-"; |
---|
6567 | FILEDESC_SUNLOCK(fdp); |
---|
6568 | vn_fullpath(curthread, vp, &fullpath, &freepath); |
---|
6569 | vfslocked = VFS_LOCK_GIANT(vp->v_mount); |
---|
6570 | vrele(vp); |
---|
6571 | VFS_UNLOCK_GIANT(vfslocked); |
---|
6572 | strlcpy(kif->kf_path, fullpath, |
---|
6573 | sizeof(kif->kf_path)); |
---|
6574 | if (freepath != NULL) |
---|
6575 | free(freepath, M_TEMP); |
---|
6576 | FILEDESC_SLOCK(fdp); |
---|
6577 | } |
---|
6578 | if (so != NULL) { |
---|
6579 | struct sockaddr *sa; |
---|
6580 | |
---|
6581 | if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) |
---|
6582 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { |
---|
6583 | bcopy(sa, &kif->kf_sa_local, sa->sa_len); |
---|
6584 | free(sa, M_SONAME); |
---|
6585 | } |
---|
6586 | if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) |
---|
6587 | == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { |
---|
6588 | bcopy(sa, &kif->kf_sa_peer, sa->sa_len); |
---|
6589 | free(sa, M_SONAME); |
---|
6590 | } |
---|
6591 | kif->kf_sock_domain = |
---|
6592 | so->so_proto->pr_domain->dom_family; |
---|
6593 | kif->kf_sock_type = so->so_type; |
---|
6594 | kif->kf_sock_protocol = so->so_proto->pr_protocol; |
---|
6595 | } |
---|
6596 | if (tp != NULL) { |
---|
6597 | strlcpy(kif->kf_path, tty_devname(tp), |
---|
6598 | sizeof(kif->kf_path)); |
---|
6599 | } |
---|
6600 | /* Pack record size down */ |
---|
6601 | kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + |
---|
6602 | strlen(kif->kf_path) + 1; |
---|
6603 | kif->kf_structsize = roundup(kif->kf_structsize, |
---|
6604 | sizeof(uint64_t)); |
---|
6605 | oldidx = req->oldidx; |
---|
6606 | error = SYSCTL_OUT(req, kif, kif->kf_structsize); |
---|
6607 | if (error) { |
---|
6608 | if (error == ENOMEM) { |
---|
6609 | /* |
---|
6610 | * The hack to keep the ABI of sysctl |
---|
6611 | * kern.proc.filedesc intact, but not |
---|
6612 | * to account a partially copied |
---|
6613 | * kinfo_file into the oldidx. |
---|
6614 | */ |
---|
6615 | req->oldidx = oldidx; |
---|
6616 | error = 0; |
---|
6617 | } |
---|
6618 | break; |
---|
6619 | } |
---|
6620 | } |
---|
6621 | FILEDESC_SUNLOCK(fdp); |
---|
6622 | fddrop(fdp); |
---|
6623 | free(kif, M_TEMP); |
---|
6624 | return (error); |
---|
6625 | } |
---|
6626 | |
---|
6627 | static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, |
---|
6628 | sysctl_kern_proc_filedesc, "Process filedesc entries"); |
---|
6629 | |
---|
6630 | #ifdef DDB |
---|
6631 | /* |
---|
6632 | * For the purposes of debugging, generate a human-readable string for the |
---|
6633 | * file type. |
---|
6634 | */ |
---|
6635 | static const char * |
---|
6636 | file_type_to_name(short type) |
---|
6637 | { |
---|
6638 | |
---|
6639 | switch (type) { |
---|
6640 | case 0: |
---|
6641 | return ("zero"); |
---|
6642 | case DTYPE_VNODE: |
---|
6643 | return ("vnod"); |
---|
6644 | case DTYPE_SOCKET: |
---|
6645 | return ("sock"); |
---|
6646 | case DTYPE_PIPE: |
---|
6647 | return ("pipe"); |
---|
6648 | case DTYPE_FIFO: |
---|
6649 | return ("fifo"); |
---|
6650 | case DTYPE_KQUEUE: |
---|
6651 | return ("kque"); |
---|
6652 | case DTYPE_CRYPTO: |
---|
6653 | return ("crpt"); |
---|
6654 | case DTYPE_MQUEUE: |
---|
6655 | return ("mque"); |
---|
6656 | case DTYPE_SHM: |
---|
6657 | return ("shm"); |
---|
6658 | case DTYPE_SEM: |
---|
6659 | return ("ksem"); |
---|
6660 | default: |
---|
6661 | return ("unkn"); |
---|
6662 | } |
---|
6663 | } |
---|
6664 | |
---|
6665 | /* |
---|
6666 | * For the purposes of debugging, identify a process (if any, perhaps one of |
---|
6667 | * many) that references the passed file in its file descriptor array. Return |
---|
6668 | * NULL if none. |
---|
6669 | */ |
---|
6670 | static struct proc * |
---|
6671 | file_to_first_proc(struct file *fp) |
---|
6672 | { |
---|
6673 | struct filedesc *fdp; |
---|
6674 | struct proc *p; |
---|
6675 | int n; |
---|
6676 | |
---|
6677 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
6678 | if (p->p_state == PRS_NEW) |
---|
6679 | continue; |
---|
6680 | fdp = p->p_fd; |
---|
6681 | if (fdp == NULL) |
---|
6682 | continue; |
---|
6683 | for (n = 0; n < fdp->fd_nfiles; n++) { |
---|
6684 | if (fp == fdp->fd_ofiles[n]) |
---|
6685 | return (p); |
---|
6686 | } |
---|
6687 | } |
---|
6688 | return (NULL); |
---|
6689 | } |
---|
6690 | |
---|
6691 | static void |
---|
6692 | db_print_file(struct file *fp, int header) |
---|
6693 | { |
---|
6694 | struct proc *p; |
---|
6695 | |
---|
6696 | if (header) |
---|
6697 | db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n", |
---|
6698 | "File", "Type", "Data", "Flag", "GCFl", "Count", |
---|
6699 | "MCount", "Vnode", "FPID", "FCmd"); |
---|
6700 | p = file_to_first_proc(fp); |
---|
6701 | db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp, |
---|
6702 | file_type_to_name(fp->f_type), fp->f_data, fp->f_flag, |
---|
6703 | 0, fp->f_count, 0, fp->f_vnode, |
---|
6704 | p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-"); |
---|
6705 | } |
---|
6706 | |
---|
6707 | DB_SHOW_COMMAND(file, db_show_file) |
---|
6708 | { |
---|
6709 | struct file *fp; |
---|
6710 | |
---|
6711 | if (!have_addr) { |
---|
6712 | db_printf("usage: show file <addr>\n"); |
---|
6713 | return; |
---|
6714 | } |
---|
6715 | fp = (struct file *)addr; |
---|
6716 | db_print_file(fp, 1); |
---|
6717 | } |
---|
6718 | |
---|
6719 | DB_SHOW_COMMAND(files, db_show_files) |
---|
6720 | { |
---|
6721 | struct filedesc *fdp; |
---|
6722 | struct file *fp; |
---|
6723 | struct proc *p; |
---|
6724 | int header; |
---|
6725 | int n; |
---|
6726 | |
---|
6727 | header = 1; |
---|
6728 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
6729 | if (p->p_state == PRS_NEW) |
---|
6730 | continue; |
---|
6731 | if ((fdp = p->p_fd) == NULL) |
---|
6732 | continue; |
---|
6733 | for (n = 0; n < fdp->fd_nfiles; ++n) { |
---|
6734 | if ((fp = fdp->fd_ofiles[n]) == NULL) |
---|
6735 | continue; |
---|
6736 | db_print_file(fp, header); |
---|
6737 | header = 0; |
---|
6738 | } |
---|
6739 | } |
---|
6740 | } |
---|
6741 | #endif |
---|
6742 | |
---|
6743 | SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, |
---|
6744 | &maxfilesperproc, 0, "Maximum files allowed open per process"); |
---|
6745 | |
---|
6746 | SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, |
---|
6747 | &maxfiles, 0, "Maximum number of files"); |
---|
6748 | |
---|
6749 | SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, |
---|
6750 | __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files"); |
---|
6751 | |
---|
6752 | /* ARGSUSED*/ |
---|
6753 | static void |
---|
6754 | filelistinit(void *dummy) |
---|
6755 | { |
---|
6756 | |
---|
6757 | file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, |
---|
6758 | NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); |
---|
6759 | mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); |
---|
6760 | mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF); |
---|
6761 | } |
---|
6762 | SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL); |
---|
6763 | #endif /* __rtems__ */ |
---|
6764 | |
---|
6765 | /*-------------------------------------------------------------------*/ |
---|
6766 | |
---|
6767 | static int |
---|
6768 | badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td) |
---|
6769 | { |
---|
6770 | |
---|
6771 | return (EBADF); |
---|
6772 | } |
---|
6773 | |
---|
6774 | static int |
---|
6775 | badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td) |
---|
6776 | { |
---|
6777 | |
---|
6778 | return (EINVAL); |
---|
6779 | } |
---|
6780 | |
---|
6781 | static int |
---|
6782 | badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td) |
---|
6783 | { |
---|
6784 | |
---|
6785 | return (EBADF); |
---|
6786 | } |
---|
6787 | |
---|
6788 | static int |
---|
6789 | badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td) |
---|
6790 | { |
---|
6791 | |
---|
6792 | return (0); |
---|
6793 | } |
---|
6794 | |
---|
6795 | static int |
---|
6796 | badfo_kqfilter(struct file *fp, struct knote *kn) |
---|
6797 | { |
---|
6798 | |
---|
6799 | return (EBADF); |
---|
6800 | } |
---|
6801 | |
---|
6802 | static int |
---|
6803 | badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td) |
---|
6804 | { |
---|
6805 | |
---|
6806 | return (EBADF); |
---|
6807 | } |
---|
6808 | |
---|
6809 | static int |
---|
6810 | badfo_close(struct file *fp, struct thread *td) |
---|
6811 | { |
---|
6812 | |
---|
6813 | return (EBADF); |
---|
6814 | } |
---|
6815 | |
---|
6816 | struct fileops badfileops = { |
---|
6817 | .fo_read = badfo_readwrite, |
---|
6818 | .fo_write = badfo_readwrite, |
---|
6819 | .fo_truncate = badfo_truncate, |
---|
6820 | .fo_ioctl = badfo_ioctl, |
---|
6821 | .fo_poll = badfo_poll, |
---|
6822 | .fo_kqfilter = badfo_kqfilter, |
---|
6823 | .fo_stat = badfo_stat, |
---|
6824 | .fo_close = badfo_close, |
---|
6825 | }; |
---|
6826 | |
---|
6827 | #ifndef __rtems__ |
---|
6828 | /*-------------------------------------------------------------------*/ |
---|
6829 | |
---|
6830 | /* |
---|
6831 | * File Descriptor pseudo-device driver (/dev/fd/). |
---|
6832 | * |
---|
6833 | * Opening minor device N dup()s the file (if any) connected to file |
---|
6834 | * descriptor N belonging to the calling process. Note that this driver |
---|
6835 | * consists of only the ``open()'' routine, because all subsequent |
---|
6836 | * references to this file will be direct to the other driver. |
---|
6837 | * |
---|
6838 | * XXX: we could give this one a cloning event handler if necessary. |
---|
6839 | */ |
---|
6840 | |
---|
6841 | /* ARGSUSED */ |
---|
6842 | static int |
---|
6843 | fdopen(struct cdev *dev, int mode, int type, struct thread *td) |
---|
6844 | { |
---|
6845 | |
---|
6846 | /* |
---|
6847 | * XXX Kludge: set curthread->td_dupfd to contain the value of the |
---|
6848 | * the file descriptor being sought for duplication. The error |
---|
6849 | * return ensures that the vnode for this device will be released |
---|
6850 | * by vn_open. Open will detect this special error and take the |
---|
6851 | * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN |
---|
6852 | * will simply report the error. |
---|
6853 | */ |
---|
6854 | td->td_dupfd = dev2unit(dev); |
---|
6855 | return (ENODEV); |
---|
6856 | } |
---|
6857 | |
---|
6858 | static struct cdevsw fildesc_cdevsw = { |
---|
6859 | .d_version = D_VERSION, |
---|
6860 | .d_open = fdopen, |
---|
6861 | .d_name = "FD", |
---|
6862 | }; |
---|
6863 | |
---|
6864 | static void |
---|
6865 | fildesc_drvinit(void *unused) |
---|
6866 | { |
---|
6867 | struct cdev *dev; |
---|
6868 | |
---|
6869 | dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0"); |
---|
6870 | make_dev_alias(dev, "stdin"); |
---|
6871 | dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1"); |
---|
6872 | make_dev_alias(dev, "stdout"); |
---|
6873 | dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2"); |
---|
6874 | make_dev_alias(dev, "stderr"); |
---|
6875 | } |
---|
6876 | |
---|
6877 | SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL); |
---|
6878 | #endif /* __rtems__ */ |
---|
6879 | * XXX Kludge: set curthread->td_dupfd to contain the value of the |
---|
6880 | * the file descriptor being sought for duplication. The error |
---|
6881 | * return ensures that the vnode for this device will be released |
---|
6882 | * by vn_open. Open will detect this special error and take the |
---|
6883 | * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN |
---|
6884 | * will simply report the error. |
---|
6885 | */ |
---|
6886 | td->td_dupfd = dev2unit(dev); |
---|
6887 | return (ENODEV); |
---|
6888 | } |
---|
6889 | |
---|
6890 | static struct cdevsw fildesc_cdevsw = { |
---|
6891 | .d_version = D_VERSION, |
---|
6892 | .d_open = fdopen, |
---|
6893 | .d_name = "FD", |
---|
6894 | }; |
---|
6895 | |
---|
6896 | static void |
---|
6897 | fildesc_drvinit(void *unused) |
---|
6898 | { |
---|
6899 | struct cdev *dev; |
---|
6900 | |
---|
6901 | dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0"); |
---|
6902 | make_dev_alias(dev, "stdin"); |
---|
6903 | dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1"); |
---|
6904 | make_dev_alias(dev, "stdout"); |
---|
6905 | dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2"); |
---|
6906 | make_dev_alias(dev, "stderr"); |
---|
6907 | } |
---|
6908 | |
---|
6909 | SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL); |
---|
6910 | #endif /* __rtems__ */ |
---|