c - difference between reading proc files via shell (cat) or via program (fread) -


i have kernel module creating entry in proc-fs , userspace-program reads file.
proc read-function looks like:

typedef struct {   int integer;   unsigned long ulong;   char string[100];   float floatt;   bool booll;   u16 crc; } struktur;  static int round = 0, len = 0, temp = 0, err; struktur *pde_data_p;  int proc_read(struct file *filp, char *buf, size_t count, loff_t *offp) {   int i;   unsigned char *crcbuf;   struktur struct_buf;    pde_data_p = pde_data(file_inode(filp));   crcbuf = (unsigned char*)pde_data_p;   memcpy(&struct_buf, pde_data_p, sizeof(struktur));   if (pde_data_p == null) {     printk(kern_err "pde->data == null\n");     round = 0;     return 0;   }    if (round == 0)     temp = sizeof(struktur);    if (count > temp)     count = temp;    struct_buf.crc = crc16(struct_buf.crc, crcbuf, sizeof(struktur)-sizeof(unsigned short));    err = copy_to_user(buf, pde_data_p, count);    //if (err == 0) {    // copy_to_user finished     round = 0;     temp = 0;        // taking line out makes work in prog not cat     return temp;   //} else {           // copy_to_user failed -> return number of bytes have not been copied     //temp = err;     //round++;     //return temp;   //} } 

my program code is:

typedef struct {   int integer;   unsigned long ulong;   char string[100];   float floatt;   bool booll;   unsigned short crc; } struktur;  int main(void) {   int i;   struktur str_inp;   unsigned short crc = 0;   unsigned char *str_p = (unsigned char*)&str_inp;     file *fp = fopen("/proc/sen/entry", "r");   fread(&str_inp, 1, sizeof(struktur), fp);   fclose(fp); } 

as can see proc read-function returns number of bytes read, not zero.
way program works fine when try read via cat (cat /proc/sen/entry) never finishes because never returns 0.
when change code , return 0 after copy_to_user has finished, reading via cat works fine program seems reads random memory. when return half of number of copied bytes, half of data read user-space program correct.

what has returned proc_read function depends on position handed on in *offp. if required work given userspace program code , cat (not partial reads of struktur) suffices to

    if (*offp) return 0;            // no more data return     else *offp = sizeof (struktur); // file position after data returned 

- statement goes before copy_to_user().


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -