1 const char errlog_rcs[] = "$Id: errlog.c,v 1.121 2014/06/03 10:24:00 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $
6 * Purpose : Log errors to a designated destination in an elegant,
9 * Copyright : Written by and Copyright (C) 2001-2014 the
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 *********************************************************************/
46 /* For gettimeofday() */
49 #if !defined(_WIN32) && !defined(__OS2__)
51 #endif /* !defined(_WIN32) && !defined(__OS2__) */
63 #endif /* ndef _WIN_CONSOLE */
64 #endif /* def _WIN32 */
66 #define inline __inline
67 #endif /* def _MSC_VER */
70 #include <sys/socket.h> /* For sock_errno */
78 #ifdef FEATURE_EXTERNAL_FILTERS
79 #include "jbsockets.h"
82 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
86 * LOG_LEVEL_FATAL cannot be turned off. (There are
87 * some exceptional situations where we need to get a
88 * message to the user).
90 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
92 /* where to log (default: stderr) */
93 static FILE *logfp = NULL;
95 /* logging detail level. XXX: stupid name. */
96 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR);
98 /* static functions */
99 static void fatal_error(const char * error_message);
101 static char *w32_socket_strerr(int errcode, char *tmp_buf);
104 static char *os2_socket_strerr(int errcode, char *tmp_buf);
107 #ifdef MUTEX_LOCKS_AVAILABLE
108 static inline void lock_logfile(void)
110 privoxy_mutex_lock(&log_mutex);
112 static inline void unlock_logfile(void)
114 privoxy_mutex_unlock(&log_mutex);
116 static inline void lock_loginit(void)
118 privoxy_mutex_lock(&log_init_mutex);
120 static inline void unlock_loginit(void)
122 privoxy_mutex_unlock(&log_init_mutex);
124 #else /* ! MUTEX_LOCKS_AVAILABLE */
126 * FIXME we need a cross-platform locking mechanism.
127 * The locking/unlocking functions below should be
128 * fleshed out for non-pthread implementations.
130 static inline void lock_logfile() {}
131 static inline void unlock_logfile() {}
132 static inline void lock_loginit() {}
133 static inline void unlock_loginit() {}
136 /*********************************************************************
138 * Function : fatal_error
140 * Description : Displays a fatal error to standard error (or, on
141 * a WIN32 GUI, to a dialog box), and exits Privoxy
142 * with status code 1.
145 * 1 : error_message = The error message to display.
147 * Returns : Does not return.
149 *********************************************************************/
150 static void fatal_error(const char *error_message)
154 fputs(error_message, logfp);
157 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
159 /* Skip timestamp and thread id for the message box. */
160 const char *box_message = strstr(error_message, "Fatal error");
161 if (NULL == box_message)
163 /* Shouldn't happen but ... */
164 box_message = error_message;
166 MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
167 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
169 /* Cleanup - remove taskbar icon etc. */
172 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
185 /*********************************************************************
187 * Function : show_version
189 * Description : Logs the Privoxy version and the program name.
192 * 1 : prog_name = The program name.
196 *********************************************************************/
197 void show_version(const char *prog_name)
199 log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
200 if (prog_name != NULL)
202 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
207 /*********************************************************************
209 * Function : init_log_module
211 * Description : Initializes the logging module to log to stderr.
212 * Can only be called while stderr hasn't been closed
213 * yet and is only supposed to be called once.
216 * 1 : prog_name = The program name.
220 *********************************************************************/
221 void init_log_module(void)
226 set_debug_level(debug);
230 /*********************************************************************
232 * Function : set_debug_level
234 * Description : Sets the debug level to the provided value
235 * plus LOG_LEVEL_MINIMUM.
237 * XXX: we should only use the LOG_LEVEL_MINIMUM
238 * until the first time the configuration file has
241 * Parameters : 1: debug_level = The debug level to set.
245 *********************************************************************/
246 void set_debug_level(int debug_level)
248 debug = debug_level | LOG_LEVEL_MINIMUM;
252 /*********************************************************************
254 * Function : debug_level_is_enabled
256 * Description : Checks if a certain debug level is enabled.
258 * Parameters : 1: debug_level = The debug level to check.
262 *********************************************************************/
263 int debug_level_is_enabled(int debug_level)
265 return (0 != (debug & debug_level));
269 /*********************************************************************
271 * Function : disable_logging
273 * Description : Disables logging.
279 *********************************************************************/
280 void disable_logging(void)
284 log_error(LOG_LEVEL_INFO,
285 "No logfile configured. Please enable it before reporting any problems.");
294 /*********************************************************************
296 * Function : init_error_log
298 * Description : Initializes the logging module to log to a file.
300 * XXX: should be renamed.
303 * 1 : prog_name = The program name.
304 * 2 : logfname = The logfile to (re)open.
308 *********************************************************************/
309 void init_error_log(const char *prog_name, const char *logfname)
313 assert(NULL != logfname);
317 if ((logfp != NULL) && (logfp != stderr))
319 log_error(LOG_LEVEL_INFO, "(Re-)Opening logfile \'%s\'", logfname);
322 /* set the designated log file */
323 fp = fopen(logfname, "a");
324 if ((NULL == fp) && (logfp != NULL))
327 * Some platforms (like OS/2) don't allow us to open
328 * the same file twice, therefore we give it another
329 * shot after closing the old file descriptor first.
331 * We don't do it right away because it prevents us
332 * from logging the "can't open logfile" message to
335 * XXX: this is a lame workaround and once the next
336 * release is out we should stop bothering reopening
337 * the logfile unless we have to.
339 * Currently we reopen it every time the config file
340 * has been reloaded, but actually we only have to
341 * reopen it if the file name changed or if the
342 * configuration reload was caused by a SIGHUP.
344 log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. "
345 "Retrying after closing the old file descriptor first. If that "
346 "doesn't work, Privoxy will exit without being able to log a message.",
352 fp = fopen(logfname, "a");
357 log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
360 #ifdef FEATURE_EXTERNAL_FILTERS
361 mark_socket_for_close_on_execute(3);
364 /* set logging to be completely unbuffered */
373 if (daemon_mode && (logfp == stderr))
375 if (dup2(1, 2) == -1)
378 * We only use fatal_error() to clear the pid
379 * file and to exit. Given that stderr has just
380 * been closed, the user will not see the error
383 fatal_error("Failed to reserve fd 2.");
390 show_version(prog_name);
394 } /* init_error_log */
397 /*********************************************************************
399 * Function : get_thread_id
401 * Description : Returns a number that is different for each thread.
403 * XXX: Should be moved elsewhere (miscutil.c?)
407 * Returns : thread_id
409 *********************************************************************/
410 static long get_thread_id(void)
416 APIRET ulrc; /* XXX: I have no clue what this does */
419 /* FIXME get current thread id */
420 #ifdef FEATURE_PTHREAD
421 this_thread = (long)pthread_self();
424 * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
425 * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
426 * pthread_t is supposed to be opaque... but it's fairly random, though, so
427 * we make it mostly presentable.
429 this_thread = abs(this_thread % 1000);
430 #endif /* def __MACH__ */
431 #elif defined(_WIN32)
432 this_thread = GetCurrentThreadId();
433 #elif defined(__OS2__)
434 ulrc = DosGetInfoBlocks(&ptib, NULL);
436 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
438 /* Forking instead of threading. */
440 #endif /* def FEATURE_PTHREAD */
446 /*********************************************************************
448 * Function : get_log_timestamp
450 * Description : Generates the time stamp for the log message prefix.
453 * 1 : buffer = Storage buffer
454 * 2 : buffer_size = Size of storage buffer
456 * Returns : Number of written characters or 0 for error.
458 *********************************************************************/
459 static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
464 struct timeval tv_now; /* XXX: stupid name */
466 int msecs_length = 0;
468 gettimeofday(&tv_now, NULL);
469 msecs = tv_now.tv_usec / 1000;
472 #ifdef HAVE_LOCALTIME_R
473 tm_now = *localtime_r(&now, &tm_now);
474 #elif defined(MUTEX_LOCKS_AVAILABLE)
475 privoxy_mutex_lock(&localtime_mutex);
476 tm_now = *localtime(&now);
477 privoxy_mutex_unlock(&localtime_mutex);
479 tm_now = *localtime(&now);
482 length = strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &tm_now);
483 if (length > (size_t)0)
485 msecs_length = snprintf(buffer+length, buffer_size - length, ".%.3ld", msecs);
487 if (msecs_length > 0)
489 length += (size_t)msecs_length;
500 /*********************************************************************
502 * Function : get_clf_timestamp
504 * Description : Generates a Common Log Format time string.
507 * 1 : buffer = Storage buffer
508 * 2 : buffer_size = Size of storage buffer
510 * Returns : Number of written characters or 0 for error.
512 *********************************************************************/
513 static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
516 * Complex because not all OSs have tm_gmtoff or
517 * the %z field in strftime()
522 #ifdef HAVE_LOCALTIME_R
531 gmt = *gmtime_r(&now, &gmt);
532 #elif defined(MUTEX_LOCKS_AVAILABLE)
533 privoxy_mutex_lock(&gmtime_mutex);
535 privoxy_mutex_unlock(&gmtime_mutex);
539 #ifdef HAVE_LOCALTIME_R
540 tm_now = localtime_r(&now, &dummy);
541 #elif defined(MUTEX_LOCKS_AVAILABLE)
542 privoxy_mutex_lock(&localtime_mutex);
543 tm_now = localtime(&now);
544 privoxy_mutex_unlock(&localtime_mutex);
546 tm_now = localtime(&now);
548 days = tm_now->tm_yday - gmt.tm_yday;
549 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
550 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
552 length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
554 if (length > (size_t)0)
556 tz_length = snprintf(buffer+length, buffer_size-length,
557 "%+03d%02d", mins / 60, abs(mins) % 60);
561 length += (size_t)tz_length;
572 /*********************************************************************
574 * Function : get_log_level_string
576 * Description : Translates a numerical loglevel into a string.
579 * 1 : loglevel = LOG_LEVEL_FOO
581 * Returns : Log level string.
583 *********************************************************************/
584 static inline const char *get_log_level_string(int loglevel)
586 char *log_level_string = NULL;
588 assert(0 < loglevel);
592 case LOG_LEVEL_ERROR:
593 log_level_string = "Error";
595 case LOG_LEVEL_FATAL:
596 log_level_string = "Fatal error";
599 log_level_string = "Request";
601 case LOG_LEVEL_CONNECT:
602 log_level_string = "Connect";
604 case LOG_LEVEL_WRITING:
605 log_level_string = "Writing";
607 case LOG_LEVEL_RECEIVED:
608 log_level_string = "Received";
610 case LOG_LEVEL_HEADER:
611 log_level_string = "Header";
614 log_level_string = "Info";
616 case LOG_LEVEL_RE_FILTER:
617 log_level_string = "Re-Filter";
619 #ifdef FEATURE_FORCE_LOAD
620 case LOG_LEVEL_FORCE:
621 log_level_string = "Force";
623 #endif /* def FEATURE_FORCE_LOAD */
624 case LOG_LEVEL_REDIRECTS:
625 log_level_string = "Redirect";
627 case LOG_LEVEL_DEANIMATE:
628 log_level_string = "Gif-Deanimate";
630 case LOG_LEVEL_CRUNCH:
631 log_level_string = "Crunch";
634 log_level_string = "CGI";
636 case LOG_LEVEL_ACTIONS:
637 log_level_string = "Actions";
640 log_level_string = "Unknown log level";
643 assert(NULL != log_level_string);
645 return log_level_string;
649 #define LOG_BUFFER_SIZE BUFFER_SIZE
650 /*********************************************************************
652 * Function : log_error
654 * Description : This is the error-reporting and logging function.
657 * 1 : loglevel = the type of message to be logged
658 * 2 : fmt = the main string we want logged, printf-like
659 * 3 : ... = arguments to be inserted in fmt (printf-like).
663 *********************************************************************/
664 void log_error(int loglevel, const char *fmt, ...)
668 static char *outbuf_save = NULL;
669 char tempbuf[LOG_BUFFER_SIZE];
671 const char * src = fmt;
675 * XXX: Make this a config option,
676 * why else do we allocate instead of using
679 size_t log_buffer_size = LOG_BUFFER_SIZE;
681 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
683 * Irrespective of debug setting, a GET/POST/CONNECT makes
684 * the taskbar icon animate. (There is an option to disable
685 * this but checking that is handled inside LogShowActivity()).
687 if ((loglevel == LOG_LEVEL_GPC) || (loglevel == LOG_LEVEL_CRUNCH))
691 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
694 * verify that the loglevel applies to current
695 * settings and that logging is enabled.
696 * Bail out otherwise.
698 if ((0 == (loglevel & debug))
704 if (loglevel == LOG_LEVEL_FATAL)
706 fatal_error("Fatal error. You're not supposed to"
707 "see this message. Please file a bug report.");
712 thread_id = get_thread_id();
713 get_log_timestamp(timestamp, sizeof(timestamp));
715 /* protect the whole function because of the static buffer (outbuf) */
718 if (NULL == outbuf_save)
720 outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
721 if (NULL == outbuf_save)
723 snprintf(tempbuf, sizeof(tempbuf),
724 "%s %08lx Fatal error: Out of memory in log_error().",
725 timestamp, thread_id);
726 fatal_error(tempbuf); /* Exit */
730 outbuf = outbuf_save;
733 * Memsetting the whole buffer to zero (in theory)
734 * makes things easier later on.
736 memset(outbuf, 0, log_buffer_size);
738 /* Add prefix for everything but Common Log Format messages */
739 if (loglevel != LOG_LEVEL_CLF)
741 length = (size_t)snprintf(outbuf, log_buffer_size, "%s %08lx %s: ",
742 timestamp, thread_id, get_log_level_string(loglevel));
745 /* get ready to scan var. args. */
748 /* build formatted message from fmt and var-args */
749 while ((*src) && (length < log_buffer_size-2))
751 const char *sval = NULL; /* %N string */
752 int ival; /* %N string length or an error code */
753 unsigned uval; /* %u value */
754 long lval; /* %l value */
755 unsigned long ulval; /* %ul value */
757 const char *format_string = tempbuf;
762 outbuf[length++] = ch;
764 * XXX: Only necessary on platforms where multiple threads
765 * can write to the buffer at the same time because we
766 * don't support mutexes (OS/2 for example).
768 outbuf[length] = '\0';
771 outbuf[length] = '\0';
779 ival = va_arg(ap, int);
780 snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
783 uval = va_arg(ap, unsigned);
784 snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
787 /* this is a modifier that must be followed by u, lu, or d */
791 lval = va_arg(ap, long);
792 snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
796 ulval = va_arg(ap, unsigned long);
797 snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
799 else if ((ch == 'l') && (*src == 'u'))
801 unsigned long long lluval = va_arg(ap, unsigned long long);
802 snprintf(tempbuf, sizeof(tempbuf), "%llu", lluval);
807 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
808 loglevel = LOG_LEVEL_FATAL;
813 * Note that char paramaters are converted to int, so we need to
814 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
816 tempbuf[0] = (char) va_arg(ap, int);
820 format_string = va_arg(ap, char *);
821 if (format_string == NULL)
823 format_string = "[null]";
828 * Non-standard: Print a counted unterminated string,
829 * replacing unprintable bytes with their hex value.
830 * Takes 2 parameters: int length, const char * string.
832 ival = va_arg(ap, int);
834 sval = va_arg(ap, char *);
835 assert(sval != NULL);
837 while ((ival-- > 0) && (length < log_buffer_size - 6))
839 if (isprint((int)*sval) && (*sval != '\\'))
841 outbuf[length++] = *sval;
842 outbuf[length] = '\0';
846 int ret = snprintf(outbuf + length,
847 log_buffer_size - length - 2, "\\x%.2x", (unsigned char)*sval);
854 * XXX: In case of printable characters at the end of
855 * the %N string, we're not using the whole buffer.
857 format_string = (length < log_buffer_size - 6) ? "" : "[too long]";
860 /* Non-standard: Print error code from errno */
862 ival = WSAGetLastError();
863 format_string = w32_socket_strerr(ival, tempbuf);
868 format_string = os2_socket_strerr(ival, tempbuf);
873 format_string = strerror(ival);
875 #else /* ifndef _WIN32 */
878 format_string = strerror(ival);
879 #else /* ifndef HAVE_STRERROR */
880 format_string = NULL;
881 #endif /* ndef HAVE_STRERROR */
884 snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
886 #endif /* ndef _WIN32 */
889 /* Non-standard: Print a Common Log File timestamp */
890 get_clf_timestamp(tempbuf, sizeof(tempbuf));
893 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
894 loglevel = LOG_LEVEL_FATAL;
898 assert(length < log_buffer_size);
899 length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
901 if (length >= log_buffer_size-2)
903 static const char warning[] = "... [too long, truncated]";
905 length = log_buffer_size - sizeof(warning) - 1;
906 length += strlcpy(outbuf + length, warning, log_buffer_size - length);
907 assert(length < log_buffer_size);
913 /* done with var. args */
916 assert(length < log_buffer_size);
917 length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
919 /* Some sanity checks */
920 if ((length >= log_buffer_size)
921 || (outbuf[log_buffer_size-1] != '\0')
922 || (outbuf[log_buffer_size] != '\0')
925 /* Repeat as assertions */
926 assert(length < log_buffer_size);
927 assert(outbuf[log_buffer_size-1] == '\0');
929 * outbuf's real size is log_buffer_size+1,
930 * so while this looks like an off-by-one,
931 * we're only checking our paranoia byte.
933 assert(outbuf[log_buffer_size] == '\0');
935 snprintf(outbuf, log_buffer_size,
936 "%s %08lx Fatal error: log_error()'s sanity checks failed."
937 "length: %d. Exiting.",
938 timestamp, thread_id, (int)length);
939 loglevel = LOG_LEVEL_FATAL;
944 * On Windows this is acceptable in case
945 * we are logging to the GUI window only.
947 assert(NULL != logfp);
950 if (loglevel == LOG_LEVEL_FATAL)
952 fatal_error(outbuf_save);
957 fputs(outbuf_save, logfp);
960 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
961 /* Write to display */
962 LogPutString(outbuf_save);
963 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
970 /*********************************************************************
972 * Function : jb_err_to_string
974 * Description : Translates JB_ERR_FOO codes into strings.
976 * XXX: the type of error codes is jb_err
977 * but the typedef'inition is currently not
978 * visible to all files that include errlog.h.
981 * 1 : jb_error = a valid jb_err code
983 * Returns : A string with the jb_err translation
985 *********************************************************************/
986 const char *jb_err_to_string(int jb_error)
991 return "Success, no error";
993 return "Out of memory";
994 case JB_ERR_CGI_PARAMS:
995 return "Missing or corrupt CGI parameters";
997 return "Error opening, reading or writing a file";
999 return "Parse error";
1000 case JB_ERR_MODIFIED:
1001 return "File has been modified outside of the CGI actions editor.";
1002 case JB_ERR_COMPRESS:
1003 return "(De)compression failure";
1006 return "Internal error";
1010 /*********************************************************************
1012 * Function : w32_socket_strerr
1014 * Description : Translate the return value from WSAGetLastError()
1018 * 1 : errcode = The return value from WSAGetLastError().
1019 * 2 : tmp_buf = A temporary buffer that might be used to
1022 * Returns : String representing the error code. This may be
1023 * a global string constant or a string stored in
1026 *********************************************************************/
1027 static char *w32_socket_strerr(int errcode, char *tmp_buf)
1029 #define TEXT_FOR_ERROR(code,text) \
1030 if (errcode == code) \
1032 return #code " - " text; \
1035 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
1036 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
1037 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
1038 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
1039 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
1040 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
1041 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
1042 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
1043 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
1044 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
1045 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
1046 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
1047 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
1048 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
1049 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
1050 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
1051 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
1052 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
1053 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
1054 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
1055 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
1056 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
1057 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
1058 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
1059 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
1060 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
1061 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
1062 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
1063 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
1064 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
1065 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
1066 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
1067 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
1068 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
1069 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
1070 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
1071 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
1072 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
1073 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
1074 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
1075 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
1076 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
1078 * The following error codes are documented in the Microsoft WinSock
1079 * reference guide, but don't actually exist.
1081 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
1082 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
1083 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
1084 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
1085 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
1086 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
1087 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
1088 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
1089 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
1090 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
1093 sprintf(tmp_buf, "(error number %d)", errcode);
1096 #endif /* def _WIN32 */
1100 /*********************************************************************
1102 * Function : os2_socket_strerr
1104 * Description : Translate the return value from sock_errno()
1108 * 1 : errcode = The return value from sock_errno().
1109 * 2 : tmp_buf = A temporary buffer that might be used to
1112 * Returns : String representing the error code. This may be
1113 * a global string constant or a string stored in
1116 *********************************************************************/
1117 static char *os2_socket_strerr(int errcode, char *tmp_buf)
1119 #define TEXT_FOR_ERROR(code,text) \
1120 if (errcode == code) \
1122 return #code " - " text; \
1125 TEXT_FOR_ERROR(SOCEPERM , "Not owner.")
1126 TEXT_FOR_ERROR(SOCESRCH , "No such process.")
1127 TEXT_FOR_ERROR(SOCEINTR , "Interrupted system call.")
1128 TEXT_FOR_ERROR(SOCENXIO , "No such device or address.")
1129 TEXT_FOR_ERROR(SOCEBADF , "Bad file number.")
1130 TEXT_FOR_ERROR(SOCEACCES , "Permission denied.")
1131 TEXT_FOR_ERROR(SOCEFAULT , "Bad address.")
1132 TEXT_FOR_ERROR(SOCEINVAL , "Invalid argument.")
1133 TEXT_FOR_ERROR(SOCEMFILE , "Too many open files.")
1134 TEXT_FOR_ERROR(SOCEPIPE , "Broken pipe.")
1135 TEXT_FOR_ERROR(SOCEWOULDBLOCK , "Operation would block.")
1136 TEXT_FOR_ERROR(SOCEINPROGRESS , "Operation now in progress.")
1137 TEXT_FOR_ERROR(SOCEALREADY , "Operation already in progress.")
1138 TEXT_FOR_ERROR(SOCENOTSOCK , "Socket operation on non-socket.")
1139 TEXT_FOR_ERROR(SOCEDESTADDRREQ , "Destination address required.")
1140 TEXT_FOR_ERROR(SOCEMSGSIZE , "Message too long.")
1141 TEXT_FOR_ERROR(SOCEPROTOTYPE , "Protocol wrong type for socket.")
1142 TEXT_FOR_ERROR(SOCENOPROTOOPT , "Protocol not available.")
1143 TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
1144 TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
1145 TEXT_FOR_ERROR(SOCEOPNOTSUPP , "Operation not supported.")
1146 TEXT_FOR_ERROR(SOCEPFNOSUPPORT , "Protocol family not supported.")
1147 TEXT_FOR_ERROR(SOCEAFNOSUPPORT , "Address family not supported by protocol family.")
1148 TEXT_FOR_ERROR(SOCEADDRINUSE , "Address already in use.")
1149 TEXT_FOR_ERROR(SOCEADDRNOTAVAIL , "Can't assign requested address.")
1150 TEXT_FOR_ERROR(SOCENETDOWN , "Network is down.")
1151 TEXT_FOR_ERROR(SOCENETUNREACH , "Network is unreachable.")
1152 TEXT_FOR_ERROR(SOCENETRESET , "Network dropped connection on reset.")
1153 TEXT_FOR_ERROR(SOCECONNABORTED , "Software caused connection abort.")
1154 TEXT_FOR_ERROR(SOCECONNRESET , "Connection reset by peer.")
1155 TEXT_FOR_ERROR(SOCENOBUFS , "No buffer space available.")
1156 TEXT_FOR_ERROR(SOCEISCONN , "Socket is already connected.")
1157 TEXT_FOR_ERROR(SOCENOTCONN , "Socket is not connected.")
1158 TEXT_FOR_ERROR(SOCESHUTDOWN , "Can't send after socket shutdown.")
1159 TEXT_FOR_ERROR(SOCETOOMANYREFS , "Too many references: can't splice.")
1160 TEXT_FOR_ERROR(SOCETIMEDOUT , "Operation timed out.")
1161 TEXT_FOR_ERROR(SOCECONNREFUSED , "Connection refused.")
1162 TEXT_FOR_ERROR(SOCELOOP , "Too many levels of symbolic links.")
1163 TEXT_FOR_ERROR(SOCENAMETOOLONG , "File name too long.")
1164 TEXT_FOR_ERROR(SOCEHOSTDOWN , "Host is down.")
1165 TEXT_FOR_ERROR(SOCEHOSTUNREACH , "No route to host.")
1166 TEXT_FOR_ERROR(SOCENOTEMPTY , "Directory not empty.")
1167 TEXT_FOR_ERROR(SOCEOS2ERR , "OS/2 Error.")
1169 sprintf(tmp_buf, "(error number %d)", errcode);
1172 #endif /* def __OS2__ */