Beispiel #1
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;
}
Beispiel #2
0
error_t
_hurd_xattr_set (io_t port, const char *name, const void *value, size_t size,
		 int flags)
{
  if (strncmp (name, "gnu.", 4))
    return EOPNOTSUPP;
  name += 4;

  if (!strcmp (name, "author"))
    switch (size)
      {
      default:
	return EINVAL;
      case 0:			/* "Clear" author by setting to st_uid. */
	{
	  struct stat64 st;
	  error_t err = __io_stat (port, &st);
	  if (err)
	    return err;
	  if (st.st_author == st.st_uid)
	    {
	      /* Nothing to do.  */
	      if (flags & XATTR_REPLACE)
		return ENODATA;
	      return 0;
	    }
	  if (flags & XATTR_CREATE)
	    return EEXIST;
	  return __file_chauthor (port, st.st_uid);
	}
      case sizeof (uid_t):	/* Set the author.  */
	{
	  uid_t id;
	  memcpy (&id, value, sizeof id);
	  if (flags & (XATTR_CREATE|XATTR_REPLACE))
	    {
	      struct stat64 st;
	      error_t err = __io_stat (port, &st);
	      if (err)
		return err;
	      if (st.st_author == st.st_uid)
		{
		  if (flags & XATTR_REPLACE)
		    return ENODATA;
		}
	      else if (flags & XATTR_CREATE)
		return EEXIST;
	      if (st.st_author == id)
		/* Nothing to do.  */
		return 0;
	    }
	  return __file_chauthor (port, id);
	}
      }

  if (!strcmp (name, "translator"))
    {
      if (flags & XATTR_REPLACE)
	{
	  /* Must make sure it's already there.  */
	  char *buf = NULL;
	  size_t bufsz = 0;
	  error_t err = __file_get_translator (port, &buf, &bufsz);
	  if (err)
	    return err;
	  if (bufsz > 0)
	    {
	      munmap (buf, bufsz);
	      return ENODATA;
	    }
	}
      return __file_set_translator (port,
				    FS_TRANS_SET | ((flags & XATTR_CREATE)
						    ? FS_TRANS_EXCL : 0), 0, 0,
				    value, size,
				    MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
    }

  return EOPNOTSUPP;
}
Beispiel #3
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);
    }
Beispiel #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, 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);
    }
Beispiel #5
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;
}