Add Book to My BookshelfPurchase This Book Online

Appendix C - The /proc Filesystem

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Appendix C: The /proc Filesystem
Overview
In older versions of UNIX, access to process data such as that obtained by the ps command is obtained by reading kernel memory directly. This process, aside from being very complex, requires superuser permissions and is inherently non-portable. To get around these problems, and to provide a general interface to process' memory images, SVR4 (as well as some other newer versions) offers the /proc filesystem.
 NoteBecause it does not provide the /proc filesystem, the information in this appendix does not apply to HP-UX 10.x.
The /proc filesystem contains one file for each process currently running on the system; the name of the file is the same as the process ID for the process. The owner of the file is set to the process' real user ID, and the permission bits are set so the file is readable and writable only by its owner. The superuser, of course, may open, read, and write any file (process). For security reasons, an open of a file in /proc fails unless both the user ID and group ID of the caller match those of the process, and the process' object file is readable by the caller. Files corresponding to set-user-id and set-group-id processes may be opened only by the superuser.
The interface to the /proc filesystem is the normal filesystem system calls: open, close, read, write, and ioctl. An open for reading and writing enables control of the process; this is used by debuggers and the like. An open for reading only allows inspection but not control of the process; this is used by ps and so forth. The control of processes as performed by debuggers is beyond the scope of this book; we will discuss only the features for process inspection here.
Information about a process is obtained via the ioctl function:
    #include <sys/types.h>
    #include <sys/signal.h>
    #include <sys/fault.h>
    #include <sys/syscall.h>
    #include <sys/procfs.h>
    int ioctl(int fd, int code, void *ptr);
The fd parameter is a file descriptor for the open process, code is a code describing the operation to be performed (see below), and ptr is a pointer to a structure in which to store results. The structure type varies depending on the value of code. The ioctl function returns 0 on success; if it fails it returns -1 and stores an error indication in errno.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback