OBJ(cached) {
human_readable(cur->cached * 1024, p, 255);
}
+ OBJ(cmdline_to_pid) {
+ print_cmdline_to_pid(obj, p, p_max_size);
+ }
OBJ(cpu) {
if (obj->data.i > info.cpu_count) {
NORM_ERR("obj->data.i %i info.cpu_count %i",
new_outline(p, obj->data.l);
}
OBJ(pid_chroot) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_chroot(obj, p, p_max_size);
}
OBJ(pid_cmdline) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_cmdline(obj, p, p_max_size);
}
OBJ(pid_cwd) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_cwd(obj, p, p_max_size);
}
OBJ(pid_environ) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_environ(obj, p, p_max_size);
}
OBJ(pid_environ_list) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_environ_list(obj, p, p_max_size);
}
OBJ(pid_exe) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_exe(obj, p, p_max_size);
}
OBJ(pid_openfiles) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_openfiles(obj, p, p_max_size);
}
OBJ(pid_state) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_state(obj, p, p_max_size);
}
+ OBJ(pid_state_short) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
+ print_pid_state_short(obj, p, p_max_size);
+ }
OBJ(pid_stderr) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_stderr(obj, p, p_max_size);
}
OBJ(pid_stdin) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_stdin(obj, p, p_max_size);
}
OBJ(pid_stdout) {
+ char buf[max_user_text];
+
+ generate_text_internal(buf, max_user_text, *obj->sub, cur);
+ obj->data.s = buf;
print_pid_stdout(obj, p, p_max_size);
}
OBJ(processes) {
END OBJ(desktop_name, &update_x11info)
#endif
END OBJ(nodename, 0)
+ END OBJ_ARG(cmdline_to_pid, 0, "cmdline_to_pid needs a command line as argument")
+ scan_cmdline_to_pid_arg(obj, arg, free_at_crash);
END OBJ_ARG(pid_chroot, 0, "pid_chroot needs a pid as argument")
- scan_pid_chroot_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_cmdline, 0, "pid_cmdline needs a pid as argument")
- scan_pid_cmdline_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_cwd, 0, "pid_cwd needs a pid as argument")
- scan_pid_cwd_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_environ, 0, "pid_environ needs arguments")
- scan_pid_environ_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_environ_list, 0, "pid_environ_list needs a pid as argument")
- scan_pid_environ_list_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_exe, 0, "pid_exe needs a pid as argument")
- scan_pid_exe_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_openfiles, 0, "pid_openfiles needs a pid as argument")
- scan_pid_openfiles_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_state, 0, "pid_state needs a pid as argument")
- scan_pid_state_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
+ END OBJ_ARG(pid_state_short, 0, "pid_state_short needs a pid as argument")
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_stderr, 0, "pid_stderr needs a pid as argument")
- scan_pid_stderr_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_stdin, 0, "pid_stdin needs a pid as argument")
- scan_pid_stdin_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ_ARG(pid_stdout, 0, "pid_stdout needs a pid as argument")
- scan_pid_stdout_arg(obj, arg, free_at_crash);
+ obj->sub = malloc(sizeof(struct text_object));
+ extract_variable_text_internal(obj->sub, arg);
END OBJ(processes, &update_total_processes)
END OBJ(running_processes, &update_running_processes)
END OBJ(threads, &update_threads)
free_sysfs_sensor(obj);
break;
#endif /* __linux__ */
- case OBJ_pid_chroot:
+ case OBJ_cmdline_to_pid:
free(data.s);
break;
+ case OBJ_pid_environ:
+ case OBJ_pid_chroot:
case OBJ_pid_cmdline:
- free(data.s);
- break;
case OBJ_pid_cwd:
- free(data.s);
- break;
- case OBJ_pid_environ:
- free_pid_environ(obj);
- break;
case OBJ_pid_environ_list:
- free(data.s);
- break;
case OBJ_pid_exe:
- free(data.s);
- break;
case OBJ_pid_openfiles:
- free(data.s);
- break;
case OBJ_pid_state:
- free(data.s);
- break;
+ case OBJ_pid_state_short:
case OBJ_pid_stderr:
- free(data.s);
- break;
case OBJ_pid_stdin:
- free(data.s);
- break;
case OBJ_pid_stdout:
- free(data.s);
+ if(obj->sub) {
+ free_text_objects(obj->sub, 1);
+ free(obj->sub);
+ }
break;
case OBJ_read_tcp:
free_read_tcp(obj);
#include <ctype.h>
#include <dirent.h>
-void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
-{
- pid_t pid;
-
- if(sscanf(arg, "%d", &pid) == 1) {
- asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
- } else {
- CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
- }
-}
-
-void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
-{
- scan_pid_arg(obj, arg, free_at_crash, "cmdline");
-}
-
-char* readfile(char* filename, int* total_read) {
+char* readfile(char* filename, int* total_read, char showerror) {
FILE* file;
char* buf = NULL;
int bytes_read;
buf[*total_read] = 0;
}while(bytes_read != 0);
fclose(file);
- } else {
+ } else if(showerror != 0) {
NORM_ERR(READERR, filename);
}
return buf;
}
+void pid_readlink(char *file, char *p, int p_max_size)
+{
+ char buf[p_max_size];
+
+ memset(buf, 0, p_max_size);
+ if(readlink(file, buf, p_max_size) >= 0) {
+ snprintf(p, p_max_size, "%s", buf);
+ } else {
+ NORM_ERR(READERR, file);
+ }
+}
+
+struct ll_string {
+ char *string;
+ struct ll_string* next;
+};
+
+struct ll_string* addnode(struct ll_string* end, char* string) {
+ struct ll_string* current = malloc(sizeof(struct ll_string));
+ current->string = strdup(string);
+ current->next = NULL;
+ if(end != NULL) end->next = current;
+ return current;
+}
+
+void freelist(struct ll_string* front) {
+ if(front != NULL) {
+ free(front->string);
+ if(front->next != NULL) {
+ freelist(front->next);
+ }
+ free(front);
+ }
+}
+
+int inlist(struct ll_string* front, char* string) {
+ struct ll_string* current;
+
+ for(current = front; current != NULL; current = current->next) {
+ if(strcmp(current->string, string) == 0) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
+ char *buffer;
+
+ asprintf(&buffer, PROCDIR "/%s/root", obj->data.s);
+ print_pid_readlink(buffer, p, p_max_size);
+ free(buffer);
+}
+
void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
{
char* buf;
int i, bytes_read;
- buf = readfile(obj->data.s, &bytes_read);
+ asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
+ strcpy(obj->data.s, buf);
+ free(buf);
+ buf = readfile(obj->data.s, &bytes_read, 1);
if(buf != NULL) {
for(i = 0; i < bytes_read-1; i++) {
if(buf[i] == 0) {
}
}
-void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
-{
- scan_pid_arg(obj, arg, free_at_crash, "cwd");
-}
-
void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
{
char buf[p_max_size];
int bytes_read;
- memset(buf, 0, p_max_size);
+ sprintf(buf, PROCDIR "/%s/cwd", obj->data.s);
+ strcpy(obj->data.s, buf);
bytes_read = readlink(obj->data.s, buf, p_max_size);
if(bytes_read != -1) {
+ buf[bytes_read] = 0;
snprintf(p, p_max_size, "%s", buf);
} else {
NORM_ERR(READERR, obj->data.s);
}
}
-void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
-{
- pid_t pid;
- int i;
- struct environ_data* ed = malloc(sizeof(struct environ_data));
-
- ed->var = malloc(strlen(arg));
- if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
- asprintf(&ed->file, PROCDIR "/%d/environ", pid);
- for(i = 0; ed->var[i] != 0; i++) {
- ed->var[i] = toupper(ed->var[i]);
- }
- obj->data.opaque = ed;
- } else {
- free(ed->var);
- free(ed);
- CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
- }
-}
-
void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
{
- char *buf = NULL;
- char *searchstring = ((struct environ_data*) obj->data.opaque)->var;
- int bytes_read, total_read = 0;
+ int i, total_read;
+ pid_t pid;
+ char *buf, *file, *var=strdup(obj->data.s);;
- buf = readfile(((struct environ_data*) obj->data.opaque)->file, &total_read);
- if(buf != NULL) {
- for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
- if(strncmp(buf + bytes_read, searchstring, strlen(searchstring)) == 0 && *(buf + bytes_read + strlen(searchstring)) == '=') {
- snprintf(p, p_max_size, "%s", buf + bytes_read + strlen(searchstring) + 1);
- free(buf);
- return;
+ if(sscanf(obj->data.s, "%d %s", &pid, var) == 2) {
+ for(i = 0; var[i] != 0; i++) {
+ var[i] = toupper(var[i]);
+ }
+ asprintf(&file, PROCDIR "/%d/environ", pid);
+ buf = readfile(file, &total_read, 1);
+ free(file);
+ if(buf != NULL) {
+ for(i = 0; i < total_read; i += strlen(buf + i) + 1) {
+ if(strncmp(buf + i, var, strlen(var)) == 0 && *(buf + i + strlen(var)) == '=') {
+ snprintf(p, p_max_size, "%s", buf + i + strlen(var) + 1);
+ free(buf);
+ free(var);
+ return;
+ }
}
+ free(buf);
}
- p[0] = 0;
- free(buf);
+ free(var);
+ *p = 0;
}
}
-void free_pid_environ(struct text_object *obj) {
- free(((struct environ_data*) obj->data.opaque)->file);
- free(((struct environ_data*) obj->data.opaque)->var);
- free(obj->data.opaque);
-}
-
-void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
-{
- scan_pid_arg(obj, arg, free_at_crash, "environ");
-}
-
void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
{
char *buf = NULL;
int bytes_read, total_read;
int i = 0;
- buf = readfile(obj->data.s, &total_read);
+ asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
+ strcpy(obj->data.s, buf);
+ free(buf);
+ buf = readfile(obj->data.s, &total_read, 1);
if(buf != NULL) {
for(bytes_read = 0; bytes_read < total_read; buf[i-1] = ';') {
buf2 = strdup(buf+bytes_read);
}
}
-void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
-{
- scan_pid_arg(obj, arg, free_at_crash, "exe");
+void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
+ char *buffer;
+
+ asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
+ print_pid_readlink(buffer, p, p_max_size);
+ free(buffer);
}
-void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
-{
+void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
+ DIR* dir;
+ struct dirent *entry;
char buf[p_max_size];
+ int length, totallength = 0;
+ struct ll_string* files_front = NULL;
+ struct ll_string* files_back = NULL;
- memset(buf, 0, p_max_size);
- if(readlink(obj->data.s, buf, p_max_size) >= 0) {
- snprintf(p, p_max_size, "%s", buf);
+ dir = opendir(obj->data.s);
+ if(dir != NULL) {
+ while ((entry = readdir(dir))) {
+ if(entry->d_name[0] != '.') {
+ snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
+ length = readlink(buf, buf, p_max_size);
+ buf[length] = 0;
+ if(inlist(files_front, buf) == 0) {
+ files_back = addnode(files_back, buf);
+ snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
+ totallength += length + strlen("; ");
+ }
+ if(files_front == NULL) {
+ files_front = files_back;
+ }
+ }
+ }
+ closedir(dir);
+ freelist(files_front);
+ p[totallength - strlen("; ")] = 0;
} else {
- NORM_ERR(READERR, obj->data.s);
+ p[0] = 0;
}
}
-void scan_pid_chroot_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "root");
-}
-
-void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
- print_pid_readlink(obj, p, p_max_size);
-}
-
-void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
- print_pid_readlink(obj, p, p_max_size);
-}
-
-void scan_pid_state_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "status");
-}
-
void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
char *begin, *end, *buf = NULL;
int bytes_read;
- buf = readfile(obj->data.s, &bytes_read);
+ asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
+ strcpy(obj->data.s, buf);
+ free(buf);
+ buf = readfile(obj->data.s, &bytes_read, 1);
if(buf != NULL) {
begin = strstr(buf, STATE_ENTRY);
if(begin != NULL) {
}
}
-void scan_pid_stderr_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "fd/2");
+void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
+ char *begin, *buf = NULL;
+ int bytes_read;
+
+ asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
+ strcpy(obj->data.s, buf);
+ free(buf);
+ buf = readfile(obj->data.s, &bytes_read, 1);
+ if(buf != NULL) {
+ begin = strstr(buf, STATE_ENTRY);
+ if(begin != NULL) {
+ snprintf(p, p_max_size, "%c", *begin);
+ } else {
+ NORM_ERR(STATENOTFOUND, obj->data.s);
+ }
+ free(buf);
+ }
}
void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
- print_pid_readlink(obj, p, p_max_size);
-}
+ char *buffer;
-void scan_pid_stdin_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "fd/0");
+ asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
+ print_pid_readlink(buffer, p, p_max_size);
+ free(buffer);
}
void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
- print_pid_readlink(obj, p, p_max_size);
-}
+ char *buffer;
-void scan_pid_stdout_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "fd/1");
+ asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
+ print_pid_readlink(buffer, p, p_max_size);
+ free(buffer);
}
void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
- print_pid_readlink(obj, p, p_max_size);
-}
+ char *buffer;
-void scan_pid_openfiles_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
- scan_pid_arg(obj, arg, free_at_crash, "fd");
+ asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
+ print_pid_readlink(buffer, p, p_max_size);
+ free(buffer);
}
-struct ll_string {
- char *string;
- struct ll_string* next;
-};
-
-struct ll_string* addnode(struct ll_string* end, char* string) {
- struct ll_string* current = malloc(sizeof(struct ll_string));
- current->string = strdup(string);
- current->next = NULL;
- if(end != NULL) end->next = current;
- return current;
-}
+void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
+ unsigned int i;
-void freelist(struct ll_string* front) {
- if(front != NULL) {
- free(front->string);
- if(front->next != NULL) {
- freelist(front->next);
+ if(strlen(arg) > 0) {
+ obj->data.s = strdup(arg);
+ for(i = 0; obj->data.s[i] != 0; i++) {
+ while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
+ memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
+ }
}
- free(front);
- }
-}
-
-int inlist(struct ll_string* front, char* string) {
- struct ll_string* current;
-
- for(current = front; current != NULL; current = current->next) {
- if(strcmp(current->string, string) == 0) {
- return 1;
+ if(obj->data.s[i - 1] == ' ') {
+ obj->data.s[i - 1] = 0;
}
+ } else {
+ CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
}
- return 0;
}
-void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
+void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
DIR* dir;
struct dirent *entry;
- char buf[p_max_size];
- int length, totallength = 0;
- struct ll_string* files_front = NULL;
- struct ll_string* files_back = NULL;
+ char *filename, *buf;
+ int bytes_read, i;
- dir = opendir(obj->data.s);
+ dir = opendir(PROCDIR);
if(dir != NULL) {
while ((entry = readdir(dir))) {
- if(entry->d_name[0] != '.') {
- snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
- length = readlink(buf, buf, p_max_size);
- buf[length] = 0;
- if(inlist(files_front, buf) == 0) {
- files_back = addnode(files_back, buf);
- snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
- totallength += length + strlen("; ");
+ asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
+ buf = readfile(filename, &bytes_read, 0);
+ free(filename);
+ if(buf != NULL) {
+ for(i = 0; i < bytes_read - 1; i++) {
+ if(buf[i] == 0) buf[i] = ' ';
}
- if(files_front == NULL) {
- files_front = files_back;
+ if(strcmp(buf, obj->data.s) == 0) {
+ snprintf(p, p_max_size, "%s", entry->d_name);
+ free(buf);
+ closedir(dir);
+ return;
}
+ free(buf);
}
}
closedir(dir);
- freelist(files_front);
- p[totallength - strlen("; ")] = 0;
} else {
- p[0] = 0;
+ NORM_ERR(READERR, PROCDIR);
}
}