Procster  0.1
Procster-ViewProcessesovertheweb
procutil.c File Reference

Misc Process utilities (not related to process lists or trees). More...

#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <jansson.h>
#include "proclister.h"
Include dependency graph for procutil.c:

Functions

void par_add_child (proc_t *par, proc_t *ch)
 Add a child process to a parent. More...
 
GSListpar_get_ch (proc_t *p)
 Get a list of direct child processes of parent process. More...
 
int proc_kill (int pid)
 Kill process by sending SIGKILL signal to it. More...
 
void proc_st_iso (proc_t *p, char *tbuf, size_t size, int fix)
 Convert / Format p->start_time into human readable ISO time. More...
 
int gsleader (proc_t *p)
 Return Process leadership position (NONE=0, GROUP=1, SESSION=2). More...
 
int list2str (char **list, char buf[], int size)
 Create approximate cmdline string out of char ** cmdline. More...
 
json_tproc_to_json (proc_t *proc, char *cmdline)
 Populate Process node into (Jansson) JSON Object. More...
 
void ptree_dump (proc_t *p, int lvl)
 Dump process tree to STDOUT. More...
 
json_tptree_json (proc_t *p, int lvl)
 Convert process tree to JSON. More...
 
void ptree_free (proc_t *p, int lvl)
 Free/Release the tree of processes. More...
 

Variables

char * leadstr [] = {"", "GRPLEAD", "SESSLEAD", NULL}
 

Detailed Description

Misc Process utilities (not related to process lists or trees).

Function Documentation

◆ gsleader()

int gsleader ( proc_t p)

Return Process leadership position (NONE=0, GROUP=1, SESSION=2).

Returned int values can be converted to string labels by

Parameters
p- process
Returns
Integer encoded value for leadership status (See above desc.).

Referenced by ptree_dump().

◆ list2str()

int list2str ( char **  list,
char  buf[],
int  size 
)

Create approximate cmdline string out of char ** cmdline.

Approximate means no quoting or escaping is done to turn char ** items back into real runnable properly formatted commandline string. Approximate is good enough for this process listing usage.

Parameters
list- Array of Strings
buf- String buffer where Array items are serialized space-separated (must contain enough space for string output)
size- Size of buf (in bytes)
Returns
true/1 for serialization being done, 0 for list being NULL (no serialization can be done).
Todo:
Pass buffer size

Referenced by proc_to_json().

◆ par_add_child()

void par_add_child ( proc_t par,
proc_t ch 
)

Add a child process to a parent.

Child is added to an "abused" (not meant for this purpose) struct member (however still a pointer member "lxcname") of the process structure. Facilitates appropriate casts to allow smooth operation and easy use.

Parameters
par- Parent
ch- Child

Referenced by proc_tree().

◆ par_get_ch()

GSList* par_get_ch ( proc_t p)

Get a list of direct child processes of parent process.

Parameters
p- Parent process whose direct children are being accessed
Returns
- List of children

Referenced by ptree_dump(), ptree_free(), and ptree_json().

◆ proc_kill()

int proc_kill ( int  pid)

Kill process by sending SIGKILL signal to it.

See also man 7 signal and man 2 kill.

Parameters
pid- PID of individual process (>= 1)
Returns
0 for process being killed successfully, 1 and up for errors

Referenced by answer_to_connection0().

◆ proc_st_iso()

void proc_st_iso ( proc_t p,
char *  tbuf,
size_t  size,
int  fix 
)

Convert / Format p->start_time into human readable ISO time.

Parameters
p- Process Object
tbuf- String buffer for ISO time
size- Size for tbuf (will be forwarded to strftime())
fix- N/A (yet), pass 0 (TODO: Fix start_time to reflect EPOC time)
Returns
None

References proc_stime_ux.

Referenced by ptree_dump().

◆ proc_to_json()

json_t* proc_to_json ( proc_t proc,
char *  cmdline 
)

Populate Process node into (Jansson) JSON Object.

Can be used to populate processes in linear list or tree formats.

Parameters
proc- Process
cmdline- Buffer to serialize (approximate) command line with arguments into.
Todo:
See https://groups.google.com/g/jansson-users/c/xD8QLQF3ex8 and the need to use json_object_set_new()

References list2str(), and proc_stime_ux.

Referenced by proc_list_json2(), and ptree_json().

◆ ptree_dump()

void ptree_dump ( proc_t p,
int  lvl 
)

Dump process tree to STDOUT.

Parameters
p- Root process of the tree
lvl- Level in the process tree (Intial caller should pass 0)
Returns
None

References gsleader(), leadstr, par_get_ch(), and proc_st_iso().

Referenced by proc_tree().

◆ ptree_free()

void ptree_free ( proc_t p,
int  lvl 
)

Free/Release the tree of processes.

Release custom used (OLD:lxcname) "sd_uunit" member (GSList *) and mark it unallocated (NULL) to not accidentally free it wrongly (or leak memory).

Parameters
p- Root process
lvl- Recursion level (explicit caller should pass 0)
Returns
None

References freeproc(), g_slist_free(), par_get_ch(), and proc_chn_init.

Referenced by answer_to_connection0(), and proc_list_json2().

◆ ptree_json()

json_t* ptree_json ( proc_t p,
int  lvl 
)

Convert process tree to JSON.

Parameters
p- Root process node of the tree (linux kernel, pid=0)
lvl- Recursion level (explicit caller should pass 0)
Returns
Jansson JSON root node

References par_get_ch(), and proc_to_json().

Referenced by answer_to_connection0(), and proc_list_json2().

Variable Documentation

◆ leadstr

char* leadstr[] = {"", "GRPLEAD", "SESSLEAD", NULL}

Referenced by ptree_dump().