Exemplo n.º 1
0
int dup2(int fd1, int fd2)
{
  /* Check the range of the descriptor to see if we got a file or a socket
   * descriptor.
   */

  if ((unsigned int)fd1 >= CONFIG_NFILE_DESCRIPTORS)
    {
      /* Not a valid file descriptor.  Did we get a valid socket descriptor? */

      if ((unsigned int)fd1 < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
        {
          /* Yes.. dup the socket descriptor */

          return net_dupsd2(fd1, fd2);
        }
      else
        {
          /* No.. then it is a bad descriptor number */

          set_errno(EBADF);
          return ERROR;
        }
    }
  else
    {
      /* Its a valid file descriptor.. dup the file descriptor */

      return fs_dupfd2(fd1, fd2);
    }
}
Exemplo n.º 2
0
int group_setupidlefiles(FAR struct task_tcb_s *tcb)
{
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
  FAR struct task_group_s *group = tcb->cmn.group;
#endif
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  int fd;
#endif

#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
  DEBUGASSERT(group);
#endif

#if CONFIG_NFILE_DESCRIPTORS > 0
  /* Initialize file descriptors for the TCB */

  files_initlist(&group->tg_filelist);
#endif

#if CONFIG_NSOCKET_DESCRIPTORS > 0
  /* Allocate socket descriptors for the TCB */

  net_initlist(&group->tg_socketlist);
#endif

  /* Open stdin, dup to get stdout and stderr. This should always
   * be the first file opened and, hence, should always get file
   * descriptor 0.
   */

#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  fd = open("/dev/console", O_RDWR);
  if (fd == 0)
    {
      /* Successfully opened /dev/console as stdin (fd == 0) */

      (void)fs_dupfd2(0, 1);
      (void)fs_dupfd2(0, 2);
    }
  else
    {
      /* We failed to open /dev/console OR for some reason, we opened
       * it and got some file descriptor other than 0.
       */

      if (fd > 0)
        {
          sinfo("Open /dev/console fd: %d\n", fd);
          (void)close(fd);
        }
      else
        {
          serr("ERROR: Failed to open /dev/console: %d\n", errno);
        }

      return -ENFILE;
    }
#endif

  /* Allocate file/socket streams for the TCB */

#if CONFIG_NFILE_STREAMS > 0
  return group_setupstreams(tcb);
#else
  return OK;
#endif
}