46#define LOG_OPENVPN LOG_DAEMON
84static char *pgmname_syslog;
170#ifdef OPENVPN_DEBUG_COMMAND_LINE
218 va_start(arglist, format);
232 return strerror(err);
247 const char *prefix_sep;
257 bool crt_error =
false;
316 prefix_sep = prefix =
"";
325 snprintf(m2,
ERR_BUF_SIZE,
"%s%s%s", prefix, prefix_sep, m1);
335 syslog(level,
"%s%s%s", prefix, prefix_sep, m1);
346 gettimeofday(&tv, NULL);
348 fprintf(fp,
"%" PRIi64
".%06ld %x %s%s%s%s", (int64_t)tv.tv_sec, (
long)tv.tv_usec,
349 flags, prefix, prefix_sep, m1,
"\n");
353 fprintf(fp,
"%s%s%s%s", prefix, prefix_sep, m1, (flags &
M_NOLF) ?
"" :
"\n");
357 fprintf(fp,
"%s %s%s%s%s",
time_string(0, 0, show_usec, &
gc), prefix, prefix_sep,
358 m1, (flags &
M_NOLF) ?
"" :
"\n");
367 msg(
M_INFO,
"Exiting due to fatal error");
409 "%d variation(s) on previous %d message(s) suppressed by --mute", suppressed,
424 msg(
M_FATAL,
"Assertion failed at %s:%d (%s)", filename, line, condition);
428 msg(
M_FATAL,
"Assertion failed at %s:%d", filename, line);
453 openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
465 "Warning on use of --daemon: this operating system lacks daemon logging features, therefore when I become a daemon, I won't be able to log status or error messages");
477 free(pgmname_syslog);
478 pgmname_syslog = NULL;
503 SECURITY_ATTRIBUTES saAttr;
504 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
505 saAttr.bInheritHandle = TRUE;
506 saAttr.lpSecurityDescriptor = NULL;
508 log_handle = CreateFileW(
wide_string(file, &
gc), GENERIC_WRITE, FILE_SHARE_READ, &saAttr,
509 append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
513 if (log_handle == INVALID_HANDLE_VALUE)
522 if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
524 msg(
M_ERR,
"Error: cannot seek to end of --log file: %s", file);
533 "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
538 log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
541 msg(
M_ERR,
"Error: --log redirect failed due to _open_osfhandle failure");
546 msgfp = _fdopen(log_fd,
"wt");
549 msg(
M_ERR,
"Error: --log redirect failed due to _fdopen");
553 if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
555 msg(
M_WARN,
"Error: --log redirect of stdout/stderr failed");
560#elif defined(HAVE_DUP2)
563 int out = open(file, O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR);
567 msg(
M_WARN |
M_ERRNO,
"Warning: Error redirecting stdout/stderr to --log file: %s",
572 if (dup2(out, 1) == -1)
574 msg(
M_ERR,
"--log file redirection error on stdout");
576 if (dup2(out, 2) == -1)
578 msg(
M_ERR,
"--log file redirection error on stderr");
591 "WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
629 const char *extended_msg = NULL;
631 bool crt_error =
false;
640#if EXTENDED_SOCKET_ERROR_CAPABILITY
645 extended_msg = format_extended_socket_error(sock->
sd, &mtu, &
gc);
646 if (mtu > 0 && sock->
mtu != mtu)
723 port_share_abort(port_share);
727#ifdef ENABLE_MEMSTATS
795 return "General failure (ERROR_GEN_FAILURE)";
798 return "I/O Operation in progress (ERROR_IO_PENDING)";
801 return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
804 return "Interrupted system call (WSAEINTR)";
807 return "Bad file number (WSAEBADF)";
810 return "Permission denied (WSAEACCES)";
813 return "Bad address (WSAEFAULT)";
816 return "Invalid argument (WSAEINVAL)";
819 return "Too many open files (WSAEMFILE)";
822 return "Operation would block (WSAEWOULDBLOCK)";
825 return "Operation now in progress (WSAEINPROGRESS)";
828 return "Operation already in progress (WSAEALREADY)";
831 return "Destination address required (WSAEDESTADDRREQ)";
834 return "Message too long (WSAEMSGSIZE)";
837 return "Protocol wrong type for socket (WSAEPROTOTYPE)";
840 return "Bad protocol option (WSAENOPROTOOPT)";
843 return "Protocol not supported (WSAEPROTONOSUPPORT)";
846 return "Socket type not supported (WSAESOCKTNOSUPPORT)";
849 return "Operation not supported on socket (WSAEOPNOTSUPP)";
852 return "Protocol family not supported (WSAEPFNOSUPPORT)";
855 return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
858 return "Address already in use (WSAEADDRINUSE)";
861 return "Network is down (WSAENETDOWN)";
864 return "Network is unreachable (WSAENETUNREACH)";
867 return "Net dropped connection or reset (WSAENETRESET)";
870 return "Software caused connection abort (WSAECONNABORTED)";
873 return "Connection reset by peer (WSAECONNRESET)";
876 return "No buffer space available (WSAENOBUFS)";
879 return "Socket is already connected (WSAEISCONN)";
882 return "Socket is not connected (WSAENOTCONN)";
885 return "Connection timed out (WSAETIMEDOUT)";
888 return "Connection refused (WSAECONNREFUSED)";
891 return "Too many levels of symbolic links (WSAELOOP)";
894 return "File name too long (WSAENAMETOOLONG)";
897 return "Host is down (WSAEHOSTDOWN)";
900 return "No Route to Host (WSAEHOSTUNREACH)";
903 return "Directory not empty (WSAENOTEMPTY)";
906 return "Too many processes (WSAEPROCLIM)";
909 return "Too many users (WSAEUSERS)";
912 return "Disc Quota Exceeded (WSAEDQUOT)";
915 return "Stale NFS file handle (WSAESTALE)";
918 return "Network SubSystem is unavailable (WSASYSNOTREADY)";
921 return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
924 return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
927 return "Too many levels of remote in path (WSAEREMOTE)";
930 return "Host not found (WSAHOST_NOT_FOUND)";
959 if (*
cp ==
'\n' || *
cp ==
'\r')
bool buf_printf(struct buffer *buf, const char *format,...)
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
char * string_alloc(const char *str, struct gc_arena *gc)
static void gc_init(struct gc_arena *a)
static void gc_free(struct gc_arena *a)
static struct gc_arena gc_new(void)
#define DEBUG_LEVEL_USEC_TIME
void remove_pid_file(void)
static int constrain_int(int x, int min, int max)
static SERVICE_STATUS status
void set_std_files_to_null(bool stdin_only)
const struct virtual_output * x_msg_virtual_output
msglvl_t get_debug_level(void)
void errors_to_stderr(void)
void open_syslog(const char *pgmname, bool stdio_to_null)
static FILE * default_err
unsigned int x_cs_info_level
void x_msg(const msglvl_t flags, const char *format,...)
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
const char * x_msg_prefix
bool dont_mute(msglvl_t flags)
Check muting filter.
void x_check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
int get_orig_stderr(void)
static FILE * default_out
void redirect_stdout_stderr(const char *file, bool append)
void assert_failed(const char *filename, int line, const char *condition)
bool set_mute_cutoff(const int cutoff)
void set_check_status(unsigned int info_level, unsigned int verbose_level)
static msglvl_t mute_category
unsigned int x_cs_verbose_level
void openvpn_exit(const int status)
void reset_check_status(void)
static bool machine_readable_output
void x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
int get_mute_cutoff(void)
void set_suppress_timestamps(bool suppressed)
void set_machine_readable_output(bool parsable)
FILE * msg_fp(const msglvl_t flags)
static const char * openvpn_strerror(int err, bool crt_error, struct gc_arena *gc)
bool set_debug_level(const int level, const unsigned int flags)
static bool suppress_timestamps
const char * msg_flags_string(const msglvl_t flags, struct gc_arena *gc)
unsigned int x_cs_err_delay_ms
static bool check_debug_level(msglvl_t level)
#define OPENVPN_DEBUG_FILE
#define DECODE_MUTE_LEVEL(flags)
#define OPENVPN_EXIT_STATUS_GOOD
static const char * msg_get_prefix(void)
static bool msg_test(msglvl_t flags)
Return true if flags represent an enabled, not muted log level.
#define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE
#define OPENVPN_EXIT_STATUS_ERROR
static bool ignore_sys_error(const int err, bool crt_error)
static int openvpn_errno_maybe_crt(bool *crt_error)
static const struct virtual_output * msg_get_virtual_output(void)
const char * time_string(time_t t, long usec, bool show_usec, struct gc_arena *gc)
static void perf_output_results(void)
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
static void virtual_output_print(const struct virtual_output *vo, const unsigned int flags, const char *str)
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
struct link_socket_info info
const char * tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
static bool tuntap_defined(const struct tuntap *tt)
char * utf16to8(const wchar_t *utf16, struct gc_arena *gc)
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)