¿Cómo crear /dev/one como /dev/zero?

¿Cómo crear /dev/one como /dev/zero?

¿Cómo puedo crear un archivo que produzca un flujo continuo de 0x01caracteres? Igual que /dev/zero, excepto que produce el personaje de mi elección:

/dev/zero – acepta y descarta todas las entradas escritas en él; produce un flujo continuo de caracteres nulos (bytes de valor cero) como salida cuando se lee.

Debe ser legible por cat. Consideré ejecutar un bucle en una tubería con nombre, pero eso requiere un proceso activo.

Respuesta1

…pero eso requiere un proceso activo.

Original /dev/zerotambién manejado por"proceso" activo, peroen el núcleo. Si no desea manejarlo a nivel de espacio de usuario, sus otras opciones son bastante limitadas; entonces debería ser algún controlador del kernel.

Respuesta2

Bueno, siempre puedes canalizar /dev/zero tro algo más

# 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  |................|
*

Si desea omitir el gato y utilizar la sustitución de procesos.

# hexdump -C <( tr '\000' '\001' </dev/zero ) 
00000000  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|

versus.

# cat /dev/zero | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*

Respuesta3

Adaptado descaradamente deLa guía de programación del módulo del kernel de Linux

#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/poll.h>

/*  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");

Archivo Make

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]

información relacionada