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>
28 #include "modechange.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
},
50 if (status
!= EXIT_SUCCESS
)
54 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name
);
56 Create named pipes (FIFOs) with the given NAMEs.\n\
59 emit_mandatory_arg_note ();
62 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
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\
69 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
70 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
71 emit_ancillary_info (PROGRAM_NAME
);
77 main (int argc
, char **argv
)
80 char const *specified_mode
= NULL
;
81 int exit_status
= EXIT_SUCCESS
;
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
);
92 atexit (close_stdout
);
94 while ((optc
= getopt_long (argc
, argv
, "m:Z", longopts
, NULL
)) != -1)
99 specified_mode
= optarg
;
102 if (is_smack_enabled ())
104 /* We don't yet support -Z to restore context with SMACK. */
107 else if (is_selinux_enabled () > 0)
112 set_security_context
= true;
117 _("warning: ignoring --context; "
118 "it requires an SELinux/SMACK-enabled kernel"));
121 case_GETOPT_HELP_CHAR
;
122 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
124 usage (EXIT_FAILURE
);
130 error (0, 0, _("missing operand"));
131 usage (EXIT_FAILURE
);
137 if (is_smack_enabled ())
138 ret
= smack_set_label_for_self (scontext
);
140 ret
= setfscreatecon (se_const (scontext
));
143 die (EXIT_FAILURE
, errno
,
144 _("failed to set default file creation context to %s"),
148 newmode
= MODE_RW_UGO
;
152 struct mode_change
*change
= mode_compile (specified_mode
);
154 die (EXIT_FAILURE
, 0, _("invalid mode"));
155 umask_value
= umask (0);
157 newmode
= mode_adjust (newmode
, false, umask_value
, change
, NULL
);
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
;