tests: adjust memory limits in head-c.sh
[coreutils.git] / src / mkfifo.c
blob51361732ab98250efbe9c896db0ca9b39c532252
1 /* mkfifo -- make fifo's (named pipes)
2 Copyright (C) 1990-2016 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 /* 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 "error.h"
27 #include "modechange.h"
28 #include "quote.h"
29 #include "selinux.h"
30 #include "smack.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME "mkfifo"
35 #define AUTHORS proper_name ("David MacKenzie")
37 static struct option const longopts[] =
39 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
40 {"mode", required_argument, NULL, 'm'},
41 {GETOPT_HELP_OPTION_DECL},
42 {GETOPT_VERSION_OPTION_DECL},
43 {NULL, 0, NULL, 0}
46 void
47 usage (int status)
49 if (status != EXIT_SUCCESS)
50 emit_try_help ();
51 else
53 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
54 fputs (_("\
55 Create named pipes (FIFOs) with the given NAMEs.\n\
56 "), stdout);
58 emit_mandatory_arg_note ();
60 fputs (_("\
61 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
62 "), stdout);
63 fputs (_("\
64 -Z set the SELinux security context to default type\n\
65 --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
66 or SMACK security context to CTX\n\
67 "), stdout);
68 fputs (HELP_OPTION_DESCRIPTION, stdout);
69 fputs (VERSION_OPTION_DESCRIPTION, stdout);
70 emit_ancillary_info (PROGRAM_NAME);
72 exit (status);
75 int
76 main (int argc, char **argv)
78 mode_t newmode;
79 char const *specified_mode = NULL;
80 int exit_status = EXIT_SUCCESS;
81 int optc;
82 char const *scontext = NULL;
83 bool set_security_context = false;
85 initialize_main (&argc, &argv);
86 set_program_name (argv[0]);
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 atexit (close_stdout);
93 while ((optc = getopt_long (argc, argv, "m:Z", longopts, NULL)) != -1)
95 switch (optc)
97 case 'm':
98 specified_mode = optarg;
99 break;
100 case 'Z':
101 if (is_smack_enabled ())
103 /* We don't yet support -Z to restore context with SMACK. */
104 scontext = optarg;
106 else if (is_selinux_enabled () > 0)
108 if (optarg)
109 scontext = optarg;
110 else
111 set_security_context = true;
113 else if (optarg)
115 error (0, 0,
116 _("warning: ignoring --context; "
117 "it requires an SELinux/SMACK-enabled kernel"));
119 break;
120 case_GETOPT_HELP_CHAR;
121 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
122 default:
123 usage (EXIT_FAILURE);
127 if (optind == argc)
129 error (0, 0, _("missing operand"));
130 usage (EXIT_FAILURE);
133 if (scontext)
135 int ret = 0;
136 if (is_smack_enabled ())
137 ret = smack_set_label_for_self (scontext);
138 else
139 ret = setfscreatecon (se_const (scontext));
141 if (ret < 0)
142 error (EXIT_FAILURE, errno,
143 _("failed to set default file creation context to %s"),
144 quote (scontext));
147 newmode = MODE_RW_UGO;
148 if (specified_mode)
150 mode_t umask_value;
151 struct mode_change *change = mode_compile (specified_mode);
152 if (!change)
153 error (EXIT_FAILURE, 0, _("invalid mode"));
154 umask_value = umask (0);
155 umask (umask_value);
156 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
157 free (change);
158 if (newmode & ~S_IRWXUGO)
159 error (EXIT_FAILURE, 0,
160 _("mode must specify only file permission bits"));
163 for (; optind < argc; ++optind)
165 if (set_security_context)
166 defaultcon (argv[optind], S_IFIFO);
167 if (mkfifo (argv[optind], newmode) != 0)
169 error (0, errno, _("cannot create fifo %s"), quoteaf (argv[optind]));
170 exit_status = EXIT_FAILURE;
172 else if (specified_mode && lchmod (argv[optind], newmode) != 0)
174 error (0, errno, _("cannot set permissions of %s"),
175 quoteaf (argv[optind]));
176 exit_status = EXIT_FAILURE;
180 return exit_status;