home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / pipe.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  4KB  |  140 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: pipe.c,v 1.4 1994/06/19 15:14:19 rluebbert Exp $
  21.  *
  22.  *  $Log: pipe.c,v $
  23.  *  Revision 1.4  1994/06/19  15:14:19  rluebbert
  24.  *  *** empty log message ***
  25.  *
  26.  *  Revision 1.2  1992/07/04  19:21:08  mwild
  27.  *  (finally..) fix the bug which could cause pipe readers/writers to deadlock
  28.  *
  29.  * Revision 1.1  1992/05/14  19:55:40  mwild
  30.  * Initial revision
  31.  *
  32.  */
  33.  
  34. #define _KERNEL
  35. #include "ixemul.h"
  36. #include "kprintf.h"
  37.  
  38. #include <sys/ioctl.h>
  39. #include <string.h>
  40. #include "select.h"
  41. #include "unp.h"
  42.  
  43. /* information for the temporary implementation of pipes.
  44.    PIPE: has the big disadvantage that it blocks in the most unpleasent
  45.    situations, and doesn't send SIGPIPE to processes that write on
  46.    readerless pipes. Unacceptable for this library ;-)) */
  47.  
  48. static int __pclose();
  49.  
  50. int
  51. pipe (int pv[2])
  52. {
  53.   struct file *f1, *f2;
  54.   struct sock_stream *ss;
  55.   int res, err, omask;
  56.   
  57.   omask = syscall (SYS_sigsetmask, ~0);
  58.   res = -1; 
  59.   err = EMFILE;
  60.   if ((ss = init_stream()))
  61.     {
  62.       if (! falloc (&f1, pv))
  63.         {
  64.           if (! falloc (&f2, pv+1))
  65.             {
  66.           f1->f_ss = ss;
  67.           f1->f_stb.st_mode = 0666 | S_IFCHR;
  68.           f1->f_stb.st_size = UNIX_SOCKET_SIZE;
  69.           f1->f_stb.st_blksize = 512;
  70.           f1->f_flags  = FREAD;
  71.           f1->f_type   = DTYPE_PIPE;
  72.           f1->f_read   = stream_read;
  73.           f1->f_write  = 0;
  74.           f1->f_ioctl  = unp_ioctl;
  75.           f1->f_close  = __pclose;
  76.           f1->f_select = unp_select;
  77.  
  78.           f2->f_ss = ss;
  79.           f2->f_stb.st_mode = 0666 | S_IFCHR;
  80.           f2->f_stb.st_size = UNIX_SOCKET_SIZE;
  81.           f2->f_stb.st_blksize = 512;
  82.           f2->f_flags  = FWRITE;
  83.           f2->f_type   = DTYPE_PIPE;
  84.           f2->f_read   = 0;
  85.           f2->f_write  = stream_write;
  86.           f2->f_ioctl  = unp_ioctl;
  87.           f2->f_close  = __pclose;
  88.           f2->f_select = unp_select;
  89.  
  90.           res = err =0;
  91.           goto ret;
  92.         }
  93.       f1->f_count = 0;
  94.     }
  95.  
  96.       kfree (ss);
  97.     }
  98.  
  99. ret:
  100.   syscall (SYS_sigsetmask, omask);
  101.  
  102.   errno = err;
  103.   KPRINTF_DISABLED (("&errno = %lx, errno = %ld\n", &errno, errno));
  104.   return res;
  105. }
  106.  
  107. static int
  108. __pclose (struct file *f)
  109. {
  110.   struct sock_stream *ss = f->f_ss;
  111.  
  112.   ix_lock_base ();
  113.  
  114.   f->f_count--;
  115.  
  116.   if (f->f_count == 0)
  117.     {
  118.       if (f->f_read)
  119.         ss->flags |= UNF_NO_READER;
  120.       else
  121.     ss->flags |= UNF_NO_WRITER;
  122.     
  123.       if ((ss->flags & (UNF_NO_READER|UNF_NO_WRITER)) ==
  124.                  (UNF_NO_READER|UNF_NO_WRITER))
  125.     kfree (ss);
  126.       else
  127.         {
  128.           Forbid();
  129.           if (ss->task)
  130.             Signal(ss->task, 1UL << u.u_pipe_sig);
  131.           Permit();
  132.           ix_wakeup ((u_int)ss);
  133.         }
  134.     }
  135.  
  136.   ix_unlock_base ();
  137.  
  138.   return 0;
  139. }
  140.