1 /* mknod -- make special files
2 Copyright (C) 1990-2018 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 <https://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@ai.mit.edu> */
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
28 #include "modechange.h"
34 /* The official name of this program (e.g., no 'g' prefix). */
35 #define PROGRAM_NAME "mknod"
37 #define AUTHORS proper_name ("David MacKenzie")
39 static struct option
const longopts
[] =
41 {GETOPT_SELINUX_CONTEXT_OPTION_DECL
},
42 {"mode", required_argument
, NULL
, 'm'},
43 {GETOPT_HELP_OPTION_DECL
},
44 {GETOPT_VERSION_OPTION_DECL
},
51 if (status
!= EXIT_SUCCESS
)
55 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
58 Create the special file NAME of the given TYPE.\n\
61 emit_mandatory_arg_note ();
64 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
67 -Z set the SELinux security context to default type\n\
68 --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
69 or SMACK security context to CTX\n\
71 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
72 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
75 Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
76 must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
77 it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
78 otherwise, as decimal. TYPE may be:\n\
82 b create a block (buffered) special file\n\
83 c, u create a character (unbuffered) special file\n\
86 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
87 emit_ancillary_info (PROGRAM_NAME
);
93 main (int argc
, char **argv
)
96 char const *specified_mode
= NULL
;
98 size_t expected_operands
;
100 char const *scontext
= NULL
;
101 bool set_security_context
= false;
103 initialize_main (&argc
, &argv
);
104 set_program_name (argv
[0]);
105 setlocale (LC_ALL
, "");
106 bindtextdomain (PACKAGE
, LOCALEDIR
);
107 textdomain (PACKAGE
);
109 atexit (close_stdout
);
111 while ((optc
= getopt_long (argc
, argv
, "m:Z", longopts
, NULL
)) != -1)
116 specified_mode
= optarg
;
119 if (is_smack_enabled ())
121 /* We don't yet support -Z to restore context with SMACK. */
124 else if (is_selinux_enabled () > 0)
129 set_security_context
= true;
134 _("warning: ignoring --context; "
135 "it requires an SELinux/SMACK-enabled kernel"));
138 case_GETOPT_HELP_CHAR
;
139 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
141 usage (EXIT_FAILURE
);
145 newmode
= MODE_RW_UGO
;
149 struct mode_change
*change
= mode_compile (specified_mode
);
151 die (EXIT_FAILURE
, 0, _("invalid mode"));
152 umask_value
= umask (0);
154 newmode
= mode_adjust (newmode
, false, umask_value
, change
, NULL
);
156 if (newmode
& ~S_IRWXUGO
)
157 die (EXIT_FAILURE
, 0,
158 _("mode must specify only file permission bits"));
161 /* If the number of arguments is 0 or 1,
162 or (if it's 2 or more and the second one starts with 'p'), then there
163 must be exactly two operands. Otherwise, there must be four. */
164 expected_operands
= (argc
<= optind
165 || (optind
+ 1 < argc
&& argv
[optind
+ 1][0] == 'p')
168 if (argc
- optind
< expected_operands
)
171 error (0, 0, _("missing operand"));
173 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
174 if (expected_operands
== 4 && argc
- optind
== 2)
175 fprintf (stderr
, "%s\n",
176 _("Special files require major and minor device numbers."));
177 usage (EXIT_FAILURE
);
180 if (expected_operands
< argc
- optind
)
182 error (0, 0, _("extra operand %s"),
183 quote (argv
[optind
+ expected_operands
]));
184 if (expected_operands
== 2 && argc
- optind
== 4)
185 fprintf (stderr
, "%s\n",
186 _("Fifos do not have major and minor device numbers."));
187 usage (EXIT_FAILURE
);
193 if (is_smack_enabled ())
194 ret
= smack_set_label_for_self (scontext
);
196 ret
= setfscreatecon (se_const (scontext
));
199 die (EXIT_FAILURE
, errno
,
200 _("failed to set default file creation context to %s"),
204 /* Only check the first character, to allow mnemonic usage like
205 'mknod /dev/rst0 character 18 0'. */
207 switch (argv
[optind
+ 1][0])
209 case 'b': /* 'block' or 'buffered' */
211 die (EXIT_FAILURE
, 0, _("block special files not supported"));
215 goto block_or_character
;
217 case 'c': /* 'character' */
218 case 'u': /* 'unbuffered' */
220 die (EXIT_FAILURE
, 0, _("character special files not supported"));
224 goto block_or_character
;
228 char const *s_major
= argv
[optind
+ 2];
229 char const *s_minor
= argv
[optind
+ 3];
230 uintmax_t i_major
, i_minor
;
233 if (xstrtoumax (s_major
, NULL
, 0, &i_major
, NULL
) != LONGINT_OK
234 || i_major
!= (major_t
) i_major
)
235 die (EXIT_FAILURE
, 0,
236 _("invalid major device number %s"), quote (s_major
));
238 if (xstrtoumax (s_minor
, NULL
, 0, &i_minor
, NULL
) != LONGINT_OK
239 || i_minor
!= (minor_t
) i_minor
)
240 die (EXIT_FAILURE
, 0,
241 _("invalid minor device number %s"), quote (s_minor
));
243 device
= makedev (i_major
, i_minor
);
246 die (EXIT_FAILURE
, 0, _("invalid device %s %s"),
250 if (set_security_context
)
251 defaultcon (argv
[optind
], node_type
);
253 if (mknod (argv
[optind
], newmode
| node_type
, device
) != 0)
254 die (EXIT_FAILURE
, errno
, "%s", quotef (argv
[optind
]));
258 case 'p': /* 'pipe' */
259 if (set_security_context
)
260 defaultcon (argv
[optind
], S_IFIFO
);
261 if (mkfifo (argv
[optind
], newmode
) != 0)
262 die (EXIT_FAILURE
, errno
, "%s", quotef (argv
[optind
]));
266 error (0, 0, _("invalid device type %s"), quote (argv
[optind
+ 1]));
267 usage (EXIT_FAILURE
);
270 if (specified_mode
&& lchmod (argv
[optind
], newmode
) != 0)
271 die (EXIT_FAILURE
, errno
, _("cannot set permissions of %s"),
272 quoteaf (argv
[optind
]));