Code review and optimization times.c and upnp.c

This commit is contained in:
Anis Ellouze 2015-08-27 10:21:21 +01:00 committed by MOHAMED Kallel
parent 1a31eef758
commit 9f9ccefeaa
4 changed files with 53 additions and 36 deletions

View file

@ -14,6 +14,8 @@
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#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)

View file

@ -11,6 +11,8 @@
#ifndef __DM_COMMON_H
#define __DM_COMMON_H
#include <sys/types.h>
#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
#endif

View file

@ -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
}

View file

@ -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;
}
}