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> */
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
27 #include "modechange.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
},
50 if (status
!= EXIT_SUCCESS
)
54 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
57 Create the special file NAME of the given TYPE.\n\
60 emit_mandatory_arg_note ();
63 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
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\
70 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
71 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
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\
81 b create a block (buffered) special file\n\
82 c, u create a character (unbuffered) special file\n\
85 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
86 emit_ancillary_info (PROGRAM_NAME
);
92 main (int argc
, char **argv
)
95 char const *specified_mode
= NULL
;
97 size_t expected_operands
;
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)
115 specified_mode
= optarg
;
118 if (is_smack_enabled ())
120 /* We don't yet support -Z to restore context with SMACK. */
123 else if (is_selinux_enabled () > 0)
128 set_security_context
= true;
133 _("warning: ignoring --context; "
134 "it requires an SELinux/SMACK-enabled kernel"));
137 case_GETOPT_HELP_CHAR
;
138 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
140 usage (EXIT_FAILURE
);
144 newmode
= MODE_RW_UGO
;
148 struct mode_change
*change
= mode_compile (specified_mode
);
150 error (EXIT_FAILURE
, 0, _("invalid mode"));
151 umask_value
= umask (0);
153 newmode
= mode_adjust (newmode
, false, umask_value
, change
, NULL
);
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')
167 if (argc
- optind
< expected_operands
)
170 error (0, 0, _("missing operand"));
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
);
192 if (is_smack_enabled ())
193 ret
= smack_set_label_for_self (scontext
);
195 ret
= setfscreatecon (se_const (scontext
));
198 error (EXIT_FAILURE
, errno
,
199 _("failed to set default file creation context to %s"),
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' */
210 error (EXIT_FAILURE
, 0, _("block special files not supported"));
214 goto block_or_character
;
216 case 'c': /* 'character' */
217 case 'u': /* 'unbuffered' */
219 error (EXIT_FAILURE
, 0, _("character special files not supported"));
223 goto 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
;
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
);
245 error (EXIT_FAILURE
, 0, _("invalid device %s %s"),
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
]));
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
]));
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
]));