ptools

Use Solaris/Illumos proc(1) tools in Node.js

Illumos ptools

Use Solaris/Illumos proc(1) tools in Node.js

NOTE: read the manpage before using any of these tools to understand their risks!

npm install ptools
var ptools = require('ptools');

Get the current working directory of a given pid (defaults to self)

Get the full process tree on the system. The return object is a series of nested objects with children and proc values, keyed off the pid.

Get the credentials of a running process (uid, gid, etc)

Get a list of loaded libraries of a running process

Fire the callback when a given pid dies

Grabs a ptime snapshot of a given pid (defaults to self)

Call prun on a given pid. No arguments other than a possible exception are given to the completion callback.

Call pstop on a given pid. No arguments other than a possible exception are given to the completion callback.

This function will parse the output of pargs -ea to get the full environment and argument list returned as { argv: [], envp: {} }.

var pwdx = require('ptools').pwdx;
 
pwdx(process.pid, function(errdir) {
  console.log(dir);
});

yields

/home/dave/dev/node-ptools
var ptree = require('ptools').ptree;
 
ptree(function(errtree) {
  console.log(tree);
});

yields (snipped for brevity)

{
  "16349": {
    "children": {},
    "proc": "/sbin/init"
  },
  "16387": {
    "children": {},
    "proc": "/lib/svc/bin/svc.configd"
  },
  "16836": {
    "children": {},
    "proc": "/sbin/sh /lib/svc/method/svc-dlmgmtd"
  },
  "18992": {
    "children": {
      "18996": {
        "children": {},
        "proc": "splunkd -p 9000 start"
      }
    },
    "proc": "splunkd -p 9000 start"
  }
}
var pcred = require('ptools').pcred;
 
pcred(function(errcred) {
  console.log(cred);
});

yields

{
  "uid": 2011,
  "gid": 2300,
  "groups": []
}
var pldd = require('ptools').pldd;
 
pldd(function(errlibraries) {
  console.log(libraries);
});

yields

[
  "/lib/libz.so.1",
  "/lib/librt.so.1",
  "/lib/libssl.so.0.9.8",
  "/lib/libcrypto.so.0.9.8",
  "/lib/libdl.so.1",
  "/lib/libsocket.so.1",
  "/lib/libnsl.so.1",
  "/lib/libkstat.so.1",
  "/opt/local/lib/libstdc++.so.6.0.16",
  "/lib/libm.so.2",
  "/opt/local/lib/libgcc_s.so.1",
  "/lib/libc.so.1"
]
var pwait = require('ptools').pwait;
 
pwait(245, function(err) {
  if (err) throw err;
  console.log('done');
});

This callback will fire when process 245 has exited. err will be set if any stderr was produced.

var ptime = require('ptools').ptime;
 
ptime(function(errtime) {
  console.log(time);
});

yields

{
  "real": 0.230678959,
  "user": 0.129828895,
  "sys": 0.079168602
}
var prun = require('ptools').prun;
 
prun(578, function(err) {
  if (err) throw err;
  console.log('done');
});

This will call prun on pid 578, and callback when it is done with a possible error.

var pstop = require('ptools').pstop;
 
pstop(578, function(err) {
  if (err) throw err;
  console.log('done');
});

This will call pstop on pid 578, and callback when it is done with a possible error.

var pargs = require('ptools').pargs;
 
pargs(function(errobj) {
  console.log(obj);
});

yields

{
  "argv": [
    "node",
    "examples/pargs-example.js"
  ],
  "envp": {
    "SHELL": "/usr/bin/bash",
    "TERM": "xterm-color",
    "MYVIMRC": "/home/dave/.vimrc",
    "USER": "dave",
    "PAGER": "less",
    "PATH": "/opt/local/bin:/opt/local/sbin:/usr/bin:/usr/sbin:/home/dave/bin",
    "MAIL": "/var/mail/dave",
    "_": "/opt/local/bin/node",
    "PWD": "/home/dave/dev/node-ptools",
    "EDITOR": "vim",
    "LANG": "en_US.UTF-8",
    "TZ": "US/Pacific",
    "HOME": "/home/dave",
    "VISUAL": "vim",
  }
}

Some scripts are JS implementations of the ptools, and some just fork+exec their respective tool.

  • pwdx
  • pargs
  • pldd
  • ptree
  • pcred
  • pwait
  • ptime
npm test

MIT Licensed