From 122558bc7927bc41bfe91b3f9e5137736e393a32 Mon Sep 17 00:00:00 2001 From: zribiahmed Date: Fri, 7 Jun 2013 13:33:57 +0000 Subject: [PATCH] --- src/config.c | 4 ++-- src/cwmp.c | 7 +++++++ src/inc/backupSession.h | 2 ++ src/inc/cwmp.h | 2 ++ src/ubus.c | 30 +++++++++++++++++++++++++++++- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/config.c b/src/config.c index aa408c6..7b64ded 100644 --- a/src/config.c +++ b/src/config.c @@ -711,8 +711,8 @@ int cwmp_init(int argc, char** argv,struct cwmp *cwmp) return error; } /* Only One instance should run*/ - int pid_file = open("/var/run/cwmpd.pid", O_CREAT | O_RDWR, 0666); - int rc = flock(pid_file, LOCK_EX | LOCK_NB); + cwmp->pid_file = open("/var/run/cwmpd.pid", O_CREAT | O_RDWR, 0666); + int rc = flock(cwmp->pid_file, LOCK_EX | LOCK_NB); if(rc) { if(EWOULDBLOCK != errno) { diff --git a/src/cwmp.c b/src/cwmp.c index 9425e13..1a39c01 100644 --- a/src/cwmp.c +++ b/src/cwmp.c @@ -488,6 +488,13 @@ void *thread_uloop_run (void *v) return NULL; } +void *thread_exit_program (void *v) +{ + CWMP_LOG(INFO,"EXIT CWMP"); + pthread_mutex_lock(&mutex_backup_session); + exit(EXIT_SUCCESS); +} + int main(int argc, char **argv) { diff --git a/src/inc/backupSession.h b/src/inc/backupSession.h index 906433d..1f17bd2 100644 --- a/src/inc/backupSession.h +++ b/src/inc/backupSession.h @@ -38,6 +38,8 @@ struct search_keywords { char *value; }; +extern pthread_mutex_t mutex_backup_session; + void bkp_session_save(); int cwmp_load_saved_session(struct cwmp *cwmp, char **acsurl, enum backup_loading load); mxml_node_t *bkp_session_insert_event(int index, char *command_key, int id, char *status); diff --git a/src/inc/cwmp.h b/src/inc/cwmp.h index 491ecb6..820dc5c 100644 --- a/src/inc/cwmp.h +++ b/src/inc/cwmp.h @@ -162,6 +162,7 @@ typedef struct cwmp { pthread_cond_t threshold_periodic; int retry_count_session; struct list_head *head_event_container; + int pid_file; } cwmp; typedef struct session { @@ -201,5 +202,6 @@ void *thread_event_periodic (void *v); void cwmp_add_notification (char *name, char *value, char *attribute, char *type); int netlink_init(void); char * mix_get_time(void); +void *thread_exit_program (void *v); #endif /* _CWMP_H__ */ diff --git a/src/ubus.c b/src/ubus.c index 08d7e5a..c2bdb1d 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -12,7 +12,7 @@ #include #include - +#include #include "cwmp.h" #include "ubus.h" @@ -115,6 +115,34 @@ cwmp_handle_command(struct ubus_context *ctx, struct ubus_object *obj, blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "freecwmp will execute the scheduled action commands at the end of the session") == -1) return -1; + } else if (!strcmp("exit", cmd)) { + pthread_t exit_thread; + int error; + CWMP_LOG(INFO, "triggered ubus exit"); + int rc = flock(cwmp_main.pid_file, LOCK_UN | LOCK_NB); + if(rc) { + char *piderr = "PID file unlock failed!"; + fprintf(stderr, "%s\n", piderr); + CWMP_LOG(ERROR, piderr); + } + blobmsg_add_u32(&b, "status", 0); + if (asprintf(&info, "cwmpd daemon stopped") == -1) + return -1; + blobmsg_add_string(&b, "info", info); + free(info); + + ubus_send_reply(ctx, req, b.head); + + blob_buf_free(&b); + + error = pthread_create(&exit_thread, NULL, &thread_exit_program, NULL); + if (error<0) + { + CWMP_LOG(ERROR,"Error when creating the exit thread!"); + return -1; + } + return 0; + } else { blobmsg_add_u32(&b, "status", -1); if (asprintf(&info, "%s command is not supported", cmd) == -1)