1 /* Create a device file relative to an open directory.  Hurd version.
2    Copyright (C) 1991-2022 Free Software Foundation, Inc.
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
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
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>
25 #include <_itoa.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/sysmacros.h>
29 #include <shlib-compat.h>
30 
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
__mknodat(int fd,const char * path,mode_t mode,dev_t dev)36 __mknodat (int fd, const char *path, mode_t mode, dev_t dev)
37 {
38   error_t errnode, err;
39   file_t dir, node;
40   char *name;
41   char buf[100], *bp;
42   const char *translator;
43   size_t len;
44 
45   if (S_ISCHR (mode))
46     {
47       translator = _HURD_CHRDEV;
48       len = sizeof (_HURD_CHRDEV);
49     }
50   else if (S_ISBLK (mode))
51     {
52       translator = _HURD_BLKDEV;
53       len = sizeof (_HURD_BLKDEV);
54     }
55   else if (S_ISFIFO (mode))
56     {
57       translator = _HURD_FIFO;
58       len = sizeof (_HURD_FIFO);
59     }
60   else if (S_ISREG (mode))
61     {
62       translator = NULL;
63       len = 0;
64     }
65   else
66     {
67       errno = EINVAL;
68       return -1;
69     }
70 
71   if (translator != NULL && ! S_ISFIFO (mode))
72     {
73       /* We set the translator to "ifmt\0major\0minor\0", where IFMT
74 	 depends on the S_IFMT bits of our MODE argument, and MAJOR and
75 	 MINOR are ASCII decimal (octal or hex would do as well)
76 	 representations of our arguments.  Thus the convention is that
77 	 CHRDEV and BLKDEV translators are invoked with two non-switch
78 	 arguments, giving the major and minor device numbers in %i format. */
79 
80       bp = buf + sizeof (buf);
81       *--bp = '\0';
82       bp = _itoa (__gnu_dev_minor (dev), bp, 10, 0);
83       *--bp = '\0';
84       bp = _itoa (__gnu_dev_major (dev), bp, 10, 0);
85       memcpy (bp - len, translator, len);
86       translator = bp - len;
87       len = buf + sizeof (buf) - translator;
88     }
89 
90   dir = __file_name_split_at (fd, path, &name);
91   if (dir == MACH_PORT_NULL)
92     return -1;
93 
94   /* Create a new, unlinked node in the target directory.  */
95   errnode = err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
96 
97   if (! err && translator != NULL)
98     /* Set the node's translator to make it a device.  */
99     err = __file_set_translator (node,
100 				 FS_TRANS_EXCL | FS_TRANS_SET,
101 				 FS_TRANS_EXCL | FS_TRANS_SET, 0,
102 				 translator, len,
103 				 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
104 
105   if (! err)
106     /* Link the node, now a valid device, into the target directory.  */
107     err = __dir_link (dir, node, name, 1);
108 
109   __mach_port_deallocate (__mach_task_self (), dir);
110   if (! errnode)
111     __mach_port_deallocate (__mach_task_self (), node);
112 
113   if (err)
114     return __hurd_fail (err);
115   return 0;
116 }
117 libc_hidden_def (__mknodat)
118 weak_alias (__mknodat, mknodat)
119