Deleted Added
full compact
kern_descrip.c (138836) kern_descrip.c (138838)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
35 */
36
37#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 138836 2004-12-14 08:44:51Z phk $");
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 138838 2004-12-14 09:09:51Z phk $");
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44
45#include <sys/conf.h>
46#include <sys/fcntl.h>

--- 1342 unchanged lines hidden (view full) ---

1389 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1390 if (newfdp->fd_fd.fd_jdir)
1391 VREF(newfdp->fd_fd.fd_jdir);
1392 FILEDESC_UNLOCK(fdp);
1393 }
1394
1395 /* Create the file descriptor table. */
1396 newfdp->fd_fd.fd_refcnt = 1;
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44
45#include <sys/conf.h>
46#include <sys/fcntl.h>

--- 1342 unchanged lines hidden (view full) ---

1389 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1390 if (newfdp->fd_fd.fd_jdir)
1391 VREF(newfdp->fd_fd.fd_jdir);
1392 FILEDESC_UNLOCK(fdp);
1393 }
1394
1395 /* Create the file descriptor table. */
1396 newfdp->fd_fd.fd_refcnt = 1;
1397 newfdp->fd_fd.fd_holdcnt = 1;
1397 newfdp->fd_fd.fd_cmask = CMASK;
1398 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1399 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1400 newfdp->fd_fd.fd_nfiles = NDFILE;
1401 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1402 return (&newfdp->fd_fd);
1403}
1404
1398 newfdp->fd_fd.fd_cmask = CMASK;
1399 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1400 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1401 newfdp->fd_fd.fd_nfiles = NDFILE;
1402 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1403 return (&newfdp->fd_fd);
1404}
1405
1406static struct filedesc *
1407fdhold(struct proc *p)
1408{
1409 struct filedesc *fdp;
1410
1411 mtx_lock(&fdesc_mtx);
1412 fdp = p->p_fd;
1413 if (fdp != NULL)
1414 fdp->fd_holdcnt++;
1415 mtx_unlock(&fdesc_mtx);
1416 return (fdp);
1417}
1418
1419static void
1420fddrop(struct filedesc *fdp)
1421{
1422 int i;
1423
1424 mtx_lock(&fdesc_mtx);
1425 i = --fdp->fd_holdcnt;
1426 mtx_unlock(&fdesc_mtx);
1427 if (i > 0)
1428 return;
1429
1430 mtx_destroy(&fdp->fd_mtx);
1431 FREE(fdp, M_FILEDESC);
1432}
1433
1405/*
1406 * Share a filedesc structure.
1407 */
1408struct filedesc *
1409fdshare(struct filedesc *fdp)
1410{
1411 FILEDESC_LOCK_FAST(fdp);
1412 fdp->fd_refcnt++;

--- 170 unchanged lines hidden (view full) ---

1583 FILEDESC_UNLOCK(fdp);
1584 return;
1585 }
1586
1587 /*
1588 * We are the last reference to the structure, so we can
1589 * safely assume it will not change out from under us.
1590 */
1434/*
1435 * Share a filedesc structure.
1436 */
1437struct filedesc *
1438fdshare(struct filedesc *fdp)
1439{
1440 FILEDESC_LOCK_FAST(fdp);
1441 fdp->fd_refcnt++;

--- 170 unchanged lines hidden (view full) ---

1612 FILEDESC_UNLOCK(fdp);
1613 return;
1614 }
1615
1616 /*
1617 * We are the last reference to the structure, so we can
1618 * safely assume it will not change out from under us.
1619 */
1591 FILEDESC_UNLOCK(fdp);
1592 fpp = fdp->fd_ofiles;
1593 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1594 if (*fpp)
1595 (void) closef(*fpp, td);
1596 }
1597
1598 /* XXX This should happen earlier. */
1599 mtx_lock(&fdesc_mtx);
1600 td->td_proc->p_fd = NULL;
1601 mtx_unlock(&fdesc_mtx);
1602
1603 if (fdp->fd_nfiles > NDFILE)
1604 FREE(fdp->fd_ofiles, M_FILEDESC);
1605 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1606 FREE(fdp->fd_map, M_FILEDESC);
1620 fpp = fdp->fd_ofiles;
1621 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1622 if (*fpp)
1623 (void) closef(*fpp, td);
1624 }
1625
1626 /* XXX This should happen earlier. */
1627 mtx_lock(&fdesc_mtx);
1628 td->td_proc->p_fd = NULL;
1629 mtx_unlock(&fdesc_mtx);
1630
1631 if (fdp->fd_nfiles > NDFILE)
1632 FREE(fdp->fd_ofiles, M_FILEDESC);
1633 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1634 FREE(fdp->fd_map, M_FILEDESC);
1635
1636 fdp->fd_nfiles = 0;
1637
1607 if (fdp->fd_cdir)
1608 vrele(fdp->fd_cdir);
1638 if (fdp->fd_cdir)
1639 vrele(fdp->fd_cdir);
1640 fdp->fd_cdir = NULL;
1609 if (fdp->fd_rdir)
1610 vrele(fdp->fd_rdir);
1641 if (fdp->fd_rdir)
1642 vrele(fdp->fd_rdir);
1643 fdp->fd_rdir = NULL;
1611 if (fdp->fd_jdir)
1612 vrele(fdp->fd_jdir);
1644 if (fdp->fd_jdir)
1645 vrele(fdp->fd_jdir);
1613 mtx_destroy(&fdp->fd_mtx);
1614 FREE(fdp, M_FILEDESC);
1646 fdp->fd_jdir = NULL;
1647
1648 FILEDESC_UNLOCK(fdp);
1649
1650 fddrop(fdp);
1615}
1616
1617/*
1618 * For setugid programs, we don't want to people to use that setugidness
1619 * to generate error messages which write to a file which otherwise would
1620 * otherwise be off-limits to the process. We check for filesystems where
1621 * the vnode can change out from under us after execve (like [lin]procfs).
1622 *

--- 634 unchanged lines hidden (view full) ---

2257}
2258
2259/*
2260 * Scan all active processes to see if any of them have a current
2261 * or root directory of `olddp'. If so, replace them with the new
2262 * mount point.
2263 */
2264void
1651}
1652
1653/*
1654 * For setugid programs, we don't want to people to use that setugidness
1655 * to generate error messages which write to a file which otherwise would
1656 * otherwise be off-limits to the process. We check for filesystems where
1657 * the vnode can change out from under us after execve (like [lin]procfs).
1658 *

--- 634 unchanged lines hidden (view full) ---

2293}
2294
2295/*
2296 * Scan all active processes to see if any of them have a current
2297 * or root directory of `olddp'. If so, replace them with the new
2298 * mount point.
2299 */
2300void
2265mountcheckdirs(olddp, newdp)
2266 struct vnode *olddp, *newdp;
2301mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2267{
2268 struct filedesc *fdp;
2269 struct proc *p;
2270 int nrele;
2271
2272 if (vrefcnt(olddp) == 1)
2273 return;
2274 sx_slock(&allproc_lock);
2275 LIST_FOREACH(p, &allproc, p_list) {
2302{
2303 struct filedesc *fdp;
2304 struct proc *p;
2305 int nrele;
2306
2307 if (vrefcnt(olddp) == 1)
2308 return;
2309 sx_slock(&allproc_lock);
2310 LIST_FOREACH(p, &allproc, p_list) {
2276 mtx_lock(&fdesc_mtx);
2277 fdp = p->p_fd;
2278 if (fdp == NULL) {
2279 mtx_unlock(&fdesc_mtx);
2311 fdp = fdhold(p);
2312 if (fdp == NULL)
2280 continue;
2313 continue;
2281 }
2282 nrele = 0;
2283 FILEDESC_LOCK_FAST(fdp);
2284 if (fdp->fd_cdir == olddp) {
2285 vref(newdp);
2286 fdp->fd_cdir = newdp;
2287 nrele++;
2288 }
2289 if (fdp->fd_rdir == olddp) {
2290 vref(newdp);
2291 fdp->fd_rdir = newdp;
2292 nrele++;
2293 }
2294 FILEDESC_UNLOCK_FAST(fdp);
2314 nrele = 0;
2315 FILEDESC_LOCK_FAST(fdp);
2316 if (fdp->fd_cdir == olddp) {
2317 vref(newdp);
2318 fdp->fd_cdir = newdp;
2319 nrele++;
2320 }
2321 if (fdp->fd_rdir == olddp) {
2322 vref(newdp);
2323 fdp->fd_rdir = newdp;
2324 nrele++;
2325 }
2326 FILEDESC_UNLOCK_FAST(fdp);
2295 mtx_unlock(&fdesc_mtx);
2296 while (nrele--)
2297 vrele(olddp);
2298 }
2299 sx_sunlock(&allproc_lock);
2300 if (rootvnode == olddp) {
2301 vrele(rootvnode);
2302 vref(newdp);
2303 rootvnode = newdp;

--- 74 unchanged lines hidden (view full) ---

2378 PROC_LOCK(p);
2379 if (p_cansee(req->td, p) != 0) {
2380 PROC_UNLOCK(p);
2381 continue;
2382 }
2383 xf.xf_pid = p->p_pid;
2384 xf.xf_uid = p->p_ucred->cr_uid;
2385 PROC_UNLOCK(p);
2327 while (nrele--)
2328 vrele(olddp);
2329 }
2330 sx_sunlock(&allproc_lock);
2331 if (rootvnode == olddp) {
2332 vrele(rootvnode);
2333 vref(newdp);
2334 rootvnode = newdp;

--- 74 unchanged lines hidden (view full) ---

2409 PROC_LOCK(p);
2410 if (p_cansee(req->td, p) != 0) {
2411 PROC_UNLOCK(p);
2412 continue;
2413 }
2414 xf.xf_pid = p->p_pid;
2415 xf.xf_uid = p->p_ucred->cr_uid;
2416 PROC_UNLOCK(p);
2386 mtx_lock(&fdesc_mtx);
2387 if ((fdp = p->p_fd) == NULL) {
2388 mtx_unlock(&fdesc_mtx);
2417 fdp = fdhold(p);
2418 if (fdp == NULL)
2389 continue;
2419 continue;
2390 }
2391 FILEDESC_LOCK_FAST(fdp);
2392 for (n = 0; n < fdp->fd_nfiles; ++n) {
2393 if ((fp = fdp->fd_ofiles[n]) == NULL)
2394 continue;
2395 xf.xf_fd = n;
2396 xf.xf_file = fp;
2397 xf.xf_data = fp->f_data;
2398 xf.xf_vnode = fp->f_vnode;
2399 xf.xf_type = fp->f_type;
2400 xf.xf_count = fp->f_count;
2401 xf.xf_msgcount = fp->f_msgcount;
2402 xf.xf_offset = fp->f_offset;
2403 xf.xf_flag = fp->f_flag;
2404 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2405 if (error)
2406 break;
2407 }
2408 FILEDESC_UNLOCK_FAST(fdp);
2420 FILEDESC_LOCK_FAST(fdp);
2421 for (n = 0; n < fdp->fd_nfiles; ++n) {
2422 if ((fp = fdp->fd_ofiles[n]) == NULL)
2423 continue;
2424 xf.xf_fd = n;
2425 xf.xf_file = fp;
2426 xf.xf_data = fp->f_data;
2427 xf.xf_vnode = fp->f_vnode;
2428 xf.xf_type = fp->f_type;
2429 xf.xf_count = fp->f_count;
2430 xf.xf_msgcount = fp->f_msgcount;
2431 xf.xf_offset = fp->f_offset;
2432 xf.xf_flag = fp->f_flag;
2433 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2434 if (error)
2435 break;
2436 }
2437 FILEDESC_UNLOCK_FAST(fdp);
2409 mtx_unlock(&fdesc_mtx);
2438 fddrop(fdp);
2410 if (error)
2411 break;
2412 }
2413 sx_sunlock(&allproc_lock);
2414 return (error);
2415}
2416
2417SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,

--- 130 unchanged lines hidden ---
2439 if (error)
2440 break;
2441 }
2442 sx_sunlock(&allproc_lock);
2443 return (error);
2444}
2445
2446SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,

--- 130 unchanged lines hidden ---