# Unraveling binary data out of the proc filesystem on Solaris

**URL:** <https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829>\
**Category:** Ruby\
**Created:** [August 25, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829 "2008-08-25T23:55:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 25, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/1 "2008-08-25T23:55:37Z")

</div>

Hi all,

Ruby 1.8.6  
Solaris 10

I’m trying to get process information on a Solaris using 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.

Here’s some sample code:

# sunos.rb

module Sys  
class ProcTable  
def self.ps(pid = nil)  
Dir.foreach("/proc") do |file|  
next if file =~ /\D/  
next if pid && file.to\_i != pid

```
        psinfo = IO.read("/proc/#{file}/psinfo")

        pid = psinfo[8,4].unpack("L")[0]
        puts "PID: #{pid}"

        argc = psinfo[188,4].unpack("L")[0] # pr_argc
        addr = psinfo[192,4].unpack("L")[0] # pr_argv

        size = argc * 4 # 4 is the sizeof(caddr32_t)
        asinfo = IO.read("/proc/#{file}/as", size, addr)

        # How do I unravel asinfo?
        p asinfo
     end
  end

```

end  
end

if $0 == **FILE**  
include Sys  
ProcTable.ps(2764) # or whatever pid you prefer  
end

The C code I’m trying to simulate, which I pulled from a post by Roger  
Faulkner on comp.unix.questions, looks something like this:

addr\_args = p.pr\_argv; /\* from the psinfo struct _/  
arg\_count = p.pr\_argc; /_ from the psinfo struct \*/

if((fd = open(as\_file, O\_RDONLY)) \< 0){  
/\* Do nothing - you can only get info on processes you rights to \*/  
}  
else  
{  
arg\_vec = malloc(arg\_count \* sizeof(uintptr\_t));  
(void)pread(fd, arg\_vec, arg\_count \* sizeof (uintptr\_t),  
addr\_args);

arg\_len = 16;  
arg = malloc(arg\_len+1);

for(i = 0; i \< arg\_count; i++) {  
if(pread(fd, arg, arg\_len, arg\_vec[i]) \< 0)  
continue;

```
  arg[arg_len] = '\0';
  if(strlen(arg) == arg_len){
     arg_len *= 2;
     arg = realloc(arg, arg_len + 1);
     i--;
     continue;
  }
  rb_ary_push(v_cmd_array, rb_str_new2(arg));

```

}  
free(arg);  
free(arg\_vec);  
}

close(fd);

Any ideas how to get the string(s) I need out of /proc//as ?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 26, 2008, 4:04am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/2 "2008-08-26T04:04:17Z")

</div>

Hi,

2008/8/26 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> arguments out of /proc//as. Basically, I need to know how to  
> next if pid && file.to\_i != pid  
> asinfo = IO.read(“/proc/#{file}/as”, size, addr)  
> ProcTable.ps(2764) # or whatever pid you prefer  
> }  
> if(pread(fd, arg, arg\_len, arg\_vec[i]) \< 0)  
> }  
> free(arg);  
> free(arg\_vec);  
> }
> 
> close(fd);
> 
> Any ideas how to get the string(s) I need out of /proc//as ?

Roger Faulkner mentioned in the com.unix.solraris like this:

/proc//as is not your ordinary sparse file.  
Attempts to read from non-existent portions of it yield a zero read().

The accessible portions of the as file are described by the  
/proc//map file. Reading the map file gives you an array  
of structures describing the legitimate addresses in the process.

Read (and re-read) the proc(4) man page documentation

Refer to  
[http://unix.derkeiler.com/Newsgroups/comp.unix.solaris/2003-05/3189.html](http://unix.derkeiler.com/Newsgroups/comp.unix.solaris/2003-05/3189.html)

According to the man page proc(4):

as  
Contains the address-space image of the process; it can be opened for  
both reading and writing. lseek(2) is used to position the file at the  
virtual address of interest and then the address space can be examined  
or changed through read(2) or write(2) (or by using pread(2) or  
pwrite(2) for the combined operation).

According to the man page pread(2):

The pread() function performs the same action as read(), except that  
it reads from a given position in the file without changing the file  
pointer. The first three arguments to pread() are the same as read()  
with the addition of a fourth argument offset for the desired position  
inside the file. pread() will read up to the maximum offset value that  
can be represented in an off\_t for regular files. An attempt to  
perform a pread() on a file that is incapable of seeking results in an  
error.

I guess you should use lseek and read combination.

HTH,

Park H.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 26, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/3 "2008-08-26T16:21:21Z")

</div>

On Aug 25, 7:59 pm, “Heesob P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > For the basic information, this is fairly straightforward, as I read  
> > class ProcTable  
> > argc = psinfo[188,4].unpack(“L”)[0] # pr\_argc  
> > end  
> > arg\_count = p.pr\_argc; /\* from the psinfo struct \*/  
> > arg\_len = 16;  
> > i–;  
> > Any ideas how to get the string(s) I need out of /proc//as ?
> 
> Roger Faulkner mentioned in the com.unix.solraris like this:
> 
> /proc//as is not your ordinary sparse file.  
> Attempts to read from non-existent portions of it yield a zero read().
> 
> The accessible portions of the as file are described by the  
> /proc//map file. Reading the map file gives you an array  
> of structures describing the legitimate addresses in the process.

That shouldn’t be necessary. I don’t do that in the C code. I wouldn’t  
think I would have to do that in Ruby.

> or changed through read(2) or write(2) (or by using pread(2) or  
> perform a pread() on a file that is incapable of seeking results in an  
> error.
> 
> I guess you should use lseek and read combination.

Ruby has no lseek that I’m aware of. I tried an open + seek + read  
approach, but I get the same result.

I guess I’ll have to wrap pread and pwrite unless there’s a way to  
read from a specific point in a file without moving the file pointer  
in Ruby that I don’t know of.

Maybe I’ll tinker with mmap.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 12:11am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/4 "2008-08-29T00:11:31Z")

</div>

On Aug 26, 8:16 am, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 1:56am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/5 "2008-08-29T01:56:12Z")

</div>

On Aug 28, 2008, at 4:04 PM, Daniel B. wrote:

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

ruby-dl ??

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 2:47am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/6 "2008-08-29T02:47:40Z")

</div>

On Aug 28, 5:48 pm, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Aug 28, 2008, at 4:04 PM, Daniel B. wrote:
> 
> > 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.
> 
> ruby-dl ??

It’s a dependency either way. I prefer the one I know…

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 3:42am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/7 "2008-08-29T03:42:41Z")

</div>

2008/8/26 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Aug 25, 7:59 pm, “Heesob P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > 2008/8/26 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

\>

> > I guess you should use lseek and read combination.
> 
> Ruby has no lseek that I’m aware of. I tried an open + seek + read  
> approach, but I get the same result.

Are you unaware IO#sysseek ? sysseek is actualy lseek.

## ------------------------------------------------------------- IO#sysseek ios.sysseek(offset, whence=SEEK\_SET) =\> integer

```
 Seeks to a given _offset_ in the stream according to the value of
 _whence_ (see +IO#seek+ for values of _whence_). Returns the new
 offset into the file.

    f = File.new("testfile")
    f.sysseek(-13, IO::SEEK_END) #=> 53
    f.sysread(10) #=> "And so on."

```

I guess open + sysseek +sysread will work for you.

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 4:10am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/8 "2008-08-29T04:10:45Z")

</div>

2008/8/29 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > \>  
> > Seeks to a given _offset_ in the stream according to the value of  
> > did something wrong, but I’m not sure what.
> 
> I also see nothing in the lseek or sysseek documentation that says it  
> doesn’t move the file pointer.

Did you check replacing pread with lseek and read combination in C  
code implementation not working? If then pread is the only way.

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 4:16am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/9 "2008-08-29T04:16:36Z")

</div>

On Aug 28, 7:53 pm, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > * * *
> 
> I tried sysseek on RHEL, but couldn’t make it work. It’s possible I  
> did something wrong, but I’m not sure what.

Er, Solaris 10, not RHEL. Whoops.

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 3:59am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/10 "2008-08-29T03:59:03Z")

</div>

On Aug 28, 7:37 pm, “Heesob P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Ruby has no lseek that I’m aware of. I tried an open + seek + read
> 
> ```
> f = File.new("testfile")
> f.sysseek(-13, IO::SEEK_END) #=> 53
> f.sysread(10) #=> "And so on."
> 
> ```
> 
> I guess open + sysseek +sysread will work for you.

I tried sysseek on RHEL, but couldn’t make it work. It’s possible I  
did something wrong, but I’m not sure what.

I also see nothing in the lseek or sysseek documentation that says it  
doesn’t move the file pointer.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 6:45am UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/11 "2008-08-29T06:45:42Z")

</div>

On Aug 28, 2008, at 6:42 PM, Daniel B. wrote:

> It’s a dependency either way. I prefer the one I know…

?? ruby dl is built in. if you access pread via that there is no  
dependancy which is not pure-ruby. just food for thought.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/12 "2008-08-29T16:31:00Z")

</div>

On Aug 29, 2008, at 4:33 AM, Daniel B. wrote:

> Oh, that’s right. I was thinking ruby-inline. Anyway, I thought ruby-  
> dl had 64 bit issues. I’ve no idea what the status of dl2 is, though.

hrm - well it works on osx and that’s 64 bit - but i honestly haven’t  
used it for quite some time. just seemed worth considering when a  
single function is needed - there is an example for libc stuff in the  
dist.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 29, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/unraveling-binary-data-out-of-the-proc-filesystem-on-solaris/146829/13 "2008-08-29T12:38:34Z")

</div>

On Aug 28, 10:40 pm, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Aug 28, 2008, at 6:42 PM, Daniel B. wrote:
> 
> > It’s a dependency either way. I prefer the one I know…
> 
> ?? ruby dl is built in. if you access pread via that there is no  
> dependancy which is not pure-ruby. just food for thought.

Oh, that’s right. I was thinking ruby-inline. Anyway, I thought ruby-  
dl had 64 bit issues. I’ve no idea what the status of dl2 is, though.

Thanks,

Dan
