liveBook · Manning

Errno

As explained in Rust in Action: Systems programming concepts and techniques

errno is a C and POSIX process-global error indicator that system calls may set to identify a failure condition.

errno is a process-global error indicator used by C and POSIX system calls. A call such as close() may return -1 and set errno to a code describing the failure. section 3.4.1

Checking close()

If close() is interrupted by a caught signal, it returns -1 with errno set to EINTR. An input/output failure may set errno to EIO. These constants have operating-system-specific values. section 3.4.1

A global error-code pattern typically works like this:

  • Call an operation such as read() or close(). section 3.4.1
  • Check the global error value immediately after the call. section 3.4.1
  • Handle a nonzero code, often by reporting or propagating the failure. section 3.4.1
Listing 3.6 Rust-like code that checks error codes from a global variable
static mut ERROR: i32 = 0;                  #1
 
// ...
 
fn main() {
  let mut f = File::new("something.txt");
 
  read(f, buffer);
  unsafe {                                  #2
    if ERROR != 0 {                         #3
      panic!("An error has occurred while reading the file ")
    }
  }
 
  close(f);
  unsafe {                                  #2
    if ERROR != 0 {                         #3
      panic!("An error has occurred while closing the file ")
    }
  }
}

The compact listing shows the unsafe Rust-like form of checking a mutable global error variable after file operations. section 3.4.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains return-value checking, symbolic failure codes, and global-state drawbacks without developing POSIX internals.
Examples
Uses read() and close(), with EINTR and EIO illustrating interruption and input/output failure.
What it assumes
Assumes familiarity with file descriptors and system calls while introducing global error indicators and unsafe access from zero.
What you can do after
Diagnose system-call failures by checking a global error value after an operation returns.
How it connects
Connects errno with C, POSIX, system calls, global variables, unsafe Rust, and error handling.

Also explained in

Compare how 3 books explain errno →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.