Linux create dev files

How to create /dev/null?

I once found that /dev/null was replaced with a normal file which only root could write to. I thought for a moment that I would have to reinstall everything but as mentioned below, the recovery is trival if you know the trick.

I managed to remove /dev/null by getting my pipe wrong flipping $ and & around when running an rm command in a script. Ouch!

3 Answers 3

mknod /dev/null c 1 3 chmod 666 /dev/null 

Use these command to create /dev/null or use null(4) manpage for further help.

For completeness, I’d note that this applies to all linux-based systems; other systems may have different numbers.

Also this only sets up the device file itself, it will not restore its permissions. You’ll have to chmod go+w it manually.

mknod -m 0666 /dev/null c 1 3 worked perfectly to restore /dev/null on CentOS 7, just to let other know.

Under many traditional unices, you can recreate devices with their default permissions with the MAKEDEV script. This script is traditionally in /dev but is in /sbin on Ubuntu. Pass it an argument that indicates what devices you want to create; on Ubuntu that’s std (you can write MAKEDEV null as well, that creates null as well as a number of other devices).

Under modern Linux systems running udev, you can tell udev to recreate all or some devices based on available drivers and connected hardware, through udevadm trigger :

udevadm trigger --sysname-match=null 

Is this answer still current? I’m on CentOS 7 and I’ve just overwritten my /dev/null by mistake, the udevadm trigger —sysname-match=null trick didn’t work for me, while mknod -m 0666 /dev/null c 1 3 worked perfectly.

@Bozzy I know it worked with the pre-systemd udev. I haven’t tried recently, but as far as I know this should still work. If you add —verbose , does it list /sys/devices/virtual/mem/null ?

Yes, it lists that. I’ve tried to overwrite again /dev/null on my dev machine, but strangely enough I’m unable to do it here, so I can’t do a more in-depth analysis now.

Источник

Device File Creation – Linux Device Driver Tutorial Part 5

Device File Creation for Character Drivers

This article is a continuation of the Series on Linux Device Driver and carries the discussion on character drivers and their implementation. The aim of this series is to provide easy and practical examples that anyone can understand. This is the Device File Creation for Character Drivers – Linux Device Driver Tutorial Part 5.

Prerequisites

To continue with this tutorial, you must have set up the Ubuntu or Raspberry Pi or Beaglebone. If you aren’t set up anything yet, we suggest you to set up the boards that you have using the below-given tutorials.

You can find a video explanation of this tutorial here. You can also find all the Linux device driver’s video playlists here.

Device File Creation for Character Drivers

In our last tutorial, we have seen how to assign a major and minor number. But if you see, it will create a major and minor number. But I did not create any device files under /dev/ directory. The device file is important to communicate with the hardware. Let’s start our tutorial.

Читайте также:  Линукс установка центра приложений

Device Files

The device file allows transparent communication between user-space applications and hardware.

They are not normal “files”, but look like files from the program’s point of view: you can read from them, write to them, mmap() onto them, and so forth. When you access such a device “file,” the kernel recognizes the I/O request and passes it to a device driver, which performs some operation, such as reading data from a serial port or sending data to hardware.

Device files (although inappropriately named, we will continue to use this term) provide a convenient way to access system resources without requiring the application programmer to know how the underlying device works. Under Linux, as with most Unix systems, device drivers themselves are part of the kernel.

All device files are stored in /dev directory. Use ls command to browse the directory:

Each device on the system should have a corresponding entry in /dev . For example, /dev/ttyS0 corresponds to the first serial port, known as COM1 under MS-DOS ; /dev/hda2 corresponds to the second partition on the first IDE drive. In fact, there should be entries in /dev for devices you do not have. The device files are generally created during system installation and include every possible device driver. They don’t necessarily correspond to the actual hardware on your system. There are a number of pseudo-devices in /dev that don’t correspond to any actual peripheral. For example, /dev/null acts as a byte sink; any write request to /dev/null will succeed, but the data are written will be ignored.

When using ls -l to list device files in /dev , you’ll see something like the following:

crw--w---- 1 root tty 4, 0 Aug 15 10:40 tty0 brw-rw---- 1 root disk 1, 0 Aug 15 10:40 ram0

First of all, note that the first letter of the permissions field is denoted that driver type. Device files are denoted either by b, for block devices, or c, for character devices.

Also, note that the size field in the ls -l listing is replaced by two numbers, separated by a comma. The first value is the major device number and the second is the minor device number. This we have discussed in the previous tutorial.

Creating Device File

We can create a dive file in two ways.

We will discuss these topics one by one.

Источник

How do dev files work?

How guys from linux make /dev files. You can write to them and immediately they’re erased. I can imagine some program which constantly read some dev file:

FILE *fp; char buffer[255]; int result; fp = fopen(fileName, "r"); if (!fp) < printf("Open file error"); return; >while (1) < result = fscanf(fp, "%254c", buffer); printf("%s", buffer); memset(buffer, 0, 255); fflush(stdout); sleep(1); >fclose(fp); 

But how to delete content in there? Closing a file and opening them once again in «w» mode is not the way how they done it, because you can do i.e. cat > /dev/tty

Читайте также:  Jre for linux debian

4 Answers 4

What are files? Files are names in a directory structure which denote objects. When you open a file like /home/joe/foo.txt , the operating system creates an object in memory representing that file (or finds an existing one, if the file is already open), binds a descriptor to it which is returned and then operations on that file descriptor (like read and write ) are directed, through the object, into file system code which manipulates the file’s representation on disk.

Device entries are also names in the directory structure. When you open some /dev/foo , the operating system creates an in-memory object representing the device, or finds an existing one (in which case there may be an error if the device does not support multiple opens!). If successful, it binds a new file descriptor to the device obejct and returns that descriptor to your program. The object is configured in such a way that the operations like read and write on the descriptor are directed to call into the specific device driver for device foo , and correspond to doing some kind of I/O with that device.

Such entries in /dev/ are not files; a better name for them is «device nodes» (a justification for which is the name of the mknod command). Only when programmers and sysadmins are speaking very loosely do they call them «device files».

When you do cat > /dev/tty , there isn’t anything which is «erasing» data «on the other end». Well, not exactly. Basically, cat is calling write on a descriptor, and this results in a chain of function calls which ends up somewhere in the kernel’s tty subsystem. The data is handed off to a tty driver which will send the data into a serial port, or socket, or into a console device which paints characters on the screen or whatever. Virtual terminals like xterm use a pair of devices: a master and slave pseudo-tty. If a tty is connected to a pseudo-tty device, then cat > /dev/tty writes go through a kind of «trombone»: they bubble up on the master side of the pseudo-tty, where in fact there is a while (1) loop in some user-space C program receiving the bytes, like from a pipe. That program is xterm (or whatever); it removes the data and draws the characters in its window, scrolls the window, etc.

Источник

how to create /dev/one like /dev/zero?

How can I create a file that produces a continuous stream of 0x01 characters? Just like /dev/zero , except producing character of my choice:

/dev/zero – accepts and discards all input written to it; produces a continuous stream of null characters (zero-value bytes) as output when read from.

It must be readable by cat . I considered running a loop into a named pipe, but that requires an active process.

3 Answers 3

… but that requires an active process.

Original /dev/zero also handled by active «process», but in kernel. If you don’t want to handle it at userspace level your other choices are pretty limited — it should be some kernel driver then.

Well you could always pipe /dev/zero through tr or something else

# cat /dev/zero | tr '\000' '\001' | hexdump -C 00000000 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 |. | * 

If you want to skip the cat and use process substitution.

# cat /dev/zero | hexdump -C 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |. | * 
#include #include #include #include #include #include #include #include #include /* Prototypes - this would normally go in a .h file */ static int device_open(struct inode *, struct file *); static int device_release(struct inode *, struct file *); static ssize_t device_read(struct file *, char __user *, size_t, loff_t *); static ssize_t device_write(struct file *, const char __user *, size_t, loff_t *); #define SUCCESS 0 #define DEVICE_NAME "one" /* Dev name as it appears in /proc/devices */ /* Global variables are declared as static, so are global within the file. */ static int major; /* major number assigned to our device driver */ static struct class *cls; static struct file_operations chardev_fops = < .read = device_read, .write = device_write, .open = device_open, .release = device_release, >; static int __init chardev_init(void) < major = register_chrdev(0, DEVICE_NAME, &chardev_fops); if (major < 0) < pr_alert("Registering char device failed with %d\n", major); return major; >pr_info("I was assigned major number %d.\n", major); cls = class_create(THIS_MODULE, DEVICE_NAME); device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); pr_info("Device created on /dev/%s\n", DEVICE_NAME); return SUCCESS; > static void __exit chardev_exit(void) < device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); /* Unregister the device */ unregister_chrdev(major, DEVICE_NAME); >/* Methods */ /* Called when a process tries to open the device file, like * "sudo cat /dev/chardev" */ static int device_open(struct inode *inode, struct file *file) < try_module_get(THIS_MODULE); return SUCCESS; >/* Called when a process closes the device file. */ static int device_release(struct inode *inode, struct file *file) < /* Decrement the usage count, or else once you opened the file, you will * never get rid of the module. */ module_put(THIS_MODULE); return SUCCESS; >/* Called when a process, which already opened the dev file, attempts to * read from it. */ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char __user *buffer, /* buffer to fill with data */ size_t length, /* length of the buffer */ loff_t *offset) < int i; for (i = 0; i < length; i++) < put_user('1', buffer + i); >/* Most read functions return the number of bytes put into the buffer. */ return length; > /* Called when a process writes to dev file: echo "hi" > /dev/hello */ static ssize_t device_write(struct file *filp, const char __user *buff, size_t len, loff_t *off) < pr_alert("Sorry, this operation is not supported.\n"); return -EINVAL; >module_init(chardev_init); module_exit(chardev_exit); MODULE_LICENSE("GPL"); 
obj-m += onedev.o PWD := $(CURDIR) ifeq ($(CONFIG_STATUS_CHECK_GCC),y) CC=$(STATUS_CHECK_GCC) ccflags-y += -fanalyzer endif all: $(MAKE) -C /lib/modules/$(shell uname -r)/build CC=$(CC) M=$(PWD) modules clean: $(MAKE) -C /lib/modules/$(shell uname -r)/build CC=$(CC) M=$(PWD) clean $(RM) other/cat_noblock *.plist indent: clang-format -i *[.ch] clang-format -i other/*[.ch] 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Dcp 1602r драйвер linux

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector