home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mint104s.zoo / mint.src / pipefs.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  26KB  |  1,061 lines

  1. /*
  2. Copyright 1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5.  */
  6.  
  7. /* simple pipefs.c */
  8.  
  9. #include "mint.h"
  10.  
  11. static int pipetime, pipedate;    /* root directory time/date stamp */
  12.  
  13. static long    ARGS_ON_STACK pipe_root    P_((int drv, fcookie *fc));
  14. static long    ARGS_ON_STACK pipe_lookup    P_((fcookie *dir, const char *name, fcookie *fc));
  15. static long    ARGS_ON_STACK pipe_getxattr    P_((fcookie *file, XATTR *xattr));
  16. static long    ARGS_ON_STACK pipe_chattr    P_((fcookie *file, int attrib));
  17. static long    ARGS_ON_STACK pipe_chown    P_((fcookie *file, int uid, int gid));
  18. static long    ARGS_ON_STACK pipe_chmode    P_((fcookie *file, unsigned mode));
  19. static long    ARGS_ON_STACK pipe_rmdir    P_((fcookie *dir, const char *name));
  20. static long    ARGS_ON_STACK pipe_remove    P_((fcookie *dir, const char *name));
  21. static long    ARGS_ON_STACK pipe_getname    P_((fcookie *root, fcookie *dir,
  22.                             char *pathname, int size));
  23. static long    ARGS_ON_STACK pipe_rename    P_((fcookie *olddir, char *oldname,
  24.                     fcookie *newdir, const char *newname));
  25. static long    ARGS_ON_STACK pipe_opendir    P_((DIR *dirh, int flags));
  26. static long    ARGS_ON_STACK pipe_readdir    P_((DIR *dirh, char *nm, int nmlen, fcookie *));
  27. static long    ARGS_ON_STACK pipe_rewinddir    P_((DIR *dirh));
  28. static long    ARGS_ON_STACK pipe_closedir    P_((DIR *dirh));
  29. static long    ARGS_ON_STACK pipe_pathconf    P_((fcookie *dir, int which));
  30. static long    ARGS_ON_STACK pipe_dfree    P_((fcookie *dir, long *buf));
  31. static long    ARGS_ON_STACK pipe_creat    P_((fcookie *dir, const char *name, unsigned mode,
  32.                     int attrib, fcookie *fc));
  33. static DEVDRV *    ARGS_ON_STACK pipe_getdev    P_((fcookie *fc, long *devsp));
  34.  
  35. static long    ARGS_ON_STACK pipe_open    P_((FILEPTR *f));
  36. static long    ARGS_ON_STACK pipe_write    P_((FILEPTR *f, const char *buf, long bytes));
  37. static long    ARGS_ON_STACK pipe_read    P_((FILEPTR *f, char *buf, long bytes));
  38. static long    ARGS_ON_STACK pipe_lseek    P_((FILEPTR *f, long where, int whence));
  39. static long    ARGS_ON_STACK pipe_ioctl    P_((FILEPTR *f, int mode, void *buf));
  40. static long    ARGS_ON_STACK pipe_datime    P_((FILEPTR *f, short *time, int rwflag));
  41. static long    ARGS_ON_STACK pipe_close    P_((FILEPTR *f, int pid));
  42. static long    ARGS_ON_STACK pipe_select    P_((FILEPTR *f, long p, int mode));
  43. static void    ARGS_ON_STACK pipe_unselect    P_((FILEPTR *f, long p, int mode));
  44.  
  45. DEVDRV pipe_device = {
  46.     pipe_open, pipe_write, pipe_read, pipe_lseek, pipe_ioctl, pipe_datime,
  47.     pipe_close, pipe_select, pipe_unselect
  48. };
  49.  
  50. /* ptys and pipes can share the same driver, for now */
  51. #define pty_device pipe_device
  52.  
  53. FILESYS pipe_filesys = {
  54.     (FILESYS *)0,
  55.     0,
  56.     pipe_root,
  57.     pipe_lookup, pipe_creat, pipe_getdev, pipe_getxattr,
  58.     pipe_chattr, pipe_chown, pipe_chmode,
  59.     nomkdir, pipe_rmdir, pipe_remove, pipe_getname, pipe_rename,
  60.     pipe_opendir, pipe_readdir, pipe_rewinddir, pipe_closedir,
  61.     pipe_pathconf, pipe_dfree,
  62.     nowritelabel, noreadlabel, nosymlink, noreadlink,
  63.     nohardlink, nofscntl, nodskchng
  64. };
  65.  
  66. /* size of pipes */
  67. #define PIPESIZ    4096        /* MUST be a multiple of 4 */
  68.  
  69. /* writes smaller than this are atomic */
  70. #define PIPE_BUF 1024        /* should be a multiple of 4 */
  71.  
  72. /* magic flag: indicates that nobody but the creator has opened this pipe */
  73. /* note: if this many processes open the pipe, we lose :-( */
  74. #define VIRGIN_PIPE    0x7fff
  75.  
  76. struct pipe {
  77.     int    readers;    /* number of readers of this pipe */
  78.     int    writers;    /* number of writers of this pipe */
  79.     int    head, tail;    /* pipe head, tail (head == tail for empty) */
  80.     long    rsel;        /* process that did select() for reads */
  81.     long    wsel;        /* process that did select() for writes */
  82.     char    buf[PIPESIZ];    /* pipe data */
  83. };
  84.  
  85. struct fifo {
  86.     char    name[NAME_MAX+1]; /* FIFO's name */
  87.     short    date, time;    /* date & time of last write */
  88.     short    dosflags;    /* DOS flags, e.g. FA_RDONLY, FA_HIDDEN */
  89.     ushort    mode;        /* file access mode, for XATTR */
  90.     ushort    uid, gid;    /* file owner; uid and gid */
  91.     short    flags;        /* various other flags (e.g. O_TTY) */
  92.     short    lockpid;    /* pid of locking process */
  93.     short    cursrate;    /* cursor flash rate for pseudo TTY's */
  94.     struct tty *tty;    /* tty struct for pseudo TTY's */
  95.     struct pipe *inp;    /* pipe for reads */
  96.     struct pipe *outp;    /* pipe for writes (0 if unidirectional) */
  97.     struct fifo *next;    /* link to next FIFO in list */
  98.     FILEPTR *open;        /* open file pointers for this fifo */
  99. } *rootlist;
  100.  
  101.  
  102. static long ARGS_ON_STACK 
  103. pipe_root(drv, fc)
  104.     int drv;
  105.     fcookie *fc;
  106. {
  107.     if (drv == PIPEDRV) {
  108.         fc->fs = &pipe_filesys;
  109.         fc->dev = drv;
  110.         fc->index = 0L;
  111.         return 0;
  112.     }
  113.     fc->fs = 0;
  114.     return EINTRN;
  115. }
  116.  
  117. static long ARGS_ON_STACK 
  118. pipe_lookup(dir, name, fc)
  119.     fcookie *dir;
  120.     const char *name;
  121.     fcookie *fc;
  122. {
  123.     struct fifo *b;
  124.  
  125.     TRACE(("pipe_lookup(%s)", name));
  126.  
  127.     if (dir->index != 0) {
  128.         DEBUG(("pipe_lookup(%s): bad directory", name));
  129.         return EPTHNF;
  130.     }
  131. /* special case: an empty name in a directory means that directory */
  132. /* so does "." */
  133.     if (!*name || (name[0] == '.' && name[1] == 0)) {
  134.         *fc = *dir;
  135.         return 0;
  136.     }
  137.  
  138. /* another special case: ".." could be a mount point */
  139.     if (!strcmp(name, "..")) {
  140.         *fc = *dir;
  141.         return EMOUNT;
  142.     }
  143.  
  144.     for (b = rootlist; b; b = b->next) {
  145.         if (!strnicmp(b->name, name, NAME_MAX)) {
  146.             fc->fs = &pipe_filesys;
  147.             fc->index = (long)b;
  148.             fc->dev = dir->dev;
  149.             return 0;
  150.         }
  151.     }
  152.     DEBUG(("pipe_lookup: name `%s' not found", name));
  153.     return EFILNF;
  154. }
  155.  
  156. static long ARGS_ON_STACK 
  157. pipe_getxattr(fc, xattr)
  158.     fcookie *fc;
  159.     XATTR *xattr;
  160. {
  161.     struct fifo *this;
  162.  
  163.     xattr->index = fc->index;
  164.     xattr->dev = fc->dev;
  165.     xattr->nlink = 1;
  166.     xattr->blksize = 1;
  167.  
  168.     if (fc->index == 0) {        /* root directory? */
  169.         xattr->uid = xattr->gid = 0;
  170.         xattr->mtime = xattr->atime = xattr->ctime = pipetime;
  171.         xattr->mdate = xattr->adate = xattr->cdate = pipedate;
  172.         xattr->mode = S_IFDIR | DEFAULT_DIRMODE;
  173.         xattr->attr = FA_DIR;
  174.         xattr->size = xattr->nblocks = 0;
  175.     } else {
  176.         this = (struct fifo *)fc->index;
  177.         xattr->uid = this->uid;
  178.         xattr->gid = this->gid;
  179.         xattr->mtime = xattr->atime = xattr->ctime = this->time;
  180.         xattr->mdate = xattr->adate = xattr->cdate = this->date;
  181.         xattr->mode = this->mode;
  182.         xattr->attr = this->dosflags;
  183.     /* note: fifo's that haven't been opened yet can be written to */
  184.         if (this->flags & O_HEAD) {
  185.             xattr->attr &= ~FA_RDONLY;
  186.         }
  187.  
  188.         xattr->nblocks = PIPESIZ;
  189.         if (this->dosflags & FA_SYSTEM) {    /* pseudo-tty */
  190.             xattr->size = PIPESIZ/4;
  191.         } else {
  192.             xattr->size = PIPESIZ;
  193.         }
  194.     }
  195.     return 0;
  196. }
  197.  
  198. static long ARGS_ON_STACK 
  199. pipe_chattr(fc, attrib)
  200.     fcookie *fc;
  201.     int attrib;
  202. {
  203.     UNUSED(fc); UNUSED(attrib);
  204.     return EACCDN;
  205. }
  206.  
  207. static long ARGS_ON_STACK 
  208. pipe_chown(fc, uid, gid)
  209.     fcookie *fc;
  210.     int uid, gid;
  211. {
  212.     struct fifo *this;
  213.  
  214.     if ((this = (struct fifo *)fc->index) == 0)
  215.         return EACCDN;
  216.  
  217.     this->uid = uid;
  218.     this->gid = gid;
  219.     return 0;
  220. }
  221.  
  222. static long ARGS_ON_STACK 
  223. pipe_chmode(fc, mode)
  224.     fcookie *fc;
  225.     unsigned mode;
  226. {
  227.     struct fifo *this;
  228.  
  229.     if ((this = (struct fifo *)fc->index) == 0)
  230.         return EACCDN;
  231.  
  232.     this->mode = (this->mode & S_IFMT) | (mode & ~S_IFMT);
  233.     return 0;
  234. }
  235.  
  236. static long ARGS_ON_STACK 
  237. pipe_rmdir(dir, name)
  238.     fcookie *dir;
  239.     const char *name;
  240. {
  241.     UNUSED(dir); UNUSED(name);
  242.  
  243. /* the kernel already checked to see if the file exists */
  244.     return EACCDN;
  245. }
  246.  
  247. static long ARGS_ON_STACK 
  248. pipe_remove(dir, name)
  249.     fcookie *dir;
  250.     const char *name;
  251. {
  252.     UNUSED(dir); UNUSED(name);
  253.  
  254. /* the kernel already checked to see if the file exists */
  255.     return EACCDN;
  256. }
  257.  
  258. static long ARGS_ON_STACK 
  259. pipe_getname(root, dir, pathname, size)
  260.     fcookie *root, *dir; char *pathname;
  261.     int size;
  262. {
  263.     UNUSED(root);
  264.     UNUSED(size);    /* BUG: we should support 'size' */
  265.  
  266.     if (dir->index == 0)
  267.         *pathname = 0;
  268.     else
  269.         strcpy(pathname, ((struct fifo *)dir->index)->name);
  270.     return 0;
  271. }
  272.  
  273. static long ARGS_ON_STACK 
  274. pipe_rename(olddir, oldname, newdir, newname)
  275.     fcookie *olddir;
  276.     char *oldname;
  277.     fcookie *newdir;
  278.     const char *newname;
  279. {
  280.     UNUSED(olddir); UNUSED(oldname);
  281.     UNUSED(newdir); UNUSED(newname);
  282.  
  283.     return EACCDN;
  284. }
  285.  
  286. static long ARGS_ON_STACK 
  287. pipe_opendir(dirh, flags)
  288.     DIR *dirh;
  289.     int flags;
  290. {
  291.     UNUSED(flags);
  292.  
  293.     if (dirh->fc.index != 0) {
  294.         DEBUG(("pipe_opendir: bad directory"));
  295.         return EPTHNF;
  296.     }
  297.     dirh->index = 0;
  298.     return 0;
  299. }
  300.  
  301. static long ARGS_ON_STACK 
  302. pipe_readdir(dirh, name, namelen, fc)
  303.     DIR *dirh;
  304.     char *name;
  305.     int namelen;
  306.     fcookie *fc;
  307. {
  308.     struct fifo *this;
  309.     int i;
  310.     int giveindex = dirh->flags == 0;
  311.  
  312.     i = dirh->index++;
  313.     this = rootlist;
  314.     while (i > 0 && this) {
  315.         --i; this = this->next;
  316.     }
  317.     if (!this)
  318.         return ENMFIL;
  319.  
  320.     fc->fs = &pipe_filesys;
  321.     fc->index = (long)this;
  322.     fc->dev = dirh->fc.dev;
  323.     if (giveindex) {
  324.         namelen -= (int) sizeof(long);
  325.         if (namelen <= 0) return ERANGE;
  326.         *((long *)name) = (long)this;
  327.         name += sizeof(long);
  328.     }
  329.     strncpy(name, this->name, namelen-1);
  330.     if (strlen(this->name) >= namelen)
  331.         return ENAMETOOLONG;
  332.     return 0;
  333. }
  334.  
  335. static long ARGS_ON_STACK 
  336. pipe_rewinddir(dirh)
  337.     DIR *dirh;
  338. {
  339.     dirh->index = 0;
  340.     return 0;
  341. }
  342.  
  343. static long ARGS_ON_STACK 
  344. pipe_closedir(dirh)
  345.     DIR *dirh;
  346. {
  347.     UNUSED(dirh);
  348.     return 0;
  349. }
  350.  
  351. static long ARGS_ON_STACK 
  352. pipe_pathconf(dir, which)
  353.     fcookie *dir;
  354.     int which;
  355. {
  356.     UNUSED(dir);
  357.  
  358.     switch(which) {
  359.     case -1:
  360.         return DP_MAXREQ;
  361.     case DP_IOPEN:
  362.         return UNLIMITED;    /* no internal limit on open files */
  363.     case DP_MAXLINKS:
  364.         return 1;        /* no hard links */
  365.     case DP_PATHMAX:
  366.         return PATH_MAX;
  367.     case DP_NAMEMAX:
  368.         return NAME_MAX;
  369.     case DP_ATOMIC:
  370.     /* BUG: for pty's, this should actually be PIPE_BUF/4 */
  371.         return PIPE_BUF;
  372.     case DP_TRUNC:
  373.         return DP_AUTOTRUNC;
  374.     case DP_CASE:
  375.         return DP_CASEINSENS;
  376.     default:
  377.         return EINVFN;
  378.     }
  379. }
  380.  
  381. static long ARGS_ON_STACK 
  382. pipe_dfree(dir, buf)
  383.     fcookie *dir;
  384.     long *buf;
  385. {
  386.     int i;
  387.     struct fifo *b;
  388.     long freemem;
  389.  
  390.     UNUSED(dir);
  391.  
  392. /* the "sector" size is the number of bytes per pipe */
  393. /* so we get the total number of sectors used by counting pipes */
  394.  
  395.     i = 0;
  396.     for (b = rootlist; b; b = b->next) {
  397.         if (b->inp) i++;
  398.         if (b->outp) i++;
  399.     }
  400.  
  401.     freemem = tot_rsize(core, 0) + tot_rsize(alt, 0);
  402.  
  403. /* note: the "free clusters" isn't quite accurate, since there's overhead
  404.  * in the fifo structure; but we're not looking for 100% accuracy here
  405.  */
  406.     buf[0] = freemem/PIPESIZ;    /* number of free clusters */
  407.     buf[1] = buf[0]+i;        /* total number of clusters */
  408.     buf[2] = PIPESIZ;        /* sector size (bytes) */
  409.     buf[3] = 1;            /* cluster size (sectors) */
  410.     return 0;
  411. }
  412.  
  413. /* create a new pipe.
  414.  * this only gets called by the kernel if a lookup already failed,
  415.  * so we know that the new pipe creation is OK
  416.  */
  417.  
  418. static long ARGS_ON_STACK 
  419. pipe_creat(dir, name, mode, attrib, fc)
  420.     fcookie *dir;
  421.     const char *name;
  422.     unsigned mode;
  423.     int attrib;
  424.     fcookie *fc;
  425. {
  426.     struct pipe *inp, *outp;
  427.     struct tty *tty;
  428.     struct fifo *b;
  429. /* selfread == 1 if we want reads to wait even if no other processes
  430.    have currently opened the file, and writes to succeed in the same
  431.    event. This is useful for servers who want to wait for requests.
  432.    Pipes should always have selfread == 0.
  433. */
  434.     int selfread = (attrib & FA_HIDDEN) ? 0 : 1;
  435.  
  436.  
  437.     /* create the new pipe */
  438.     if (0 == (inp = (struct pipe *)kmalloc(SIZEOF(struct pipe)))) {
  439.         return ENSMEM;
  440.     }
  441.     if (attrib & FA_RDONLY) {    /* read only FIFOs are unidirectional */
  442.         outp = 0;
  443.     } else {
  444.         outp = (struct pipe *)kmalloc(SIZEOF(struct pipe));
  445.         if (!outp) {
  446.             kfree(inp);
  447.             return ENSMEM;
  448.         }
  449.     }
  450.     b = (struct fifo *)kmalloc(SIZEOF(struct fifo));
  451.     if (!b) {
  452.         kfree(inp);
  453.         if (outp) kfree(outp);
  454.         return ENSMEM;
  455.     }
  456.     if (attrib & FA_SYSTEM) {    /* pseudo-tty */
  457.         tty = (struct tty *)kmalloc(SIZEOF(struct tty));
  458.         if (!tty) {
  459.             kfree(inp);
  460.             kfree(b);
  461.             if (outp) kfree(outp);
  462.             return ENSMEM;
  463.         }
  464.         tty->use_cnt = 0;
  465.             /* do_open does the rest of tty initialization */
  466.     } else tty = 0;
  467.  
  468. /* set up the pipes appropriately */
  469.     inp->head = inp->tail = 0;
  470.     inp->readers = selfread ? 1 : VIRGIN_PIPE; inp->writers = 1;
  471.     inp->rsel = inp->wsel = 0;
  472.     if (outp) {
  473.         outp->head = outp->tail = 0;
  474.         outp->readers = 1; outp->writers = selfread ? 1 : VIRGIN_PIPE;
  475.         outp->wsel = outp->rsel = 0;
  476.     }
  477.     strncpy(b->name, name, NAME_MAX);
  478.     b->time = timestamp;
  479.     b->date = datestamp;
  480.     b->dosflags = attrib;
  481.     b->mode = ((attrib & FA_SYSTEM) ? S_IFCHR : S_IFIFO) | mode;
  482.     b->uid = curproc->ruid;
  483.     b->gid = curproc->rgid;
  484.  
  485. /* the O_HEAD flag indicates that the file hasn't actually been opened
  486.  * yet; the next open gets to be the pty master. pipe_open will
  487.  * clear the flag when this happens.
  488.  */
  489.     b->flags = ((attrib & FA_SYSTEM) ? O_TTY : 0) | O_HEAD;
  490.     b->inp = inp; b->outp = outp; b->tty = tty;
  491.  
  492.     b->next = rootlist;
  493.     b->open = (FILEPTR *)0;
  494.     rootlist = b;
  495.  
  496. /* we have to return a file cookie as well */
  497.     fc->fs = &pipe_filesys;
  498.     fc->index = (long)b;
  499.     fc->dev = dir->dev;
  500.  
  501. /* update time/date stamps for u:\pipe */
  502.     pipetime = timestamp;
  503.     pipedate = datestamp;
  504.  
  505.     return 0;
  506. }
  507.  
  508. static DEVDRV * ARGS_ON_STACK 
  509. pipe_getdev(fc, devsp)
  510.     fcookie *fc;
  511.     long *devsp;
  512. {
  513.     struct fifo *b = (struct fifo *)fc->index;
  514.  
  515.     UNUSED(devsp);
  516.     return (b->flags & O_TTY) ? &pty_device : &pipe_device;
  517. }
  518.  
  519. /*
  520.  * PIPE device driver
  521.  */
  522.  
  523. static long ARGS_ON_STACK 
  524. pipe_open(f)
  525.     FILEPTR *f;
  526. {
  527.     struct fifo *p;
  528.     int rwmode = f->flags & O_RWMODE;
  529.  
  530.     p = (struct fifo *)f->fc.index;
  531.     f->flags |= p->flags;
  532. /*
  533.  * if this is the first open for this file, then the O_HEAD flag is
  534.  * set in p->flags. If not, and someone was trying to create the file,
  535.  * return an error
  536.  */
  537.     if (p->flags & O_HEAD) {
  538.         if (!(f->flags & O_CREAT)) {
  539.             DEBUG(("pipe_open: file hasn't been created yet"));
  540.             return EINTRN;
  541.         }
  542.         p->flags &= ~O_HEAD;
  543.     } else {
  544.         if (f->flags & O_CREAT) {
  545.             DEBUG(("pipe_open: fifo already exists"));
  546.             return EACCDN;
  547.         }
  548.     }
  549. /*
  550.  * check for file sharing compatibility. note that O_COMPAT gets mutated
  551.  * into O_DENYNONE, because any old programs that know about pipes will
  552.  * already handle multitasking correctly
  553.  */
  554.     if ( (f->flags & O_SHMODE) == O_COMPAT ) {
  555.         f->flags = (f->flags & ~O_SHMODE) | O_DENYNONE;
  556.     }
  557.     if (denyshare(p->open, f))
  558.         return EACCDN;
  559.     f->next = p->open;        /* add this open fileptr to the list */
  560.     p->open = f;
  561.  
  562. /*
  563.  * add readers/writers to the list
  564.  */
  565.     if (!(f->flags & O_HEAD)) {
  566.         if (rwmode == O_RDONLY || rwmode == O_RDWR) {
  567.             if (p->inp->readers == VIRGIN_PIPE)
  568.                 p->inp->readers = 1;
  569.             else
  570.                 p->inp->readers++;
  571.         }
  572.         if ((rwmode == O_WRONLY || rwmode == O_RDWR) && p->outp) {
  573.             if (p->outp->writers == VIRGIN_PIPE)
  574.                 p->outp->writers = 1;
  575.             else
  576.                 p->outp->writers++;
  577.         }
  578.     }
  579.  
  580. /* TTY devices need a tty structure in f->devinfo */
  581.     f->devinfo = (long)p->tty;
  582.  
  583.     return 0;
  584. }
  585.  
  586. static long ARGS_ON_STACK 
  587. pipe_write(f, buf, nbytes)
  588.     FILEPTR *f; const char *buf; long nbytes;
  589. {
  590.     int ptail, phead, j;
  591.     char *pbuf;
  592.     struct pipe *p;
  593.     struct fifo *this;
  594.     long bytes_written = 0;
  595.     long r;
  596.  
  597.     this = (struct fifo *)f->fc.index;
  598.     p = (f->flags & O_HEAD) ? this->inp : this->outp;
  599.     if (!p) {
  600.         DEBUG(("pipe_write: write on wrong end of pipe"));
  601.         return EACCDN;
  602.     }
  603.  
  604.     if (nbytes > 0 && nbytes <= PIPE_BUF) {
  605. check_atomicity:
  606.         r = p->tail - p->head;
  607.         if (r < 0) r += PIPESIZ;
  608.         r = (PIPESIZ-1) - r; /* r is the number of bytes we can write */
  609.         if (r < nbytes) {
  610.     /* check for broken pipes */
  611.             if (p->readers == 0 || p->readers == VIRGIN_PIPE) {
  612.                 check_sigs();
  613.                 DEBUG(("pipe_write: broken pipe"));
  614.                 raise(SIGPIPE);
  615.                 return EPIPE;
  616.             }
  617. /* wake up any readers, and wait for them to gobble some data */
  618.             if (p->rsel) {
  619.                 wakeselect(p->rsel);
  620.                 p->rsel = 0;
  621.             }
  622.             wake(IO_Q, (long)p);
  623.             sleep(IO_Q, (long)p);
  624.             goto check_atomicity;
  625.         }
  626.     }
  627.  
  628.     while (nbytes > 0) {
  629.         ptail = p->tail; phead = p->head;
  630.         j = ptail+1;
  631.         if (j >= PIPESIZ) j = 0;
  632.         if (j != phead) {
  633.             pbuf = &p->buf[ptail];
  634.             do {
  635.                 *pbuf++ = *buf++;
  636.                 nbytes--; bytes_written++;
  637.                 if ( (ptail = j) == 0 )
  638.                     pbuf = &p->buf[0];
  639.                 j++;
  640.                 if (j >= PIPESIZ) j = 0;
  641.             } while ( (nbytes > 0) && (j != phead) );
  642.             p->tail = ptail;
  643.         } else {        /* pipe full */
  644.             if (p->readers == 0 || p->readers == VIRGIN_PIPE) {
  645.             /* maybe some other signal is waiting for us? */
  646.                 check_sigs();
  647.                 DEBUG(("pipe_write: broken pipe"));
  648.                 raise(SIGPIPE);
  649.                 return EPIPE;
  650.             }
  651.             if (f->flags & O_NDELAY) {
  652.                 break;
  653.             }
  654.     /* is someone select()ing the other end of the pipe for reading? */
  655.             if (p->rsel) {
  656.                 wakeselect(p->rsel);
  657.                 p->rsel = 0;
  658.             }
  659.             wake(IO_Q, (long)p);    /* readers may continue */
  660. DEBUG(("pipe_write: sleep on %lx", p));
  661.             sleep(IO_Q, (long)p);
  662.         }
  663.     }
  664.     this->time = timestamp;
  665.     this->date = datestamp;
  666.     if (bytes_written > 0) {
  667.         if (p->rsel) {
  668.             wakeselect(p->rsel);
  669.             p->rsel = 0;
  670.         }
  671.         wake(IO_Q, (long)p);    /* maybe someone wants this data */
  672.     }
  673.  
  674.     return bytes_written;
  675. }
  676.  
  677. static long ARGS_ON_STACK 
  678. pipe_read(f, buf, nbytes)
  679.     FILEPTR *f; char *buf; long nbytes;
  680. {
  681.     int phead, ptail;
  682.     struct fifo *this;
  683.     struct pipe *p;
  684.     long bytes_read = 0;
  685.     char *pbuf;
  686.  
  687.     this = (struct fifo *)f->fc.index;
  688.     p = (f->flags & O_HEAD) ? this->outp : this->inp;
  689.     if (!p) {
  690.         DEBUG(("pipe_read: read on the wrong end of a pipe"));
  691.         return EACCDN;
  692.     }
  693.  
  694.     while (nbytes > 0) {
  695.         phead = p->head; ptail = p->tail;
  696.         if (ptail != phead) {
  697.             pbuf = &p->buf[phead];
  698.             do {
  699.                 *buf++ = *pbuf++;
  700.                 nbytes--; bytes_read++;
  701.                 phead++;
  702.                 if (phead >= PIPESIZ) {
  703.                     phead = 0;
  704.                     pbuf = &p->buf[phead];
  705.                 }
  706.             } while ( (nbytes > 0) && (phead != ptail) );
  707.             p->head = phead;
  708.         }
  709.         else if (p->writers <= 0 || p->writers == VIRGIN_PIPE) {
  710.             TRACE(("pipe_read: no more writers"));
  711.             break;
  712.         }
  713.         else if ((f->flags & O_NDELAY) ||
  714.                ((this->dosflags & FA_CHANGED) && bytes_read > 0) )
  715.         {
  716.             break;
  717.         }
  718.         else {
  719.     /* is someone select()ing the other end of the pipe for writing? */
  720.             if (p->wsel) {
  721.                 wakeselect(p->wsel);
  722.                 p->wsel = 0;
  723.             }
  724.             wake(IO_Q, (long)p);    /* writers may continue */
  725.             sleep(IO_Q, (long)p);
  726.         }
  727.     }
  728.     if (bytes_read > 0) {
  729.         if (p->wsel) {
  730.             wakeselect(p->wsel);
  731.             p->wsel = 0;
  732.         }
  733.         wake(IO_Q, (long)p);    /* wake writers */
  734.     }
  735.     return bytes_read;
  736. }
  737.  
  738. static long ARGS_ON_STACK 
  739. pipe_ioctl(f, mode, buf)
  740.     FILEPTR *f; int mode; void *buf;
  741. {
  742.     struct pipe *p;
  743.     struct fifo *this;
  744.     struct flock *lck;
  745.  
  746.     long r;
  747.  
  748.     this = (struct fifo *)f->fc.index;
  749.  
  750.     if (mode == FIONREAD) {
  751.             p = (f->flags & O_HEAD) ? this->outp : this->inp;
  752.             assert(p != 0);
  753.             if (p->writers <= 0 || p->writers == VIRGIN_PIPE) {
  754.                 DEBUG(("pipe FIONREAD: no writers"));
  755.                 r = -1;
  756.             } else {
  757.                 r = p->tail - p->head;
  758.                 if (r < 0) r += PIPESIZ;
  759.                 if (is_terminal(f))
  760.                     r = r >> 2;    /* r /= 4 */
  761.             }
  762.             *((long *) buf) = r;
  763.     }
  764.     else if (mode == FIONWRITE) {
  765.             p = (f->flags & O_HEAD) ? this->inp : this->outp;
  766.             assert(p != 0);
  767.             if (p->readers <= 0) {
  768.                 r = -1;
  769.             } else {
  770.                 r = p->tail - p->head;
  771.                 if (r < 0) r += PIPESIZ;
  772.                 r = (PIPESIZ-1) - r;
  773.                 if (is_terminal(f))
  774.                     r = r >> 2;    /* r /= 4 */
  775.             }
  776.             *((long *) buf) = r;
  777.     }
  778.     else if (mode == F_SETLK || mode == F_SETLKW) {
  779.         lck = (struct flock *)buf;
  780.         while (this->flags & O_LOCK) {
  781.             if (this->lockpid != curproc->pid) {
  782.                 DEBUG(("pipe_ioctl: pipe already locked"));
  783.                 if (mode == F_SETLKW && lck->l_type != F_UNLCK) {
  784.                     sleep(IO_Q, (long)this);        /* sleep a while */
  785.                 }
  786.                 else
  787.                     return ELOCKED;
  788.             } else
  789.                 break;
  790.         }
  791.         if (lck->l_type == F_UNLCK) {
  792.             if (!(f->flags & O_LOCK)) {
  793.                 DEBUG(("pipe_ioctl: wrong file descriptor for UNLCK"));
  794.                 return ENSLOCK;
  795.             }
  796.             this->flags &= ~O_LOCK;
  797.             this->lockpid = 0;
  798.             f->flags &= ~O_LOCK;
  799.             wake(IO_Q, (long)this);    /* wake up anyone waiting on the lock */
  800.         }
  801.         else {
  802.             this->flags |= O_LOCK;
  803.             this->lockpid = curproc->pid;
  804.             f->flags |= O_LOCK;
  805.         }
  806.     }
  807.     else if (mode == F_GETLK) {
  808.         lck = (struct flock *)buf;
  809.         if (this->flags & O_LOCK) {
  810.             lck->l_type = F_WRLCK;
  811.             lck->l_start = lck->l_len = 0;
  812.             lck->l_pid = this->lockpid;
  813.         }
  814.         else
  815.             lck->l_type = F_UNLCK;
  816.     }
  817.     else if (mode == TIOCFLUSH) {
  818.         if (this->inp) {
  819.             this->inp->head = this->inp->tail;
  820.             wake(IO_Q, (long)this->inp);
  821.         }
  822.         if (this->outp) {
  823.             this->outp->head = this->outp->tail;
  824.             wake(IO_Q, (long)this->outp);
  825.         }
  826.     } else if (mode == TIOCIBAUD || mode == TIOCOBAUD) {
  827.         *(long *)buf = -1L;
  828.     } else if (mode == TIOCGFLAGS) {
  829.         *((unsigned short *)buf) = 0;
  830.     } else if (mode >= TCURSOFF && mode <= TCURSGRATE) {
  831.     /* kludge: this assumes TOSWIN style escape sequences */
  832.         tty_putchar(f, (long)'\033', RAW);
  833.         switch (mode) {
  834.         case TCURSOFF:
  835.             tty_putchar(f, (long)'f', RAW);
  836.             break;
  837.         case TCURSON:
  838.             tty_putchar(f, (long)'e', RAW);
  839.             break;
  840.         case TCURSSRATE:
  841.             this->cursrate = *((int *)buf);
  842.             /* fall through */
  843.         case TCURSBLINK:
  844.             tty_putchar(f, (long)'t', RAW);
  845.             tty_putchar(f, (long)this->cursrate+32, RAW);
  846.             break;
  847.         case TCURSSTEADY:
  848.             tty_putchar(f, (long)'t', RAW);
  849.             tty_putchar(f, (long)32, RAW);
  850.             break;
  851.         case TCURSGRATE:
  852.             return this->cursrate;
  853.         }
  854.     } else {
  855.     /* if the file is a terminal, Fcntl will automatically
  856.      * call tty_ioctl for us to handle 'generic' terminal
  857.      * functions
  858.      */
  859.         return EINVFN;
  860.     }
  861.  
  862.     return 0;
  863. }
  864.  
  865. static long ARGS_ON_STACK 
  866. pipe_lseek(f, where, whence)
  867.     FILEPTR *f; long where; int whence;
  868. {
  869.     UNUSED(f); UNUSED(where); UNUSED(whence);
  870.     return EACCDN;
  871. }
  872.  
  873. static long ARGS_ON_STACK 
  874. pipe_datime(f, timeptr, rwflag)
  875.     FILEPTR *f;
  876.     short *timeptr;
  877.     int rwflag;
  878. {
  879.     struct fifo *this;
  880.  
  881.     this = (struct fifo *)f->fc.index;
  882.     if (rwflag) {
  883.         this->time = timeptr[0];
  884.         this->date = timeptr[1];
  885.     }
  886.     else {
  887.         timeptr[0] = this->time;
  888.         timeptr[1] = this->date;
  889.     }
  890.     return 0;
  891. }
  892.  
  893. static long ARGS_ON_STACK 
  894. pipe_close(f, pid)
  895.     FILEPTR *f;
  896.     int pid;
  897. {
  898.     struct fifo *this, *old;
  899.     struct pipe *p;
  900.     int rwmode;
  901.     FILEPTR **old_x, *x;
  902.  
  903.     this = (struct fifo *)f->fc.index;
  904.  
  905. /* wake any processes waiting on this pipe */
  906.     wake(IO_Q, (long)this->inp);
  907.     if (this->inp->rsel)
  908.         wakeselect(this->inp->rsel);
  909.     if (this->inp->wsel)
  910.         wakeselect(this->inp->wsel);
  911.  
  912.     if (this->outp) {
  913.         wake(IO_Q, (long)this->outp);
  914.         if (this->outp->wsel)
  915.             wakeselect(this->outp->wsel);
  916.         if (this->outp->rsel)
  917.             wakeselect(this->outp->rsel);
  918.     }
  919.  
  920.     if (f->links <= 0) {
  921. /* remove the file pointer from the list of open file pointers 
  922.  * of this pipe
  923.  */
  924.         old_x = &this->open;
  925.         x = this->open;
  926.         while (x && x != f) {
  927.                 old_x = &x->next;
  928.                 x = x->next;
  929.         }
  930.         assert(x);
  931.         *old_x = f->next;
  932.         /* f->next = 0; */
  933.  
  934.         rwmode = f->flags & O_RWMODE;
  935.         if (rwmode == O_RDONLY || rwmode == O_RDWR) {
  936.             p = (f->flags & O_HEAD) ? this->outp : this->inp;
  937. /* note that this can never be a virgin pipe, since we had a handle
  938.  * on it!
  939.  */            if (p)
  940.                 p->readers--;
  941.         }
  942.         if (rwmode == O_WRONLY || rwmode == O_RDWR) {
  943.             p = (f->flags & O_HEAD) ? this->inp : this->outp;
  944.             if (p) p->writers--;
  945.         }
  946.  
  947. /* correct for the "selfread" flag (see pipe_creat) */
  948.         if ((f->flags & O_HEAD) && !(this->dosflags & 0x02))
  949.             this->inp->readers--;
  950.  
  951. /* check for locks */
  952.         if ((f->flags & O_LOCK) && (this->lockpid == pid)) {
  953.             this->flags &= ~O_LOCK;
  954.             wake(IO_Q, (long)this);    /* wake up anyone waiting on the lock */
  955.         }
  956.     }
  957.  
  958. /* see if we're finished with the pipe */
  959.     if (this->inp->readers == VIRGIN_PIPE)
  960.         this->inp->readers = 0;
  961.     if (this->inp->writers == VIRGIN_PIPE)
  962.         this->inp->writers = 0;
  963.  
  964.     if (this->inp->readers <= 0 && this->inp->writers <= 0) {
  965.         TRACE(("disposing of closed fifo"));
  966. /* unlink from list of FIFOs */
  967.         if (rootlist == this)
  968.             rootlist = this->next;
  969.         else {
  970.             for (old = rootlist; old->next != this;
  971.                     old = old->next) {
  972.                 if (!old) {
  973.                     ALERT("fifo not on list???");
  974.                     return EINTRN;
  975.                 }
  976.             }
  977.             old->next = this->next;
  978.         }
  979.         kfree(this->inp);
  980.         if (this->outp) kfree(this->outp);
  981.         kfree(this);
  982.         pipetime = timestamp;
  983.         pipedate = datestamp;
  984.     }
  985.  
  986.     return 0;
  987. }
  988.  
  989. static long ARGS_ON_STACK 
  990. pipe_select(f, proc, mode)
  991.     FILEPTR *f;
  992.     long proc;
  993.     int mode;
  994. {
  995.     struct fifo *this;
  996.     struct pipe *p;
  997.     int j;
  998.  
  999.     this = (struct fifo *)f->fc.index;
  1000.  
  1001.     if (mode == O_RDONLY) {
  1002.         p = (f->flags & O_HEAD) ? this->outp : this->inp;
  1003.         if (!p) {
  1004.             DEBUG(("read select on wrong end of pipe"));
  1005.             return 0;
  1006.         }
  1007.  
  1008. /* NOTE: if p->writers <= 0 then reads won't block (they'll fail) */
  1009.         if (p->tail != p->head || p->writers <= 0) {
  1010.             return 1;
  1011.         }
  1012.  
  1013. /* BUG: multiple selects fail, only the first one works */
  1014.         if (!p->rsel)
  1015.             p->rsel = proc;
  1016.         return 0;
  1017.     } else if (mode == O_WRONLY) {
  1018.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  1019.         if (!p) {
  1020.             DEBUG(("write select on wrong end of pipe"));
  1021.             return 0;
  1022.         }
  1023.         j = p->tail+1;
  1024.         if (j >= PIPESIZ) j = 0;
  1025.         if (j != p->head || p->readers <= 0)
  1026.             return 1;    /* data may be written */
  1027.         if (!p->wsel)
  1028.             p->wsel = proc;
  1029.         return 0;
  1030.     }
  1031.     return 0;
  1032. }
  1033.  
  1034. static void ARGS_ON_STACK 
  1035. pipe_unselect(f, proc, mode)
  1036.     FILEPTR *f;
  1037.     long proc;
  1038.     int mode;
  1039. {
  1040.     struct fifo *this;
  1041.     struct pipe *p;
  1042.  
  1043.     this = (struct fifo *)f->fc.index;
  1044.  
  1045.     if (mode == O_RDONLY) {
  1046.         p = (f->flags & O_HEAD) ? this->outp : this->inp;
  1047.         if (!p) {
  1048.             return;
  1049.         }
  1050.         if (p->rsel == proc)
  1051.             p->rsel = 0;
  1052.     } else if (mode == O_WRONLY) {
  1053.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  1054.         if (!p) {
  1055.             return;
  1056.         }
  1057.         if (p->wsel == proc)
  1058.             p->wsel = 0;
  1059.     }
  1060. }
  1061.