Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/mkdir.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* mkdir -- make directories | - | ||||||||||||
2 | Copyright (C) 1990-2018 Free Software Foundation, Inc. | - | ||||||||||||
3 | - | |||||||||||||
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. | - | ||||||||||||
8 | - | |||||||||||||
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. | - | ||||||||||||
13 | - | |||||||||||||
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/>. */ | - | ||||||||||||
16 | - | |||||||||||||
17 | /* David MacKenzie <djm@ai.mit.edu> */ | - | ||||||||||||
18 | - | |||||||||||||
19 | #include <config.h> | - | ||||||||||||
20 | #include <stdio.h> | - | ||||||||||||
21 | #include <getopt.h> | - | ||||||||||||
22 | #include <sys/types.h> | - | ||||||||||||
23 | #include <selinux/selinux.h> | - | ||||||||||||
24 | - | |||||||||||||
25 | #include "system.h" | - | ||||||||||||
26 | #include "die.h" | - | ||||||||||||
27 | #include "error.h" | - | ||||||||||||
28 | #include "mkdir-p.h" | - | ||||||||||||
29 | #include "modechange.h" | - | ||||||||||||
30 | #include "prog-fprintf.h" | - | ||||||||||||
31 | #include "quote.h" | - | ||||||||||||
32 | #include "savewd.h" | - | ||||||||||||
33 | #include "selinux.h" | - | ||||||||||||
34 | #include "smack.h" | - | ||||||||||||
35 | - | |||||||||||||
36 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||||||||
37 | #define PROGRAM_NAME "mkdir" | - | ||||||||||||
38 | - | |||||||||||||
39 | #define AUTHORS proper_name ("David MacKenzie") | - | ||||||||||||
40 | - | |||||||||||||
41 | static struct option const longopts[] = | - | ||||||||||||
42 | { | - | ||||||||||||
43 | {GETOPT_SELINUX_CONTEXT_OPTION_DECL}, | - | ||||||||||||
44 | {"mode", required_argument, NULL, 'm'}, | - | ||||||||||||
45 | {"parents", no_argument, NULL, 'p'}, | - | ||||||||||||
46 | {"verbose", no_argument, NULL, 'v'}, | - | ||||||||||||
47 | {GETOPT_HELP_OPTION_DECL}, | - | ||||||||||||
48 | {GETOPT_VERSION_OPTION_DECL}, | - | ||||||||||||
49 | {NULL, 0, NULL, 0} | - | ||||||||||||
50 | }; | - | ||||||||||||
51 | - | |||||||||||||
52 | void | - | ||||||||||||
53 | usage (int status) | - | ||||||||||||
54 | { | - | ||||||||||||
55 | if (status != EXIT_SUCCESS)
| 1-2 | ||||||||||||
56 | emit_try_help (); executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
57 | else | - | ||||||||||||
58 | { | - | ||||||||||||
59 | printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name); | - | ||||||||||||
60 | fputs (_("\ | - | ||||||||||||
61 | Create the DIRECTORY(ies), if they do not already exist.\n\ | - | ||||||||||||
62 | "), stdout); | - | ||||||||||||
63 | - | |||||||||||||
64 | emit_mandatory_arg_note (); | - | ||||||||||||
65 | - | |||||||||||||
66 | fputs (_("\ | - | ||||||||||||
67 | -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\ | - | ||||||||||||
68 | -p, --parents no error if existing, make parent directories as needed\n\ | - | ||||||||||||
69 | -v, --verbose print a message for each created directory\n\ | - | ||||||||||||
70 | "), stdout); | - | ||||||||||||
71 | fputs (_("\ | - | ||||||||||||
72 | -Z set SELinux security context of each created directory\n\ | - | ||||||||||||
73 | to the default type\n\ | - | ||||||||||||
74 | --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\ | - | ||||||||||||
75 | or SMACK security context to CTX\n\ | - | ||||||||||||
76 | "), stdout); | - | ||||||||||||
77 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
78 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
79 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||||||||
80 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
81 | exit (status); executed 3 times by 1 test: exit (status); Executed by:
| 3 | ||||||||||||
82 | } | - | ||||||||||||
83 | - | |||||||||||||
84 | /* Options passed to subsidiary functions. */ | - | ||||||||||||
85 | struct mkdir_options | - | ||||||||||||
86 | { | - | ||||||||||||
87 | /* Function to make an ancestor, or NULL if ancestors should not be | - | ||||||||||||
88 | made. */ | - | ||||||||||||
89 | int (*make_ancestor_function) (char const *, char const *, void *); | - | ||||||||||||
90 | - | |||||||||||||
91 | /* Umask value in effect. */ | - | ||||||||||||
92 | mode_t umask_value; | - | ||||||||||||
93 | - | |||||||||||||
94 | /* Mode for directory itself. */ | - | ||||||||||||
95 | mode_t mode; | - | ||||||||||||
96 | - | |||||||||||||
97 | /* File mode bits affected by MODE. */ | - | ||||||||||||
98 | mode_t mode_bits; | - | ||||||||||||
99 | - | |||||||||||||
100 | /* Set the SELinux File Context. */ | - | ||||||||||||
101 | bool set_security_context; | - | ||||||||||||
102 | - | |||||||||||||
103 | /* If not null, format to use when reporting newly made directories. */ | - | ||||||||||||
104 | char const *created_directory_format; | - | ||||||||||||
105 | }; | - | ||||||||||||
106 | - | |||||||||||||
107 | /* Report that directory DIR was made, if OPTIONS requests this. */ | - | ||||||||||||
108 | static void | - | ||||||||||||
109 | announce_mkdir (char const *dir, void *options) | - | ||||||||||||
110 | { | - | ||||||||||||
111 | struct mkdir_options const *o = options; | - | ||||||||||||
112 | if (o->created_directory_format)
| 0-22392 | ||||||||||||
113 | prog_fprintf (stdout, o->created_directory_format, quoteaf (dir)); never executed: prog_fprintf ( stdout , o->created_directory_format, quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
114 | } executed 22392 times by 1 test: end of block Executed by:
| 22392 | ||||||||||||
115 | - | |||||||||||||
116 | /* Make ancestor directory DIR, whose last component is COMPONENT, | - | ||||||||||||
117 | with options OPTIONS. Assume the working directory is COMPONENT's | - | ||||||||||||
118 | parent. Return 0 if successful and the resulting directory is | - | ||||||||||||
119 | readable, 1 if successful but the resulting directory is not | - | ||||||||||||
120 | readable, -1 (setting errno) otherwise. */ | - | ||||||||||||
121 | static int | - | ||||||||||||
122 | make_ancestor (char const *dir, char const *component, void *options) | - | ||||||||||||
123 | { | - | ||||||||||||
124 | struct mkdir_options const *o = options; | - | ||||||||||||
125 | - | |||||||||||||
126 | if (o->set_security_context && defaultcon (component, S_IFDIR) < 0
| 0-12282 | ||||||||||||
127 | && ! ignorable_ctx_err (errno))
| 0 | ||||||||||||
128 | error (0, errno, _("failed to set default creation context for %s"), never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to set default creation context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
129 | quoteaf (dir)); never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to set default creation context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
130 | - | |||||||||||||
131 | mode_t user_wx = S_IWUSR | S_IXUSR; | - | ||||||||||||
132 | bool self_denying_umask = (o->umask_value & user_wx) != 0; | - | ||||||||||||
133 | if (self_denying_umask)
| 0-12282 | ||||||||||||
134 | umask (o->umask_value & ~user_wx); never executed: umask (o->umask_value & ~user_wx); | 0 | ||||||||||||
135 | int r = mkdir (component, S_IRWXUGO); | - | ||||||||||||
136 | if (self_denying_umask)
| 0-12282 | ||||||||||||
137 | { | - | ||||||||||||
138 | int mkdir_errno = errno; | - | ||||||||||||
139 | umask (o->umask_value); | - | ||||||||||||
140 | errno = mkdir_errno; | - | ||||||||||||
141 | } never executed: end of block | 0 | ||||||||||||
142 | if (r == 0)
| 158-12124 | ||||||||||||
143 | { | - | ||||||||||||
144 | r = (o->umask_value & S_IRUSR) != 0; | - | ||||||||||||
145 | announce_mkdir (dir, options); | - | ||||||||||||
146 | } executed 12124 times by 1 test: end of block Executed by:
| 12124 | ||||||||||||
147 | return r; executed 12282 times by 1 test: return r; Executed by:
| 12282 | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | /* Process a command-line file name. */ | - | ||||||||||||
151 | static int | - | ||||||||||||
152 | process_dir (char *dir, struct savewd *wd, void *options) | - | ||||||||||||
153 | { | - | ||||||||||||
154 | struct mkdir_options const *o = options; | - | ||||||||||||
155 | - | |||||||||||||
156 | /* If possible set context before DIR created. */ | - | ||||||||||||
157 | if (o->set_security_context)
| 0-10268 | ||||||||||||
158 | { | - | ||||||||||||
159 | if (! o->make_ancestor_function && defaultcon (dir, S_IFDIR) < 0
| 0 | ||||||||||||
160 | && ! ignorable_ctx_err (errno))
| 0 | ||||||||||||
161 | error (0, errno, _("failed to set default creation context for %s"), never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to set default creation context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
162 | quoteaf (dir)); never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to set default creation context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
163 | } never executed: end of block | 0 | ||||||||||||
164 | - | |||||||||||||
165 | int ret = (make_dir_parents (dir, wd, o->make_ancestor_function, options,
| 0-10268 | ||||||||||||
166 | o->mode, announce_mkdir,
| 0-10268 | ||||||||||||
167 | o->mode_bits, (uid_t) -1, (gid_t) -1, true)
| 0-10268 | ||||||||||||
168 | ? EXIT_SUCCESS | - | ||||||||||||
169 | : EXIT_FAILURE); | - | ||||||||||||
170 | - | |||||||||||||
171 | /* FIXME: Due to the current structure of make_dir_parents() | - | ||||||||||||
172 | we don't have the facility to call defaultcon() before the | - | ||||||||||||
173 | final component of DIR is created. So for now, create the | - | ||||||||||||
174 | final component with the context from previous component | - | ||||||||||||
175 | and here we set the context for the final component. */ | - | ||||||||||||
176 | if (ret == EXIT_SUCCESS && o->set_security_context
| 0-10268 | ||||||||||||
177 | && o->make_ancestor_function)
| 0 | ||||||||||||
178 | { | - | ||||||||||||
179 | if (! restorecon (last_component (dir), false, false)
| 0 | ||||||||||||
180 | && ! ignorable_ctx_err (errno))
| 0 | ||||||||||||
181 | error (0, errno, _("failed to restore context for %s"), never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to restore context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
182 | quoteaf (dir)); never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), "failed to restore context for %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir)); | 0 | ||||||||||||
183 | } never executed: end of block | 0 | ||||||||||||
184 | - | |||||||||||||
185 | return ret; executed 10268 times by 1 test: return ret; Executed by:
| 10268 | ||||||||||||
186 | } | - | ||||||||||||
187 | - | |||||||||||||
188 | int | - | ||||||||||||
189 | main (int argc, char **argv) | - | ||||||||||||
190 | { | - | ||||||||||||
191 | const char *specified_mode = NULL; | - | ||||||||||||
192 | int optc; | - | ||||||||||||
193 | char const *scontext = NULL; | - | ||||||||||||
194 | struct mkdir_options options; | - | ||||||||||||
195 | - | |||||||||||||
196 | options.make_ancestor_function = NULL; | - | ||||||||||||
197 | options.mode = S_IRWXUGO; | - | ||||||||||||
198 | options.mode_bits = 0; | - | ||||||||||||
199 | options.created_directory_format = NULL; | - | ||||||||||||
200 | options.set_security_context = false; | - | ||||||||||||
201 | - | |||||||||||||
202 | initialize_main (&argc, &argv); | - | ||||||||||||
203 | set_program_name (argv[0]); | - | ||||||||||||
204 | setlocale (LC_ALL, ""); | - | ||||||||||||
205 | bindtextdomain (PACKAGE, LOCALEDIR); | - | ||||||||||||
206 | textdomain (PACKAGE); | - | ||||||||||||
207 | - | |||||||||||||
208 | atexit (close_stdout); | - | ||||||||||||
209 | - | |||||||||||||
210 | while ((optc = getopt_long (argc, argv, "pm:vZ", longopts, NULL)) != -1)
| 106-247 | ||||||||||||
211 | { | - | ||||||||||||
212 | switch (optc) | - | ||||||||||||
213 | { | - | ||||||||||||
214 | case 'p': executed 97 times by 1 test: case 'p': Executed by:
| 97 | ||||||||||||
215 | options.make_ancestor_function = make_ancestor; | - | ||||||||||||
216 | break; executed 97 times by 1 test: break; Executed by:
| 97 | ||||||||||||
217 | case 'm': executed 3 times by 1 test: case 'm': Executed by:
| 3 | ||||||||||||
218 | specified_mode = optarg; | - | ||||||||||||
219 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||
220 | case 'v': /* --verbose */ never executed: case 'v': | 0 | ||||||||||||
221 | options.created_directory_format = _("created directory %s"); | - | ||||||||||||
222 | break; never executed: break; | 0 | ||||||||||||
223 | case 'Z': never executed: case 'Z': | 0 | ||||||||||||
224 | if (is_smack_enabled ())
| 0 | ||||||||||||
225 | { | - | ||||||||||||
226 | /* We don't yet support -Z to restore context with SMACK. */ | - | ||||||||||||
227 | scontext = optarg; | - | ||||||||||||
228 | } never executed: end of block | 0 | ||||||||||||
229 | else if (is_selinux_enabled () > 0)
| 0 | ||||||||||||
230 | { | - | ||||||||||||
231 | if (optarg)
| 0 | ||||||||||||
232 | scontext = optarg; never executed: scontext = optarg; | 0 | ||||||||||||
233 | else | - | ||||||||||||
234 | options.set_security_context = true; never executed: options.set_security_context = 1 ; | 0 | ||||||||||||
235 | } | - | ||||||||||||
236 | else if (optarg)
| 0 | ||||||||||||
237 | { | - | ||||||||||||
238 | error (0, 0, | - | ||||||||||||
239 | _("warning: ignoring --context; " | - | ||||||||||||
240 | "it requires an SELinux/SMACK-enabled kernel")); | - | ||||||||||||
241 | } never executed: end of block | 0 | ||||||||||||
242 | break; never executed: break; | 0 | ||||||||||||
243 | case_GETOPT_HELP_CHAR; never executed: break; executed 2 times by 1 test: case GETOPT_HELP_CHAR: Executed by:
| 0-2 | ||||||||||||
244 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); executed 3 times by 1 test: exit ( 0 ); Executed by:
never executed: break; executed 3 times by 1 test: case GETOPT_VERSION_CHAR: Executed by:
| 0-3 | ||||||||||||
245 | default: executed 1 time by 1 test: default: Executed by:
| 1 | ||||||||||||
246 | usage (EXIT_FAILURE); | - | ||||||||||||
247 | } never executed: end of block | 0 | ||||||||||||
248 | } | - | ||||||||||||
249 | - | |||||||||||||
250 | if (optind == argc)
| 0-247 | ||||||||||||
251 | { | - | ||||||||||||
252 | error (0, 0, _("missing operand")); | - | ||||||||||||
253 | usage (EXIT_FAILURE); | - | ||||||||||||
254 | } never executed: end of block | 0 | ||||||||||||
255 | - | |||||||||||||
256 | /* FIXME: This assumes mkdir() is done in the same process. | - | ||||||||||||
257 | If that's not always the case we would need to call this | - | ||||||||||||
258 | like we do when options.set_security_context == true. */ | - | ||||||||||||
259 | if (scontext)
| 0-247 | ||||||||||||
260 | { | - | ||||||||||||
261 | int ret = 0; | - | ||||||||||||
262 | if (is_smack_enabled ())
| 0 | ||||||||||||
263 | ret = smack_set_label_for_self (scontext); never executed: ret = smack_set_label_for_self (scontext); | 0 | ||||||||||||
264 | else | - | ||||||||||||
265 | ret = setfscreatecon (se_const (scontext)); never executed: ret = setfscreatecon (se_const (scontext)); | 0 | ||||||||||||
266 | - | |||||||||||||
267 | if (ret < 0)
| 0 | ||||||||||||
268 | die (EXIT_FAILURE, errno, never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to set default file creation context to %s\", 5), quote (scontext)), assume (false))" ")"); int _gl_dummy; })) ? ((erro... (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to set default file creation context to %s" , 5) , quote (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
269 | _("failed to set default file creation context to %s"), never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to set default file creation context to %s\", 5), quote (scontext)), assume (false))" ")"); int _gl_dummy; })) ? ((erro... (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to set default file creation context to %s" , 5) , quote (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
270 | quote (scontext)); never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to set default file creation context to %s\", 5), quote (scontext)), assume (false))" ")"); int _gl_dummy; })) ? ((erro... (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to set default file creation context to %s" , 5) , quote (scontext)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
271 | } never executed: end of block | 0 | ||||||||||||
272 | - | |||||||||||||
273 | - | |||||||||||||
274 | if (options.make_ancestor_function || specified_mode)
| 2-150 | ||||||||||||
275 | { | - | ||||||||||||
276 | mode_t umask_value = umask (0); | - | ||||||||||||
277 | umask (umask_value); | - | ||||||||||||
278 | options.umask_value = umask_value; | - | ||||||||||||
279 | - | |||||||||||||
280 | if (specified_mode)
| 3-96 | ||||||||||||
281 | { | - | ||||||||||||
282 | struct mode_change *change = mode_compile (specified_mode); | - | ||||||||||||
283 | if (!change)
| 0-3 | ||||||||||||
284 | die (EXIT_FAILURE, 0, _("invalid mode %s"), never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"invalid mode %s\", 5), quote (specified_mode)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "invalid mode %s" , 5) , quote (specified_mode)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "invalid mode %s" , 5) , quote (specified_mode)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
285 | quote (specified_mode)); never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"invalid mode %s\", 5), quote (specified_mode)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "invalid mode %s" , 5) , quote (specified_mode)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "invalid mode %s" , 5) , quote (specified_mode)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
286 | options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change, | - | ||||||||||||
287 | &options.mode_bits); | - | ||||||||||||
288 | free (change); | - | ||||||||||||
289 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
290 | else | - | ||||||||||||
291 | options.mode = S_IRWXUGO; executed 96 times by 1 test: options.mode = ((0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3)) ; Executed by:
| 96 | ||||||||||||
292 | } | - | ||||||||||||
293 | - | |||||||||||||
294 | return savewd_process_files (argc - optind, argv + optind, executed 247 times by 1 test: return savewd_process_files (argc - optind, argv + optind, process_dir, &options); Executed by:
| 247 | ||||||||||||
295 | process_dir, &options); executed 247 times by 1 test: return savewd_process_files (argc - optind, argv + optind, process_dir, &options); Executed by:
| 247 | ||||||||||||
296 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |