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> */
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
27 #include "modechange.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
},
49 if (status
!= EXIT_SUCCESS
)
53 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name
);
55 Create named pipes (FIFOs) with the given NAMEs.\n\
58 emit_mandatory_arg_note ();
61 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
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\
68 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
69 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
70 emit_ancillary_info (PROGRAM_NAME
);
76 main (int argc
, char **argv
)
79 char const *specified_mode
= NULL
;
80 int exit_status
= EXIT_SUCCESS
;
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
);
91 atexit (close_stdout
);
93 while ((optc
= getopt_long (argc
, argv
, "m:Z", longopts
, NULL
)) != -1)
98 specified_mode
= optarg
;
101 if (is_smack_enabled ())
103 /* We don't yet support -Z to restore context with SMACK. */
106 else if (is_selinux_enabled () > 0)
111 set_security_context
= true;
116 _("warning: ignoring --context; "
117 "it requires an SELinux/SMACK-enabled kernel"));
120 case_GETOPT_HELP_CHAR
;
121 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
123 usage (EXIT_FAILURE
);
129 error (0, 0, _("missing operand"));
130 usage (EXIT_FAILURE
);
136 if (is_smack_enabled ())
137 ret
= smack_set_label_for_self (scontext
);
139 ret
= setfscreatecon (se_const (scontext
));
142 error (EXIT_FAILURE
, errno
,
143 _("failed to set default file creation context to %s"),
147 newmode
= MODE_RW_UGO
;
151 struct mode_change
*change
= mode_compile (specified_mode
);
153 error (EXIT_FAILURE
, 0, _("invalid mode"));
154 umask_value
= umask (0);
156 newmode
= mode_adjust (newmode
, false, umask_value
, change
, NULL
);
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
;