1 const char errlog_rcs[] = "$Id: errlog.c,v 1.105 2010/07/26 11:20:53 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-2010 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 */
79 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
83 * LOG_LEVEL_FATAL cannot be turned off. (There are
84 * some exceptional situations where we need to get a
85 * message to the user).
87 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
89 /* where to log (default: stderr) */
90 static FILE *logfp = NULL;
92 /* logging detail level. XXX: stupid name. */
93 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR);
95 /* static functions */
96 static void fatal_error(const char * error_message);
98 static char *w32_socket_strerr(int errcode, char *tmp_buf);
101 static char *os2_socket_strerr(int errcode, char *tmp_buf);
104 #ifdef MUTEX_LOCKS_AVAILABLE
105 static inline void lock_logfile(void)
107 privoxy_mutex_lock(&log_mutex);
109 static inline void unlock_logfile(void)
111 privoxy_mutex_unlock(&log_mutex);
113 static inline void lock_loginit(void)
115 privoxy_mutex_lock(&log_init_mutex);
117 static inline void unlock_loginit(void)
119 privoxy_mutex_unlock(&log_init_mutex);
121 #else /* ! MUTEX_LOCKS_AVAILABLE */
123 * FIXME we need a cross-platform locking mechanism.
124 * The locking/unlocking functions below should be
125 * fleshed out for non-pthread implementations.
127 static inline void lock_logfile() {}
128 static inline void unlock_logfile() {}
129 static inline void lock_loginit() {}
130 static inline void unlock_loginit() {}
133 /*********************************************************************
135 * Function : fatal_error
137 * Description : Displays a fatal error to standard error (or, on
138 * a WIN32 GUI, to a dialog box), and exits Privoxy
139 * with status code 1.
142 * 1 : error_message = The error message to display.
144 * Returns : Does not return.
146 *********************************************************************/
147 static void fatal_error(const char *error_message)
149 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
150 /* Skip timestamp and thread id for the message box. */
151 const char *box_message = strstr(error_message, "Fatal error");
152 if (NULL == box_message)
154 /* Shouldn't happen but ... */
155 box_message = error_message;
157 MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
158 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
160 /* Cleanup - remove taskbar icon etc. */
162 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
166 fputs(error_message, logfp);
180 /*********************************************************************
182 * Function : show_version
184 * Description : Logs the Privoxy version and the program name.
187 * 1 : prog_name = The program name.
191 *********************************************************************/
192 void show_version(const char *prog_name)
194 log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
195 if (prog_name != NULL)
197 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
202 /*********************************************************************
204 * Function : init_log_module
206 * Description : Initializes the logging module to log to stderr.
207 * Can only be called while stderr hasn't been closed
208 * yet and is only supposed to be called once.
211 * 1 : prog_name = The program name.
215 *********************************************************************/
216 void init_log_module(void)
221 set_debug_level(debug);
225 /*********************************************************************
227 * Function : set_debug_level
229 * Description : Sets the debug level to the provided value
230 * plus LOG_LEVEL_MINIMUM.
232 * XXX: we should only use the LOG_LEVEL_MINIMUM
233 * until the first time the configuration file has
236 * Parameters : 1: debug_level = The debug level to set.
240 *********************************************************************/
241 void set_debug_level(int debug_level)
243 debug = debug_level | LOG_LEVEL_MINIMUM;
247 /*********************************************************************
249 * Function : disable_logging
251 * Description : Disables logging.
257 *********************************************************************/
258 void disable_logging(void)
262 log_error(LOG_LEVEL_INFO,
263 "No logfile configured. Please enable it before reporting any problems.");
272 /*********************************************************************
274 * Function : init_error_log
276 * Description : Initializes the logging module to log to a file.
278 * XXX: should be renamed.
281 * 1 : prog_name = The program name.
282 * 2 : logfname = The logfile to (re)open.
286 *********************************************************************/
287 void init_error_log(const char *prog_name, const char *logfname)
291 assert(NULL != logfname);
295 if ((logfp != NULL) && (logfp != stderr))
297 log_error(LOG_LEVEL_INFO, "(Re-)Opening logfile \'%s\'", logfname);
300 /* set the designated log file */
301 fp = fopen(logfname, "a");
302 if ((NULL == fp) && (logfp != NULL))
305 * Some platforms (like OS/2) don't allow us to open
306 * the same file twice, therefore we give it another
307 * shot after closing the old file descriptor first.
309 * We don't do it right away because it prevents us
310 * from logging the "can't open logfile" message to
313 * XXX: this is a lame workaround and once the next
314 * release is out we should stop bothering reopening
315 * the logfile unless we have to.
317 * Currently we reopen it every time the config file
318 * has been reloaded, but actually we only have to
319 * reopen it if the file name changed or if the
320 * configuration reload was caused by a SIGHUP.
322 log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. "
323 "Retrying after closing the old file descriptor first. If that "
324 "doesn't work, Privoxy will exit without being able to log a message.",
330 fp = fopen(logfname, "a");
335 log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
338 /* set logging to be completely unbuffered */
347 if (daemon_mode && (logfp == stderr))
349 if (dup2(1, 2) == -1)
352 * We only use fatal_error() to clear the pid
353 * file and to exit. Given that stderr has just
354 * been closed, the user will not see the error
357 fatal_error("Failed to reserve fd 2.");
364 show_version(prog_name);
368 } /* init_error_log */
371 /*********************************************************************
373 * Function : get_thread_id
375 * Description : Returns a number that is different for each thread.
377 * XXX: Should be moved elsewhere (miscutil.c?)
381 * Returns : thread_id
383 *********************************************************************/
384 static long get_thread_id(void)
386 long this_thread = 1; /* was: pthread_t this_thread;*/
390 APIRET ulrc; /* XXX: I have no clue what this does */
393 /* FIXME get current thread id */
394 #ifdef FEATURE_PTHREAD
395 this_thread = (long)pthread_self();
398 * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
399 * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
400 * pthread_t is supposed to be opaque... but it's fairly random, though, so
401 * we make it mostly presentable.
403 this_thread = abs(this_thread % 1000);
404 #endif /* def __MACH__ */
405 #elif defined(_WIN32)
406 this_thread = GetCurrentThreadId();
407 #elif defined(__OS2__)
408 ulrc = DosGetInfoBlocks(&ptib, NULL);
410 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
411 #endif /* def FEATURE_PTHREAD */
417 /*********************************************************************
419 * Function : get_log_timestamp
421 * Description : Generates the time stamp for the log message prefix.
424 * 1 : buffer = Storage buffer
425 * 2 : buffer_size = Size of storage buffer
427 * Returns : Number of written characters or 0 for error.
429 *********************************************************************/
430 static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
435 struct timeval tv_now; /* XXX: stupid name */
437 int msecs_length = 0;
439 gettimeofday(&tv_now, NULL);
440 msecs = tv_now.tv_usec / 1000;
443 #ifdef HAVE_LOCALTIME_R
444 tm_now = *localtime_r(&now, &tm_now);
445 #elif defined(MUTEX_LOCKS_AVAILABLE)
446 privoxy_mutex_lock(&localtime_mutex);
447 tm_now = *localtime(&now);
448 privoxy_mutex_unlock(&localtime_mutex);
450 tm_now = *localtime(&now);
453 length = strftime(buffer, buffer_size, "%b %d %H:%M:%S", &tm_now);
454 if (length > (size_t)0)
456 msecs_length = snprintf(buffer+length, buffer_size - length, ".%.3ld", msecs);
458 if (msecs_length > 0)
460 length += (size_t)msecs_length;
471 /*********************************************************************
473 * Function : get_clf_timestamp
475 * Description : Generates a Common Log Format time string.
478 * 1 : buffer = Storage buffer
479 * 2 : buffer_size = Size of storage buffer
481 * Returns : Number of written characters or 0 for error.
483 *********************************************************************/
484 static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
487 * Complex because not all OSs have tm_gmtoff or
488 * the %z field in strftime()
493 #ifdef HAVE_LOCALTIME_R
502 gmt = *gmtime_r(&now, &gmt);
503 #elif defined(MUTEX_LOCKS_AVAILABLE)
504 privoxy_mutex_lock(&gmtime_mutex);
506 privoxy_mutex_unlock(&gmtime_mutex);
510 #ifdef HAVE_LOCALTIME_R
511 tm_now = localtime_r(&now, &dummy);
512 #elif defined(MUTEX_LOCKS_AVAILABLE)
513 privoxy_mutex_lock(&localtime_mutex);
514 tm_now = localtime(&now);
515 privoxy_mutex_unlock(&localtime_mutex);
517 tm_now = localtime(&now);
519 days = tm_now->tm_yday - gmt.tm_yday;
520 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
521 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
523 length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
525 if (length > (size_t)0)
527 tz_length = snprintf(buffer+length, buffer_size-length,
528 "%+03d%02d", mins / 60, abs(mins) % 60);
532 length += (size_t)tz_length;
543 /*********************************************************************
545 * Function : get_log_level_string
547 * Description : Translates a numerical loglevel into a string.
550 * 1 : loglevel = LOG_LEVEL_FOO
552 * Returns : Log level string.
554 *********************************************************************/
555 static inline const char *get_log_level_string(int loglevel)
557 char *log_level_string = NULL;
559 assert(0 < loglevel);
563 case LOG_LEVEL_ERROR:
564 log_level_string = "Error";
566 case LOG_LEVEL_FATAL:
567 log_level_string = "Fatal error";
570 log_level_string = "Request";
572 case LOG_LEVEL_CONNECT:
573 log_level_string = "Connect";
576 log_level_string = "Writing";
578 case LOG_LEVEL_HEADER:
579 log_level_string = "Header";
582 log_level_string = "Info";
584 case LOG_LEVEL_RE_FILTER:
585 log_level_string = "Re-Filter";
587 #ifdef FEATURE_FORCE_LOAD
588 case LOG_LEVEL_FORCE:
589 log_level_string = "Force";
591 #endif /* def FEATURE_FORCE_LOAD */
592 case LOG_LEVEL_REDIRECTS:
593 log_level_string = "Redirect";
595 case LOG_LEVEL_DEANIMATE:
596 log_level_string = "Gif-Deanimate";
598 case LOG_LEVEL_CRUNCH:
599 log_level_string = "Crunch";
602 log_level_string = "CGI";
605 log_level_string = "Unknown log level";
608 assert(NULL != log_level_string);
610 return log_level_string;
614 /*********************************************************************
616 * Function : log_error
618 * Description : This is the error-reporting and logging function.
621 * 1 : loglevel = the type of message to be logged
622 * 2 : fmt = the main string we want logged, printf-like
623 * 3 : ... = arguments to be inserted in fmt (printf-like).
627 *********************************************************************/
628 void log_error(int loglevel, const char *fmt, ...)
632 static char *outbuf_save = NULL;
633 char tempbuf[BUFFER_SIZE];
635 const char * src = fmt;
639 * XXX: Make this a config option,
640 * why else do we allocate instead of using
643 size_t log_buffer_size = BUFFER_SIZE;
645 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
647 * Irrespective of debug setting, a GET/POST/CONNECT makes
648 * the taskbar icon animate. (There is an option to disable
649 * this but checking that is handled inside LogShowActivity()).
651 if ((loglevel == LOG_LEVEL_GPC) || (loglevel == LOG_LEVEL_CRUNCH))
655 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
658 * verify that the loglevel applies to current
659 * settings and that logging is enabled.
660 * Bail out otherwise.
662 if ((0 == (loglevel & debug))
668 if (loglevel == LOG_LEVEL_FATAL)
670 fatal_error("Fatal error. You're not supposed to"
671 "see this message. Please file a bug report.");
676 thread_id = get_thread_id();
677 get_log_timestamp(timestamp, sizeof(timestamp));
679 /* protect the whole function because of the static buffer (outbuf) */
682 if (NULL == outbuf_save)
684 outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
685 if (NULL == outbuf_save)
687 snprintf(tempbuf, sizeof(tempbuf),
688 "%s %08lx Fatal error: Out of memory in log_error().",
689 timestamp, thread_id);
690 fatal_error(tempbuf); /* Exit */
694 outbuf = outbuf_save;
697 * Memsetting the whole buffer to zero (in theory)
698 * makes things easier later on.
700 memset(outbuf, 0, log_buffer_size);
702 /* Add prefix for everything but Common Log Format messages */
703 if (loglevel != LOG_LEVEL_CLF)
705 length = (size_t)snprintf(outbuf, log_buffer_size, "%s %08lx %s: ",
706 timestamp, thread_id, get_log_level_string(loglevel));
709 /* get ready to scan var. args. */
712 /* build formatted message from fmt and var-args */
713 while ((*src) && (length < log_buffer_size-2))
715 const char *sval = NULL; /* %N string */
716 int ival; /* %N string length or an error code */
717 unsigned uval; /* %u value */
718 long lval; /* %l value */
719 unsigned long ulval; /* %ul value */
721 const char *format_string = tempbuf;
726 outbuf[length++] = ch;
728 * XXX: Only necessary on platforms where multiple threads
729 * can write to the buffer at the same time because we
730 * don't support mutexes (OS/2 for example).
732 outbuf[length] = '\0';
735 outbuf[length] = '\0';
743 ival = va_arg( ap, int );
744 snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
747 uval = va_arg( ap, unsigned );
748 snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
751 /* this is a modifier that must be followed by u, lu, or d */
755 lval = va_arg( ap, long );
756 snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
760 ulval = va_arg( ap, unsigned long );
761 snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
763 else if ((ch == 'l') && (*src == 'u'))
765 unsigned long long lluval = va_arg(ap, unsigned long long);
766 snprintf(tempbuf, sizeof(tempbuf), "%llu", lluval);
771 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
772 loglevel = LOG_LEVEL_FATAL;
777 * Note that char paramaters are converted to int, so we need to
778 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
780 tempbuf[0] = (char) va_arg(ap, int);
784 format_string = va_arg(ap, char *);
785 if (format_string == NULL)
787 format_string = "[null]";
792 * Non-standard: Print a counted unterminated string,
793 * replacing unprintable bytes with their hex value.
794 * Takes 2 parameters: int length, const char * string.
796 ival = va_arg(ap, int);
798 sval = va_arg(ap, char *);
799 assert(sval != NULL);
801 while ((ival-- > 0) && (length < log_buffer_size - 6))
803 if (isprint((int)*sval) && (*sval != '\\'))
805 outbuf[length++] = *sval;
806 outbuf[length] = '\0';
810 snprintf(outbuf + length, log_buffer_size - length - 2,
811 "\\x%.2x", (int)*sval);
817 * XXX: In case of printable characters at the end of
818 * the %N string, we're not using the whole buffer.
820 format_string = (length < log_buffer_size - 6) ? "" : "[too long]";
823 /* Non-standard: Print error code from errno */
825 ival = WSAGetLastError();
826 format_string = w32_socket_strerr(ival, tempbuf);
831 format_string = os2_socket_strerr(ival, tempbuf);
836 format_string = strerror(ival);
838 #else /* ifndef _WIN32 */
841 format_string = strerror(ival);
842 #else /* ifndef HAVE_STRERROR */
843 format_string = NULL;
844 #endif /* ndef HAVE_STRERROR */
847 snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
849 #endif /* ndef _WIN32 */
852 /* Non-standard: Print a Common Log File timestamp */
853 get_clf_timestamp(tempbuf, sizeof(tempbuf));
856 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
857 loglevel = LOG_LEVEL_FATAL;
861 assert(length < log_buffer_size);
862 length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
864 if (length >= log_buffer_size-2)
866 static const char warning[] = "... [too long, truncated]";
868 length = log_buffer_size - sizeof(warning) - 1;
869 length += strlcpy(outbuf + length, warning, log_buffer_size - length);
870 assert(length < log_buffer_size);
876 /* done with var. args */
879 assert(length < log_buffer_size);
880 length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
882 /* Some sanity checks */
883 if ((length >= log_buffer_size)
884 || (outbuf[log_buffer_size-1] != '\0')
885 || (outbuf[log_buffer_size] != '\0')
888 /* Repeat as assertions */
889 assert(length < log_buffer_size);
890 assert(outbuf[log_buffer_size-1] == '\0');
892 * outbuf's real size is log_buffer_size+1,
893 * so while this looks like an off-by-one,
894 * we're only checking our paranoia byte.
896 assert(outbuf[log_buffer_size] == '\0');
898 snprintf(outbuf, log_buffer_size,
899 "%s %08lx Fatal error: log_error()'s sanity checks failed."
900 "length: %d. Exiting.",
901 timestamp, thread_id, (int)length);
902 loglevel = LOG_LEVEL_FATAL;
907 * On Windows this is acceptable in case
908 * we are logging to the GUI window only.
910 assert(NULL != logfp);
913 if (loglevel == LOG_LEVEL_FATAL)
915 fatal_error(outbuf_save);
920 fputs(outbuf_save, logfp);
923 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
924 /* Write to display */
925 LogPutString(outbuf_save);
926 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
933 /*********************************************************************
935 * Function : jb_err_to_string
937 * Description : Translates JB_ERR_FOO codes into strings.
939 * XXX: the type of error codes is jb_err
940 * but the typedef'inition is currently not
941 * visible to all files that include errlog.h.
944 * 1 : jb_error = a valid jb_err code
946 * Returns : A string with the jb_err translation
948 *********************************************************************/
949 const char *jb_err_to_string(int jb_error)
954 return "Success, no error";
956 return "Out of memory";
957 case JB_ERR_CGI_PARAMS:
958 return "Missing or corrupt CGI parameters";
960 return "Error opening, reading or writing a file";
962 return "Parse error";
963 case JB_ERR_MODIFIED:
964 return "File has been modified outside of the CGI actions editor.";
965 case JB_ERR_COMPRESS:
966 return "(De)compression failure";
969 return "Unknown error";
972 return "Internal error";
976 /*********************************************************************
978 * Function : w32_socket_strerr
980 * Description : Translate the return value from WSAGetLastError()
984 * 1 : errcode = The return value from WSAGetLastError().
985 * 2 : tmp_buf = A temporary buffer that might be used to
988 * Returns : String representing the error code. This may be
989 * a global string constant or a string stored in
992 *********************************************************************/
993 static char *w32_socket_strerr(int errcode, char *tmp_buf)
995 #define TEXT_FOR_ERROR(code,text) \
996 if (errcode == code) \
998 return #code " - " text; \
1001 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
1002 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
1003 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
1004 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
1005 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
1006 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
1007 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
1008 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
1009 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
1010 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
1011 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
1012 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
1013 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
1014 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
1015 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
1016 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
1017 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
1018 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
1019 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
1020 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
1021 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
1022 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
1023 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
1024 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
1025 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
1026 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
1027 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
1028 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
1029 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
1030 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
1031 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
1032 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
1033 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
1034 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
1035 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
1036 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
1037 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
1038 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
1039 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
1040 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
1041 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
1042 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
1044 * The following error codes are documented in the Microsoft WinSock
1045 * reference guide, but don't actually exist.
1047 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
1048 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
1049 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
1050 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
1051 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
1052 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
1053 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
1054 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
1055 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
1056 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
1059 sprintf(tmp_buf, "(error number %d)", errcode);
1062 #endif /* def _WIN32 */
1066 /*********************************************************************
1068 * Function : os2_socket_strerr
1070 * Description : Translate the return value from sock_errno()
1074 * 1 : errcode = The return value from sock_errno().
1075 * 2 : tmp_buf = A temporary buffer that might be used to
1078 * Returns : String representing the error code. This may be
1079 * a global string constant or a string stored in
1082 *********************************************************************/
1083 static char *os2_socket_strerr(int errcode, char *tmp_buf)
1085 #define TEXT_FOR_ERROR(code,text) \
1086 if (errcode == code) \
1088 return #code " - " text; \
1091 TEXT_FOR_ERROR(SOCEPERM , "Not owner.")
1092 TEXT_FOR_ERROR(SOCESRCH , "No such process.")
1093 TEXT_FOR_ERROR(SOCEINTR , "Interrupted system call.")
1094 TEXT_FOR_ERROR(SOCENXIO , "No such device or address.")
1095 TEXT_FOR_ERROR(SOCEBADF , "Bad file number.")
1096 TEXT_FOR_ERROR(SOCEACCES , "Permission denied.")
1097 TEXT_FOR_ERROR(SOCEFAULT , "Bad address.")
1098 TEXT_FOR_ERROR(SOCEINVAL , "Invalid argument.")
1099 TEXT_FOR_ERROR(SOCEMFILE , "Too many open files.")
1100 TEXT_FOR_ERROR(SOCEPIPE , "Broken pipe.")
1101 TEXT_FOR_ERROR(SOCEWOULDBLOCK , "Operation would block.")
1102 TEXT_FOR_ERROR(SOCEINPROGRESS , "Operation now in progress.")
1103 TEXT_FOR_ERROR(SOCEALREADY , "Operation already in progress.")
1104 TEXT_FOR_ERROR(SOCENOTSOCK , "Socket operation on non-socket.")
1105 TEXT_FOR_ERROR(SOCEDESTADDRREQ , "Destination address required.")
1106 TEXT_FOR_ERROR(SOCEMSGSIZE , "Message too long.")
1107 TEXT_FOR_ERROR(SOCEPROTOTYPE , "Protocol wrong type for socket.")
1108 TEXT_FOR_ERROR(SOCENOPROTOOPT , "Protocol not available.")
1109 TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
1110 TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
1111 TEXT_FOR_ERROR(SOCEOPNOTSUPP , "Operation not supported.")
1112 TEXT_FOR_ERROR(SOCEPFNOSUPPORT , "Protocol family not supported.")
1113 TEXT_FOR_ERROR(SOCEAFNOSUPPORT , "Address family not supported by protocol family.")
1114 TEXT_FOR_ERROR(SOCEADDRINUSE , "Address already in use.")
1115 TEXT_FOR_ERROR(SOCEADDRNOTAVAIL , "Can't assign requested address.")
1116 TEXT_FOR_ERROR(SOCENETDOWN , "Network is down.")
1117 TEXT_FOR_ERROR(SOCENETUNREACH , "Network is unreachable.")
1118 TEXT_FOR_ERROR(SOCENETRESET , "Network dropped connection on reset.")
1119 TEXT_FOR_ERROR(SOCECONNABORTED , "Software caused connection abort.")
1120 TEXT_FOR_ERROR(SOCECONNRESET , "Connection reset by peer.")
1121 TEXT_FOR_ERROR(SOCENOBUFS , "No buffer space available.")
1122 TEXT_FOR_ERROR(SOCEISCONN , "Socket is already connected.")
1123 TEXT_FOR_ERROR(SOCENOTCONN , "Socket is not connected.")
1124 TEXT_FOR_ERROR(SOCESHUTDOWN , "Can't send after socket shutdown.")
1125 TEXT_FOR_ERROR(SOCETOOMANYREFS , "Too many references: can't splice.")
1126 TEXT_FOR_ERROR(SOCETIMEDOUT , "Operation timed out.")
1127 TEXT_FOR_ERROR(SOCECONNREFUSED , "Connection refused.")
1128 TEXT_FOR_ERROR(SOCELOOP , "Too many levels of symbolic links.")
1129 TEXT_FOR_ERROR(SOCENAMETOOLONG , "File name too long.")
1130 TEXT_FOR_ERROR(SOCEHOSTDOWN , "Host is down.")
1131 TEXT_FOR_ERROR(SOCEHOSTUNREACH , "No route to host.")
1132 TEXT_FOR_ERROR(SOCENOTEMPTY , "Directory not empty.")
1133 TEXT_FOR_ERROR(SOCEOS2ERR , "OS/2 Error.")
1135 sprintf(tmp_buf, "(error number %d)", errcode);
1138 #endif /* def __OS2__ */