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> */
22 #include <sys/types.h>
23 #include <selinux/label.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 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
);
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)
113 set_security_context
= selabel_open (SELABEL_CTX_FILE
,
115 if (! set_security_context
)
116 error (0, errno
, _("warning: ignoring --context"));
122 _("warning: ignoring --context; "
123 "it requires an SELinux/SMACK-enabled kernel"));
126 case_GETOPT_HELP_CHAR
;
127 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
129 usage (EXIT_FAILURE
);
135 error (0, 0, _("missing operand"));
136 usage (EXIT_FAILURE
);
142 if (is_smack_enabled ())
143 ret
= smack_set_label_for_self (scontext
);
145 ret
= setfscreatecon (scontext
);
148 die (EXIT_FAILURE
, errno
,
149 _("failed to set default file creation context to %s"),
153 newmode
= MODE_RW_UGO
;
157 struct mode_change
*change
= mode_compile (specified_mode
);
159 die (EXIT_FAILURE
, 0, _("invalid mode"));
160 umask_value
= umask (0);
162 newmode
= mode_adjust (newmode
, false, umask_value
, change
, NULL
);
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
;