2

From what I understand, on Linux/UNIX files from which you get system info aren't regular files but rather device files and yet I have encountered a file that doesn't seem to be regular (due to its behaviour and size determined by ls and other C functions) but is listed as such (both by ls and when testing with -f in a sh script).

The file is at path /sys/class/thermal/thermal_zone0/temp on a Raspberry Pi, the result of ls gives that:
-r--r--r-- 1 root root 4096 janv. 18 13:55 /sys/class/thermal/thermal_zone0/temp

There should be a c or b if it was a device file.

The issue is that the detected size for this file is 4096 bytes but the real size of the file is actually much smaller.

So I have a few questions: why isn't this file listed as a special file? What C functions should you use to efficiently detect such files and how to measure the size of their content when they are "finite size" files ?

Thank you

terdon
  • 242,166
  • 2
    The entire filesystem mounted at /sys will be of type sysfs for making visible system information via a filesystem interface. The individual pseudo-files in the pseudo-filesystem are not character special or block special device files. See for example answers on https://unix.stackexchange.com/questions/236533/sysfs-and-devtmpfs – user4556274 Jan 18 '18 at 15:58

1 Answers1

4

You'll see such files almost exclusively in /proc and /sys since they aren't actually real filesystems, more like interfaces to the kernel. I assume they're not marked as device nodes, since devices would be identified by the major/minor device numbers, and proc and sysfs don't use that, but identify the virtual files in them by other means.

Identifying them is usually not an issue, since few programs are used to access files in /proc or /sys. For many of them, the data within is generated dynamically, so there may not be any way to measure the size other than by reading all contents. E.g. for the files I tested, seeking to the end didn't do anything useful, the seek goes to position 4096 for files that advertise that size, regardless of the actual size of the contents.

ilkkachu
  • 138,973