46#define LOG_OPENVPN LOG_DAEMON
84static char *pgmname_syslog;
106 const int ceiling = 15;
108 if (level >= 0 && level <= ceiling)
172#ifdef OPENVPN_DEBUG_COMMAND_LINE
217x_msg(
const unsigned int flags,
const char *format, ...)
220 va_start(arglist, format);
234 return strerror(err);
238x_msg_va(
const unsigned int flags,
const char *format, va_list arglist)
249 const char *prefix_sep;
259 bool crt_error =
false;
318 prefix_sep = prefix =
"";
327 snprintf(m2,
ERR_BUF_SIZE,
"%s%s%s", prefix, prefix_sep, m1);
337 syslog(level,
"%s%s%s", prefix, prefix_sep, m1);
348 gettimeofday(&tv, NULL);
350 fprintf(fp,
"%" PRIi64
".%06ld %x %s%s%s%s", (int64_t)tv.tv_sec, (
long)tv.tv_usec,
351 flags, prefix, prefix_sep, m1,
"\n");
355 fprintf(fp,
"%s%s%s%s", prefix, prefix_sep, m1, (flags &
M_NOLF) ?
"" :
"\n");
359 fprintf(fp,
"%s %s%s%s%s",
time_string(0, 0, show_usec, &
gc), prefix, prefix_sep,
360 m1, (flags &
M_NOLF) ?
"" :
"\n");
369 msg(
M_INFO,
"Exiting due to fatal error");
411 "%d variation(s) on previous %d message(s) suppressed by --mute", suppressed,
426 msg(
M_FATAL,
"Assertion failed at %s:%d (%s)", filename, line, condition);
430 msg(
M_FATAL,
"Assertion failed at %s:%d", filename, line);
442 fprintf(stderr, PACKAGE_NAME
": Out of Memory\n");
454 pgmname_syslog =
string_alloc(pgmname ? pgmname : PACKAGE, NULL);
455 openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
467 "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");
479 free(pgmname_syslog);
480 pgmname_syslog = NULL;
505 SECURITY_ATTRIBUTES saAttr;
506 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
507 saAttr.bInheritHandle = TRUE;
508 saAttr.lpSecurityDescriptor = NULL;
510 log_handle = CreateFileW(
wide_string(file, &
gc), GENERIC_WRITE, FILE_SHARE_READ, &saAttr,
511 append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
515 if (log_handle == INVALID_HANDLE_VALUE)
524 if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
526 msg(
M_ERR,
"Error: cannot seek to end of --log file: %s", file);
535 "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
540 log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
543 msg(
M_ERR,
"Error: --log redirect failed due to _open_osfhandle failure");
548 msgfp = _fdopen(log_fd,
"wt");
551 msg(
M_ERR,
"Error: --log redirect failed due to _fdopen");
555 if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
557 msg(
M_WARN,
"Error: --log redirect of stdout/stderr failed");
562#elif defined(HAVE_DUP2)
565 int out = open(file, O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR);
569 msg(
M_WARN |
M_ERRNO,
"Warning: Error redirecting stdout/stderr to --log file: %s",
574 if (dup2(out, 1) == -1)
576 msg(
M_ERR,
"--log file redirection error on stdout");
578 if (dup2(out, 2) == -1)
580 msg(
M_ERR,
"--log file redirection error on stderr");
593 "WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
631 const char *extended_msg = NULL;
633 bool crt_error =
false;
642#if EXTENDED_SOCKET_ERROR_CAPABILITY
647 extended_msg = format_extended_socket_error(sock->
sd, &mtu, &
gc);
648 if (mtu > 0 && sock->
mtu != mtu)
725 port_share_abort(port_share);
729#ifdef ENABLE_MEMSTATS
797 return "General failure (ERROR_GEN_FAILURE)";
800 return "I/O Operation in progress (ERROR_IO_PENDING)";
803 return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
806 return "Interrupted system call (WSAEINTR)";
809 return "Bad file number (WSAEBADF)";
812 return "Permission denied (WSAEACCES)";
815 return "Bad address (WSAEFAULT)";
818 return "Invalid argument (WSAEINVAL)";
821 return "Too many open files (WSAEMFILE)";
824 return "Operation would block (WSAEWOULDBLOCK)";
827 return "Operation now in progress (WSAEINPROGRESS)";
830 return "Operation already in progress (WSAEALREADY)";
833 return "Destination address required (WSAEDESTADDRREQ)";
836 return "Message too long (WSAEMSGSIZE)";
839 return "Protocol wrong type for socket (WSAEPROTOTYPE)";
842 return "Bad protocol option (WSAENOPROTOOPT)";
845 return "Protocol not supported (WSAEPROTONOSUPPORT)";
848 return "Socket type not supported (WSAESOCKTNOSUPPORT)";
851 return "Operation not supported on socket (WSAEOPNOTSUPP)";
854 return "Protocol family not supported (WSAEPFNOSUPPORT)";
857 return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
860 return "Address already in use (WSAEADDRINUSE)";
863 return "Network is down (WSAENETDOWN)";
866 return "Network is unreachable (WSAENETUNREACH)";
869 return "Net dropped connection or reset (WSAENETRESET)";
872 return "Software caused connection abort (WSAECONNABORTED)";
875 return "Connection reset by peer (WSAECONNRESET)";
878 return "No buffer space available (WSAENOBUFS)";
881 return "Socket is already connected (WSAEISCONN)";
884 return "Socket is not connected (WSAENOTCONN)";
887 return "Connection timed out (WSAETIMEDOUT)";
890 return "Connection refused (WSAECONNREFUSED)";
893 return "Too many levels of symbolic links (WSAELOOP)";
896 return "File name too long (WSAENAMETOOLONG)";
899 return "Host is down (WSAEHOSTDOWN)";
902 return "No Route to Host (WSAEHOSTUNREACH)";
905 return "Directory not empty (WSAENOTEMPTY)";
908 return "Too many processes (WSAEPROCLIM)";
911 return "Too many users (WSAEUSERS)";
914 return "Disc Quota Exceeded (WSAEDQUOT)";
917 return "Stale NFS file handle (WSAESTALE)";
920 return "Network SubSystem is unavailable (WSASYSNOTREADY)";
923 return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
926 return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
929 return "Too many levels of remote in path (WSAEREMOTE)";
932 return "Host not found (WSAHOST_NOT_FOUND)";
961 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)