views:

232

answers:

3

(1) Is /proc always present on all linux systems or is it an option somewhere (e.g. kernel compile, boot, etc)?

(2) What determines what sub-directories are present in /proc? Are these always the same or are they configurable and/or creatable by user-space programs (presumably run as root)?

(3) I noticed that /dev/shm was present on my system without having to mount it nor is it present in /etc/fstab. Is this automatic or does something create it? Conversely, I have to mount /dev/mqueue. Shouldn't this be auto mounted as well?

(4) What is the normal/best way to determine programatically if a device or directory is mounted? Should one just look for open errors or is there an API call for this.

Thanks.

A: 
  1. Yes. All Linux systems have a /proc file system. In fact, all Unix systems have a /proc. But Linux is unique(?) in the way it has made it a 'dumping' ground for all runtime information. Solaris OTOH just has the current processes related information in /proc
  2. You will see a lot of sub-directories in /proc which are basically numbers, which are the PIDs of the currently running processes. So they change with time. Yes, you can create a Kernel module to have a new entry in /proc.

I do not have definitive answers regarding the other 2, but HTH.

Amit
Whoever did the -1, care to help me understand why?
Amit
Not all Unixen _mount_ /proc by default - OpenBSD for example.
Nikolai N Fetissov
Not all Linux systems have a /proc filesystem; it's a kernel option (CONFIG_PROC_FS), and even if it's compiled in, it can simply not be mounted. FreeBSD has deprecated their procfs, citing security concerns, and /proc isn't mounted by default there.
Stephen Veiss
It's definitely not a UNIX thing -- it was invented by Plan 9, and copied (partially) by Linux. Most of the BSDs gained it later only for Linux compatibility.
ephemient
+4  A: 
  1. /proc is typically mounted by the init scripts, like this:

    mount none /proc -t proc
    

    If this is not done, you will not see the expected contents of /proc (but then you will also have a non-POSIX-compliant system).

  2. $ grep proc /proc/filesystems
    nodev   proc
    

    proc is a virtual filesystem exported by the kernel. Its contents are entirely* determined by the kernel and loaded modules which can register to add additional entries.

    * Except that there are some subdirectories in /proc which are commonly overmounted with other virtual filesystems, for example:
    mount none /proc/bus/usb -t usbfs -o devmode=0664,devgid=plugdev
    mount none /proc/sys/fs/binfmt_misc -t binfmt_misc
    mount none /proc/fs/nfsd -t nfsd

  3. Check your init scripts. /sbin/init looks at /etc/inittab to determine what to run, which is often /sbin/rc or similar, which then goes through /etc/rcS.d/* etc., and there is also a good possibility that your distribution will do preliminary setup in the initramfs as well.

  4. Depends.

    You can read the output of the mount command, or the /proc/mounts file, or the /proc/self/mounts / /proc/self/mountinfo files on newer kernels, to determine what is currently mounted.

    You can compare the st_dev of stat of a directory to its parent; if different, the directory is a mountpoint.

    Unless you are writing code to be running on a non-standard system or at boot before the system has been set up, you may assume that /dev, /proc, /sys, etc. are mounted.

ephemient
+2  A: 
  1. Most if not all publicly available distributions configure the proc file system and mount it at startup, because it is so useful. But you can certainly disable it and get a working kernel - userspace will be hobbled though.

  2. The directories available under /proc are determined by the drivers and their options when you compiled the kernel. You cannot create new directories or files there from userspace.

  3. The developers of your distribution choose what to mount and where.

  4. You can run mount(8) or if /proc is mounted you can snoop at /proc/mounts . The mount program also maintains /etc/mtab with the same information, but formatted slightly differently.

florin