From 1e8022c3b7beb179e021e2cce143c58b6087b989 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Tue, 21 Jun 2016 15:24:49 +0800 Subject: [PATCH] Refine #682 --- src/manager.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/manager.c b/src/manager.c index 0cc2e498..792e3c2a 100644 --- a/src/manager.c +++ b/src/manager.c @@ -37,6 +37,7 @@ #include #include #include +#include #ifndef __MINGW32__ #include @@ -293,6 +294,25 @@ static void add_server(struct manager_ctx *manager, struct server *server) } } +static void kill_server(char *prefix, char *pid_file) +{ + char path[PATH_MAX]; + int pid; + snprintf(path, PATH_MAX, "%s/%s", prefix, pid_file); + FILE *f = fopen(path, "r"); + if (f == NULL) { + if (verbose) { + LOGE("unable to open pid file"); + } + return; + } + if (fscanf(f, "%d", &pid) != EOF) { + kill(pid, SIGTERM); + } + fclose(f); + remove(path); +} + static void stop_server(char *prefix, char *port) { char path[PATH_MAX]; @@ -772,6 +792,23 @@ int main(int argc, char **argv) FATAL("unable to create working directory"); } + // Clean up all existed processes + DIR *dp; + struct dirent *ep; + dp = opendir(working_dir); + if (dp != NULL) { + while ((ep = readdir(dp)) != NULL) { + size_t len = strlen(ep->d_name); + if (strcmp(ep->d_name + len - 3, "pid") == 0) { + kill_server(working_dir, ep->d_name); + if (verbose) LOGI("kill %s", ep->d_name); + } + } + closedir (dp); + } else { + FATAL("Couldn't open the directory"); + } + server_table = cork_string_hash_table_new(MAX_PORT_NUM, 0); if (conf != NULL) {