build: ensure sys/select.h is included
[coreutils.git] / src / mkfifo.c
blob8ebcf481b9a3f041770e5e43acf5f1fb599bdcc2
1 /* mkfifo -- make fifo's (named pipes)
2 Copyright (C) 1990-2019 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/selinux.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 bool set_security_context = false;
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
112 set_security_context = true;
114 else if (optarg)
116 error (0, 0,
117 _("warning: ignoring --context; "
118 "it requires an SELinux/SMACK-enabled kernel"));
120 break;
121 case_GETOPT_HELP_CHAR;
122 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
123 default:
124 usage (EXIT_FAILURE);
128 if (optind == argc)
130 error (0, 0, _("missing operand"));
131 usage (EXIT_FAILURE);
134 if (scontext)
136 int ret = 0;
137 if (is_smack_enabled ())
138 ret = smack_set_label_for_self (scontext);
139 else
140 ret = setfscreatecon (se_const (scontext));
142 if (ret < 0)
143 die (EXIT_FAILURE, errno,
144 _("failed to set default file creation context to %s"),
145 quote (scontext));
148 newmode = MODE_RW_UGO;
149 if (specified_mode)
151 mode_t umask_value;
152 struct mode_change *change = mode_compile (specified_mode);
153 if (!change)
154 die (EXIT_FAILURE, 0, _("invalid mode"));
155 umask_value = umask (0);
156 umask (umask_value);
157 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
158 free (change);
159 if (newmode & ~S_IRWXUGO)
160 die (EXIT_FAILURE, 0,
161 _("mode must specify only file permission bits"));
164 for (; optind < argc; ++optind)
166 if (set_security_context)
167 defaultcon (argv[optind], S_IFIFO);
168 if (mkfifo (argv[optind], newmode) != 0)
170 error (0, errno, _("cannot create fifo %s"), quoteaf (argv[optind]));
171 exit_status = EXIT_FAILURE;
173 else if (specified_mode && lchmod (argv[optind], newmode) != 0)
175 error (0, errno, _("cannot set permissions of %s"),
176 quoteaf (argv[optind]));
177 exit_status = EXIT_FAILURE;
181 return exit_status;