dd: synchronize output after write errors
[coreutils.git] / src / mkfifo.c
blob85c7050440d4dd528f7feb17aee040f8b9d5ee06
1 /* mkfifo -- make fifo's (named pipes)
2 Copyright (C) 1990-2022 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 /* 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/label.h>
25 #include "system.h"
26 #include "die.h"
27 #include "error.h"
28 #include "modechange.h"
29 #include "quote.h"
30 #include "selinux.h"
31 #include "smack.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "mkfifo"
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...\n"), program_name);
55 fputs (_("\
56 Create named pipes (FIFOs) with the given NAMEs.\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 set the SELinux security context to default type\n\
66 --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
67 or SMACK security context to CTX\n\
68 "), stdout);
69 fputs (HELP_OPTION_DESCRIPTION, stdout);
70 fputs (VERSION_OPTION_DESCRIPTION, stdout);
71 emit_ancillary_info (PROGRAM_NAME);
73 exit (status);
76 int
77 main (int argc, char **argv)
79 mode_t newmode;
80 char const *specified_mode = NULL;
81 int exit_status = EXIT_SUCCESS;
82 int optc;
83 char const *scontext = NULL;
84 struct selabel_handle *set_security_context = NULL;
86 initialize_main (&argc, &argv);
87 set_program_name (argv[0]);
88 setlocale (LC_ALL, "");
89 bindtextdomain (PACKAGE, LOCALEDIR);
90 textdomain (PACKAGE);
92 atexit (close_stdout);
94 while ((optc = getopt_long (argc, argv, "m:Z", longopts, NULL)) != -1)
96 switch (optc)
98 case 'm':
99 specified_mode = optarg;
100 break;
101 case 'Z':
102 if (is_smack_enabled ())
104 /* We don't yet support -Z to restore context with SMACK. */
105 scontext = optarg;
107 else if (is_selinux_enabled () > 0)
109 if (optarg)
110 scontext = optarg;
111 else
113 set_security_context = selabel_open (SELABEL_CTX_FILE,
114 NULL, 0);
115 if (! set_security_context)
116 error (0, errno, _("warning: ignoring --context"));
119 else if (optarg)
121 error (0, 0,
122 _("warning: ignoring --context; "
123 "it requires an SELinux/SMACK-enabled kernel"));
125 break;
126 case_GETOPT_HELP_CHAR;
127 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
128 default:
129 usage (EXIT_FAILURE);
133 if (optind == argc)
135 error (0, 0, _("missing operand"));
136 usage (EXIT_FAILURE);
139 if (scontext)
141 int ret = 0;
142 if (is_smack_enabled ())
143 ret = smack_set_label_for_self (scontext);
144 else
145 ret = setfscreatecon (scontext);
147 if (ret < 0)
148 die (EXIT_FAILURE, errno,
149 _("failed to set default file creation context to %s"),
150 quote (scontext));
153 newmode = MODE_RW_UGO;
154 if (specified_mode)
156 mode_t umask_value;
157 struct mode_change *change = mode_compile (specified_mode);
158 if (!change)
159 die (EXIT_FAILURE, 0, _("invalid mode"));
160 umask_value = umask (0);
161 umask (umask_value);
162 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
163 free (change);
164 if (newmode & ~S_IRWXUGO)
165 die (EXIT_FAILURE, 0,
166 _("mode must specify only file permission bits"));
169 for (; optind < argc; ++optind)
171 if (set_security_context)
172 defaultcon (set_security_context, argv[optind], S_IFIFO);
173 if (mkfifo (argv[optind], newmode) != 0)
175 error (0, errno, _("cannot create fifo %s"), quoteaf (argv[optind]));
176 exit_status = EXIT_FAILURE;
178 else if (specified_mode && lchmod (argv[optind], newmode) != 0)
180 error (0, errno, _("cannot set permissions of %s"),
181 quoteaf (argv[optind]));
182 exit_status = EXIT_FAILURE;
186 return exit_status;