Deleted Added
full compact
sys_pipe.c (133049) sys_pipe.c (133741)
1/*
2 * Copyright (c) 1996 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

66 * locking mechanism.
67 *
68 * As pipelock() may have to sleep before it can acquire the flag, it
69 * is important to reread all data after a call to pipelock(); everything
70 * in the structure may have changed.
71 */
72
73#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1996 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

66 * locking mechanism.
67 *
68 * As pipelock() may have to sleep before it can acquire the flag, it
69 * is important to reread all data after a call to pipelock(); everything
70 * in the structure may have changed.
71 */
72
73#include <sys/cdefs.h>
74__FBSDID("$FreeBSD: head/sys/kern/sys_pipe.c 133049 2004-08-03 02:59:15Z silby $");
74__FBSDID("$FreeBSD: head/sys/kern/sys_pipe.c 133741 2004-08-15 06:24:42Z jmg $");
75
76#include "opt_mac.h"
77
78#include <sys/param.h>
79#include <sys/systm.h>
80#include <sys/fcntl.h>
81#include <sys/file.h>
82#include <sys/filedesc.h>

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

499
500 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
501 if (cpipe->pipe_state & PIPE_SEL) {
502 cpipe->pipe_state &= ~PIPE_SEL;
503 selwakeuppri(&cpipe->pipe_sel, PSOCK);
504 }
505 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
506 pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
75
76#include "opt_mac.h"
77
78#include <sys/param.h>
79#include <sys/systm.h>
80#include <sys/fcntl.h>
81#include <sys/file.h>
82#include <sys/filedesc.h>

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

499
500 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
501 if (cpipe->pipe_state & PIPE_SEL) {
502 cpipe->pipe_state &= ~PIPE_SEL;
503 selwakeuppri(&cpipe->pipe_sel, PSOCK);
504 }
505 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
506 pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
507 KNOTE(&cpipe->pipe_sel.si_note, 0);
507 KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0);
508}
509
510/*
511 * Initialize and allocate VM and memory for pipe. The structure
512 * will start out zero'd from the ctor, so we just manage the kmem.
513 */
514static int
515pipe_create(pipe)
516 struct pipe *pipe;
517{
518 int error;
519
520 /*
521 * Reduce to 1/4th pipe size if we're over our global max.
522 */
523 if (amountpipekva > maxpipekva / 2)
524 error = pipespace_new(pipe, SMALL_PIPE_SIZE);
525 else
526 error = pipespace_new(pipe, PIPE_SIZE);
508}
509
510/*
511 * Initialize and allocate VM and memory for pipe. The structure
512 * will start out zero'd from the ctor, so we just manage the kmem.
513 */
514static int
515pipe_create(pipe)
516 struct pipe *pipe;
517{
518 int error;
519
520 /*
521 * Reduce to 1/4th pipe size if we're over our global max.
522 */
523 if (amountpipekva > maxpipekva / 2)
524 error = pipespace_new(pipe, SMALL_PIPE_SIZE);
525 else
526 error = pipespace_new(pipe, PIPE_SIZE);
527 knlist_init(&pipe->pipe_sel.si_note, PIPE_MTX(pipe));
527 return (error);
528}
529
530/* ARGSUSED */
531static int
532pipe_read(fp, uio, active_cred, flags, td)
533 struct file *fp;
534 struct uio *uio;

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

1419 * Disconnect from peer, if any.
1420 */
1421 ppipe = cpipe->pipe_peer;
1422 if (ppipe->pipe_present != 0) {
1423 pipeselwakeup(ppipe);
1424
1425 ppipe->pipe_state |= PIPE_EOF;
1426 wakeup(ppipe);
528 return (error);
529}
530
531/* ARGSUSED */
532static int
533pipe_read(fp, uio, active_cred, flags, td)
534 struct file *fp;
535 struct uio *uio;

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

1420 * Disconnect from peer, if any.
1421 */
1422 ppipe = cpipe->pipe_peer;
1423 if (ppipe->pipe_present != 0) {
1424 pipeselwakeup(ppipe);
1425
1426 ppipe->pipe_state |= PIPE_EOF;
1427 wakeup(ppipe);
1427 KNOTE(&ppipe->pipe_sel.si_note, 0);
1428 KNOTE_LOCKED(&ppipe->pipe_sel.si_note, 0);
1428 }
1429
1430 /*
1431 * Mark this endpoint as free. Release kmem resources. We
1432 * don't mark this endpoint as unused until we've finished
1433 * doing that, or the pipe might disappear out from under
1434 * us.
1435 */
1436 PIPE_UNLOCK(cpipe);
1437 pipe_free_kmem(cpipe);
1438 PIPE_LOCK(cpipe);
1439 cpipe->pipe_present = 0;
1440 pipeunlock(cpipe);
1429 }
1430
1431 /*
1432 * Mark this endpoint as free. Release kmem resources. We
1433 * don't mark this endpoint as unused until we've finished
1434 * doing that, or the pipe might disappear out from under
1435 * us.
1436 */
1437 PIPE_UNLOCK(cpipe);
1438 pipe_free_kmem(cpipe);
1439 PIPE_LOCK(cpipe);
1440 cpipe->pipe_present = 0;
1441 pipeunlock(cpipe);
1442 knlist_clear(&cpipe->pipe_sel.si_note, 1);
1443 knlist_destroy(&cpipe->pipe_sel.si_note);
1441
1442 /*
1443 * If both endpoints are now closed, release the memory for the
1444 * pipe pair. If not, unlock.
1445 */
1446 if (ppipe->pipe_present == 0) {
1447 PIPE_UNLOCK(cpipe);
1448#ifdef MAC

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

1471 /* other end of pipe has been closed */
1472 PIPE_UNLOCK(cpipe);
1473 return (EPIPE);
1474 }
1475 cpipe = cpipe->pipe_peer;
1476 break;
1477 default:
1478 PIPE_UNLOCK(cpipe);
1444
1445 /*
1446 * If both endpoints are now closed, release the memory for the
1447 * pipe pair. If not, unlock.
1448 */
1449 if (ppipe->pipe_present == 0) {
1450 PIPE_UNLOCK(cpipe);
1451#ifdef MAC

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

1474 /* other end of pipe has been closed */
1475 PIPE_UNLOCK(cpipe);
1476 return (EPIPE);
1477 }
1478 cpipe = cpipe->pipe_peer;
1479 break;
1480 default:
1481 PIPE_UNLOCK(cpipe);
1479 return (1);
1482 return (EINVAL);
1480 }
1481
1483 }
1484
1482 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1485 knlist_add(&cpipe->pipe_sel.si_note, kn, 1);
1483 PIPE_UNLOCK(cpipe);
1484 return (0);
1485}
1486
1487static void
1488filt_pipedetach(struct knote *kn)
1489{
1490 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1491
1492 PIPE_LOCK(cpipe);
1493 if (kn->kn_filter == EVFILT_WRITE) {
1494 if (!cpipe->pipe_peer->pipe_present) {
1495 PIPE_UNLOCK(cpipe);
1496 return;
1497 }
1498 cpipe = cpipe->pipe_peer;
1499 }
1486 PIPE_UNLOCK(cpipe);
1487 return (0);
1488}
1489
1490static void
1491filt_pipedetach(struct knote *kn)
1492{
1493 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1494
1495 PIPE_LOCK(cpipe);
1496 if (kn->kn_filter == EVFILT_WRITE) {
1497 if (!cpipe->pipe_peer->pipe_present) {
1498 PIPE_UNLOCK(cpipe);
1499 return;
1500 }
1501 cpipe = cpipe->pipe_peer;
1502 }
1500 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1503 knlist_remove(&cpipe->pipe_sel.si_note, kn, 1);
1501 PIPE_UNLOCK(cpipe);
1502}
1503
1504/*ARGSUSED*/
1505static int
1506filt_piperead(struct knote *kn, long hint)
1507{
1508 struct pipe *rpipe = kn->kn_fp->f_data;
1509 struct pipe *wpipe = rpipe->pipe_peer;
1504 PIPE_UNLOCK(cpipe);
1505}
1506
1507/*ARGSUSED*/
1508static int
1509filt_piperead(struct knote *kn, long hint)
1510{
1511 struct pipe *rpipe = kn->kn_fp->f_data;
1512 struct pipe *wpipe = rpipe->pipe_peer;
1513 int ret;
1510
1511 PIPE_LOCK(rpipe);
1512 kn->kn_data = rpipe->pipe_buffer.cnt;
1513 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1514 kn->kn_data = rpipe->pipe_map.cnt;
1515
1516 if ((rpipe->pipe_state & PIPE_EOF) ||
1517 (!wpipe->pipe_present) || (wpipe->pipe_state & PIPE_EOF)) {
1518 kn->kn_flags |= EV_EOF;
1519 PIPE_UNLOCK(rpipe);
1520 return (1);
1521 }
1514
1515 PIPE_LOCK(rpipe);
1516 kn->kn_data = rpipe->pipe_buffer.cnt;
1517 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1518 kn->kn_data = rpipe->pipe_map.cnt;
1519
1520 if ((rpipe->pipe_state & PIPE_EOF) ||
1521 (!wpipe->pipe_present) || (wpipe->pipe_state & PIPE_EOF)) {
1522 kn->kn_flags |= EV_EOF;
1523 PIPE_UNLOCK(rpipe);
1524 return (1);
1525 }
1526 ret = kn->kn_data > 0;
1522 PIPE_UNLOCK(rpipe);
1527 PIPE_UNLOCK(rpipe);
1523 return (kn->kn_data > 0);
1528 return ret;
1524}
1525
1526/*ARGSUSED*/
1527static int
1528filt_pipewrite(struct knote *kn, long hint)
1529{
1530 struct pipe *rpipe = kn->kn_fp->f_data;
1531 struct pipe *wpipe = rpipe->pipe_peer;

--- 15 unchanged lines hidden ---
1529}
1530
1531/*ARGSUSED*/
1532static int
1533filt_pipewrite(struct knote *kn, long hint)
1534{
1535 struct pipe *rpipe = kn->kn_fp->f_data;
1536 struct pipe *wpipe = rpipe->pipe_peer;

--- 15 unchanged lines hidden ---