ccharkerneldriversysfs

Passing int to sysfs


I want to pass int to sysfs_store

What I tried

userspace

int tid = 5234; // can be negative as well
char buf[5];
sprintf(buf, "%d\n", tid);
write (fd, buf, 1);

driver sysfs_store

int v;
kstrtoint(buf,10,&v);
pr_info("%d\n",v); // printing only first digit 5. Should print 5234

Solution

  • Since sprintf() returns bytes copied count; use it

    Also switch to snprintf() to avoid buffer overflows.

    int data_len = snprintf(buf, sizeof(buf), "%d\n", tid);
    write (fd, buf, data_len);
    

    It's always better to have bigger buffers to cover all your scenarios.