referer: http://osxbook.com/book/bonus/chapter11/procfs/

 

Processes as Files

USENIX Summer Conference Proceedings, Salt Lake City, UT, USA (June 1984).

/proc on Mac OS X (not)

Mac OS X does not provide a process file system. It does provide alternative interfaces such as sysctl(3) and the now obsolete kvm(3). The sysctl(3) interface provides read and write access to a management information base (MIB) whose contents are various categories of kernel information, such as information related to processes, file systems, virtual memory, networking, and debugging in general. When the /dev/kmem device is available, the kvm(3) interface provides access to raw kernel memory. Besides, the I/O Kit programming interfaces and tools such as ioreg and IORegistryExplorer.app allow user-space inspection of a variety of kernel information on Mac OS X.

The sysctl() system call was introduced in 4.4BSD as a safe, reliable, and portable (across kernel versions) way to perform user-kernel data exchange.

procfs and pseudofs source from FreeBSD and morph them to work on Mac OS X. Shortly after I had the thing mounting, I ran into a kernel panic. (Writing in-kernel file systems is very conducive to this behavior.) Before I could begin two-machine debugging, one of the two Macintosh computers I had access to had catastrophic hardware failure. Apple gave me extremely poor repair experience (taking weeks to fix one problem and returning the machine with something else broken; repeat this several times over). So much so that by the time the machine finally worked, I had no interest left in debugging procfs for Mac OS X.

A MacFUSE-Based procfs

MacFUSE, a Mac OS X implementation of the FUSE (File System in User Space) mechanism. The first Mac OS X-specific example file system I wrote for MacFUSE was procfs—this time as a user-space file system, of course. I've been meaning to release it as an open source MacFUSE example, but didn't find the time to package it up until now.

pcre (Perl Compatible Regular Expressions) library. The macros are meant to make it easier to extend the file system.

Mac OS X Internals. In particular, refer to Chapters 6 (The xnu Kernel), 7 (Processes), 8 (Virtual Memory), and 9 (Interprocess Communication).

Compiling and Using procfs

pcre libraries, you will first need to download, compile, and install pcre. Note that if you install pcre in a location other than /usr/local/, you will need to modify the Makefile in procfs source. The Makefilealso assumes that you have the MacFUSE libraries installed under /usr/local/.

sudo make install ...

MacFUSE source tree checked out under /work/macfuse/, you can compile procfs as follows.

make ...

Having successfully compiled procfs, you can mount it, say, on the /proc directory, as follows.

mount ... procfs on /proc (read-only, synchronous)

Now you can explore the process file system on Mac OS X.

ls -F /proc 0/ 151/ 212/ 35/ 47/ 1/ 165/ 215/ 36/ 51/ 1166/ 187/ 218/ 38/ 5338/ 1187/ 194/ 221/ 39/ 54/ 1196/ 195/ 23/ 3954/ 59/ 1216/ 196/ 27/ 3976/ 62/ 131/ 200/ 28807/ 40/ 64/ 140/ 201/ 28902/ 42/ 67/ 145/ 202/ 32/ 43/ 867/ 146/ 204/ 33/ 45/ 95/ 15094/ 206/ 34/ 46/ hardware/

There is a directory under /proc for each process on the system, with the numeric process ID (pid) being the directory name. pid 0 corresponds to the kernel, pid 1 is launchd, and so on.

ls -l total 0 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 . 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 .. 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 carbon 0 -r--r--r-- 1 root wheel 0 May 7 23:44 cmdline 0 -r--r--r-- 1 root wheel 0 May 7 23:44 jobc 0 -r--r--r-- 1 root wheel 0 May 7 23:44 paddr 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 pcred 0 -r--r--r-- 1 root wheel 0 May 7 23:44 pgid 0 -r--r--r-- 1 root wheel 0 May 7 23:44 ppid 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 task 0 -r--r--r-- 1 root wheel 0 May 7 23:44 tdev 0 -r--r--r-- 1 root wheel 0 May 7 23:44 tpgid 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 ucred 0 -r--r--r-- 1 root wheel 0 May 7 23:44 wchan

For a given pid, the /proc/pid/ directory has some files containing certain BSD-level information. The carbon/ subdirectory contains additional information for processes that are also Carbon processes. The pcred/ and ucred/subdirectories contains files exporting process and user credentials, respectively.

cat ucred/groups ucred/uid 501(singh) 81(appserveradm) 79(appserverusr) 80(admin) 501(singh)

The task/ subdirectory contains a variety of Mach task information: scheduling data, virtual memory regions, Mach ports, threads, register state, and so on.

cat threads/7b03/states/thread/esp bfffef6c

The hardware/ subdirectory contains subdirectories for several pieces of hardware on the machine, allowing certain hardware data to be read from virtual files.

In a subsequent version of procfs, the /hardware/ subdirectory was moved to appear under the /system/ subdirectory, which in turn now contains hardware/ and firmware subdirectories.

cat /proc/hardware/cpus/0/data slot 0 (master), running type 7, subtype 4 6751211 ticks (user 91036 system 141938 idle 6517257 nice 980) cpu uptime 18h 45m 12s

cat /proc/hardware/lightsensor/data 49 893

Compatibility

Although there's no universal standard for either the hierarchy of file system objects in procfs or the contents of these objects, some implementations are rather popular because of the prevalence of their underlying operating systems. For example, FreeBSD contains linprocfs, a process file system that emulates a subset of Linux procfs. linprocfs was necessary for Linux binary emulation to work completely. This procfs implementation doesn't attempt to conform to any other implementation. For somebody with enough determination and time, it could be an interesting project to create a Linux-compatible (or Solaris-compatible, or whatever) version of MacFUSE-based procfs for Mac OS X.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-06-29
  • 2021-12-06
  • 2022-12-23
  • 2021-09-24
猜你喜欢
  • 2022-02-07
  • 2021-05-18
  • 2021-12-26
  • 2021-11-15
  • 2022-12-23
相关资源
相似解决方案