From: Daniel Berger Date: 2008-08-29T07:04:03+09:00 Subject: Re: Unraveling binary data out of the proc filesystem on Solaris On Aug 26, 8:16 am, Daniel Berger wrote: > On Aug 25, 7:59 pm, "Heesob Park" wrote: > > > > > Hi, > > > 2008/8/26 DanielBerger: > > > > Hi all, > > > > Ruby 1.8.6 > > >Solaris10 > > > > I'm trying to get process information on aSolarisusing pure Ruby. > > > For the basic information, this is fairly straightforward, as I read > > > out of /proc//psinfo. > > > > However, where I'm having trouble is getting the command line > > > arguments out of /proc//as. Basically, I need to know how to > > > unravel the data I'm reading into a human readable string. The bad news is that it requires pread() after all as far as I can tell. The good news is that I've added pread and pwrite support to the io-extra library, and it's available in io-extra-1.1.0 and later. Here's a snippet: require 'io/extra' # Mozilla on Solaris. Use whatever pid you wish. # # ps -ef shows the args to be "/bin/ksh", "-p", and "/usr/sfw/bin/ mozilla" # pid = 2253 # First, get the psinfo struct data and unravel some information we need psinfo = IO.read("/proc/#{pid}/psinfo") pr_argc = psinfo[188, 4].unpack("L")[0] pr_argv = psinfo[192, 4].unpack("L")[0] # Second, get our initial address from /proc//as fd = File.open("/proc/#{pid}/as") addr = IO.pread(fd.fileno, pr_argc * 4, pr_argv).unpack("L")[0] # Third, get each command argument, incrementing the address as needed. I've # hard coded the read limit at 128. Adjust as you see fit. 0.upto(pr_argc - 1){ |i| data = IO.pread(fd.fileno, 128, addr) addr += data.length + 1 # Add 1 for the space p data } # Finally, close our file descriptor fd.close On my system, that prints "/bin/ksh", "-p" and "/usr/sfw/bin/mozilla" as expected. Regards, Dan PS - Doing this also helped me realize that there's a bug in the C implementation, too. :/