Fork me on GitHub

root/kernel/syscalls/pipe.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_pipe

   1 /*
   2  * fiwix/kernel/syscalls/pipe.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/fs.h>
   9 #include <fiwix/filesystems.h>
  10 #include <fiwix/fcntl.h>
  11 #include <fiwix/stat.h>
  12 #include <fiwix/errno.h>
  13 #include <fiwix/stdio.h>
  14 
  15 int sys_pipe(int pipefd[2])
  16 {
  17         int rfd, rufd;
  18         int wfd, wufd;
  19         struct filesystems *fs;
  20         struct inode *i;
  21         int errno;
  22 
  23 #ifdef __DEBUG__
  24         printk("(pid %d) sys_pipe()", current->pid);
  25 #endif /*__DEBUG__ */
  26 
  27         if(!(fs = get_filesystem("pipefs"))) {
  28                 printk("WARNING: %s(): pipefs filesystem is not registered!\n", __FUNCTION__);
  29                 return -EINVAL;
  30         }
  31         if((errno = check_user_area(VERIFY_WRITE, pipefd, sizeof(int) * 2))) {
  32                 return errno;
  33         }
  34         if(!(i = ialloc(&fs->mt->sb, S_IFIFO))) {
  35                 return -EINVAL;
  36         }
  37         if((rfd = get_new_fd(i)) < 0) {
  38                 iput(i);
  39                 return -ENFILE;
  40         }
  41         if((wfd = get_new_fd(i)) < 0) {
  42                 release_fd(rfd);
  43                 iput(i);
  44                 return -ENFILE;
  45         }
  46         if((rufd = get_new_user_fd(0)) < 0) {
  47                 release_fd(rfd);
  48                 release_fd(wfd);
  49                 iput(i);
  50                 return -EMFILE;
  51         }
  52         if((wufd = get_new_user_fd(0)) < 0) {
  53                 release_fd(rfd);
  54                 release_fd(wfd);
  55                 release_user_fd(rufd);
  56                 iput(i);
  57                 return -EMFILE;
  58         }
  59 
  60         pipefd[0] = rufd;
  61         pipefd[1] = wufd;
  62         current->fd[rufd] = rfd;
  63         current->fd[wufd] = wfd;
  64         fd_table[rfd].flags = O_RDONLY;
  65         fd_table[wfd].flags = O_WRONLY;
  66 
  67 #ifdef __DEBUG__
  68         printk(" -> inode=%d, rufd=%d wufd=%d (rfd=%d wfd=%d)\n", i->inode, rufd, wufd, rfd, wfd);
  69 #endif /*__DEBUG__ */
  70 
  71         return 0;
  72 }

/* [previous][next][first][last][top][bottom][index][help] */