What really happens when you redirect to /dev/null?

A deep dive into the Linux device driver that handles /dev/null

Heve you ever wondered what’s going on behind the scenes when you use > /dev/null to discard output?

The Mystery of /dev/null

First, let’s look at what /dev/null actually is. If you run ls -l /dev/null, you’ll see something like this:

crw-rw-rw- 1 root root 1, 3 Mar 26 18:31 /dev/null

That c at the start? It tells us this is a character device - a special type of file that Linux uses to communicate with device drivers. The numbers 1, 3 are particularly interesting:

  • 1 is the major number (identifies the driver)
  • 3 is the minor number (identifies the specific device)

Finding the Driver

So, which driver handles our /dev/null? Let’s check /proc/devices:

grep " 1 " /proc/devices
  1 mem

You’ll see: 1 mem

The mem driver (major number 1) is responsible for handling /dev/null, along with other special devices like /dev/zero and /dev/mem. This driver lives in the Linux kernel source at drivers/char/mem.c.

The Driver’s Secret Sauce

Inside the kernel, the mem driver defines how it handles operations through a structure called file_operations:

static const struct file_operations __maybe_unused mem_fops = {
	.llseek		= memory_lseek,
	.read		= read_mem,
	.write		= write_mem,
	.mmap		= mmap_mem,
	.open		= open_mem,
#ifndef CONFIG_MMU
	.get_unmapped_area = get_unmapped_area_mem,
	.mmap_capabilities = memory_mmap_capabilities,
#endif
	.fop_flags	= FOP_UNSIGNED_OFFSET,
};

This is like a menu of operations the driver can perform. When you write to /dev/null, the kernel calls the write_mem function, which… well, does nothing! It’s designed to discard any data written to it.

The Device Tree

In modern Linux systems, you can also find /dev/null in the sysfs filesystem:

ls -l /sys/dev/char/1:3
lrwxrwxrwx 1 root root 0 Mar 27 09:40 /sys/dev/char/1:3 -> ../../devices/virtual/mem/null

You’ll see it’s actually a symbolic link to ../../devices/virtual/mem/null, showing us that it’s a virtual device handled by the memory driver.

Why Should You Care?

You’re actually using a sophisticated piece of kernel infrastructure designed specifically for discarding data efficiently. It’s like having a digital black hole in your system!