how can i show the size of files in /proc? it should not be size zero
Asked Answered
C

4

6

from the following message, we know that there are two characters in file /proc/sys/net/ipv4/ip_forward, but why ls just showed this file is of size zero?

i know this is not a file on disk, but a file in the memory, so is there any command which i can see the real size of the files in /proc?

root@OpenWrt:/proc/sys/net/ipv4# cat ip_forward | wc -c
2
root@OpenWrt:/proc/sys/net/ipv4# ls -l ip_forward
-rw-r--r--    1 root     root            0 Sep  3 00:20 ip_forward
root@OpenWrt:/proc/sys/net/ipv4# pwd
/proc/sys/net/ipv4 
Chromatography answered 2/9, 2012 at 16:28 Comment(0)
F
6

Those are not really files on disk (as you mention) but they are also not files in memory - the names in /proc correspond to calls into the running kernel in the operating system, and the contents are generated on the fly.

The system doesn't know how large the files would be without generating them, but if you read the "file" twice there's no guarantee you get the same data because the system may have changed.

You might be looking for the program sysctl -a instead.

Fuss answered 2/9, 2012 at 16:37 Comment(1)
so with sysctl, do i still need to cat files in /proc?or i can do all the things with sysctl?Chromatography
S
6

Things in /proc are not really files. In most cases, they're not even files in memory. When you access these files, the proc filesystem driver performs a system call that gets data appropriate for the file, and then formats it for output. This is usually dynamic data that's constructed on the fly. An example of this is /proc/net/arp, which contains the current ARP cache.

Getting the size of these things can only be done by formatting the entire output, so it's not done just when listing the file. If you want the sizes, use wc -c as you did.

Shanna answered 2/9, 2012 at 16:38 Comment(0)
F
3

The /proc/ filesystem is an "illusion" maintained by the kernel, which does not bother giving the size of (most of) its pseudo-files (since computing that "real" size would usually involve having built the entire textual pseudo-file's content), and expects most [pseudo-] textual files from /proc/ to be read in sequence from first to last byte (i.e. till EOF), in reasonably sized (e.g. 1K) blocks. See proc(5) man page for details.

So there is no way to get the true size (of some file like /proc/self/maps or /proc/sys/net/ipv4/ip_forward) in a single syscall (like stat(2), because it would give a size of 0, as reported by stat(1) or ls(1) commands). A typical way of reading these textual files might be

FILE* f = fopen("/proc/self/maps", "r"); 
                // or some other textual /proc file, 
                // e.g. /proc/sys/net/ipv4/ip_forward
if (f) 
  {
      do {
        // you could use readline instead of fgets
        char line[256];
        memset (line, 0, sizeof(line));
        if (NULL == fgets(line, sizeof(line), f))
          break;
        // do something with line, for example:
        fputs(line, stdout);
      } while (!feof (f));
    fclose (f);
  }

Of course, some files (e.g. /proc/self/cmdline) are documented as possibly containing NUL bytes. You'll need some fread for them.

Feltie answered 2/9, 2012 at 16:36 Comment(0)
M
0

It's not really a file in the memory, it's an interface between the user and the kernel.

Martita answered 2/9, 2012 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.