| 1 | /* Create a device file relative to an open directory. Hurd version. |
| 2 | Copyright (C) 1991-2024 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 |
| 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 | return __hurd_fail (EINVAL); |
| 67 | |
| 68 | if (translator != NULL && ! S_ISFIFO (mode)) |
| 69 | { |
| 70 | /* We set the translator to "ifmt\0major\0minor\0", where IFMT |
| 71 | depends on the S_IFMT bits of our MODE argument, and MAJOR and |
| 72 | MINOR are ASCII decimal (octal or hex would do as well) |
| 73 | representations of our arguments. Thus the convention is that |
| 74 | CHRDEV and BLKDEV translators are invoked with two non-switch |
| 75 | arguments, giving the major and minor device numbers in %i format. */ |
| 76 | |
| 77 | bp = buf + sizeof (buf); |
| 78 | *--bp = '\0'; |
| 79 | bp = _itoa (__gnu_dev_minor (dev), bp, 10, 0); |
| 80 | *--bp = '\0'; |
| 81 | bp = _itoa (__gnu_dev_major (dev), bp, 10, 0); |
| 82 | memcpy (bp - len, translator, len); |
| 83 | translator = bp - len; |
| 84 | len = buf + sizeof (buf) - translator; |
| 85 | } |
| 86 | |
| 87 | dir = __file_name_split_at (fd, path, &name); |
| 88 | if (dir == MACH_PORT_NULL) |
| 89 | return -1; |
| 90 | |
| 91 | /* Create a new, unlinked node in the target directory. */ |
| 92 | errnode = err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node); |
| 93 | |
| 94 | if (! err && translator != NULL) |
| 95 | /* Set the node's translator to make it a device. */ |
| 96 | err = __file_set_translator (node, |
| 97 | FS_TRANS_EXCL | FS_TRANS_SET, |
| 98 | FS_TRANS_EXCL | FS_TRANS_SET, 0, |
| 99 | translator, len, |
| 100 | MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); |
| 101 | |
| 102 | if (! err) |
| 103 | /* Link the node, now a valid device, into the target directory. */ |
| 104 | err = __dir_link (dir, node, name, 1); |
| 105 | |
| 106 | __mach_port_deallocate (__mach_task_self (), dir); |
| 107 | if (! errnode) |
| 108 | __mach_port_deallocate (__mach_task_self (), node); |
| 109 | |
| 110 | if (err) |
| 111 | return __hurd_fail (err); |
| 112 | return 0; |
| 113 | } |
| 114 | libc_hidden_def (__mknodat) |
| 115 | weak_alias (__mknodat, mknodat) |
| 116 | |