diff options
Diffstat (limited to 'src/tool/eathena-monitor.cpp')
-rw-r--r-- | src/tool/eathena-monitor.cpp | 125 |
1 files changed, 67 insertions, 58 deletions
diff --git a/src/tool/eathena-monitor.cpp b/src/tool/eathena-monitor.cpp index e070c8a..30e7184 100644 --- a/src/tool/eathena-monitor.cpp +++ b/src/tool/eathena-monitor.cpp @@ -7,18 +7,21 @@ * gcc -o eathena-monitor eathena-monitor.c */ -#include <unistd.h> -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <sys/types.h> +#include <sys/wait.h> -#include <time.h> #include <fcntl.h> -#include <sys/wait.h> -#include <signal.h> +#include <unistd.h> + +#include <csignal> +#include <cstdlib> +#include <cstring> +#include <ctime> + +#include "../common/cxxstdio.hpp" +#include "../common/utils.hpp" + +#include "../poison.hpp" -#define HOME getenv("HOME") #define LOGIN_SERVER "./login-server" #define MAP_SERVER "./map-server" #define CHAR_SERVER "./char-server" @@ -26,55 +29,60 @@ #define LOGFILE "log/eathena-monitor.log" -#define SKIP_BLANK(ptr) ptr += skip_blank(ptr) -static inline size_t skip_blank(const char* ptr) { - size_t i = 0; +static +void SKIP_BLANK(char *& ptr) +{ while ( - (ptr[i] == ' ') || - (ptr[i] == '\b') || - (ptr[i] == '\n') || - (ptr[i] == '\r') - ) ptr++; - return i; + (*ptr == ' ') || + (*ptr == '\b') || + (*ptr == '\n') || + (*ptr == '\r') + ) + ptr++; } -#define GOTO_EQL(ptr) ptr += goto_eql(ptr) -static inline size_t goto_eql(const char* ptr) { - size_t i = 0; +static +void GOTO_EQL(char *& ptr) { while ( - (ptr[i] != '\0') && - (ptr[i] != '=') && - (ptr[i] != '\n') && - (ptr[i] != '\r') - ) ptr++; - return i; + (*ptr != '\0') && + (*ptr != '=') && + (*ptr != '\n') && + (*ptr != '\r') + ) + ptr++; } -#define GOTO_EOL(ptr) ptr += goto_newline(ptr) -static inline size_t goto_newline(const char* ptr) { - size_t i = 0; +static +void GOTO_EOL(char *& ptr) { while ( - (ptr[i] != '\0') && - (ptr[i] != '\n') && - (ptr[i] != '\r') - ) ptr++; - return i; + (*ptr != '\0') && + (*ptr != '\n') && + (*ptr != '\r') + ) + ptr++; } // initialiized to $HOME/tmwserver +static const char *workdir; //the rest are relative to workdir +static const char *login_server = LOGIN_SERVER; +static const char *map_server = MAP_SERVER; +static const char *char_server = CHAR_SERVER; +static const char *logfile = LOGFILE; // this variable is hard-coded, but the command-line is checked first +static const char *config = CONFIG; +static pid_t pid_login, pid_map, pid_char; static -const char* make_path (const char* base, const char* path) { +const char* make_path(const char* base, const char* path) { size_t base_len = strlen(base); size_t path_len = strlen(path); char* out = (char *)malloc(base_len + 1 + path_len + 1); @@ -86,7 +94,7 @@ const char* make_path (const char* base, const char* path) { } static -void parse_option (char *name, char *value) { +void parse_option(char *name, char *value) { if (!strcasecmp(name, "login_server")) { login_server = strdup(value); } else if (!strcasecmp(name, "map_server")) { @@ -98,7 +106,7 @@ void parse_option (char *name, char *value) { } else if (!strcasecmp(name, "logfile")) { logfile = strdup(value); } else { - fprintf(stderr, "WARNING: ingnoring invalid option '%s' = '%s'\n", name, value); + FPRINTF(stderr, "WARNING: ingnoring invalid option '%s' = '%s'\n", name, value); } } @@ -107,24 +115,24 @@ void read_config(const char *filename) { FILE *input; char string[1000]; - if (!(input = fopen(filename,"r")) && !(input = fopen (config, "r"))) { + if (!(input = fopen(filename,"r")) && !(input = fopen(config, "r"))) { perror("Unable to load config file"); return; } while (1) { - if (fgets (string, sizeof (string) - 1, input) == NULL) + if (fgets(string, sizeof(string) - 1, input) == NULL) break; char *str = string, *name, *value; SKIP_BLANK(str); - string[sizeof (string) - 1] = '\0'; + string[sizeof(string) - 1] = '\0'; if (*str == '#') continue; if (*str == '\0') continue; name = str; - GOTO_EQL (str); + GOTO_EQL(str); if (*str != '=') { continue; @@ -138,7 +146,7 @@ void read_config(const char *filename) { parse_option(name, value); } - fclose (input); + fclose(input); } static @@ -146,11 +154,14 @@ pid_t start_process(const char *exec) { const char *args[2] = {exec, NULL}; pid_t pid = fork(); if (pid == -1) { - fprintf(stderr, "Failed to fork"); + FPRINTF(stderr, "Failed to fork"); return 0; } if (pid == 0) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" execv(exec, (char**)args); +#pragma GCC diagnostic pop perror("Failed to exec"); kill(getppid(), SIGABRT); exit(1); @@ -176,24 +187,24 @@ int main(int argc, char *argv[]) { signal(SIGQUIT, stop_process); signal(SIGABRT, stop_process); - workdir = make_path(HOME, "tmwserver"); + workdir = make_path(getenv("HOME"), "tmwserver"); read_config(argc>1 ? argv[1] : NULL); if (chdir(workdir) < 0) perror("Failed to change directory"), exit(1); - printf ("Starting:\n"); - printf ("* workdir: %s\n", workdir); - printf ("* login_server: %s\n", login_server); - printf ("* map_server: %s\n", map_server); - printf ("* char_server: %s\n", char_server); + PRINTF("Starting:\n"); + PRINTF("* workdir: %s\n", workdir); + PRINTF("* login_server: %s\n", login_server); + PRINTF("* map_server: %s\n", map_server); + PRINTF("* char_server: %s\n", char_server); { //make sure all possible file descriptors are free for use by the servers //if there are file descriptors higher than the max open from before the limit dropped, that's not our problem int fd = sysconf(_SC_OPEN_MAX); while (--fd > 2) if (close(fd) == 0) - fprintf(stderr, "close fd %d\n", fd); + FPRINTF(stderr, "close fd %d\n", fd); fd = open("/dev/null", O_RDWR); if (fd < 0) perror("open /dev/null"), exit(1); dup2(fd, 0); @@ -202,22 +213,20 @@ int main(int argc, char *argv[]) { } while (1) { // write stuff to stderr - time_t t = time(NULL); - struct tm *tmp = localtime(&t); - char timestamp[256]; - strftime(timestamp, sizeof(timestamp), "%F %T", tmp); + timestamp_seconds_buffer timestamp; + stamp_time(timestamp); if (!pid_login) { pid_login = start_process(login_server); - fprintf (stderr, "[%s] forked login server: %lu\n", timestamp, (unsigned long)pid_login); + FPRINTF(stderr, "[%s] forked login server: %lu\n", timestamp, (unsigned long)pid_login); } if (!pid_char) { pid_char = start_process(char_server); - fprintf (stderr, "[%s] forked char server: %lu\n", timestamp, (unsigned long)pid_char); + FPRINTF(stderr, "[%s] forked char server: %lu\n", timestamp, (unsigned long)pid_char); } if (!pid_map) { pid_map = start_process(map_server); - fprintf (stderr, "[%s] forked map server: %lu\n", timestamp, (unsigned long)pid_map); + FPRINTF(stderr, "[%s] forked map server: %lu\n", timestamp, (unsigned long)pid_map); } pid_t dead = wait(NULL); if (dead < 0) perror("Failed to wait for child"), exit(1); |