1 /* mkfifo -- make fifo's (named pipes)
2 Copyright (C) 1990-2024 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>
26 #include "modechange.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "mkfifo"
34 #define AUTHORS proper_name ("David MacKenzie")
36 static struct option
const longopts
[] =
38 {GETOPT_SELINUX_CONTEXT_OPTION_DECL
},
39 {"mode", required_argument
, nullptr, 'm'},
40 {GETOPT_HELP_OPTION_DECL
},
41 {GETOPT_VERSION_OPTION_DECL
},
42 {nullptr, 0, nullptr, 0}
48 if (status
!= EXIT_SUCCESS
)
52 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name
);
54 Create named pipes (FIFOs) with the given NAMEs.\n\
57 emit_mandatory_arg_note ();
60 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
63 -Z set the SELinux security context to default type\n\
64 --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
65 or SMACK security context to CTX\n\
67 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
68 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
69 emit_ancillary_info (PROGRAM_NAME
);
75 main (int argc
, char **argv
)
78 char const *specified_mode
= nullptr;
79 int exit_status
= EXIT_SUCCESS
;
81 char const *scontext
= nullptr;
82 struct selabel_handle
*set_security_context
= nullptr;
84 initialize_main (&argc
, &argv
);
85 set_program_name (argv
[0]);
86 setlocale (LC_ALL
, "");
87 bindtextdomain (PACKAGE
, LOCALEDIR
);
90 atexit (close_stdout
);
92 while ((optc
= getopt_long (argc
, argv
, "m:Z", longopts
, nullptr)) != -1)
97 specified_mode
= optarg
;
100 if (is_smack_enabled ())
102 /* We don't yet support -Z to restore context with SMACK. */
105 else if (is_selinux_enabled () > 0)
111 set_security_context
= selabel_open (SELABEL_CTX_FILE
,
113 if (! set_security_context
)
114 error (0, errno
, _("warning: ignoring --context"));
120 _("warning: ignoring --context; "
121 "it requires an SELinux/SMACK-enabled kernel"));
124 case_GETOPT_HELP_CHAR
;
125 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
127 usage (EXIT_FAILURE
);
133 error (0, 0, _("missing operand"));
134 usage (EXIT_FAILURE
);
140 if (is_smack_enabled ())
141 ret
= smack_set_label_for_self (scontext
);
143 ret
= setfscreatecon (scontext
);
146 error (EXIT_FAILURE
, errno
,
147 _("failed to set default file creation context to %s"),
151 newmode
= MODE_RW_UGO
;
155 struct mode_change
*change
= mode_compile (specified_mode
);
157 error (EXIT_FAILURE
, 0, _("invalid mode"));
158 umask_value
= umask (0);
160 newmode
= mode_adjust (newmode
, false, umask_value
, change
, nullptr);
162 if (newmode
& ~S_IRWXUGO
)
163 error (EXIT_FAILURE
, 0,
164 _("mode must specify only file permission bits"));
167 for (; optind
< argc
; ++optind
)
169 if (set_security_context
)
170 defaultcon (set_security_context
, argv
[optind
], S_IFIFO
);
171 if (mkfifo (argv
[optind
], newmode
) != 0)
173 error (0, errno
, _("cannot create fifo %s"), quoteaf (argv
[optind
]));
174 exit_status
= EXIT_FAILURE
;
176 else if (specified_mode
&& lchmod (argv
[optind
], newmode
) != 0)
178 error (0, errno
, _("cannot set permissions of %s"),
179 quoteaf (argv
[optind
]));
180 exit_status
= EXIT_FAILURE
;