UNIX - Portable Operating System Interface (POSIX)

UNIX - Portable Operating System Interface (POSIX)

Portable Operating System Interface (POSIX)

  • is an IEEE standard designed to facilitate application portability
  • is an attempt by a consortium of vendors to create a single standard version of UNIX
  • If they are successful, it will make it easier to port applications between hardware platforms

POSIX - Standards

C API

 Click here to expand...

Greatly extends ANSI C with things like:

  • more file operations: mkdirdirnamesymlinkreadlinklink (hardlinks), poll()statsyncnftw()
  • process and threads: forkexeclwaitpipe, semaphores sem_*, shared memory (shm_*), kill, scheduling parameters (nicesched_*), sleepmkfifosetpgid()
  • networking: socket()
  • memory management: mmapmlockmprotectmadvisebrk()
  • utilities: regular expressions (reg*)

Those APIs also determine underlying system concepts on which they depend, e.g. fork requires a concept of a process.

Many Linux system calls exist to implement a specific POSIX C API function and make Linux compliant, e.g. sys_writesys_read, ... Many of those syscalls also have Linux-specific extensions however.

Major Linux desktop implementation: glibc, which in many cases just provides a shallow wrapper to system calls.

CLI Utilities

 Click here to expand...

e.g.: cdlsecho, ...

Many utilities are direct shell front ends for a corresponding C API function, e.g. mkdir.

Major Linux desktop implementation: GNU Coreutils for the small ones, separate GNU projects for the big ones: sedgrepawk, ... Some CLI utilities are implemented by Bash as built-ins.

Shell Language

 Click here to expand...

e.g. a=b; echo "$a"

Major Linux desktop implementation: GNU Bash.

Environment Variables

 Click here to expand...

Program Exit Status

 Click here to expand...

ANSI C says 0 or EXIT_SUCCESS for success, EXIT_FAILURE for failure, and leaves the rest implementation defined.

POSIX adds:

  • 126: command found but not executable
  • 127: command not found
  • >128: terminated by a signal

But POSIX does not seem to specify the 128 + SIGNAL_ID rule used by Bash: https://unix.stackexchange.com/questions/99112/default-exit-code-when-process-is-terminated

Regular Expression

 Click here to expand...

There are two types: BRE (Basic) and ERE (Extended). Basic is deprecated and only kept to not break APIs.

Those are implemented by C API functions, and used throughout CLI utilities, e.g. grep accepts BREs by default, and EREs with -E.

e.g. echo 'a.1' | grep -E 'a.[[:digit:]]'

Major Linux implementation: glibc implements the functions under regex.h which programs like grep can use as backend.

Directory Structure

 Click here to expand...

e.g. /dev/null/tmp

The Linux FHS greatly extends POSIX

Filenames

 Click here to expand...
  • / is the path separator
  • NUL cannot be used
  • . is cwd.. parent
  • portable filenames
    • use at most max 14 chars and 256 for the full path
    • can only contain: a-zA-Z0-9._-

See also: what is posix compliance for filesystem?

Command Line Utility API Conventions

 Click here to expand...

Not mandatory, used by POSIX, but almost nowhere else, notably not in GNU. But true, it is too restrictive, e.g. single letter flags only (e.g. -a), no double hyphen long versions (e.g. --all).

A few widely used conventions:

  • - means stdin where a file is expected
  • -- terminates flags, e.g. ls -- -l to list a directory named -l

See also: Are there standards for Linux command line switches and arguments?

"POSIX ACLs" (Access Control Lists)

 Click here to expand...

e.g. as used as backend for setfacl

This was withdrawn but it was implemented in several OSes, including in Linux with setxattr

Subpages