From 4a3e3f1b9eef457f3f216d2b2a4ccf0a32eb9599 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Wed, 6 Jul 2016 18:58:56 +0200 Subject: lib, irmd, ipcpd: Provide the feature to write to logs Writing output to log files is now enabled by default. Logs are written to /var/log/ouroboros, which is created on install. There is a log file for the irmd and one per IPCP. To still get (colored) output on stdout, provide the --stdout switch when starting the irmd. Fixes #17 --- src/irmd/main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'src/irmd') diff --git a/src/irmd/main.c b/src/irmd/main.c index de100cf8..ddce2d61 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -50,6 +50,8 @@ #include #include #include +#include +#include #define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */ @@ -1707,17 +1709,81 @@ static struct irm * irm_create() return instance; } -int main() +static void usage() +{ + LOG_ERR("Usage: irmd \n\n" + " [--stdout (Print to stdout instead of logs)]\n"); +} + +int main(int argc, char ** argv) { struct sigaction sig_act; int t = 0; + char * log_file = INSTALL_PREFIX LOG_DIR "irmd.log"; + DIR * log_dir; + struct dirent * ent; + char * point; + char * log_path; + size_t len = 0; + bool use_stdout = false; + if (geteuid() != 0) { LOG_ERR("IPC Resource Manager must be run as root."); exit(EXIT_FAILURE); } + argc--; + argv++; + while (argc > 0) { + if (strcmp(*argv, "--stdout") == 0) { + use_stdout = true; + argc--; + argv++; + } else { + usage(); + exit(EXIT_FAILURE); + } + } + + + if (!use_stdout && + (log_dir = opendir(INSTALL_PREFIX LOG_DIR)) != NULL) { + while ((ent = readdir(log_dir)) != NULL) { + point = strrchr(ent->d_name,'.'); + if (point == NULL || + strcmp(point, ".log") != 0) + continue; + + len += strlen(INSTALL_PREFIX); + len += strlen(LOG_DIR); + len += strlen(ent->d_name); + + log_path = malloc(len + 1); + if (log_path == NULL) { + LOG_ERR("Failed to malloc"); + exit(EXIT_FAILURE); + } + + strcpy(log_path, INSTALL_PREFIX); + strcat(log_path, LOG_DIR); + strcat(log_path, ent->d_name); + + unlink(log_path); + + free(log_path); + len = 0; + } + closedir(log_dir); + } + + if (!use_stdout) + if (set_logfile(log_file)) + LOG_ERR("Cannot open %s, falling back to " + "stdout for logs.", + log_file); + /* init sig_act */ memset(&sig_act, 0, sizeof sig_act); -- cgit v1.2.3