0

I am attepting to write a Linux Kernel Module for the Raspberry Pi. All is good, except that when I try to use either copy_to_user, or put_user, it always returns a value of "34336" if I print it as an llu, and if I print it as a character, it is nothing.

The interesting thing is that it was working, I made some changes, it stopped working, I reverted back to the working version, and it no longer works.

Code from module:

    ssize_t st_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
    {
            char memory_buffer = 'b';
            /* Transferring data to user space */
            copy_to_user(buf,memory_buffer,1);

            /* Changing reading position as best suits */
            if (*f_pos == 0) {
                    *f_pos+=1;
                    return 1;
            } else {
                    return 0;
            }
    }

Code reading value:

    fd = open("/dev/systimer", O_RDONLY);

    // check for errors
    if(fd < 0) {
            perror("open(O_RDONLY)");
            return errno;
    } else
            close(fd);

    read(fd, &buf, 1);
    printf("Buffer: %llu\n", buf);
    printf("Buffer2: %c\n", buf);

Output is:

    Buffer: 34336
    Buffer2:

Thanks.

4

3 回答 3

0

您在阅读之前已关闭。您应该始终检查的返回值read()

// check for errors
if(fd < 0) {
        perror("open(O_RDONLY)");
        return errno; 
} else
        close(fd);   // <-- you are closing the fd here

read(fd, &buf, 1);   // <-- fd is closed
于 2014-10-30T10:43:38.257 回答
-1

你有没有尝试过?

copy_to_user(buf, &memory_buffer, 1);
于 2013-04-08T23:40:04.227 回答
-2

您可以尝试以下代码而不是 copy_to_user

 sprintf(buf, "%u\n", memory_buffer);

-帕尼

于 2014-10-30T10:31:33.433 回答