From: Brian Aker Date: Tue, 3 Aug 2010 04:30:04 +0000 (-0700) Subject: Remove use of system(). Not very portable X-Git-Tag: 0.44~5 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=6b46b12051fa265bbd9c2a17899a280c8bd82769;p=awesomized%2Flibmemcached Remove use of system(). Not very portable --- diff --git a/tests/server.c b/tests/server.c index 70c0ae3b..e2781b26 100644 --- a/tests/server.c +++ b/tests/server.c @@ -17,19 +17,45 @@ #include "config.h" +#include +#include +#include #include #include #include +#include #include -#include -#include +#include + #include #include -#include -#include #include "server.h" +static void kill_file(const char *file_buffer) +{ + FILE *fp= fopen(file_buffer, "r"); + + if (fp != NULL) + { + char pid_buffer[1024]; + + if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL) + { + pid_t pid= (pid_t)atoi(pid_buffer); + if (pid != 0) + { + if (kill(pid, SIGTERM) == -1) + { + perror(file_buffer); + } + } + } + + fclose(fp); + } +} + void server_startup(server_startup_st *construct) { if ((construct->server_list= getenv("MEMCACHED_SERVERS"))) @@ -48,7 +74,6 @@ void server_startup(server_startup_st *construct) for (uint32_t x= 0; x < construct->count; x++) { - char buffer[1024]; /* Nothing special for number */ int count; int status; in_port_t port; @@ -69,34 +94,36 @@ void server_startup(server_startup_st *construct) } } - sprintf(buffer, "/tmp/%umemc.pid", x); - if (access(buffer, F_OK) == 0) - { - FILE *fp= fopen(buffer, "r"); - remove(buffer); + char buffer[PATH_MAX]; - if (fp != NULL) + snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x); + + uint32_t counter= 3; + while (--counter) + { + if (access(buffer, F_OK) == 0) { - if (fgets(buffer, sizeof(buffer), fp) != NULL) - { - pid_t pid= (pid_t)atoi(buffer); - if (pid != 0) - kill(pid, SIGTERM); - } - - fclose(fp); + kill_file(buffer); +#ifndef WIN32 + struct timespec req= { .tv_sec= 0, .tv_nsec= 5000 }; + nanosleep(&req, NULL); +#endif + } + else + { + break; } } if (x == 0) { - sprintf(buffer, "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128", - MEMCACHED_BINARY, x, port, port); + snprintf(buffer, sizeof(buffer), "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128", + MEMCACHED_BINARY, x, port, port); } else { - sprintf(buffer, "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u", - MEMCACHED_BINARY, x, port, port); + snprintf(buffer, sizeof(buffer), "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u", + MEMCACHED_BINARY, x, port, port); } if (libmemcached_util_ping("localhost", port, NULL)) { @@ -115,10 +142,11 @@ void server_startup(server_startup_st *construct) for (uint32_t x= 0; x < construct->count; x++) { uint32_t counter= 3; - char buffer[1024]; /* Nothing special for number */ + char buffer[PATH_MAX]; /* Nothing special for number */ snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x); + bool was_started= false; while (--counter) { int memcached_pid; @@ -135,15 +163,20 @@ void server_startup(server_startup_st *construct) } char *found= fgets(buffer, sizeof(buffer), file); if (!found) - { - abort(); - } + continue; + // Yes, we currently pitch this and don't make use of it. memcached_pid= atoi(buffer); fclose(file); + was_started= true; + break; } - + if (was_started == false) + { + fprintf(stderr, "Failed to open buffer %s\n", buffer); + abort(); + } } construct->server_list= strdup(server_string_buffer); @@ -172,13 +205,9 @@ void server_shutdown(server_startup_st *construct) { for (uint32_t x= 0; x < construct->count; x++) { - char buffer[1024]; /* Nothing special for number */ - sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x); - /* We have to check the return value of this or the compiler will yell */ - int sys_ret= system(buffer); - assert(sys_ret != -1); - sprintf(buffer, "/tmp/%umemc.pid", x); - unlink(buffer); + char file_buffer[PATH_MAX]; /* Nothing special for number */ + snprintf(file_buffer, sizeof(file_buffer), "/tmp/%umemc.pid", x); + kill_file(file_buffer); } free(construct->server_list);