Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing and reading to linux /proc/... filesystem without lseek()

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).

like image 975
Toby Brull Avatar asked Dec 15 '13 14:12

Toby Brull


People also ask

Is proc read-only?

Most of the /proc file system is read-only; however, some files allow kernel variable to be changed.

What is the purpose of Procfs?

Answer. The /proc file system is a mounted file system used to trace a process system call, receive signals, and incurred machine faults. The /proc file system provides access to the state of each active process and thread in the system.

What is self proc?

/proc/self is a real symbolic link to the /proc/ subdirectory of the process that is making the call. When you do ls /proc/$$ the shell expands it to ls /proc/pid-of-bash and that is what you see, the contents of the shell process. But when you do ls /proc/self you see the contents of the short lived ls process.

What is Procfs in Linux?

The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized method for dynamically accessing process data held in the kernel than traditional ...


2 Answers

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.

like image 81
Roman Nikitchenko Avatar answered Oct 05 '22 04:10

Roman Nikitchenko


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.

like image 31
sim Avatar answered Oct 05 '22 04:10

sim