2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / xmknodat.c
blobb2227593c9039c479460ce4a06e532eae1ed4719
1 /* Create a device file relative to an open directory. Hurd version.
2 Copyright (C) 1991,1992,1993,1994,1995,1996,1999,2002,2005,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <sys/stat.h>
23 #include <hurd.h>
24 #include <hurd/fd.h>
25 #include <hurd/paths.h>
26 #include <fcntl.h>
27 #include "stdio-common/_itoa.h"
28 #include <string.h>
29 #include <sys/types.h>
31 /* Create a device file named PATH relative to FD, with permission and
32 special bits MODE and device number DEV (which can be constructed
33 from major and minor device numbers with the `makedev' macro
34 above). */
35 int
36 __xmknodat (int vers, int fd, const char *path, mode_t mode, dev_t *dev)
38 error_t err;
39 file_t dir, node;
40 char *name;
41 char buf[100], *bp;
42 const char *translator;
43 size_t len;
45 if (vers != _MKNOD_VER)
46 return __hurd_fail (EINVAL);
48 if (S_ISCHR (mode))
50 translator = _HURD_CHRDEV;
51 len = sizeof (_HURD_CHRDEV);
53 else if (S_ISBLK (mode))
55 translator = _HURD_BLKDEV;
56 len = sizeof (_HURD_BLKDEV);
58 else if (S_ISFIFO (mode))
60 translator = _HURD_FIFO;
61 len = sizeof (_HURD_FIFO);
63 else if (S_ISREG (mode))
65 translator = NULL;
66 len = 0;
68 else
70 errno = EINVAL;
71 return -1;
74 if (translator != NULL && ! S_ISFIFO (mode))
76 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
77 depends on the S_IFMT bits of our MODE argument, and MAJOR and
78 MINOR are ASCII decimal (octal or hex would do as well)
79 representations of our arguments. Thus the convention is that
80 CHRDEV and BLKDEV translators are invoked with two non-switch
81 arguments, giving the major and minor device numbers in %i format. */
83 bp = buf + sizeof (buf);
84 *--bp = '\0';
85 bp = _itoa (minor (*dev), bp, 10, 0);
86 *--bp = '\0';
87 bp = _itoa (major (*dev), bp, 10, 0);
88 memcpy (bp - len, translator, len);
89 translator = bp - len;
90 len = buf + sizeof (buf) - translator;
93 dir = __file_name_split_at (fd, path, &name);
94 if (dir == MACH_PORT_NULL)
95 return -1;
97 /* Create a new, unlinked node in the target directory. */
98 err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
100 if (! err && translator != NULL)
101 /* Set the node's translator to make it a device. */
102 err = __file_set_translator (node,
103 FS_TRANS_EXCL | FS_TRANS_SET,
104 FS_TRANS_EXCL | FS_TRANS_SET, 0,
105 translator, len,
106 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
108 if (! err)
109 /* Link the node, now a valid device, into the target directory. */
110 err = __dir_link (dir, node, name, 1);
112 __mach_port_deallocate (__mach_task_self (), dir);
113 __mach_port_deallocate (__mach_task_self (), node);
115 if (err)
116 return __hurd_fail (err);
117 return 0;