all: avoid quoting file names when possible
[coreutils.git] / src / mknod.c
blobe0611836808bc1efa94bf35ee645ec399bc00840
1 /* mknod -- make special files
2 Copyright (C) 1990-2015 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 "selinux.h"
30 #include "smack.h"
31 #include "xstrtol.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "mknod"
36 #define AUTHORS proper_name ("David MacKenzie")
38 static struct option const longopts[] =
40 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
41 {"mode", required_argument, NULL, 'm'},
42 {GETOPT_HELP_OPTION_DECL},
43 {GETOPT_VERSION_OPTION_DECL},
44 {NULL, 0, NULL, 0}
47 void
48 usage (int status)
50 if (status != EXIT_SUCCESS)
51 emit_try_help ();
52 else
54 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
55 program_name);
56 fputs (_("\
57 Create the special file NAME of the given TYPE.\n\
58 "), stdout);
60 emit_mandatory_arg_note ();
62 fputs (_("\
63 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
64 "), stdout);
65 fputs (_("\
66 -Z set the SELinux security context to default type\n\
67 --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
68 or SMACK security context to CTX\n\
69 "), stdout);
70 fputs (HELP_OPTION_DESCRIPTION, stdout);
71 fputs (VERSION_OPTION_DESCRIPTION, stdout);
72 fputs (_("\
73 \n\
74 Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
75 must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
76 it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
77 otherwise, as decimal. TYPE may be:\n\
78 "), stdout);
79 fputs (_("\
80 \n\
81 b create a block (buffered) special file\n\
82 c, u create a character (unbuffered) special file\n\
83 p create a FIFO\n\
84 "), stdout);
85 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
86 emit_ancillary_info (PROGRAM_NAME);
88 exit (status);
91 int
92 main (int argc, char **argv)
94 mode_t newmode;
95 char const *specified_mode = NULL;
96 int optc;
97 size_t expected_operands;
98 mode_t node_type;
99 char const *scontext = NULL;
100 bool set_security_context = false;
102 initialize_main (&argc, &argv);
103 set_program_name (argv[0]);
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 atexit (close_stdout);
110 while ((optc = getopt_long (argc, argv, "m:Z", longopts, NULL)) != -1)
112 switch (optc)
114 case 'm':
115 specified_mode = optarg;
116 break;
117 case 'Z':
118 if (is_smack_enabled ())
120 /* We don't yet support -Z to restore context with SMACK. */
121 scontext = optarg;
123 else if (is_selinux_enabled () > 0)
125 if (optarg)
126 scontext = optarg;
127 else
128 set_security_context = true;
130 else if (optarg)
132 error (0, 0,
133 _("warning: ignoring --context; "
134 "it requires an SELinux/SMACK-enabled kernel"));
136 break;
137 case_GETOPT_HELP_CHAR;
138 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
139 default:
140 usage (EXIT_FAILURE);
144 newmode = MODE_RW_UGO;
145 if (specified_mode)
147 mode_t umask_value;
148 struct mode_change *change = mode_compile (specified_mode);
149 if (!change)
150 error (EXIT_FAILURE, 0, _("invalid mode"));
151 umask_value = umask (0);
152 umask (umask_value);
153 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
154 free (change);
155 if (newmode & ~S_IRWXUGO)
156 error (EXIT_FAILURE, 0,
157 _("mode must specify only file permission bits"));
160 /* If the number of arguments is 0 or 1,
161 or (if it's 2 or more and the second one starts with 'p'), then there
162 must be exactly two operands. Otherwise, there must be four. */
163 expected_operands = (argc <= optind
164 || (optind + 1 < argc && argv[optind + 1][0] == 'p')
165 ? 2 : 4);
167 if (argc - optind < expected_operands)
169 if (argc <= optind)
170 error (0, 0, _("missing operand"));
171 else
172 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
173 if (expected_operands == 4 && argc - optind == 2)
174 fprintf (stderr, "%s\n",
175 _("Special files require major and minor device numbers."));
176 usage (EXIT_FAILURE);
179 if (expected_operands < argc - optind)
181 error (0, 0, _("extra operand %s"),
182 quote (argv[optind + expected_operands]));
183 if (expected_operands == 2 && argc - optind == 4)
184 fprintf (stderr, "%s\n",
185 _("Fifos do not have major and minor device numbers."));
186 usage (EXIT_FAILURE);
189 if (scontext)
191 int ret = 0;
192 if (is_smack_enabled ())
193 ret = smack_set_label_for_self (scontext);
194 else
195 ret = setfscreatecon (se_const (scontext));
197 if (ret < 0)
198 error (EXIT_FAILURE, errno,
199 _("failed to set default file creation context to %s"),
200 quote (scontext));
203 /* Only check the first character, to allow mnemonic usage like
204 'mknod /dev/rst0 character 18 0'. */
206 switch (argv[optind + 1][0])
208 case 'b': /* 'block' or 'buffered' */
209 #ifndef S_IFBLK
210 error (EXIT_FAILURE, 0, _("block special files not supported"));
211 #else
212 node_type = S_IFBLK;
213 #endif
214 goto block_or_character;
216 case 'c': /* 'character' */
217 case 'u': /* 'unbuffered' */
218 #ifndef S_IFCHR
219 error (EXIT_FAILURE, 0, _("character special files not supported"));
220 #else
221 node_type = S_IFCHR;
222 #endif
223 goto block_or_character;
225 block_or_character:
227 char const *s_major = argv[optind + 2];
228 char const *s_minor = argv[optind + 3];
229 uintmax_t i_major, i_minor;
230 dev_t device;
232 if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
233 || i_major != (major_t) i_major)
234 error (EXIT_FAILURE, 0,
235 _("invalid major device number %s"), quote (s_major));
237 if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
238 || i_minor != (minor_t) i_minor)
239 error (EXIT_FAILURE, 0,
240 _("invalid minor device number %s"), quote (s_minor));
242 device = makedev (i_major, i_minor);
243 #ifdef NODEV
244 if (device == NODEV)
245 error (EXIT_FAILURE, 0, _("invalid device %s %s"),
246 s_major, s_minor);
247 #endif
249 if (set_security_context)
250 defaultcon (argv[optind], node_type);
252 if (mknod (argv[optind], newmode | node_type, device) != 0)
253 error (EXIT_FAILURE, errno, "%s", quotef (argv[optind]));
255 break;
257 case 'p': /* 'pipe' */
258 if (set_security_context)
259 defaultcon (argv[optind], S_IFIFO);
260 if (mkfifo (argv[optind], newmode) != 0)
261 error (EXIT_FAILURE, errno, "%s", quotef (argv[optind]));
262 break;
264 default:
265 error (0, 0, _("invalid device type %s"), quote (argv[optind + 1]));
266 usage (EXIT_FAILURE);
269 if (specified_mode && lchmod (argv[optind], newmode) != 0)
270 error (EXIT_FAILURE, errno, _("cannot set permissions of %s"),
271 quoteaf (argv[optind]));
273 return EXIT_SUCCESS;