47#define LOG_OPENVPN LOG_DAEMON
85static char *pgmname_syslog;
107 const int ceiling = 15;
109 if (level >= 0 && level <= ceiling)
173#ifdef OPENVPN_DEBUG_COMMAND_LINE
208#define SWAP { tmp = m1; m1 = m2; m2 = tmp; }
213x_msg(
const unsigned int flags,
const char *format, ...)
216 va_start(arglist, format);
230 return strerror(err);
234x_msg_va(
const unsigned int flags,
const char *format, va_list arglist)
245 const char *prefix_sep;
255 bool crt_error =
false;
315 prefix_sep = prefix =
"";
337 syslog(level,
"%s%s%s",
351 gettimeofday(&tv, NULL);
353 fprintf(fp,
"%" PRIi64
".%06ld %x %s%s%s%s",
365 fprintf(fp,
"%s%s%s%s",
369 (flags&
M_NOLF) ?
"" :
"\n");
373 fprintf(fp,
"%s %s%s%s%s",
378 (flags&
M_NOLF) ?
"" :
"\n");
387 msg(
M_INFO,
"Exiting due to fatal error");
430 "%d variation(s) on previous %d message(s) suppressed by --mute",
446 msg(
M_FATAL,
"Assertion failed at %s:%d (%s)", filename, line, condition);
450 msg(
M_FATAL,
"Assertion failed at %s:%d", filename, line);
462 fprintf(stderr, PACKAGE_NAME
": Out of Memory\n");
474 pgmname_syslog =
string_alloc(pgmname ? pgmname : PACKAGE, NULL);
475 openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
486 msg(
M_WARN,
"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");
498 free(pgmname_syslog);
499 pgmname_syslog = NULL;
524 SECURITY_ATTRIBUTES saAttr;
525 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
526 saAttr.bInheritHandle = TRUE;
527 saAttr.lpSecurityDescriptor = NULL;
533 append ? OPEN_ALWAYS : CREATE_ALWAYS,
534 FILE_ATTRIBUTE_NORMAL,
539 if (log_handle == INVALID_HANDLE_VALUE)
548 if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
550 msg(
M_ERR,
"Error: cannot seek to end of --log file: %s", file);
558 msg(
M_WARN |
M_ERRNO,
"Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
563 log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
566 msg(
M_ERR,
"Error: --log redirect failed due to _open_osfhandle failure");
571 msgfp = _fdopen(log_fd,
"wt");
574 msg(
M_ERR,
"Error: --log redirect failed due to _fdopen");
578 if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
580 msg(
M_WARN,
"Error: --log redirect of stdout/stderr failed");
585#elif defined(HAVE_DUP2)
589 O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC),
594 msg(
M_WARN|
M_ERRNO,
"Warning: Error redirecting stdout/stderr to --log file: %s", file);
598 if (dup2(out, 1) == -1)
600 msg(
M_ERR,
"--log file redirection error on stdout");
602 if (dup2(out, 2) == -1)
604 msg(
M_ERR,
"--log file redirection error on stderr");
616 msg(
M_WARN,
"WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
653 const char *description,
657 const char *extended_msg = NULL;
659 bool crt_error =
false;
670#if EXTENDED_SOCKET_ERROR_CAPABILITY
675 extended_msg = format_extended_socket_error(sock->
sd, &mtu, &
gc);
676 if (mtu > 0 && sock->
mtu != mtu)
699 sock ? sock->
sd : -1, my_errno);
706 sock ? sock->
sd : -1, my_errno);
755 port_share_abort(port_share);
759#ifdef ENABLE_MEMSTATS
827 return "General failure (ERROR_GEN_FAILURE)";
830 return "I/O Operation in progress (ERROR_IO_PENDING)";
833 return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
836 return "Interrupted system call (WSAEINTR)";
839 return "Bad file number (WSAEBADF)";
842 return "Permission denied (WSAEACCES)";
845 return "Bad address (WSAEFAULT)";
848 return "Invalid argument (WSAEINVAL)";
851 return "Too many open files (WSAEMFILE)";
854 return "Operation would block (WSAEWOULDBLOCK)";
857 return "Operation now in progress (WSAEINPROGRESS)";
860 return "Operation already in progress (WSAEALREADY)";
863 return "Destination address required (WSAEDESTADDRREQ)";
866 return "Message too long (WSAEMSGSIZE)";
869 return "Protocol wrong type for socket (WSAEPROTOTYPE)";
872 return "Bad protocol option (WSAENOPROTOOPT)";
875 return "Protocol not supported (WSAEPROTONOSUPPORT)";
878 return "Socket type not supported (WSAESOCKTNOSUPPORT)";
881 return "Operation not supported on socket (WSAEOPNOTSUPP)";
884 return "Protocol family not supported (WSAEPFNOSUPPORT)";
887 return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
890 return "Address already in use (WSAEADDRINUSE)";
893 return "Network is down (WSAENETDOWN)";
896 return "Network is unreachable (WSAENETUNREACH)";
899 return "Net dropped connection or reset (WSAENETRESET)";
902 return "Software caused connection abort (WSAECONNABORTED)";
905 return "Connection reset by peer (WSAECONNRESET)";
908 return "No buffer space available (WSAENOBUFS)";
911 return "Socket is already connected (WSAEISCONN)";
914 return "Socket is not connected (WSAENOTCONN)";
917 return "Connection timed out (WSAETIMEDOUT)";
920 return "Connection refused (WSAECONNREFUSED)";
923 return "Too many levels of symbolic links (WSAELOOP)";
926 return "File name too long (WSAENAMETOOLONG)";
929 return "Host is down (WSAEHOSTDOWN)";
932 return "No Route to Host (WSAEHOSTUNREACH)";
935 return "Directory not empty (WSAENOTEMPTY)";
938 return "Too many processes (WSAEPROCLIM)";
941 return "Too many users (WSAEUSERS)";
944 return "Disc Quota Exceeded (WSAEDQUOT)";
947 return "Stale NFS file handle (WSAESTALE)";
950 return "Network SubSystem is unavailable (WSASYSNOTREADY)";
953 return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
956 return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
959 return "Too many levels of remote in path (WSAEREMOTE)";
962 return "Host not found (WSAHOST_NOT_FOUND)";
997 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)
unsigned int x_debug_level
const struct virtual_output * x_msg_virtual_output
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
bool dont_mute(unsigned int flags)
Check muting filter.
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
const char * x_msg_prefix
void x_check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
void x_msg(const unsigned int flags, const char *format,...)
int get_debug_level(void)
int get_orig_stderr(void)
static FILE * default_out
void redirect_stdout_stderr(const char *file, bool append)
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
void assert_failed(const char *filename, int line, const char *condition)
bool set_mute_cutoff(const int cutoff)
const char * msg_flags_string(const unsigned int flags, struct gc_arena *gc)
void set_check_status(unsigned int info_level, unsigned int verbose_level)
unsigned int x_cs_verbose_level
FILE * msg_fp(const unsigned int flags)
void openvpn_exit(const int status)
void reset_check_status(void)
static bool machine_readable_output
int get_mute_cutoff(void)
void set_suppress_timestamps(bool suppressed)
void set_machine_readable_output(bool parsable)
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
unsigned int x_cs_err_delay_ms
static bool msg_test(unsigned int flags)
Return true if flags represent an enabled, not muted log level.
#define OPENVPN_DEBUG_FILE
#define DECODE_MUTE_LEVEL(flags)
#define OPENVPN_EXIT_STATUS_GOOD
static const char * msg_get_prefix(void)
#define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE
static bool check_debug_level(unsigned int level)
#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)