示例#1
0
文件: tmpfile.c 项目: zhuli/glibc
/* This returns a new stream opened on a temporary file (generated
   by tmpnam).  The file is opened with mode "w+b" (binary read/write).
   If we couldn't generate a unique filename or the file couldn't
   be opened, NULL is returned.  */
FILE *
__tmpfile (void)
{
  error_t err;
  file_t file;
  int fd;
  FILE *f;

  /* Get a port to the directory that will contain the file.  */
  const char *dirname = __libc_secure_getenv ("TMPDIR") ?: P_tmpdir;
  file_t dir = __file_name_lookup (dirname, 0, 0);
  if (dir == MACH_PORT_NULL)
    return NULL;

  /* Create an unnamed file in the temporary directory.  */
  err = __dir_mkfile (dir, O_RDWR, S_IRUSR | S_IWUSR, &file);
  __mach_port_deallocate (__mach_task_self (), dir);
  if (err)
    return __hurd_fail (err), NULL;

  /* Get a file descriptor for that port.  POSIX.1 requires that streams
     returned by tmpfile allocate file descriptors as fopen would.  */
  fd = _hurd_intern_fd (file, O_RDWR, 1); /* dealloc on error */
  if (fd < 0)
    return NULL;

  /* Open a stream on the unnamed file.
     It will cease to exist when this stream is closed.  */
  if ((f = _IO_fdopen (fd, "w+b")) == NULL)
    __close (fd);

  return f;
}
示例#2
0
/* Make a link to FROM called TO.  */
int
__symlink (const char *from, const char *to)
{
  error_t err;
  file_t dir, node;
  char *name;
  const size_t len = strlen (from) + 1;
  char buf[sizeof (_HURD_SYMLINK) + len];

  /* A symlink is a file whose translator is "/hurd/symlink\0target\0".  */

  memcpy (buf, _HURD_SYMLINK, sizeof (_HURD_SYMLINK));
  memcpy (&buf[sizeof (_HURD_SYMLINK)], from, len);

  dir = __file_name_split (to, &name);
  if (dir == MACH_PORT_NULL)
    return -1;

  /* Create a new, unlinked node in the target directory.  */
  err = __dir_mkfile (dir, O_WRITE, 0777 & ~_hurd_umask, &node);

  if (! err)
    {
      /* Set the node's translator to make it a symlink.  */
      err = __file_set_translator (node,
                                   FS_TRANS_EXCL|FS_TRANS_SET,
                                   FS_TRANS_EXCL|FS_TRANS_SET, 0,
                                   buf, sizeof (_HURD_SYMLINK) + len,
                                   MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);

      if (! err)
        /* Link the node, now a valid symlink, into the target directory.  */
        err = __dir_link (dir, node, name, 1);

      __mach_port_deallocate (__mach_task_self (), node);
    }

  __mach_port_deallocate (__mach_task_self (), dir);

  if (err)
    return __hurd_fail (err);
  return 0;
}
示例#3
0
文件: shmget.c 项目: OSLL/elfperf
/* Create a new shared memory segment file without linking it into the
   filesystem.  Return the directory and file ports in R_DIR and R_FILE.  */
static error_t
create_shm_file (size_t size, int flags, file_t *r_dir, file_t *r_file)
{
  error_t err;
  file_t dir;
  file_t file;

  flags &= 0777;

  /* Get a port to the directory that will contain the file.  */
  dir = __file_name_lookup (SHM_DIR, 0, 0);
  if (dir == MACH_PORT_NULL)
    return errno;

  /* Create an unnamed file in the directory.  */
  err = __dir_mkfile (dir, O_RDWR, flags, &file);
  if (err)
    {
      __mach_port_deallocate (__mach_task_self (), dir);
      return err;
    }

  err = __file_set_size (file, size);
  if (err)
    {
      __mach_port_deallocate (__mach_task_self (), file);
      __mach_port_deallocate (__mach_task_self (), dir);

      return err;
    }

  *r_dir = dir;
  *r_file = file;

  return 0;
}
示例#4
0
/* Give the socket FD the local address ADDR (which is LEN bytes long).  */
int
__bind  (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
{
  addr_port_t aport;
  error_t err;
  const struct sockaddr_un *addr = addrarg.__sockaddr_un__;

  if (addr->sun_family == AF_LOCAL)
    {
      /* For the local domain, we must create a node in the filesystem
	 using the ifsock translator and then fetch the address from it.  */
      file_t dir, node;
      char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n;

      strncpy (name, addr->sun_path, sizeof name - 1);
      name[sizeof name - 1] = '\0'; /* Make sure */

      dir = __file_name_split (name, &n);
      if (dir == MACH_PORT_NULL)
	return -1;

      /* Create a new, unlinked node in the target directory.  */
      err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);

      if (! err)
	{
	  /* Set the node's translator to make it a local-domain socket.  */
	  err = __file_set_translator (node,
				       FS_TRANS_EXCL | FS_TRANS_SET,
				       FS_TRANS_EXCL | FS_TRANS_SET, 0,
				       _HURD_IFSOCK, sizeof _HURD_IFSOCK,
				       MACH_PORT_NULL,
				       MACH_MSG_TYPE_COPY_SEND);
	  if (! err)
	    {
	      /* Link the node, now a socket, into the target directory.  */
	      err = __dir_link (dir, node, n, 1);
	      if (err == EEXIST)
		err = EADDRINUSE;
	    }
	  __mach_port_deallocate (__mach_task_self (), node);
	  if (! err)
	    {
	      /* Get a port to the ifsock translator.  */
	      file_t ifsock = __file_name_lookup_under (dir, n, 0, 0);
	      if (ifsock == MACH_PORT_NULL)
		{
		  err = errno;
		  /* If we failed, get rid of the node we created.  */
		  __dir_unlink (dir, n);
		}
	      else
		{
		  /* Get the address port.  */
		  err = __ifsock_getsockaddr (ifsock, &aport);
		  if (err == MIG_BAD_ID || err == EOPNOTSUPP)
		    /* We are not talking to /hurd/ifsock.  Probably
		       someone came in after we linked our node, unlinked
		       it, and replaced it with a different node, before we
		       did our lookup.  Treat it as if our link had failed
		       with EEXIST.  */
		    err = EADDRINUSE;
		}
	      __mach_port_deallocate (__mach_task_self (), ifsock);
	    }
	}
      __mach_port_deallocate (__mach_task_self (), dir);

      if (err)
	return __hurd_fail (err);
    }
示例#5
0
/* Give the socket FD the local address ADDR (which is LEN bytes long).  */
int
__bind  (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
{
    addr_port_t aport;
    error_t err;
    const struct sockaddr_un *addr = addrarg.__sockaddr_un__;

    if (addr->sun_family == AF_LOCAL)
    {
        /* For the local domain, we must create a node in the filesystem
        using the ifsock translator and then fetch the address from it.  */
        file_t dir, node, ifsock;
        char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n;

        strncpy (name, addr->sun_path, sizeof name - 1);
        name[sizeof name - 1] = '\0'; /* Make sure */

        dir = __file_name_split (name, &n);
        if (dir == MACH_PORT_NULL)
            return -1;

        /* Create a new, unlinked node in the target directory.  */
        err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);

        if (! err)
        {
            /* Set the node's translator to make it a local-domain socket.  */
            err = __file_set_translator (node,
                                         FS_TRANS_EXCL | FS_TRANS_SET,
                                         FS_TRANS_EXCL | FS_TRANS_SET, 0,
                                         _HURD_IFSOCK, sizeof _HURD_IFSOCK,
                                         MACH_PORT_NULL,
                                         MACH_MSG_TYPE_COPY_SEND);
            if (! err)
            {
                enum retry_type doretry;
                char retryname[1024];
                /* Get a port to the ifsock translator.  */
                err = __dir_lookup (node, "", 0, 0, &doretry, retryname, &ifsock);
                if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
                    err = EADDRINUSE;
            }
            if (! err)
            {
                /* Get the address port.  */
                err = __ifsock_getsockaddr (ifsock, &aport);
                if (err == MIG_BAD_ID || err == EOPNOTSUPP)
                    err = EGRATUITOUS;
                if (! err)
                {
                    /* Link the node, now a socket with proper mode, into the
                       target directory.  */
                    err = __dir_link (dir, node, n, 1);
                    if (err == EEXIST)
                        err = EADDRINUSE;
                    if (err)
                        __mach_port_deallocate (__mach_task_self (), aport);
                }
                __mach_port_deallocate (__mach_task_self (), ifsock);
            }
            __mach_port_deallocate (__mach_task_self (), node);
        }
        __mach_port_deallocate (__mach_task_self (), dir);

        if (err)
            return __hurd_fail (err);
    }
示例#6
0
文件: hurdsig.c 项目: VGEAREN/mygit
/* Call the crash dump server to mummify us before we die.
   Returns nonzero if a core file was written.  */
static int
write_corefile (int signo, const struct hurd_signal_detail *detail)
{
  error_t err;
  mach_port_t coreserver;
  file_t file, coredir;
  const char *name;

  /* Don't bother locking since we just read the one word.  */
  rlim_t corelimit = _hurd_rlimits[RLIMIT_CORE].rlim_cur;

  if (corelimit == 0)
    /* No core dumping, thank you very much.  Note that this makes
       `ulimit -c 0' prevent crash-suspension too, which is probably
       what the user wanted.  */
    return 0;

  /* XXX RLIMIT_CORE:
     When we have a protocol to make the server return an error
     for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
     value in place of the RLIMIT_FSIZE value.  */

  /* First get a port to the core dumping server.  */
  coreserver = MACH_PORT_NULL;
  name = _hurdsig_getenv ("CRASHSERVER");
  if (name != NULL)
    coreserver = __file_name_lookup (name, 0, 0);
  if (coreserver == MACH_PORT_NULL)
    coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0);
  if (coreserver == MACH_PORT_NULL)
    return 0;

  /* Get a port to the directory where the new core file will reside.  */
  file = MACH_PORT_NULL;
  name = _hurdsig_getenv ("COREFILE");
  if (name == NULL)
    name = "core";
  coredir = __file_name_split (name, (char **) &name);
  if (coredir != MACH_PORT_NULL)
    /* Create the new file, but don't link it into the directory yet.  */
    __dir_mkfile (coredir, O_WRONLY|O_CREAT,
		  0600 & ~_hurd_umask, /* XXX ? */
		  &file);

  /* Call the core dumping server to write the core file.  */
  err = __crash_dump_task (coreserver,
			   __mach_task_self (),
			   file,
			   signo, detail->code, detail->error,
			   detail->exc, detail->exc_code, detail->exc_subcode,
			   _hurd_ports[INIT_PORT_CTTYID].port,
			   MACH_MSG_TYPE_COPY_SEND);
  __mach_port_deallocate (__mach_task_self (), coreserver);

  if (! err && file != MACH_PORT_NULL)
    /* The core dump into FILE succeeded, so now link it into the
       directory.  */
    err = __dir_link (coredir, file, name, 1);
  __mach_port_deallocate (__mach_task_self (), file);
  __mach_port_deallocate (__mach_task_self (), coredir);
  return !err && file != MACH_PORT_NULL;
}
示例#7
0
/* Create a device file named FILE_NAME, with permission and special bits MODE
   and device number DEV (which can be constructed from major and minor
   device numbers with the `makedev' macro above).  */
int
__xmknod (int vers, const char *file_name, mode_t mode, dev_t *dev)
{
    error_t err;
    file_t dir, node;
    char *name;
    char buf[100], *bp;
    const char *translator;
    size_t len;

    if (vers != _MKNOD_VER)
        return __hurd_fail (EINVAL);

    if (S_ISCHR (mode))
    {
        translator = _HURD_CHRDEV;
        len = sizeof (_HURD_CHRDEV);
    }
    else if (S_ISBLK (mode))
    {
        translator = _HURD_BLKDEV;
        len = sizeof (_HURD_BLKDEV);
    }
    else if (S_ISFIFO (mode))
    {
        translator = _HURD_FIFO;
        len = sizeof (_HURD_FIFO);
    }
    else
    {
        errno = EINVAL;
        return -1;
    }

    if (! S_ISFIFO (mode))
    {
        /* We set the translator to "ifmt\0major\0minor\0", where IFMT
        depends on the S_IFMT bits of our MODE argument, and MAJOR and
         MINOR are ASCII decimal (octal or hex would do as well)
         representations of our arguments.  Thus the convention is that
         CHRDEV and BLKDEV translators are invoked with two non-switch
         arguments, giving the major and minor device numbers in %i format. */

        bp = buf + sizeof (buf);
        *--bp = '\0';
        bp = _itoa (minor (*dev), bp, 10, 0);
        *--bp = '\0';
        bp = _itoa (major (*dev), bp, 10, 0);
        memcpy (bp - len, translator, len);
        translator = bp - len;
        len = buf + sizeof (buf) - translator;
    }

    dir = __file_name_split (file_name, &name);
    if (dir == MACH_PORT_NULL)
        return -1;

    /* Create a new, unlinked node in the target directory.  */
    err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);

    if (! err)
        /* Set the node's translator to make it a device.  */
        err = __file_set_translator (node,
                                     FS_TRANS_EXCL | FS_TRANS_SET,
                                     FS_TRANS_EXCL | FS_TRANS_SET, 0,
                                     translator, len,
                                     MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);

    if (! err)
        /* Link the node, now a valid device, into the target directory.  */
        err = __dir_link (dir, node, name, 1);

    __mach_port_deallocate (__mach_task_self (), dir);
    __mach_port_deallocate (__mach_task_self (), node);

    if (err)
        return __hurd_fail (err);
    return 0;
}