Blame sysdeps/mach/hurd/xmknodat.c

Packit 6c4009
/* Create a device file relative to an open directory.  Hurd version.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/paths.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/sysmacros.h>
Packit 6c4009
Packit 6c4009
/* Create a device file named PATH relative to FD, with permission and
Packit 6c4009
   special bits MODE and device number DEV (which can be constructed
Packit 6c4009
   from major and minor device numbers with the `makedev' macro
Packit 6c4009
   above).  */
Packit 6c4009
int
Packit 6c4009
__xmknodat (int vers, int fd, const char *path, mode_t mode, dev_t *dev)
Packit 6c4009
{
Packit 6c4009
  error_t errnode, err;
Packit 6c4009
  file_t dir, node;
Packit 6c4009
  char *name;
Packit 6c4009
  char buf[100], *bp;
Packit 6c4009
  const char *translator;
Packit 6c4009
  size_t len;
Packit 6c4009
Packit 6c4009
  if (vers != _MKNOD_VER)
Packit 6c4009
    return __hurd_fail (EINVAL);
Packit 6c4009
Packit 6c4009
  if (S_ISCHR (mode))
Packit 6c4009
    {
Packit 6c4009
      translator = _HURD_CHRDEV;
Packit 6c4009
      len = sizeof (_HURD_CHRDEV);
Packit 6c4009
    }
Packit 6c4009
  else if (S_ISBLK (mode))
Packit 6c4009
    {
Packit 6c4009
      translator = _HURD_BLKDEV;
Packit 6c4009
      len = sizeof (_HURD_BLKDEV);
Packit 6c4009
    }
Packit 6c4009
  else if (S_ISFIFO (mode))
Packit 6c4009
    {
Packit 6c4009
      translator = _HURD_FIFO;
Packit 6c4009
      len = sizeof (_HURD_FIFO);
Packit 6c4009
    }
Packit 6c4009
  else if (S_ISREG (mode))
Packit 6c4009
    {
Packit 6c4009
      translator = NULL;
Packit 6c4009
      len = 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      errno = EINVAL;
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (translator != NULL && ! S_ISFIFO (mode))
Packit 6c4009
    {
Packit 6c4009
      /* We set the translator to "ifmt\0major\0minor\0", where IFMT
Packit 6c4009
	 depends on the S_IFMT bits of our MODE argument, and MAJOR and
Packit 6c4009
	 MINOR are ASCII decimal (octal or hex would do as well)
Packit 6c4009
	 representations of our arguments.  Thus the convention is that
Packit 6c4009
	 CHRDEV and BLKDEV translators are invoked with two non-switch
Packit 6c4009
	 arguments, giving the major and minor device numbers in %i format. */
Packit 6c4009
Packit 6c4009
      bp = buf + sizeof (buf);
Packit 6c4009
      *--bp = '\0';
Packit 6c4009
      bp = _itoa (__gnu_dev_minor (*dev), bp, 10, 0);
Packit 6c4009
      *--bp = '\0';
Packit 6c4009
      bp = _itoa (__gnu_dev_major (*dev), bp, 10, 0);
Packit 6c4009
      memcpy (bp - len, translator, len);
Packit 6c4009
      translator = bp - len;
Packit 6c4009
      len = buf + sizeof (buf) - translator;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dir = __file_name_split_at (fd, path, &name);
Packit 6c4009
  if (dir == MACH_PORT_NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Create a new, unlinked node in the target directory.  */
Packit 6c4009
  errnode = err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
Packit 6c4009
Packit 6c4009
  if (! err && translator != NULL)
Packit 6c4009
    /* Set the node's translator to make it a device.  */
Packit 6c4009
    err = __file_set_translator (node,
Packit 6c4009
				 FS_TRANS_EXCL | FS_TRANS_SET,
Packit 6c4009
				 FS_TRANS_EXCL | FS_TRANS_SET, 0,
Packit 6c4009
				 translator, len,
Packit 6c4009
				 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
Packit 6c4009
Packit 6c4009
  if (! err)
Packit 6c4009
    /* Link the node, now a valid device, into the target directory.  */
Packit 6c4009
    err = __dir_link (dir, node, name, 1);
Packit 6c4009
Packit 6c4009
  __mach_port_deallocate (__mach_task_self (), dir);
Packit 6c4009
  if (! errnode)
Packit 6c4009
    __mach_port_deallocate (__mach_task_self (), node);
Packit 6c4009
Packit 6c4009
  if (err)
Packit 6c4009
    return __hurd_fail (err);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
libc_hidden_def (__xmknodat)