7

In this source code http://man7.org/tlpi/code/online/dist/sysinfo/procfs_pidmax.c.html the file /proc/sys/kernel/pid_max is first simply read (using the read syscall) and then simply written (using the write syscall).

Why is it no necessary to lseek to the beginning before writing? I thought the file-offset pointer is the same for read's and write's (that's what the author of the associated books says).

levzettelin
  • 2,600
  • 19
  • 32
  • 1
    `procfs` is not an ordinary filesystem, the files it contains aren't files. So normal rules don't necessarily apply. – Mat Dec 15 '13 at 15:00
  • 3
    OK, but which rules do then apply? – levzettelin Dec 15 '13 at 15:02
  • 1
    Probably depends which file specifically you're looking at. I'd wager some of them aren't seekable too. – Mat Dec 15 '13 at 15:04
  • @Mat I don't agree. I've just looked at kernel space api for creating procfs, you can't block lseek, for the user space application file in procfs is a normal file with "normal rules", ie all fs system calls can be used. – sim Dec 15 '13 at 15:25
  • I wasn't sure about lseek, but I'm pretty sure some of the files in there are special (can't be rewound in particular). And being able to write at any position with the same effect is not "normal" in my book. @sim – Mat Dec 15 '13 at 15:27

2 Answers2

6

This is because of /proc is not real file system so pid_max writes are handled in a way you don't need any seek. I even don't know if seeks are supported here.

Just to give you feeling of how different /proc files are here is reference for pretty old but illustrative kernel bug specially related to pid_max: https://bugzilla.kernel.org/show_bug.cgi?id=13090

This link should explain you even more details: T H E /proc F I L E S Y S T E M

And finally developerWorks article "Access the Linux kernel using the /proc filesystem" with step-by-step illustration of kernel module code which have /proc FS API. This looks like 100% what you need.

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
  • I understand that `/proc` is special. I am, however, looking for a mental model that explains how `read`'s and `write`'s to these files work on a more general level. – levzettelin Dec 15 '13 at 15:13
  • Look last "Access the Linux kernel using the /proc filesystem" article. Seems to be 100% for you :-). – Roman Nikitchenko Dec 15 '13 at 15:38
  • Just to clarify: adding file under /proc fs and /proc/sys is not the same, if you want to add some file to /proc/sys your must work with sysctl table, but for normal lkm module it's not recomended. – sim Dec 17 '13 at 11:05
1

I've looked at kernel source, files under /proc/sys/ is under sysctl table control, read/write callbacks for each entry support file offset. "pid_max entry" has one int value to operate and, hence, offset in those callbacks actually is not using.

sim
  • 756
  • 6
  • 18