3

I'm currently developing an application which needs a lot of system and process information, some of which is only available through /proc, and I have some general questions about accessing the structures.

The application will be run on Linux (kernel >= 2.6), not on any other Unix-flavored OS. It should have access to any data in /proc, I can't say what is necessary now as the specifications are not clear yet, but the whole /proc directory is relevant to the application.


First of all: Is there a good documentation which covers all the features added / removed from kernel version to kernel version? One thing I'm curious about in particular is the format of the individual files. Can I take that for granted? Does it change among kernel versions?

Hooking up the parsing process based on the kernel wouldn't be a problem at all, it's just that I couldn't find any good docs on what has changed from version to version which could help me catching parsing errors in beforehand.


In addition: Is there a definite list of features that can be activated / deactivated by kernel options (except of course the /proc-feature itself)? I'm looking for a list of files / directories that only exist with the appropriate options being set in the kernel.

As an example of what I'm thinking of, this is a link to the proc manpage (http://linux.die.net/man/5/proc) which includes a lot of good information, e.g. some options include the earliest kernel version they were available at, some include whether a module is necessary to be loaded. This does not describe the output format of all information though, which is something I need if I want to parse it (e.g. if it is consistent throughout all kernel versions or changed at some point).


The second thing I'm wondering about is what happens if the process queried dies while being queried. What is my time interval? For example if I'm going to fetch a list of processes reading all the structures, and parse them one after another, what happens if my process x dies before I get to read it? Even if I check if the directory exists, it could still be gone one application call later.


Last but not least: Is there any major distribution out there that is not mounting proc?

From what I understand, a lot of common tools are based on the /proc interface such as lsmod or free, so I'm guessing that I can expect /proc to exist almost always.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Robin
  • 1,322
  • 1
  • 13
  • 23
  • Which part of the proc do you need? The per-process info or the generic kernel info? The per-process info looks the same across several Unix variants (Plan9, Solaris, Irix, *BSD, Linux) and large range of versions. The general system info is another story. – Jan Hudec Sep 14 '11 at 09:21
  • Sorry, I forgot to mention that the application is only targeting Linux (Kernel >= 2.6). I edited my post. – Robin Sep 14 '11 at 09:43
  • But which kind of info do you need from proc. Because the various parts of it are documented separately and with varying quality. – Jan Hudec Sep 14 '11 at 09:50
  • It would be best to access as much of the data available as possible, meaning every documentation irrelevant of its level of quality would come in handy. I have no problem reading mailing-lists and commit logs if that is what it means :) – Robin Sep 14 '11 at 10:12
  • The application should be doing something. You have idea what that something should be, no? Than you should know what information you want. – Jan Hudec Sep 14 '11 at 10:25
  • I do not have the specs for the application yet but from what was discussed the whole /proc directory is relevant. As I said, it would be best to access as much of the data available under /proc as possible. The nature of the application itself is irrelevant to the questions, this is just about parsing / accessing proc. – Robin Sep 14 '11 at 10:48

1 Answers1

5

The /proc interfaces are pretty stable (unlike the /sys interfaces), even if nothing is guaranteed. Almost all changes are backwards compatible, at least if they've been around for a few versions. You should stick to the documented interfaces to be safe. If a file exists, its format may be extended in later versions, but normally in a backwards compatible way, e.g. adding columns to a table. The parts that are most at risk of disappearing are parts concerning hardware susbystems such as ACPI or SCSI, which are migrating to /sys (with a long transition period when both exist).

Most of the information is architecture-independent, except for hardware information (e.g. /proc/cpuinfo has very different fields on different architectures).

The main documentation is Documentation/filesystems/proc.txt in the kernel source. Consider proc(5) to be the overview and proc.txt to be the fine details. The kernel documentation is often incomplete, so don't be surprised if you need to resort to reading the source sometimes.

Most optional parts of /proc are activated by default if the driver whose data it exposes is included in the kernel. The exceptions are mostly related to hardware features that rarely need to be accessed from outside the kernel; if you need to access these features, you're probably already expecting to need to dig deeper. Look through Kconfig files in the kernel source for detailed information.

Process data (or hardware data related to removable hardware or provided by unloadable modules) can disappear under your nose. Most files under /proc can be read atomically, with a single read call with a reasonably-sized buffer; if you perform multiple read calls in sequence, drivers are supposed to guarantee that you get well-formed data. There is no way to guarantee atomicity between reads of separate files; if you're reading information about a process, this process can die at any time, and in principle could even be replaced by another process with the same PID before you're finished.

As it says in the description of /proc, “everyone should say Y here”. All desktop/server Linux systems and most embedded Linux systems must have /proc; a lot of things, including ps and other process management commands, many filesystem and device-related tools, and module loading, require it. The only systems that might be able to dispense with /proc are very small single-purpose embedded systems that support a single hardware configuration and run a fixed set of programs. You can count on its being here.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254