libamxp  1.4.0
Patterns C Implementation
amxp_proc_info.c File Reference
#include <sys/resource.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <dirent.h>
#include <ctype.h>
#include <amxc/amxc.h>
#include <amxc/amxc_macros.h>
#include <amxp/amxp.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static amxp_expr_status_t amxp_proci_expr_get_field (UNUSED amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
 
static void amxp_proci_fill (amxp_proc_info_t *proc_info, amxc_var_t *data)
 
static amxp_proc_info_tamxp_proci_read_stat (amxc_string_t *file)
 
static int amxp_proci_scan_proc (amxc_llist_t *procs, amxp_expr_t *expr)
 
void amxp_proci_free_it (amxc_llist_it_t *it)
 Delete a amxp_proc_info_t by it is linked list iterator. More...
 
int amxp_proci_findf (amxc_llist_t *result, const char *filter,...)
 Build a linked list of running processes. More...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxp_proc_info.c.

Function Documentation

◆ amxp_proci_expr_get_field()

static amxp_expr_status_t amxp_proci_expr_get_field ( UNUSED amxp_expr_t expr,
amxc_var_t *  value,
const char *  path,
void *  priv 
)
static

Definition at line 75 of file amxp_proc_info.c.

78  {
80  amxp_proc_info_t* pi = (amxp_proc_info_t*) priv;
81 
82  if(strcmp(path, "name") == 0) {
83  amxc_var_set(cstring_t, value, pi->name);
84  } else if(strcmp(path, "parent_pid") == 0) {
85  amxc_var_set(int32_t, value, pi->parent_pid);
86  } else if(strcmp(path, "ppid") == 0) {
87  amxc_var_set(int32_t, value, pi->parent_pid);
88  } else if(strcmp(path, "state") == 0) {
89  amxc_var_set(int8_t, value, pi->state);
90  } else {
92  }
93 
94  return status;
95 }
enum _expr_status amxp_expr_status_t
Expression status/error codes.
@ amxp_expr_status_field_not_found
@ amxp_expr_status_ok
Structure containing the process information.
uint8_t state
int32_t parent_pid
static amxc_string_t path

◆ amxp_proci_fill()

static void amxp_proci_fill ( amxp_proc_info_t proc_info,
amxc_var_t *  data 
)
static

Definition at line 97 of file amxp_proc_info.c.

97  {
98  int index = 0;
99  amxc_var_for_each(item, data) {
100  switch(index) {
101  case 0:
102  proc_info->pid = amxc_var_dyncast(int32_t, item);
103  break;
104  case 1: {
105  amxc_string_t name;
106  char* n = amxc_var_take(cstring_t, item);
107  amxc_string_init(&name, 0);
108  amxc_string_push_buffer(&name, n, n == NULL ? 0 : strlen(n) + 1);
109  amxc_string_trim(&name, ispunct);
110  proc_info->name = amxc_string_take_buffer(&name);
111  }
112  break;
113  case 2:
114  proc_info->state = amxc_var_dyncast(int8_t, item);
115  break;
116  case 3:
117  proc_info->parent_pid = amxc_var_dyncast(int32_t, item);
118  break;
119  case 4:
120  proc_info->process_gid = amxc_var_dyncast(int32_t, item);
121  break;
122  case 5:
123  proc_info->session_id = amxc_var_dyncast(int32_t, item);
124  break;
125  }
126  index++;
127  }
128 }
int32_t session_id
int32_t pid
int32_t process_gid

◆ amxp_proci_read_stat()

static amxp_proc_info_t* amxp_proci_read_stat ( amxc_string_t *  file)
static

Definition at line 130 of file amxp_proc_info.c.

130  {
131  FILE* fp = NULL;
132  amxp_proc_info_t* proc_info = NULL;
133  amxc_var_t stat;
134  ssize_t read = 0;
135  char* line = NULL;
136  size_t len = 0;
137 
138  amxc_var_init(&stat);
139 
140  fp = fopen(amxc_string_get(file, 0), "r");
141  if(fp == NULL) {
142  goto exit;
143  }
144 
145  read = getline(&line, &len, fp);
146  if(read == -1) {
147  fclose(fp);
148  free(line);
149  goto exit;
150  }
151  fclose(fp);
152 
153  amxc_var_push(ssv_string_t, &stat, line);
154  amxc_var_cast(&stat, AMXC_VAR_ID_LIST);
155 
156  proc_info = (amxp_proc_info_t*) calloc(1, sizeof(amxp_proc_info_t));
157  when_null(proc_info, exit);
158 
159  amxp_proci_fill(proc_info, &stat);
160 
161 exit:
162  amxc_var_clean(&stat);
163  return proc_info;
164 }
static void amxp_proci_fill(amxp_proc_info_t *proc_info, amxc_var_t *data)

◆ amxp_proci_scan_proc()

static int amxp_proci_scan_proc ( amxc_llist_t *  procs,
amxp_expr_t expr 
)
static

Definition at line 166 of file amxp_proc_info.c.

166  {
167  int retval = -1;
168  DIR* dp;
169  struct dirent* ep;
170  amxc_string_t filename;
171  amxp_proc_info_t* pi = NULL;
173 
174  amxc_string_init(&filename, 128);
175 
176  dp = opendir("/proc/");
177  when_null(dp, exit);
178 
179  for(ep = readdir(dp); ep; ep = readdir(dp)) {
180  if(ep->d_type != DT_DIR) {
181  continue;
182  }
183 
184  if(ep->d_name[0] == '.') {
185  continue;
186  }
187 
188  amxc_string_reset(&filename);
189  amxc_string_setf(&filename, "/proc/%s/stat", ep->d_name);
190  pi = amxp_proci_read_stat(&filename);
191  if(pi != NULL) {
192  if(expr != NULL) {
193  if(amxp_expr_evaluate(expr, amxp_proci_expr_get_field, pi, &status)) {
194  amxc_llist_append(procs, &pi->it);
195  } else {
196  amxp_proci_free_it(&pi->it);
197  }
198  } else {
199  amxc_llist_append(procs, &pi->it);
200  }
201  }
202 
203  }
204  closedir(dp);
205 
206  retval = 0;
207 
208 exit:
209  amxc_string_clean(&filename);
210  return retval;
211 }
static amxp_expr_status_t amxp_proci_expr_get_field(UNUSED amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
static amxp_proc_info_t * amxp_proci_read_stat(amxc_string_t *file)
bool amxp_expr_evaluate(amxp_expr_t *expr, amxp_expr_get_field_t fn, void *priv, amxp_expr_status_t *status)
Evaluates an expression.
void amxp_proci_free_it(amxc_llist_it_t *it)
Delete a amxp_proc_info_t by it is linked list iterator.
amxc_llist_it_t it