From ac97850fb2b8c1e4efbaf782bf8afbc4b3d6a058 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 10:01:34 +0200 Subject: syslog && compile script --- compile.sh | 11 +++++++++++ configure.ac | 4 ++-- src/aconfig.h.in | 15 ++++++++++++--- src/log.c | 28 ++++++++++++++++++++-------- src/opt.c | 6 ++++-- 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100755 compile.sh diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..3db651f --- /dev/null +++ b/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -e + +DIR=$(dirname $0) +PWD=$(pwd) + +cd ${DIR} +./autogen.sh +./configure +make +cd ${PWD} diff --git a/configure.ac b/configure.ac index 5ea94ee..740f02a 100644 --- a/configure.ac +++ b/configure.ac @@ -34,7 +34,7 @@ AC_HEADER_STDC AC_HEADER_STAT AC_HEADER_DIRENT AC_HEADER_ASSERT -AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h alloca.h]) +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h syslog.h],[],[AC_MSG_ERROR([*** missing essential header files])]) # Checks for typedefs, structures, and compiler characteristics. AC_COMPUTE_INT @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol openlog vsyslog closelog],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" diff --git a/src/aconfig.h.in b/src/aconfig.h.in index a24ff8d..8dcbf12 100644 --- a/src/aconfig.h.in +++ b/src/aconfig.h.in @@ -3,9 +3,6 @@ /* Define to 1 if you have the `alarm' function. */ #undef HAVE_ALARM -/* Define to 1 if you have the header file. */ -#undef HAVE_ALLOCA_H - /* Define to 1 if you have the `asprintf' function. */ #undef HAVE_ASPRINTF @@ -15,6 +12,9 @@ /* Define to 1 if you have the `close' function. */ #undef HAVE_CLOSE +/* Define to 1 if you have the `closelog' function. */ +#undef HAVE_CLOSELOG + /* Do NOT change THIS! */ #undef HAVE_CONFIG @@ -74,6 +74,9 @@ /* Define to 1 if you have the `open' function. */ #undef HAVE_OPEN +/* Define to 1 if you have the `openlog' function. */ +#undef HAVE_OPENLOG + /* Define to 1 if you have the `printf' function. */ #undef HAVE_PRINTF @@ -130,6 +133,9 @@ /* Define to 1 if you have the `strtol' function. */ #undef HAVE_STRTOL +/* Define to 1 if you have the header file. */ +#undef HAVE_SYSLOG_H + /* Define to 1 if you have the `system' function. */ #undef HAVE_SYSTEM @@ -171,6 +177,9 @@ /* Define to 1 if you have the `vprintf' function. */ #undef HAVE_VPRINTF +/* Define to 1 if you have the `vsyslog' function. */ +#undef HAVE_VSYSLOG + /* Define to 1 if `fork' works. */ #undef HAVE_WORKING_FORK diff --git a/src/log.c b/src/log.c index ea52fda..cd8fc7c 100644 --- a/src/log.c +++ b/src/log.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "log.h" @@ -12,16 +13,23 @@ static FILE* logfile = NULL; int log_init(char* file) { - if (!file) return -1; - logfile = fopen(file, "a+"); - return (logfile ? 0 : errno); + if (!file) { + openlog("naskpass", LOG_NDELAY | LOG_PID, LOG_DAEMON); + return 0; + } else { + logfile = fopen(file, "a+"); + return (logfile ? 0 : errno); + } } void log_free(void) { - if (logfile) + if (!logfile) { + closelog(); + } else { fclose(logfile); - logfile = NULL; + logfile = NULL; + } } int logs(char* format, ...) @@ -29,10 +37,14 @@ int logs(char* format, ...) int ret; va_list vargs; - if (!logfile) return -1; va_start(vargs, format); - ret = vfprintf(logfile, format, vargs); - fflush(logfile); + if (!logfile) { + vsyslog(LOG_DEBUG, format, vargs); + ret = 0; + } else { + ret = vfprintf(logfile, format, vargs); + fflush(logfile); + } va_end(vargs); return ret; } diff --git a/src/opt.c b/src/opt.c index e1ea835..0c2071a 100644 --- a/src/opt.c +++ b/src/opt.c @@ -28,7 +28,7 @@ parse_cmd(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "hf:c:l:")) != -1) { + while ((opt = getopt(argc, argv, "hf:c:l::")) != -1) { switch (opt) { case 'h': usage(argv[0]); @@ -40,7 +40,9 @@ parse_cmd(int argc, char **argv) SETOPT_str(CRYPT_CMD, strdup(optarg)); break; case 'l': - SETOPT_str(LOG_FILE, strdup(optarg)); + if (optarg) { + SETOPT_str(LOG_FILE, strdup(optarg)); + } else SETOPT_str(LOG_FILE, NULL); break; default: usage(argv[0]); -- cgit v1.2.3