Procster  0.1
Procster-ViewProcessesovertheweb
proclist.c File Reference

Initial prototyping functions to create JSON process listings. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include "proclister.h"
Include dependency graph for proclist.c:

Macros

#define IS_KTHREAD(proc)   (proc.ppid <= 2)
 

Functions

int arr2json (char **arr, int cnt)
 
char * proc_list_json (int flags)
 Create a process list directly from readproc() listing as JSON. More...
 
json_tproc_list_json2 (int flags)
 Create Process list JSON. More...
 

Detailed Description

Initial prototyping functions to create JSON process listings.

  • Create raw/rudimentatry JSON by sprintf() to string using shortest processing path
  • Create ("populate") JSON (AoO) data structure using jansson library.

Includes a small CLI testbench for testing output (to stdout).

Installing Dependencies

... on (Ubuntu/Debian):

sudo apt-get install libpropcs-dev g++ -o proctest proctest.c -lprocps \
  gcc -o proctest proctest.c -lprocps

Running

Store process list to a file:

./proctest list > procs.json

See proc_t structure def in proc/readproc.h (/usr/include/proc/readproc.h):

find /usr/include/ -name readproc.h | xargs -n 1 less -N
# Very likely the /usr/include/proc/readproc.h

Testing

Validate JSON output:

./proctest list | python -m json.tool

Naming of process members in JSON

Naming comes from

  • readproc.h (struct proc_t)
  • NPM process-list

Alternatives to printf-JSON

Info / References

Notes on ps STIME and procps start_time

See https://gitlab.com/procps-ng/procps/-/blob/master/ps/output.c Possibly do:

time_t t = getbtime() + pp->start_time / Hertz;

Macro Definition Documentation

◆ IS_KTHREAD

#define IS_KTHREAD (   proc)    (proc.ppid <= 2)

Referenced by proc_list_json(), and proc_list_json2().

Function Documentation

◆ arr2json()

int arr2json ( char **  arr,
int  cnt 
)

◆ proc_list_json()

char* proc_list_json ( int  flags)

Create a process list directly from readproc() listing as JSON.

For flags see FLAGS in man openproc.

Parameters
flagsThe openproc() flags that define the extent of parsing process info from /proc/$PID (There are ~ 20 flags available).
Returns
Dynamically allocated process listing JSON string or NULL in case of errors (error messages get written to stderr).
Todo:

Make process attibutes more configurable. Possibly corralate fields to generate to flags passed as input (need lookup tables for those).

Possibly wrap response with additional JSON object to be able to convey status and error messages.

References IS_KTHREAD, and PROC_FLAGS_DEFAULT.

◆ proc_list_json2()

json_t* proc_list_json2 ( int  flags)

Create Process list JSON.

Serialization is done using Jansson JSON library by building AoO and serializing it. The linear list of processes can be built using single stack based process entry buffer.

Parameters
flags- Flags for openproc() (See man openproc)
Returns
JSON array of (process) objects as jansson library data structure (has to be freed by caller)

References IS_KTHREAD, main(), proc_chn_init, PROC_FLAGS_DEFAULT, proc_list_json2(), proc_to_json(), proc_tree(), ptree_free(), and ptree_json().

Referenced by answer_to_connection0(), and proc_list_json2().