sys_pipe.c (cd72f2180bfff020d03180e6eba1c3a0e0125468) sys_pipe.c (48e3128b34dad9618402f1f4095f7655e779843c)
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

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

233 * Warning: once we've gotten past allocation of the fd for the
234 * read-side, we can only drop the read side via fdrop() in order
235 * to avoid races against processes which manage to dup() the read
236 * side while we are blocked trying to allocate the write side.
237 */
238 FILE_LOCK(rf);
239 rf->f_flag = FREAD | FWRITE;
240 rf->f_type = DTYPE_PIPE;
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

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

233 * Warning: once we've gotten past allocation of the fd for the
234 * read-side, we can only drop the read side via fdrop() in order
235 * to avoid races against processes which manage to dup() the read
236 * side while we are blocked trying to allocate the write side.
237 */
238 FILE_LOCK(rf);
239 rf->f_flag = FREAD | FWRITE;
240 rf->f_type = DTYPE_PIPE;
241 rf->un_data.pipe = rpipe;
241 rf->f_data = rpipe;
242 rf->f_ops = &pipeops;
243 FILE_UNLOCK(rf);
244 error = falloc(td, &wf, &fd);
245 if (error) {
246 FILEDESC_LOCK(fdp);
247 if (fdp->fd_ofiles[td->td_retval[0]] == rf) {
248 fdp->fd_ofiles[td->td_retval[0]] = NULL;
249 FILEDESC_UNLOCK(fdp);

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

254 /* rpipe has been closed by fdrop(). */
255 pipeclose(wpipe);
256 free(pmtx, M_TEMP);
257 return (error);
258 }
259 FILE_LOCK(wf);
260 wf->f_flag = FREAD | FWRITE;
261 wf->f_type = DTYPE_PIPE;
242 rf->f_ops = &pipeops;
243 FILE_UNLOCK(rf);
244 error = falloc(td, &wf, &fd);
245 if (error) {
246 FILEDESC_LOCK(fdp);
247 if (fdp->fd_ofiles[td->td_retval[0]] == rf) {
248 fdp->fd_ofiles[td->td_retval[0]] = NULL;
249 FILEDESC_UNLOCK(fdp);

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

254 /* rpipe has been closed by fdrop(). */
255 pipeclose(wpipe);
256 free(pmtx, M_TEMP);
257 return (error);
258 }
259 FILE_LOCK(wf);
260 wf->f_flag = FREAD | FWRITE;
261 wf->f_type = DTYPE_PIPE;
262 wf->un_data.pipe = wpipe;
262 wf->f_data = wpipe;
263 wf->f_ops = &pipeops;
264 FILE_UNLOCK(wf);
265 td->td_retval[1] = fd;
266 rpipe->pipe_peer = wpipe;
267 wpipe->pipe_peer = rpipe;
268#ifdef MAC
269 /*
270 * struct pipe represents a pipe endpoint. The MAC label is shared

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

447static int
448pipe_read(fp, uio, active_cred, flags, td)
449 struct file *fp;
450 struct uio *uio;
451 struct ucred *active_cred;
452 struct thread *td;
453 int flags;
454{
263 wf->f_ops = &pipeops;
264 FILE_UNLOCK(wf);
265 td->td_retval[1] = fd;
266 rpipe->pipe_peer = wpipe;
267 wpipe->pipe_peer = rpipe;
268#ifdef MAC
269 /*
270 * struct pipe represents a pipe endpoint. The MAC label is shared

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

447static int
448pipe_read(fp, uio, active_cred, flags, td)
449 struct file *fp;
450 struct uio *uio;
451 struct ucred *active_cred;
452 struct thread *td;
453 int flags;
454{
455 struct pipe *rpipe = fp->un_data.pipe;
455 struct pipe *rpipe = fp->f_data;
456 int error;
457 int nread = 0;
458 u_int size;
459
460 PIPE_LOCK(rpipe);
461 ++rpipe->pipe_busy;
462 error = pipelock(rpipe, 1);
463 if (error)

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

863 struct ucred *active_cred;
864 struct thread *td;
865 int flags;
866{
867 int error = 0;
868 int orig_resid;
869 struct pipe *wpipe, *rpipe;
870
456 int error;
457 int nread = 0;
458 u_int size;
459
460 PIPE_LOCK(rpipe);
461 ++rpipe->pipe_busy;
462 error = pipelock(rpipe, 1);
463 if (error)

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

863 struct ucred *active_cred;
864 struct thread *td;
865 int flags;
866{
867 int error = 0;
868 int orig_resid;
869 struct pipe *wpipe, *rpipe;
870
871 rpipe = fp->un_data.pipe;
871 rpipe = fp->f_data;
872 wpipe = rpipe->pipe_peer;
873
874 PIPE_LOCK(rpipe);
875 /*
876 * detect loss of pipe read side, issue SIGPIPE if lost.
877 */
878 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
879 PIPE_UNLOCK(rpipe);

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

1150static int
1151pipe_ioctl(fp, cmd, data, active_cred, td)
1152 struct file *fp;
1153 u_long cmd;
1154 void *data;
1155 struct ucred *active_cred;
1156 struct thread *td;
1157{
872 wpipe = rpipe->pipe_peer;
873
874 PIPE_LOCK(rpipe);
875 /*
876 * detect loss of pipe read side, issue SIGPIPE if lost.
877 */
878 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
879 PIPE_UNLOCK(rpipe);

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

1150static int
1151pipe_ioctl(fp, cmd, data, active_cred, td)
1152 struct file *fp;
1153 u_long cmd;
1154 void *data;
1155 struct ucred *active_cred;
1156 struct thread *td;
1157{
1158 struct pipe *mpipe = fp->un_data.pipe;
1158 struct pipe *mpipe = fp->f_data;
1159#ifdef MAC
1160 int error;
1161#endif
1162
1163 PIPE_LOCK(mpipe);
1164
1165#ifdef MAC
1166 error = mac_check_pipe_ioctl(active_cred, mpipe, cmd, data);

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

1218
1219static int
1220pipe_poll(fp, events, active_cred, td)
1221 struct file *fp;
1222 int events;
1223 struct ucred *active_cred;
1224 struct thread *td;
1225{
1159#ifdef MAC
1160 int error;
1161#endif
1162
1163 PIPE_LOCK(mpipe);
1164
1165#ifdef MAC
1166 error = mac_check_pipe_ioctl(active_cred, mpipe, cmd, data);

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

1218
1219static int
1220pipe_poll(fp, events, active_cred, td)
1221 struct file *fp;
1222 int events;
1223 struct ucred *active_cred;
1224 struct thread *td;
1225{
1226 struct pipe *rpipe = fp->un_data.pipe;
1226 struct pipe *rpipe = fp->f_data;
1227 struct pipe *wpipe;
1228 int revents = 0;
1229#ifdef MAC
1230 int error;
1231#endif
1232
1233 wpipe = rpipe->pipe_peer;
1234 PIPE_LOCK(rpipe);

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

1279 */
1280static int
1281pipe_stat(fp, ub, active_cred, td)
1282 struct file *fp;
1283 struct stat *ub;
1284 struct ucred *active_cred;
1285 struct thread *td;
1286{
1227 struct pipe *wpipe;
1228 int revents = 0;
1229#ifdef MAC
1230 int error;
1231#endif
1232
1233 wpipe = rpipe->pipe_peer;
1234 PIPE_LOCK(rpipe);

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

1279 */
1280static int
1281pipe_stat(fp, ub, active_cred, td)
1282 struct file *fp;
1283 struct stat *ub;
1284 struct ucred *active_cred;
1285 struct thread *td;
1286{
1287 struct pipe *pipe = fp->un_data.pipe;
1287 struct pipe *pipe = fp->f_data;
1288#ifdef MAC
1289 int error;
1290
1291 PIPE_LOCK(pipe);
1292 error = mac_check_pipe_stat(active_cred, pipe);
1293 PIPE_UNLOCK(pipe);
1294 if (error)
1295 return (error);

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

1312}
1313
1314/* ARGSUSED */
1315static int
1316pipe_close(fp, td)
1317 struct file *fp;
1318 struct thread *td;
1319{
1288#ifdef MAC
1289 int error;
1290
1291 PIPE_LOCK(pipe);
1292 error = mac_check_pipe_stat(active_cred, pipe);
1293 PIPE_UNLOCK(pipe);
1294 if (error)
1295 return (error);

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

1312}
1313
1314/* ARGSUSED */
1315static int
1316pipe_close(fp, td)
1317 struct file *fp;
1318 struct thread *td;
1319{
1320 struct pipe *cpipe = fp->un_data.pipe;
1320 struct pipe *cpipe = fp->f_data;
1321
1322 fp->f_ops = &badfileops;
1321
1322 fp->f_ops = &badfileops;
1323 fp->un_data.pipe = NULL;
1323 fp->f_data = NULL;
1324 funsetown(&cpipe->pipe_sigio);
1325 pipeclose(cpipe);
1326 return (0);
1327}
1328
1329static void
1330pipe_free_kmem(cpipe)
1331 struct pipe *cpipe;

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

1423}
1424
1425/*ARGSUSED*/
1426static int
1427pipe_kqfilter(struct file *fp, struct knote *kn)
1428{
1429 struct pipe *cpipe;
1430
1324 funsetown(&cpipe->pipe_sigio);
1325 pipeclose(cpipe);
1326 return (0);
1327}
1328
1329static void
1330pipe_free_kmem(cpipe)
1331 struct pipe *cpipe;

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

1423}
1424
1425/*ARGSUSED*/
1426static int
1427pipe_kqfilter(struct file *fp, struct knote *kn)
1428{
1429 struct pipe *cpipe;
1430
1431 cpipe = kn->kn_fp->un_data.pipe;
1431 cpipe = kn->kn_fp->f_data;
1432 switch (kn->kn_filter) {
1433 case EVFILT_READ:
1434 kn->kn_fop = &pipe_rfiltops;
1435 break;
1436 case EVFILT_WRITE:
1437 kn->kn_fop = &pipe_wfiltops;
1438 cpipe = cpipe->pipe_peer;
1439 if (cpipe == NULL)

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

1460 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1461 PIPE_UNLOCK(cpipe);
1462}
1463
1464/*ARGSUSED*/
1465static int
1466filt_piperead(struct knote *kn, long hint)
1467{
1432 switch (kn->kn_filter) {
1433 case EVFILT_READ:
1434 kn->kn_fop = &pipe_rfiltops;
1435 break;
1436 case EVFILT_WRITE:
1437 kn->kn_fop = &pipe_wfiltops;
1438 cpipe = cpipe->pipe_peer;
1439 if (cpipe == NULL)

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

1460 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1461 PIPE_UNLOCK(cpipe);
1462}
1463
1464/*ARGSUSED*/
1465static int
1466filt_piperead(struct knote *kn, long hint)
1467{
1468 struct pipe *rpipe = kn->kn_fp->un_data.pipe;
1468 struct pipe *rpipe = kn->kn_fp->f_data;
1469 struct pipe *wpipe = rpipe->pipe_peer;
1470
1471 PIPE_LOCK(rpipe);
1472 kn->kn_data = rpipe->pipe_buffer.cnt;
1473 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1474 kn->kn_data = rpipe->pipe_map.cnt;
1475
1476 if ((rpipe->pipe_state & PIPE_EOF) ||

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

1482 PIPE_UNLOCK(rpipe);
1483 return (kn->kn_data > 0);
1484}
1485
1486/*ARGSUSED*/
1487static int
1488filt_pipewrite(struct knote *kn, long hint)
1489{
1469 struct pipe *wpipe = rpipe->pipe_peer;
1470
1471 PIPE_LOCK(rpipe);
1472 kn->kn_data = rpipe->pipe_buffer.cnt;
1473 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1474 kn->kn_data = rpipe->pipe_map.cnt;
1475
1476 if ((rpipe->pipe_state & PIPE_EOF) ||

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

1482 PIPE_UNLOCK(rpipe);
1483 return (kn->kn_data > 0);
1484}
1485
1486/*ARGSUSED*/
1487static int
1488filt_pipewrite(struct knote *kn, long hint)
1489{
1490 struct pipe *rpipe = kn->kn_fp->un_data.pipe;
1490 struct pipe *rpipe = kn->kn_fp->f_data;
1491 struct pipe *wpipe = rpipe->pipe_peer;
1492
1493 PIPE_LOCK(rpipe);
1494 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1495 kn->kn_data = 0;
1496 kn->kn_flags |= EV_EOF;
1497 PIPE_UNLOCK(rpipe);
1498 return (1);
1499 }
1500 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1501 if (wpipe->pipe_state & PIPE_DIRECTW)
1502 kn->kn_data = 0;
1503
1504 PIPE_UNLOCK(rpipe);
1505 return (kn->kn_data >= PIPE_BUF);
1506}
1491 struct pipe *wpipe = rpipe->pipe_peer;
1492
1493 PIPE_LOCK(rpipe);
1494 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1495 kn->kn_data = 0;
1496 kn->kn_flags |= EV_EOF;
1497 PIPE_UNLOCK(rpipe);
1498 return (1);
1499 }
1500 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1501 if (wpipe->pipe_state & PIPE_DIRECTW)
1502 kn->kn_data = 0;
1503
1504 PIPE_UNLOCK(rpipe);
1505 return (kn->kn_data >= PIPE_BUF);
1506}