51

I am learning to write character device drivers from the Kernel Module Programming Guide, and used mknod to create a node in /dev to talk to my driver.

However, I cannot find any obvious way to remove it, after checking the manpage and observing that rmnod is a non-existent command.

What is the correct way to reverse the effect of mknod, and safely remove the node created in /dev?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 8
    Probably just `rm` – Keith Thompson Mar 18 '14 at 18:33
  • Is that safe and clean though, in the sense that it doesn't leave any dangling pointers in the kernel? – merlin2011 Mar 18 '14 at 18:35
  • 4
    As far as the kernel is concerned, the node is just data on disk. It only does anything to the kernel when you open it. Without checking, I'd suspect the usual unix-y file behavior to apply, in that an open device probably remains open even if you delete the node from disk which you used to open it. – Chris Stratton Mar 18 '14 at 18:55
  • 1
    I'd be very concerned if a userspace operation is able to leave dangling pointers in the kernel. Pretty sure that'd be called a kernel bug – tangrs Mar 18 '14 at 22:17

2 Answers2

59

The correct command is just rm :)

A device node created by mknod is just a file that contains a device major and minor number. When you access that file the first time, Linux looks for a driver that advertises that major/minor and loads it. Your driver then handles all I/O with that file.

When you delete a device node, the usual Un*x file behavior aplies: Linux will wait until there are no more references to the file and then it will be deleted from disk.

Your driver doesn't really notice anything of this. Linux does not automatically unload modules. Your driver wil simply no longer receive requests to do anything. But it will be ready in case anybody recreates the device node.

Enlico
  • 23,259
  • 6
  • 48
  • 102
1000 Bites
  • 1,010
  • 9
  • 9
1

You are probably looking for a function rather than a command. unlink() is the answer. unlink() will remove the file/special file if no process has the file open. If any processes have the file open, then the file will remain until the last file descriptor referring to it is closed. Read more here: http://man7.org/linux/man-pages/man2/unlink.2.html