43#define LOG_OPENVPN LOG_DAEMON
81static char *pgmname_syslog;
169#ifdef OPENVPN_DEBUG_COMMAND_LINE
217 va_start(arglist, format);
222#if defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
223#pragma GCC diagnostic push
224#pragma GCC diagnostic ignored "-Wsign-conversion"
236 return strerror(err);
251 const char *prefix_sep;
261 bool crt_error =
false;
320 prefix_sep = prefix =
"";
329 snprintf(m2,
ERR_BUF_SIZE,
"%s%s%s", prefix, prefix_sep, m1);
339 syslog(level,
"%s%s%s", prefix, prefix_sep, m1);
350 gettimeofday(&tv, NULL);
352 fprintf(fp,
"%" PRIi64
".%06ld %x %s%s%s%s", (int64_t)tv.tv_sec, (
long)tv.tv_usec,
353 flags, prefix, prefix_sep, m1,
"\n");
357 fprintf(fp,
"%s%s%s%s", prefix, prefix_sep, m1, (flags &
M_NOLF) ?
"" :
"\n");
361 fprintf(fp,
"%s %s%s%s%s",
time_string(0, 0, show_usec, &
gc), prefix, prefix_sep,
362 m1, (flags &
M_NOLF) ?
"" :
"\n");
371 msg(
M_INFO,
"Exiting due to fatal error");
413 "%d variation(s) on previous %d message(s) suppressed by --mute", suppressed,
428 msg(
M_FATAL,
"Assertion failed at %s:%d (%s)", filename, line, condition);
432 msg(
M_FATAL,
"Assertion failed at %s:%d", filename, line);
457 openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
469 "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");
481 free(pgmname_syslog);
482 pgmname_syslog = NULL;
507 SECURITY_ATTRIBUTES saAttr;
508 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
509 saAttr.bInheritHandle = TRUE;
510 saAttr.lpSecurityDescriptor = NULL;
512 log_handle = CreateFileW(
wide_string(file, &
gc), GENERIC_WRITE, FILE_SHARE_READ, &saAttr,
513 append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
517 if (log_handle == INVALID_HANDLE_VALUE)
526 if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
528 msg(
M_ERR,
"Error: cannot seek to end of --log file: %s", file);
537 "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
542 log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
545 msg(
M_ERR,
"Error: --log redirect failed due to _open_osfhandle failure");
550 msgfp = _fdopen(log_fd,
"wt");
553 msg(
M_ERR,
"Error: --log redirect failed due to _fdopen");
557 if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
559 msg(
M_WARN,
"Error: --log redirect of stdout/stderr failed");
564#elif defined(HAVE_DUP2)
567 int out = open(file, O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR);
571 msg(
M_WARN |
M_ERRNO,
"Warning: Error redirecting stdout/stderr to --log file: %s",
576 if (dup2(out, 1) == -1)
578 msg(
M_ERR,
"--log file redirection error on stdout");
580 if (dup2(out, 2) == -1)
582 msg(
M_ERR,
"--log file redirection error on stderr");
595 "WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
633 const char *extended_msg = NULL;
635 bool crt_error =
false;
644#if EXTENDED_SOCKET_ERROR_CAPABILITY
649 extended_msg = format_extended_socket_error(sock->
sd, &mtu, &
gc);
650 if (mtu > 0 && sock->
mtu != mtu)
697#if defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
698#pragma GCC diagnostic pop
738 port_share_abort(port_share);
801 return "General failure (ERROR_GEN_FAILURE)";
804 return "I/O Operation in progress (ERROR_IO_PENDING)";
807 return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
810 return "Interrupted system call (WSAEINTR)";
813 return "Bad file number (WSAEBADF)";
816 return "Permission denied (WSAEACCES)";
819 return "Bad address (WSAEFAULT)";
822 return "Invalid argument (WSAEINVAL)";
825 return "Too many open files (WSAEMFILE)";
828 return "Operation would block (WSAEWOULDBLOCK)";
831 return "Operation now in progress (WSAEINPROGRESS)";
834 return "Operation already in progress (WSAEALREADY)";
837 return "Destination address required (WSAEDESTADDRREQ)";
840 return "Message too long (WSAEMSGSIZE)";
843 return "Protocol wrong type for socket (WSAEPROTOTYPE)";
846 return "Bad protocol option (WSAENOPROTOOPT)";
849 return "Protocol not supported (WSAEPROTONOSUPPORT)";
852 return "Socket type not supported (WSAESOCKTNOSUPPORT)";
855 return "Operation not supported on socket (WSAEOPNOTSUPP)";
858 return "Protocol family not supported (WSAEPFNOSUPPORT)";
861 return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
864 return "Address already in use (WSAEADDRINUSE)";
867 return "Network is down (WSAENETDOWN)";
870 return "Network is unreachable (WSAENETUNREACH)";
873 return "Net dropped connection or reset (WSAENETRESET)";
876 return "Software caused connection abort (WSAECONNABORTED)";
879 return "Connection reset by peer (WSAECONNRESET)";
882 return "No buffer space available (WSAENOBUFS)";
885 return "Socket is already connected (WSAEISCONN)";
888 return "Socket is not connected (WSAENOTCONN)";
891 return "Connection timed out (WSAETIMEDOUT)";
894 return "Connection refused (WSAECONNREFUSED)";
897 return "Too many levels of symbolic links (WSAELOOP)";
900 return "File name too long (WSAENAMETOOLONG)";
903 return "Host is down (WSAEHOSTDOWN)";
906 return "No Route to Host (WSAEHOSTUNREACH)";
909 return "Directory not empty (WSAENOTEMPTY)";
912 return "Too many processes (WSAEPROCLIM)";
915 return "Too many users (WSAEUSERS)";
918 return "Disc Quota Exceeded (WSAEDQUOT)";
921 return "Stale NFS file handle (WSAESTALE)";
924 return "Network SubSystem is unavailable (WSASYSNOTREADY)";
927 return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
930 return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
933 return "Too many levels of remote in path (WSAEREMOTE)";
936 return "Host not found (WSAHOST_NOT_FOUND)";
965 if (*
cp ==
'\n' || *
cp ==
'\r')
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static void gc_init(struct gc_arena *a)
Initialise a garbage collection arena to an empty state.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
#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.
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_check_status(ssize_t status, const char *description, struct link_socket *sock, const struct tuntap *tt)
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)
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, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
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)