From 65ccbe645cb2d4b154c5539dcb34152525982885 Mon Sep 17 00:00:00 2001 From: manojampalam Date: Wed, 23 Mar 2016 20:39:00 -0700 Subject: [PATCH] Moved win specific logging code from each exe to win32iocompat --- contrib/win32/openssh/win32compat.vcxproj | 2 - .../win32/openssh/win32compat.vcxproj.filters | 10 +---- contrib/win32/openssh/win32iocompat.vcxproj | 2 + .../openssh/win32iocompat.vcxproj.filters | 4 ++ .../win32compat/{includes => inc}/syslog.h | 4 +- contrib/win32/win32compat/w32log.c | 42 +++++++++++++++++++ log.c | 41 ++---------------- regress/unittests/win32compat/tests.c | 3 -- sftp-server.c | 26 +----------- sshd.c | 32 -------------- 10 files changed, 56 insertions(+), 110 deletions(-) rename contrib/win32/win32compat/{includes => inc}/syslog.h (94%) create mode 100644 contrib/win32/win32compat/w32log.c diff --git a/contrib/win32/openssh/win32compat.vcxproj b/contrib/win32/openssh/win32compat.vcxproj index cfcebf8..579da91 100644 --- a/contrib/win32/openssh/win32compat.vcxproj +++ b/contrib/win32/openssh/win32compat.vcxproj @@ -157,7 +157,6 @@ - @@ -197,7 +196,6 @@ - diff --git a/contrib/win32/openssh/win32compat.vcxproj.filters b/contrib/win32/openssh/win32compat.vcxproj.filters index 8b7f797..fe6f98b 100644 --- a/contrib/win32/openssh/win32compat.vcxproj.filters +++ b/contrib/win32/openssh/win32compat.vcxproj.filters @@ -75,9 +75,6 @@ Source Files - - Source Files - Source Files @@ -87,10 +84,10 @@ Source Files - + Source Files - + Source Files @@ -158,9 +155,6 @@ includes - - includes - includes diff --git a/contrib/win32/openssh/win32iocompat.vcxproj b/contrib/win32/openssh/win32iocompat.vcxproj index 964598b..10b874b 100644 --- a/contrib/win32/openssh/win32iocompat.vcxproj +++ b/contrib/win32/openssh/win32iocompat.vcxproj @@ -147,6 +147,7 @@ + @@ -162,6 +163,7 @@ + diff --git a/contrib/win32/openssh/win32iocompat.vcxproj.filters b/contrib/win32/openssh/win32iocompat.vcxproj.filters index 89d2ed0..092b33b 100644 --- a/contrib/win32/openssh/win32iocompat.vcxproj.filters +++ b/contrib/win32/openssh/win32iocompat.vcxproj.filters @@ -7,6 +7,7 @@ + @@ -46,6 +47,9 @@ inc\sys + + inc + diff --git a/contrib/win32/win32compat/includes/syslog.h b/contrib/win32/win32compat/inc/syslog.h similarity index 94% rename from contrib/win32/win32compat/includes/syslog.h rename to contrib/win32/win32compat/inc/syslog.h index 1062996..28e67fb 100644 --- a/contrib/win32/win32compat/includes/syslog.h +++ b/contrib/win32/win32compat/inc/syslog.h @@ -1,5 +1,4 @@ -#ifndef COMPAT_SYSLOG_H -#define COMPAT_SYSLOG_H 1 +#pragma once /* Compatibility header to give us some syslog-like functionality on Win32 */ @@ -27,4 +26,3 @@ void openlog (char *, unsigned int, int); void closelog (void); void syslog (int, const char *, const char *); -#endif diff --git a/contrib/win32/win32compat/w32log.c b/contrib/win32/win32compat/w32log.c new file mode 100644 index 0000000..83f046c --- /dev/null +++ b/contrib/win32/win32compat/w32log.c @@ -0,0 +1,42 @@ + +#include +#include +#include +#include +#include "inc\syslog.h" + +#define MSGBUFSIZ 1024 +static int logfd = -1; + +void openlog(char *ident, unsigned int option, int facility) { + if ((logfd == -1) && (ident != NULL)) { + char path[MAX_PATH]; + GetModuleFileNameA(NULL, path, MAX_PATH); + path[MAX_PATH - 1] = '\0'; + memcpy(path + strlen(path) - 3, "log", 3); + logfd = _open(path, O_WRONLY | O_CREAT | O_APPEND, + S_IREAD | S_IWRITE); + if (logfd != -1) + SetHandleInformation((HANDLE)_get_osfhandle(logfd), + HANDLE_FLAG_INHERIT, 0); + } +} + +void closelog(void) { + //NOOP +} + +void +syslog(int priority, const char *format, const char *formatBuffer) { + char msgbufTimestamp[MSGBUFSIZ]; + SYSTEMTIME st; + + if (logfd == -1) + return; + + GetLocalTime(&st); + snprintf(msgbufTimestamp, sizeof msgbufTimestamp, "%d %02d:%02d:%02d %03d %s\n", + GetCurrentProcessId(), st.wHour, st.wMinute, st.wSecond, + st.wMilliseconds, formatBuffer); + _write(logfd, msgbufTimestamp, strlen(msgbufTimestamp)); +} \ No newline at end of file diff --git a/log.c b/log.c index e6d743b..04eea20 100644 --- a/log.c +++ b/log.c @@ -52,15 +52,6 @@ #include "log.h" -#ifdef WIN32_FIXME - - #include - #include - - int logfd = 0; - -#endif - static LogLevel log_level = SYSLOG_LEVEL_INFO; static int log_on_stderr = 1; static int log_stderr_fd = STDERR_FILENO; @@ -450,43 +441,20 @@ do_log(LogLevel level, const char *fmt, va_list args) log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); #endif -#ifndef WIN32_FIXME if (log_handler != NULL) { /* Avoid recursion */ tmp_handler = log_handler; log_handler = NULL; tmp_handler(level, fmtbuf, log_handler_ctx); log_handler = tmp_handler; - } else -#endif - if (log_on_stderr) { + } else if (log_on_stderr) { snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); #ifdef WIN32_FIXME - _write(STDERR_FILENO, msgbuf, strlen(msgbuf)); + _write(STDERR_FILENO, msgbuf, strlen(msgbuf)); #else - (void)write(log_stderr_fd, msgbuf, strlen(msgbuf)); -#endif - + (void)write(log_stderr_fd, msgbuf, strlen(msgbuf)); +#endif } else { - - #ifdef WIN32_FIXME - - if (logfd > 0) - { - char msgbufTimestamp[MSGBUFSIZ]; - - SYSTEMTIME st; - - GetLocalTime(&st); - - snprintf(msgbufTimestamp, sizeof msgbufTimestamp, "%d %02d:%02d:%02d %03d %s\n", - GetCurrentProcessId(), st.wHour, st.wMinute, st.wSecond, - st.wMilliseconds, msgbuf); - - _write(logfd, msgbufTimestamp, strlen(msgbufTimestamp)); - } - - #else #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata); syslog_r(pri, &sdata, "%.500s", fmtbuf); @@ -495,7 +463,6 @@ do_log(LogLevel level, const char *fmt, va_list args) openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility); syslog(pri, "%.500s", fmtbuf); closelog(); -#endif #endif } errno = saved_errno; diff --git a/regress/unittests/win32compat/tests.c b/regress/unittests/win32compat/tests.c index e3d545f..b9758be 100644 --- a/regress/unittests/win32compat/tests.c +++ b/regress/unittests/win32compat/tests.c @@ -9,7 +9,6 @@ #include "test_helper.h" extern void log_init(char *av0, int level, int facility, int on_stderr); -extern int logfd; void socket_tests(); void file_tests(); @@ -18,9 +17,7 @@ void tests(void) { _set_abort_behavior(0, 1); log_init(NULL, 7, 2, 0); - logfd = _open("unittests.log", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE ); socket_tests(); file_tests(); - if (logfd > 0) _close(logfd); return; } \ No newline at end of file diff --git a/sftp-server.c b/sftp-server.c index 1be13be..7e8424e 100644 --- a/sftp-server.c +++ b/sftp-server.c @@ -90,14 +90,6 @@ #define realpath realpathWin32 - - /* - * Handle to log file. - */ - - extern int logfd; - extern int sfd_start; - #endif /* WIN32_FIXME */ /* Our verbosity */ @@ -1998,27 +1990,11 @@ sftp_server_main(int argc, char **argv, struct passwd *user_pw) extern char *optarg; extern char *__progname; -#ifdef WIN32_FIXME - - /* - * Initialize Win32 log. - */ - - logfd = _open("sftp-server.log", O_WRONLY | O_CREAT | O_APPEND , S_IREAD | S_IWRITE); - - log_level = SYSLOG_LEVEL_INFO; - - __progname = ssh_get_progname(argv[0]); - - log_init(__progname, log_level, log_facility, log_stderr); - - #else __progname = ssh_get_progname(argv[0]); log_init(__progname, log_level, log_facility, log_stderr); - #endif - pw = pwcopy(user_pw); + pw = pwcopy(user_pw); while (!skipargs && (ch = getopt(argc, argv, diff --git a/sshd.c b/sshd.c index 17e6836..469da8a 100644 --- a/sshd.c +++ b/sshd.c @@ -193,8 +193,6 @@ FIXME: GFPZR: Function stat() may be undeclared. char *fake_fork_args; -extern int logfd; -extern int sfd_start; #endif /* re-exec */ @@ -1967,33 +1965,6 @@ main(int ac, char **av) SetConsoleCtrlHandler(CtrlHandlerRoutine, TRUE); w32posix_initialize(); - //authctxt -> hTokenLsa_ = NULL; - - /*WSHELPinitialize(); - - allocate_standard_descriptor(STDIN_FILENO); - allocate_standard_descriptor(STDOUT_FILENO); - allocate_standard_descriptor(STDERR_FILENO); - - sfd_start = 3;*/ - - /* - * Initialize log. - */ - - logfd = _open("sshd.log", O_WRONLY | O_CREAT | O_APPEND, - S_IREAD | S_IWRITE ); - - /* - * Forbid to inherit log file handle. - */ - - if (SetHandleInformation(_get_osfhandle(logfd), - HANDLE_FLAG_INHERIT, 0) == FALSE) - { - debug("ERROR: Cannot clear inherit flag for logfd handle. " - "Error code : %u.", GetLastError()); - } #endif /* WIN32_FIXME */ @@ -2190,9 +2161,6 @@ main(int ac, char **av) /* * Win32 only. */ - - //WSHELPinitialize(); - w32posix_initialize(); /* * Handle install and uninstall service options