c - Linux kernel module copy_to_user not working -
i attepting write linux kernel module raspberry pi. good, except when try use either copy_to_user, or put_user, returns value of "34336" if print llu, , if print character, nothing.
the interesting thing working, made changes, stopped working, reverted working version, , no longer works.
code module:
ssize_t st_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { char memory_buffer = 'b'; /* transferring data user space */ copy_to_user(buf,memory_buffer,1); /* changing reading position best suits */ if (*f_pos == 0) { *f_pos+=1; return 1; } else { return 0; } }
code reading value:
fd = open("/dev/systimer", o_rdonly); // check 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.
you have closed before read. should check return value of read()
// check errors if(fd < 0) { perror("open(o_rdonly)"); return errno; } else close(fd); // <-- closing fd here read(fd, &buf, 1); // <-- fd closed
Comments
Post a Comment