X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fproc.c;h=218393e0002e7919707eddd1e0030ca480d69aa5;hb=7a8c1e45c8d53a3b84a712c09c1fbdf9eaaa3e5a;hp=7030c05440c7d935f6e737a631d79b9cfa3f22d3;hpb=038e182c38b5aa7be9978c817a01d41f58785bf7;p=monky diff --git a/src/proc.c b/src/proc.c index 7030c05..218393e 100644 --- a/src/proc.c +++ b/src/proc.c @@ -10,7 +10,7 @@ * Please see COPYING for details * * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen - * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. * (see AUTHORS) * All rights reserved. * @@ -115,18 +115,22 @@ void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size) char* buf; int i, 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) { - buf[i] = ' '; + if(*(obj->data.s) != 0) { + 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) { + buf[i] = ' '; + } } + snprintf(p, p_max_size, "%s", buf); + free(buf); } - snprintf(p, p_max_size, "%s", buf); - free(buf); + } else { + NORM_ERR("$pid_cmdline didn't receive a argument"); } } @@ -208,6 +212,26 @@ void print_pid_exe(struct text_object *obj, char *p, int p_max_size) { free(buffer); } +void print_pid_nice(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + int bytes_read; + long int nice_value; + + if(*(obj->data.s) != 0) { + asprintf(&buf, PROCDIR "/%s/stat", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value); + snprintf(p, p_max_size, "%ld", nice_value); + free(buf); + } + } else { + NORM_ERR("$pid_nice didn't receive a argument"); + } +} + void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) { DIR* dir; struct dirent *entry; @@ -241,7 +265,55 @@ void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) { } } +void print_pid_parent(struct text_object *obj, char *p, int p_max_size) { +#define PARENT_ENTRY "PPid:\t" +#define PARENTNOTFOUND "Can't find the process parent in '%s'" + char *begin, *end, *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, PARENT_ENTRY); + if(begin != NULL) { + begin += strlen(PARENT_ENTRY); + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(PARENTNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_priority(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + int bytes_read; + long int priority; + + if(*(obj->data.s) != 0) { + asprintf(&buf, PROCDIR "/%s/stat", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority); + snprintf(p, p_max_size, "%ld", priority); + free(buf); + } + } else { + NORM_ERR("$pid_priority didn't receive a argument"); + } +} + void print_pid_state(struct text_object *obj, char *p, int p_max_size) { +#define STATE_ENTRY "State:\t" +#define STATENOTFOUND "Can't find the process state in '%s'" char *begin, *end, *buf = NULL; int bytes_read; @@ -255,7 +327,7 @@ void print_pid_state(struct text_object *obj, char *p, int p_max_size) { begin += strlen(STATE_ENTRY) + 3; // +3 will strip the char representing the short state and the space and '(' that follow end = strchr(begin, '\n'); if(end != NULL) { - *(end-1) = 0; + *(end-1) = 0; // -1 strips the ')' } snprintf(p, p_max_size, "%s", begin); } else { @@ -356,3 +428,444 @@ void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) { NORM_ERR(READERR, PROCDIR); } } + +void print_pid_threads(struct text_object *obj, char *p, int p_max_size) { +#define THREADS_ENTRY "Threads:\t" +#define THREADSNOTFOUND "Can't find the number of the threads of the process in '%s'" + char *begin, *end, *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, THREADS_ENTRY); + if(begin != NULL) { + begin += strlen(THREADS_ENTRY); + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(THREADSNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + DIR* dir; + struct dirent *entry; + int totallength = 0; + + asprintf(&buf, PROCDIR "/%s/task", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + dir = opendir(obj->data.s); + if(dir != NULL) { + while ((entry = readdir(dir))) { + if(entry->d_name[0] != '.') { + snprintf(p + totallength, p_max_size - totallength, "%s," , entry->d_name); + totallength += strlen(entry->d_name)+1; + } + } + closedir(dir); + if(p[totallength - 1] == ',') p[totallength - 1] = 0; + } else { + p[0] = 0; + } +} + +void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + int bytes_read; + unsigned long int umtime; + + if(*(obj->data.s) != 0) { + asprintf(&buf, PROCDIR "/%s/stat", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime); + snprintf(p, p_max_size, "%.2f", (float) umtime / 100); + free(buf); + } + } else { + NORM_ERR("$pid_time_kernelmode didn't receive a argument"); + } +} + +void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + int bytes_read; + unsigned long int kmtime; + + if(*(obj->data.s) != 0) { + asprintf(&buf, PROCDIR "/%s/stat", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime); + snprintf(p, p_max_size, "%.2f", (float) kmtime / 100); + free(buf); + } + } else { + NORM_ERR("$pid_time_usermode didn't receive a argument"); + } +} + +void print_pid_time(struct text_object *obj, char *p, int p_max_size) { + char *buf = NULL; + int bytes_read; + unsigned long int umtime, kmtime; + + if(*(obj->data.s) != 0) { + asprintf(&buf, PROCDIR "/%s/stat", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime); + snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100); + free(buf); + } + } else { + NORM_ERR("$pid_time didn't receive a argument"); + } +} + +#define UID_ENTRY "Uid:\t" +void print_pid_uid(struct text_object *obj, char *p, int p_max_size) { +#define UIDNOTFOUND "Can't find the process real uid in '%s'" + char *begin, *end, *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, UID_ENTRY); + if(begin != NULL) { + begin += strlen(UID_ENTRY); + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(UIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_euid(struct text_object *obj, char *p, int p_max_size) { +#define EUIDNOTFOUND "Can't find the process effective uid in '%s'" + char *begin, *end, *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, UID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(EUIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_suid(struct text_object *obj, char *p, int p_max_size) { +#define SUIDNOTFOUND "Can't find the process saved set uid in '%s'" + char *begin, *end, *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, UID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(SUIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) { +#define FSUIDNOTFOUND "Can't find the process file system uid in '%s'" + char *begin, *end, *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, UID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(FSUIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +#define GID_ENTRY "Gid:\t" +void print_pid_gid(struct text_object *obj, char *p, int p_max_size) { +#define GIDNOTFOUND "Can't find the process real gid in '%s'" + char *begin, *end, *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, GID_ENTRY); + if(begin != NULL) { + begin += strlen(GID_ENTRY); + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(GIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_egid(struct text_object *obj, char *p, int p_max_size) { +#define EGIDNOTFOUND "Can't find the process effective gid in '%s'" + char *begin, *end, *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, GID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(EGIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) { +#define SGIDNOTFOUND "Can't find the process saved set gid in '%s'" + char *begin, *end, *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, GID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\t'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(SGIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) { +#define FSGIDNOTFOUND "Can't find the process file system gid in '%s'" + char *begin, *end, *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, GID_ENTRY); + if(begin != NULL) { + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + begin = strchr(begin, '\t'); begin++; + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(FSGIDNOTFOUND, obj->data.s); + } + free(buf); + } +} + +void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) { + char *begin, *end, *buf = NULL; + int bytes_read; + + asprintf(&buf, PROCDIR "/%s/status", pid); + strcpy(pid, buf); + free(buf); + buf = readfile(pid, &bytes_read, 1); + if(buf != NULL) { + begin = strstr(buf, entry); + if(begin != NULL) { + begin += strlen(entry); + while(*begin == '\t' || *begin == ' ') { + begin++; + } + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(errorstring, pid); + } + free(buf); + } +} + +void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'"); +} + +void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'"); +} + +void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'"); +} + +void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'"); +} + +void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'"); +} + +void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'"); +} + +void print_pid_vmstk(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process stack segment size in '%s'"); +} + +void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'"); +} + +void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'"); +} + +void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) { + internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'"); +} + +#define READ_ENTRY "read_bytes: " +#define READNOTFOUND "Can't find the amount of bytes read in '%s'" +void print_pid_read(struct text_object *obj, char *p, int p_max_size) { + char *begin, *end, *buf = NULL; + int bytes_read; + + asprintf(&buf, PROCDIR "/%s/io", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + begin = strstr(buf, READ_ENTRY); + if(begin != NULL) { + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(READNOTFOUND, obj->data.s); + } + free(buf); + } +} + +#define WRITE_ENTRY "write_bytes: " +#define WRITENOTFOUND "Can't find the amount of bytes written in '%s'" +void print_pid_write(struct text_object *obj, char *p, int p_max_size) { + char *begin, *end, *buf = NULL; + int bytes_read; + + asprintf(&buf, PROCDIR "/%s/io", obj->data.s); + strcpy(obj->data.s, buf); + free(buf); + buf = readfile(obj->data.s, &bytes_read, 1); + if(buf != NULL) { + begin = strstr(buf, WRITE_ENTRY); + if(begin != NULL) { + end = strchr(begin, '\n'); + if(end != NULL) { + *(end) = 0; + } + snprintf(p, p_max_size, "%s", begin); + } else { + NORM_ERR(WRITENOTFOUND, obj->data.s); + } + free(buf); + } +}