From 9f9ccefeaa3d4d41b03a4f5327ecff4d32505270 Mon Sep 17 00:00:00 2001 From: Anis Ellouze Date: Thu, 27 Aug 2015 10:21:21 +0100 Subject: [PATCH] Code review and optimization times.c and upnp.c --- dm/dmcommon.c | 57 +++++++++++++++++++++++++++++++++-------------- dm/dmcommon.h | 6 +++-- dm/dmtree/times.c | 7 +++--- dm/dmtree/upnp.c | 19 +++++----------- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/dm/dmcommon.c b/dm/dmcommon.c index 03762ea..4ac946f 100644 --- a/dm/dmcommon.c +++ b/dm/dmcommon.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include "dmcwmp.h" void compress_spaces(char *str) //REMOVE TO DMCOMMON @@ -41,24 +43,45 @@ char *cut_fx(char *str, char *delimiter, int occurence) return pch; } -char *get_pid(char *pname) +pid_t get_pid(char *pname) { - FILE* f = NULL; - char str[TAILLE_MAX] = ""; - char *v; - f = popen(pname, "r"); - if (f != NULL) { - fgets(str, TAILLE_MAX, f); - if (str[0] == '\0') { - pclose(f); - return ""; - } - pid_t pid = strtoul(str, NULL, 10); - pclose(f); - dmasprintf(&v, "%d", pid); // MEM WILL BE FREED IN DMMEMCLEAN - return v; - } - return ""; + DIR* dir; + struct dirent* ent; + char* endptr; + char buf[512]; + + if (!(dir = opendir("/proc"))) { + return -1; + } + + while((ent = readdir(dir)) != NULL) { + /* if endptr is not a null character, the directory is not + * entirely numeric, so ignore it */ + long lpid = strtol(ent->d_name, &endptr, 10); + if (*endptr != '\0') { + continue; + } + + /* try to open the cmdline file */ + snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid); + FILE* fp = fopen(buf, "r"); + + if (fp) { + if (fgets(buf, sizeof(buf), fp) != NULL) { + /* check the first token in the file, the program name */ + char* first = strtok(buf, " "); + if (strstr(first, name)) { + fclose(fp); + closedir(dir); + return (pid_t)lpid; + } + } + fclose(fp); + } + } + + closedir(dir); + return -1; } int check_file(char *path) diff --git a/dm/dmcommon.h b/dm/dmcommon.h index b20116d..efd27e4 100644 --- a/dm/dmcommon.h +++ b/dm/dmcommon.h @@ -11,6 +11,8 @@ #ifndef __DM_COMMON_H #define __DM_COMMON_H +#include + #define DM_ASSERT(X, Y) \ do { \ if(!(X)) { \ @@ -20,8 +22,8 @@ do { \ } while(0) char *cut_fx(char *str, char *delimiter, int occurence); -char *get_pid(char *pname); +pid_t get_pid(char *pname); int check_file(char *path); void compress_spaces(char *str); -#endif \ No newline at end of file +#endif diff --git a/dm/dmtree/times.c b/dm/dmtree/times.c index 50cb604..8cec19c 100644 --- a/dm/dmtree/times.c +++ b/dm/dmtree/times.c @@ -32,7 +32,7 @@ int set_time_enable(char *refparam, struct dmctx *ctx, int action, char *value) { static bool b; int check; - char *pname, *v; + pid_t pid; switch (action) { VALUECHECK: @@ -45,9 +45,8 @@ int set_time_enable(char *refparam, struct dmctx *ctx, int action, char *value) ///etc/init.d/sysntpd enable } else { - pname = dmstrdup("pidof ntpd"); - v = get_pid(pname); - if (v[0] != '\0') { + pid = get_pid("ntpd"); + if (pid > 0) { //etc/init.d/sysntpd stop; //TODO //etc/init.d/sysntpd disable //TODO } diff --git a/dm/dmtree/upnp.c b/dm/dmtree/upnp.c index c9185f2..6317582 100644 --- a/dm/dmtree/upnp.c +++ b/dm/dmtree/upnp.c @@ -22,15 +22,14 @@ int get_upnp_enable(char *refparam, struct dmctx *ctx, char **value) { dmuci_get_option_value_string("upnpd","config","enabled", value); if (*value[0] == '\0') { - dmfree(*value); - *value = dmstrdup("1"); + *value = "1"; } return 0; } int set_upnp_enable(char *refparam, struct dmctx *ctx, int action, char *value) { - bool b; + static bool b; int check; switch (action) { VALUECHECK: @@ -38,14 +37,10 @@ int set_upnp_enable(char *refparam, struct dmctx *ctx, int action, char *value) return FAULT_9007; return 0; VALUESET: - check = string_to_bool(value, &b); - if (check == -1) - return 0; if(b) dmuci_set_value("upnpd", "config", "enabled", "1"); else dmuci_set_value("upnpd", "config", "enabled", ""); - //delay_service restart "miniupnpd" "1" //TODO return 0; } return 0; @@ -53,16 +48,14 @@ int set_upnp_enable(char *refparam, struct dmctx *ctx, int action, char *value) int get_upnp_status(char *refparam, struct dmctx *ctx, char **value) { - char *pname = dmstrdup("pidof miniupnpd"); - *value = get_pid(pname); + pid_t pid = get_pid("miniupnpd"); - if (*value[0] == '\0') { + if (pid < 0) { *value = dmstrdup("Down"); } else { *value = dmstrdup("Up"); } - dmfree(pname); return 0; } @@ -72,8 +65,8 @@ int entry_method_root_upnp(struct dmctx *ctx) DMOBJECT(DMROOT"UPnP.", ctx, "0", 1, NULL, NULL, NULL); DMOBJECT(DMROOT"UPnP.Device.", ctx, "0", 1, NULL, NULL, NULL); DMPARAM("Enable", ctx, "1", get_upnp_enable, set_upnp_enable, "xsd:boolean", 0, 1, UNDEF, NULL); - DMPARAM("X_INTENO_SE_Status", ctx, "0", get_upnp_status, NULL, "", 0, 1, UNDEF, NULL); + DMPARAM("X_INTENO_SE_Status", ctx, "0", get_upnp_status, NULL, NULL, 0, 1, UNDEF, NULL); return 0; } return FAULT_9005; -} \ No newline at end of file +}