3

From the documentation:

  • store() should return the number of bytes used from the buffer. If the entire buffer has been used, just return the count argument.

What does it do with this value? What's the difference if from a buffer of size FOO I read 4 and not 6 bytes?

maayank
  • 4,200
  • 2
  • 24
  • 23

1 Answers1

5

You must realize that by implementing a sysfs file, you are trying to behave like a file.

Let's see this from the other side first. From the man page of fwrite(3):

RETURN VALUE
fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

And even better, from the man page of write(2):

The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)

What this means is that store(), which is implementing the other end of the write(2) function for your particular file should return the number of bytes written (i.e. read by you), in the very least so that write(2) can return that value to the user.

In most cases, if there is no error in the input, you would just want to return count to acknowledge that you have read everything and all is ok.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Great (and fast!) answer :-) Thanks! – maayank Apr 15 '13 at 13:28
  • @maayank, quick question. Did you manage to figure out how to work with directories in sysfs? I use `kobject_create_and_add` which takes a parent node for creating a directory, but I don't know how to get the pointer to already existing nodes! (I give `NULL` so it creates a directory in the root of sysfs). – Shahbaz Apr 15 '13 at 13:37
  • It's a bit different (same idea, different function names) if you use devices/classes/buses etc., but what I do is creating a class structure and register it with class_register(). If I need files inside that directory I add (before registration) default attributes. If I need directories inside the class directory I created I create a device with device_create() (it receives the class structure for reference) and add the "files" for that in the "dev_attrs" field of the class structure. I don't know if that's the "Linux Kernel way", but it conforms to the project spec I'm working with. – maayank Apr 15 '13 at 13:46
  • ^^ this will give you with class FOO and device BAR and file inside BAR named FILE the file /sys/class/FOO/BAR/FILE. If you want the folder to be under a specific /sys/something then you need to look for the specific model API that 'something' references (i.e. block devices for block) – maayank Apr 15 '13 at 13:48
  • I see, thanks. Any idea how to get the handles for `/sys/module/`? When you insert a module, there would automatically be created a folder under `/sys/module/module_name`. I would be happy to know how to add files under that specific folder. I can't imagine a function like `sysfs_get_self()` doesn't exist. – Shahbaz Apr 15 '13 at 13:55
  • I think you can add parameter "files" under /sys/module/mymodule/parameters with the macro module_param. See http://www.makelinux.net/ldd3/chp-2-sect-8 for exact argument details. – maayank Apr 15 '13 at 14:03