]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/xmknodat.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / xmknodat.c
CommitLineData
16a10468 1/* Create a device file relative to an open directory. Hurd version.
04277e02 2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
16a10468
RM
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
16a10468
RM
18
19#include <errno.h>
20#include <sys/stat.h>
21#include <hurd.h>
22#include <hurd/fd.h>
23#include <hurd/paths.h>
24#include <fcntl.h>
eb96ffb0 25#include <_itoa.h>
16a10468
RM
26#include <string.h>
27#include <sys/types.h>
85285a0c 28#include <sys/sysmacros.h>
16a10468
RM
29
30/* Create a device file named PATH relative to FD, with permission and
31 special bits MODE and device number DEV (which can be constructed
32 from major and minor device numbers with the `makedev' macro
33 above). */
34int
35__xmknodat (int vers, int fd, const char *path, mode_t mode, dev_t *dev)
36{
918d4d71 37 error_t errnode, err;
16a10468
RM
38 file_t dir, node;
39 char *name;
40 char buf[100], *bp;
41 const char *translator;
42 size_t len;
43
44 if (vers != _MKNOD_VER)
45 return __hurd_fail (EINVAL);
46
47 if (S_ISCHR (mode))
48 {
49 translator = _HURD_CHRDEV;
50 len = sizeof (_HURD_CHRDEV);
51 }
52 else if (S_ISBLK (mode))
53 {
54 translator = _HURD_BLKDEV;
55 len = sizeof (_HURD_BLKDEV);
56 }
57 else if (S_ISFIFO (mode))
58 {
59 translator = _HURD_FIFO;
60 len = sizeof (_HURD_FIFO);
61 }
62 else if (S_ISREG (mode))
63 {
64 translator = NULL;
65 len = 0;
66 }
67 else
68 {
69 errno = EINVAL;
70 return -1;
71 }
72
73 if (translator != NULL && ! S_ISFIFO (mode))
74 {
75 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
76 depends on the S_IFMT bits of our MODE argument, and MAJOR and
77 MINOR are ASCII decimal (octal or hex would do as well)
78 representations of our arguments. Thus the convention is that
79 CHRDEV and BLKDEV translators are invoked with two non-switch
80 arguments, giving the major and minor device numbers in %i format. */
81
82 bp = buf + sizeof (buf);
83 *--bp = '\0';
8b4a1182 84 bp = _itoa (__gnu_dev_minor (*dev), bp, 10, 0);
16a10468 85 *--bp = '\0';
8b4a1182 86 bp = _itoa (__gnu_dev_major (*dev), bp, 10, 0);
16a10468
RM
87 memcpy (bp - len, translator, len);
88 translator = bp - len;
89 len = buf + sizeof (buf) - translator;
90 }
91
92 dir = __file_name_split_at (fd, path, &name);
93 if (dir == MACH_PORT_NULL)
94 return -1;
95
96 /* Create a new, unlinked node in the target directory. */
918d4d71 97 errnode = err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
16a10468
RM
98
99 if (! err && translator != NULL)
100 /* Set the node's translator to make it a device. */
101 err = __file_set_translator (node,
102 FS_TRANS_EXCL | FS_TRANS_SET,
103 FS_TRANS_EXCL | FS_TRANS_SET, 0,
104 translator, len,
105 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
106
107 if (! err)
108 /* Link the node, now a valid device, into the target directory. */
109 err = __dir_link (dir, node, name, 1);
110
111 __mach_port_deallocate (__mach_task_self (), dir);
918d4d71
ST
112 if (! errnode)
113 __mach_port_deallocate (__mach_task_self (), node);
16a10468
RM
114
115 if (err)
116 return __hurd_fail (err);
117 return 0;
118}
6e45e6ef
ST
119
120libc_hidden_def (__xmknodat)