Skip to content
  • Lucas De Marchi's avatar
    lib/igt_sysfs: make sure to write empty strings · 1c0b492a
    Lucas De Marchi authored
    
    
    echo -n "" > /sys/module/<modulename>/parameters/<param>
    
    doesn't really work as it will just create a open() + close() expecting
    the file to be truncated. The same issue happens with igt as it will
    stop writing when there are 0 chars to write. Special case the empty
    string so it always write a '\0' and make sure igt_sysfs_set() accounts
    for the extra null char.
    
    Shell example:
    	# echo -n "/foo" > /sys/module/firmware_class/parameters/path
    	# cat /sys/module/firmware_class/parameters/path
    	/foo
    	# echo -n "" > /sys/module/firmware_class/parameters/path
    	/foo
    	# # make sure to actually write a \0:
    	echo -ne "\0" > /sys/module/firmware_class/parameters/path
    	# cat /sys/module/firmware_class/parameters/path
    
    Same thing happens when testing igt_sysfs_set():
           int dirfd = open("/sys/module/firmware_class/parameters", O_RDONLY);
           igt_sysfs_set(dirfd, "path", "");
    
    Previously it was not really setting the param.
    
    v2:
      - Fix return code from igt_sysfs_vprintf() to differentiate between
        writing 1 or 0 chars (Janusz)
      - Document the behavior of igt_sysfs_set() as being a higher-level
        than igt_sysfs_write().
    v3:
      - Partial "back to v1": include the \0 in the write only when writing
        an empty string: there are some files in sysfs like pm_test that
        don't like
    v4:
      - Fix return code in igt_sysfs_vprintf() when fmt string is something
        like "%c<more_fmt>", c, ..." so it still returns > 0 (Janusz)
    
    Reviewed-by: default avatarJanusz Krzysztofik <janusz.krzysztofik@linux.intel.com>
    Link: https://lore.kernel.org/r/20240228223134.3908035-4-lucas.demarchi@intel.com
    
    
    Signed-off-by: default avatarLucas De Marchi <lucas.demarchi@intel.com>
    1c0b492a