diff options
author | toni <toni@devlap.local> | 2016-01-30 00:17:25 +0100 |
---|---|---|
committer | toni <toni@devlap.local> | 2016-01-30 00:17:25 +0100 |
commit | fbd15154bea20f5b29e0d193b60f648891684f8a (patch) | |
tree | 4e34c6fea46ba05a88fd7484e6ddcb833eed0836 | |
parent | 2decdd502f1c9b33800cc41daff25536066f7560 (diff) |
early logging 2
-rw-r--r-- | src/log.c | 16 | ||||
-rw-r--r-- | src/log.h | 2 | ||||
-rw-r--r-- | src/main.c | 12 | ||||
-rw-r--r-- | src/opt.c | 13 | ||||
-rw-r--r-- | src/opt.h | 7 |
5 files changed, 35 insertions, 15 deletions
@@ -7,24 +7,32 @@ #define LOG_BUFSIZ 128 -static FILE* logfile; +static FILE* logfile = NULL; int log_init(char* file) { + if (!file) return -1; logfile = fopen(file, "a+"); return (logfile ? 0 : errno); } +void log_free(void) +{ + if (logfile) + fclose(logfile); + logfile = NULL; +} + int logs(char* format, ...) { int ret; - char* buf; va_list vargs; - buf = (char*) calloc(LOG_BUFSIZ, sizeof(char)); + if (!logfile) return -1; va_start(vargs, format); - ret = vsnprintf(buf, LOG_BUFSIZ, format, vargs); + ret = vfprintf(logfile, format, vargs); + fflush(logfile); va_end(vargs); return ret; } @@ -3,6 +3,8 @@ int log_init(char* file); +void log_free(void); + int logs(char* format, ...); #endif @@ -15,6 +15,7 @@ #include "config.h" #include "opt.h" +#include "log.h" #include "ui.h" #include "ui_ipc.h" @@ -114,12 +115,13 @@ main(int argc, char **argv) memset(pbuf, '\0', IPC_MQSIZ+1); if ( parse_cmd(argc, argv) != 0 ) goto error; + log_init( GETOPT(LOG_FILE).str ); + logs("%s\n", "log init"); if (OPT(CRYPT_CMD).found == 0) { - usage(argv[0]); + fprintf(stderr, "%s: crypt cmd is mandatory\n", argv[0]); goto error; } if (check_fifo(GETOPT(FIFO_PATH).str) == false) { - usage(argv[0]); goto error; } if ((ffd = open(GETOPT(FIFO_PATH).str, O_NONBLOCK | O_RDWR)) < 0) { @@ -130,6 +132,7 @@ main(int argc, char **argv) ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ + logs("%s\n", "child"); if (ffd >= 0) close(ffd); fclose(stderr); /* Slave process: TUI */ @@ -140,6 +143,7 @@ main(int argc, char **argv) exit(0); } else if (child > 0) { /* parent */ + logs("%s\n", "parent"); fclose(stdin); fclose(stdout); ui_ipc_sempost(SEM_BS); @@ -180,9 +184,13 @@ main(int argc, char **argv) goto error; } + logs("%s\n", "finished"); + log_free(); ret = EXIT_SUCCESS; error: if (ffd >= 0) close(ffd); ui_ipc_free(1); + logs("%s\n", "init error"); + log_free(); exit(ret); } @@ -9,7 +9,7 @@ #define CONFIG_OPT(default_val) { {0},0,{default_val} } -struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL), CONFIG_OPT("/tmp/naskpass.log") }; +struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL), CONFIG_OPT(NULL) }; const int opt_siz = ( sizeof(config_opts)/sizeof(config_opts[0]) ); @@ -20,7 +20,7 @@ usage(char *arg0) fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\n"); fprintf(stderr, " Command:\n\t%s [args]\n", arg0); - fprintf(stderr, " Arguments:\n\t-h this\n\t-l [logfile] default: %s\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", GETOPT(LOG_FILE), GETOPT(FIFO_PATH)); + fprintf(stderr, " Arguments:\n\t-h this\n\t-l [logfile]\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", GETOPT(FIFO_PATH).str); } int @@ -28,16 +28,19 @@ parse_cmd(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "hf:c:")) != -1) { + while ((opt = getopt(argc, argv, "hf:c:l:")) != -1) { switch (opt) { case 'h': usage(argv[0]); return 1; case 'f': - s_OPT(FIFO_PATH, strdup(optarg)); + SETOPT_str(FIFO_PATH, strdup(optarg)); break; case 'c': - s_OPT(CRYPT_CMD, strdup(optarg)); + SETOPT_str(CRYPT_CMD, strdup(optarg)); + break; + case 'l': + SETOPT_str(LOG_FILE, strdup(optarg)); break; default: usage(argv[0]); @@ -2,14 +2,13 @@ #define OPT_H 1 #define OPT(opt_index) config_opts[opt_index] +#define SETOPT_str(opt_index, value) { OPT(opt_index).found = 1; OPT(opt_index).opt.str = value; } #define GETOPT(opt_index) (OPT(opt_index).found != 0 ? OPT(opt_index).opt : OPT(opt_index).def) -#define OPT_USED(opt_index, uvalue) OPT(opt_index).found = uvalue; -#define d_OPT(opt_index, rvalue) OPT(opt_index).opt.dec = rvalue; OPT_USED(opt_index, 1); -#define s_OPT(opt_index, rvalue) OPT(opt_index).opt.str = rvalue; OPT_USED(opt_index, 1); + union opt_entry { char *str; - int *dec; + int dec; }; struct opt { |