43#define LOG_OPENVPN LOG_DAEMON
81static char *pgmname_syslog;
169#ifdef OPENVPN_DEBUG_COMMAND_LINE
217 va_start(arglist, format);
231 return strerror(err);
246 const char *prefix_sep;
256 bool crt_error =
false;
315 prefix_sep = prefix =
"";
324 snprintf(m2,
ERR_BUF_SIZE,
"%s%s%s", prefix, prefix_sep, m1);
334 syslog(level,
"%s%s%s", prefix, prefix_sep, m1);
345 gettimeofday(&tv, NULL);
347 fprintf(fp,
"%" PRIi64
".%06ld %x %s%s%s%s", (int64_t)tv.tv_sec, (
long)tv.tv_usec,
348 flags, prefix, prefix_sep, m1,
"\n");
352 fprintf(fp,
"%s%s%s%s", prefix, prefix_sep, m1, (flags &
M_NOLF) ?
"" :
"\n");
356 fprintf(fp,
"%s %s%s%s%s",
time_string(0, 0, show_usec, &
gc), prefix, prefix_sep,
357 m1, (flags &
M_NOLF) ?
"" :
"\n");
366 msg(
M_INFO,
"Exiting due to fatal error");
408 "%d variation(s) on previous %d message(s) suppressed by --mute", suppressed,
423 msg(
M_FATAL,
"Assertion failed at %s:%d (%s)", filename, line, condition);
427 msg(
M_FATAL,
"Assertion failed at %s:%d", filename, line);
439 fprintf(stderr, PACKAGE_NAME
": Out of Memory\n");
451 pgmname_syslog =
string_alloc(pgmname ? pgmname : PACKAGE, NULL);
452 openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
464 "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");
476 free(pgmname_syslog);
477 pgmname_syslog = NULL;
502 SECURITY_ATTRIBUTES saAttr;
503 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
504 saAttr.bInheritHandle = TRUE;
505 saAttr.lpSecurityDescriptor = NULL;
507 log_handle = CreateFileW(
wide_string(file, &
gc), GENERIC_WRITE, FILE_SHARE_READ, &saAttr,
508 append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
512 if (log_handle == INVALID_HANDLE_VALUE)
521 if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
523 msg(
M_ERR,
"Error: cannot seek to end of --log file: %s", file);
532 "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
537 log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
540 msg(
M_ERR,
"Error: --log redirect failed due to _open_osfhandle failure");
545 msgfp = _fdopen(log_fd,
"wt");
548 msg(
M_ERR,
"Error: --log redirect failed due to _fdopen");
552 if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
554 msg(
M_WARN,
"Error: --log redirect of stdout/stderr failed");
559#elif defined(HAVE_DUP2)
562 int out = open(file, O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR);
566 msg(
M_WARN |
M_ERRNO,
"Warning: Error redirecting stdout/stderr to --log file: %s",
571 if (dup2(out, 1) == -1)
573 msg(
M_ERR,
"--log file redirection error on stdout");
575 if (dup2(out, 2) == -1)
577 msg(
M_ERR,
"--log file redirection error on stderr");
590 "WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
628 const char *extended_msg = NULL;
630 bool crt_error =
false;
639#if EXTENDED_SOCKET_ERROR_CAPABILITY
644 extended_msg = format_extended_socket_error(sock->
sd, &mtu, &
gc);
645 if (mtu > 0 && sock->
mtu != mtu)
729 port_share_abort(port_share);
792 return "General failure (ERROR_GEN_FAILURE)";
795 return "I/O Operation in progress (ERROR_IO_PENDING)";
798 return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
801 return "Interrupted system call (WSAEINTR)";
804 return "Bad file number (WSAEBADF)";
807 return "Permission denied (WSAEACCES)";
810 return "Bad address (WSAEFAULT)";
813 return "Invalid argument (WSAEINVAL)";
816 return "Too many open files (WSAEMFILE)";
819 return "Operation would block (WSAEWOULDBLOCK)";
822 return "Operation now in progress (WSAEINPROGRESS)";
825 return "Operation already in progress (WSAEALREADY)";
828 return "Destination address required (WSAEDESTADDRREQ)";
831 return "Message too long (WSAEMSGSIZE)";
834 return "Protocol wrong type for socket (WSAEPROTOTYPE)";
837 return "Bad protocol option (WSAENOPROTOOPT)";
840 return "Protocol not supported (WSAEPROTONOSUPPORT)";
843 return "Socket type not supported (WSAESOCKTNOSUPPORT)";
846 return "Operation not supported on socket (WSAEOPNOTSUPP)";
849 return "Protocol family not supported (WSAEPFNOSUPPORT)";
852 return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
855 return "Address already in use (WSAEADDRINUSE)";
858 return "Network is down (WSAENETDOWN)";
861 return "Network is unreachable (WSAENETUNREACH)";
864 return "Net dropped connection or reset (WSAENETRESET)";
867 return "Software caused connection abort (WSAECONNABORTED)";
870 return "Connection reset by peer (WSAECONNRESET)";
873 return "No buffer space available (WSAENOBUFS)";
876 return "Socket is already connected (WSAEISCONN)";
879 return "Socket is not connected (WSAENOTCONN)";
882 return "Connection timed out (WSAETIMEDOUT)";
885 return "Connection refused (WSAECONNREFUSED)";
888 return "Too many levels of symbolic links (WSAELOOP)";
891 return "File name too long (WSAENAMETOOLONG)";
894 return "Host is down (WSAEHOSTDOWN)";
897 return "No Route to Host (WSAEHOSTUNREACH)";
900 return "Directory not empty (WSAENOTEMPTY)";
903 return "Too many processes (WSAEPROCLIM)";
906 return "Too many users (WSAEUSERS)";
909 return "Disc Quota Exceeded (WSAEDQUOT)";
912 return "Stale NFS file handle (WSAESTALE)";
915 return "Network SubSystem is unavailable (WSASYSNOTREADY)";
918 return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
921 return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
924 return "Too many levels of remote in path (WSAEREMOTE)";
927 return "Host not found (WSAHOST_NOT_FOUND)";
956 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)
void x_check_status(ssize_t status, const char *description, struct link_socket *sock, struct tuntap *tt)
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_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)