Writing to a Linux device driver causes an endless loop

I am writing a kernel space driver that can be read and written from user space. The open, read, release operations work fine. The problem I encountered is the user space code, which must gain access to the device driver and write something to it.

The user space program writes in two files: 1) to the .txt file (and prints to the console so that the user knows that it has been completed) and 2) to the device driver (and also prints text to let the user know that it was also completed).

The following is the full user space code:

int main() {
    FILE *fp;

    fp = fopen("./test.txt","w");
    fputs("Test\n", fp);
    fclose(fp);
    printf("Printed to txt\n"); //Prints normally.

    fp = fopen("/dev/testchar", "w");        
    fputs("Test\n", fp);
    fclose(fp);
    printf("Printed to dev\n"); //Never gets to this point

    return 0;
}

When I compile and run the code that the program spits out

Printed to txt

ctrl + c. fputs().

kern.log .

:

static char msg[256] = {0};

static struct file_operations fops =
{
    .write = dev_write
};


static ssize_t dev_write(struct file *file, const char *buf, size_t len, loff_t *ppos)
{
    sprintf(msg, "Input:%s, Chars:%lu\n", buf, len);
    printk(KERN_NOTICE "%s\n", msg);

    return 0;
}

uname -r: 4.10.0-38-generic

gcc -v: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

: ?

. .

+4
1

, . 0. , 0. , stdio, , , . , stdio , . write , fputs. - , . strace .

+4

Source: https://habr.com/ru/post/1689507/


All Articles