Decoded: mkfifo (coreutils)

[Back to Project Main Page]

Note: This page explores the design of command-line utilities. It is not a user guide.
[GNU Manual] [POSIX requirement] [Linux man] [FreeBSD man]

Logical flow of mkfifo command (coreutils)

Summary

mkfifo - make fifos (named pipes)

[Source] [Code Walkthrough]

Lines of code: 183
Principal syscall: mkfifo() (BSD syscall, otherwise this is standard library call to mknod())
Support syscalls: fchmodat() via lchmod() for links
Options: 6 (2 short, 4 long)

Descended from mknod introduced in Version 6 UNIX (1975)
Added to Fileutils in October 1992 [First version]
Number of revisions: 129 [Code Evolution]

Helpers:
  • None
External non-standard helpers:
  • die() - Exit with mandatory non-zero error and message to stderr
  • error() - Outputs error message to standard error with possible process termination
  • mode_adjust() - Performs file permissions changes
  • mode_compile() - Creates an array of mode change operations
  • quote() - Outputs a printable string
  • quoteaf() - Outputs a printable argument

Setup

mkfifo initializes local variables in main(), including:

  • newmode - A file mode in UGO format
  • *specified_mode - A file mode as input by the user
  • exit_status - Utility exit status (EXIT_*)
  • optc - The next option to process
  • *scontext - The user specified security context
  • set_security_context - Flag to check security context labels in SELinux

Parsing

Parsing mkfifo considers two questions:

  • What access mode is the named pipe?
  • Are there security considerations? (SELinux / Smack)

Parsing failures

These failure cases are explicitly checked:

  • Security context provided without a compliant kernel
  • No operand provided
  • Mode includes extraneous permissions

This failure result in a short error message followed by the usage instructions.


Execution

The utility has to prepare input arguments in to a usable form by the mkfifo() call. The syscall/function call has this prototype:

int mkfifo(const char *path, mode_t mode);

While the user passes this to the utility:

mkfifo [option] nameā€¦

The name is passed to path, while the utility must provide a proper mode.

The mkfifo utility follows this logic:

  • Verify the security context for file creation
  • For each file:
    • Set the security context
    • Make the via mkfifo() or change file via lchmod()
  • Return exit status (any failure forces EXIT_FAILURE)

Execution may fail if the mkfifo() syscall is unable to create the file, or if the lchmod() syscall cannot set permissions.


[Back to Project Main Page]