shred: enable direct I/O when possible
[coreutils.git] / src / mknod.c
blobc79468c6dd2a4f84587b8d0b89cadc988e6d39ea
1 /* mknod -- make special files
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@ai.mit.edu> */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
25 #include "system.h"
26 #include "error.h"
27 #include "modechange.h"
28 #include "quote.h"
29 #include "smack.h"
30 #include "xstrtol.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME "mknod"
35 #define AUTHORS proper_name ("David MacKenzie")
37 static struct option const longopts[] =
39 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
40 {"mode", required_argument, NULL, 'm'},
41 {GETOPT_HELP_OPTION_DECL},
42 {GETOPT_VERSION_OPTION_DECL},
43 {NULL, 0, NULL, 0}
46 void
47 usage (int status)
49 if (status != EXIT_SUCCESS)
50 emit_try_help ();
51 else
53 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
54 program_name);
55 fputs (_("\
56 Create the special file NAME of the given TYPE.\n\
57 "), stdout);
59 emit_mandatory_arg_note ();
61 fputs (_("\
62 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
63 "), stdout);
64 fputs (_("\
65 -Z, --context=CTX set the SELinux security context of NAME to CTX\n\
66 "), stdout);
67 fputs (HELP_OPTION_DESCRIPTION, stdout);
68 fputs (VERSION_OPTION_DESCRIPTION, stdout);
69 fputs (_("\
70 \n\
71 Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
72 must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
73 it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
74 otherwise, as decimal. TYPE may be:\n\
75 "), stdout);
76 fputs (_("\
77 \n\
78 b create a block (buffered) special file\n\
79 c, u create a character (unbuffered) special file\n\
80 p create a FIFO\n\
81 "), stdout);
82 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
83 emit_ancillary_info ();
85 exit (status);
88 int
89 main (int argc, char **argv)
91 mode_t newmode;
92 char const *specified_mode = NULL;
93 int optc;
94 int expected_operands;
95 mode_t node_type;
96 security_context_t scontext = NULL;
98 initialize_main (&argc, &argv);
99 set_program_name (argv[0]);
100 setlocale (LC_ALL, "");
101 bindtextdomain (PACKAGE, LOCALEDIR);
102 textdomain (PACKAGE);
104 atexit (close_stdout);
106 while ((optc = getopt_long (argc, argv, "m:Z:", longopts, NULL)) != -1)
108 switch (optc)
110 case 'm':
111 specified_mode = optarg;
112 break;
113 case 'Z':
114 scontext = optarg;
115 break;
116 case_GETOPT_HELP_CHAR;
117 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
118 default:
119 usage (EXIT_FAILURE);
123 newmode = MODE_RW_UGO;
124 if (specified_mode)
126 mode_t umask_value;
127 struct mode_change *change = mode_compile (specified_mode);
128 if (!change)
129 error (EXIT_FAILURE, 0, _("invalid mode"));
130 umask_value = umask (0);
131 umask (umask_value);
132 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
133 free (change);
134 if (newmode & ~S_IRWXUGO)
135 error (EXIT_FAILURE, 0,
136 _("mode must specify only file permission bits"));
139 /* If the number of arguments is 0 or 1,
140 or (if it's 2 or more and the second one starts with 'p'), then there
141 must be exactly two operands. Otherwise, there must be four. */
142 expected_operands = (argc <= optind
143 || (optind + 1 < argc && argv[optind + 1][0] == 'p')
144 ? 2 : 4);
146 if (argc - optind < expected_operands)
148 if (argc <= optind)
149 error (0, 0, _("missing operand"));
150 else
151 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
152 if (expected_operands == 4 && argc - optind == 2)
153 fprintf (stderr, "%s\n",
154 _("Special files require major and minor device numbers."));
155 usage (EXIT_FAILURE);
158 if (expected_operands < argc - optind)
160 error (0, 0, _("extra operand %s"),
161 quote (argv[optind + expected_operands]));
162 if (expected_operands == 2 && argc - optind == 4)
163 fprintf (stderr, "%s\n",
164 _("Fifos do not have major and minor device numbers."));
165 usage (EXIT_FAILURE);
168 if (scontext)
170 int ret = 0;
171 if (is_smack_enabled ())
172 ret = smack_set_label_for_self (scontext);
173 else
174 ret = setfscreatecon (scontext);
176 if (ret < 0)
177 error (EXIT_FAILURE, errno,
178 _("failed to set default file creation context to %s"),
179 quote (scontext));
182 /* Only check the first character, to allow mnemonic usage like
183 'mknod /dev/rst0 character 18 0'. */
185 switch (argv[optind + 1][0])
187 case 'b': /* 'block' or 'buffered' */
188 #ifndef S_IFBLK
189 error (EXIT_FAILURE, 0, _("block special files not supported"));
190 #else
191 node_type = S_IFBLK;
192 #endif
193 goto block_or_character;
195 case 'c': /* 'character' */
196 case 'u': /* 'unbuffered' */
197 #ifndef S_IFCHR
198 error (EXIT_FAILURE, 0, _("character special files not supported"));
199 #else
200 node_type = S_IFCHR;
201 #endif
202 goto block_or_character;
204 block_or_character:
206 char const *s_major = argv[optind + 2];
207 char const *s_minor = argv[optind + 3];
208 uintmax_t i_major, i_minor;
209 dev_t device;
211 if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
212 || i_major != (major_t) i_major)
213 error (EXIT_FAILURE, 0,
214 _("invalid major device number %s"), quote (s_major));
216 if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
217 || i_minor != (minor_t) i_minor)
218 error (EXIT_FAILURE, 0,
219 _("invalid minor device number %s"), quote (s_minor));
221 device = makedev (i_major, i_minor);
222 #ifdef NODEV
223 if (device == NODEV)
224 error (EXIT_FAILURE, 0, _("invalid device %s %s"), s_major, s_minor);
225 #endif
227 if (mknod (argv[optind], newmode | node_type, device) != 0)
228 error (EXIT_FAILURE, errno, "%s", quote (argv[optind]));
230 break;
232 case 'p': /* 'pipe' */
233 if (mkfifo (argv[optind], newmode) != 0)
234 error (EXIT_FAILURE, errno, "%s", quote (argv[optind]));
235 break;
237 default:
238 error (0, 0, _("invalid device type %s"), quote (argv[optind + 1]));
239 usage (EXIT_FAILURE);
242 if (specified_mode && lchmod (argv[optind], newmode) != 0)
243 error (EXIT_FAILURE, errno, _("cannot set permissions of `%s'"),
244 quote (argv[optind]));
246 exit (EXIT_SUCCESS);