1 const char errlog_rcs[] = "$Id: errlog.c,v 1.99 2009/12/26 11:34:01 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-2009 the SourceForge
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 *********************************************************************/
45 /* For gettimeofday() */
48 #if !defined(_WIN32) && !defined(__OS2__)
50 #endif /* !defined(_WIN32) && !defined(__OS2__) */
62 #endif /* ndef _WIN_CONSOLE */
63 #endif /* def _WIN32 */
65 #define inline __inline
66 #endif /* def _MSC_VER */
69 #include <sys/socket.h> /* For sock_errno */
78 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
82 * LOG_LEVEL_FATAL cannot be turned off. (There are
83 * some exceptional situations where we need to get a
84 * message to the user).
86 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
88 /* where to log (default: stderr) */
89 static FILE *logfp = NULL;
91 /* logging detail level. XXX: stupid name. */
92 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR);
94 /* static functions */
95 static void fatal_error(const char * error_message);
97 static char *w32_socket_strerr(int errcode, char *tmp_buf);
100 static char *os2_socket_strerr(int errcode, char *tmp_buf);
103 #ifdef MUTEX_LOCKS_AVAILABLE
104 static inline void lock_logfile(void)
106 privoxy_mutex_lock(&log_mutex);
108 static inline void unlock_logfile(void)
110 privoxy_mutex_unlock(&log_mutex);
112 static inline void lock_loginit(void)
114 privoxy_mutex_lock(&log_init_mutex);
116 static inline void unlock_loginit(void)
118 privoxy_mutex_unlock(&log_init_mutex);
120 #else /* ! MUTEX_LOCKS_AVAILABLE */
122 * FIXME we need a cross-platform locking mechanism.
123 * The locking/unlocking functions below should be
124 * fleshed out for non-pthread implementations.
126 static inline void lock_logfile() {}
127 static inline void unlock_logfile() {}
128 static inline void lock_loginit() {}
129 static inline void unlock_loginit() {}
132 /*********************************************************************
134 * Function : fatal_error
136 * Description : Displays a fatal error to standard error (or, on
137 * a WIN32 GUI, to a dialog box), and exits Privoxy
138 * with status code 1.
141 * 1 : error_message = The error message to display.
143 * Returns : Does not return.
145 *********************************************************************/
146 static void fatal_error(const char *error_message)
148 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
149 /* Skip timestamp and thread id for the message box. */
150 const char *box_message = strstr(error_message, "Fatal error");
151 if (NULL == box_message)
153 /* Shouldn't happen but ... */
154 box_message = error_message;
156 MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
157 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
159 /* Cleanup - remove taskbar icon etc. */
161 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
165 fputs(error_message, logfp);
179 /*********************************************************************
181 * Function : show_version
183 * Description : Logs the Privoxy version and the program name.
186 * 1 : prog_name = The program name.
190 *********************************************************************/
191 void show_version(const char *prog_name)
193 log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
194 if (prog_name != NULL)
196 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
201 /*********************************************************************
203 * Function : init_log_module
205 * Description : Initializes the logging module to log to stderr.
206 * Can only be called while stderr hasn't been closed
207 * yet and is only supposed to be called once.
210 * 1 : prog_name = The program name.
214 *********************************************************************/
215 void init_log_module(void)
220 set_debug_level(debug);
224 /*********************************************************************
226 * Function : set_debug_level
228 * Description : Sets the debug level to the provided value
229 * plus LOG_LEVEL_MINIMUM.
231 * XXX: we should only use the LOG_LEVEL_MINIMUM
232 * until the first time the configuration file has
235 * Parameters : 1: debug_level = The debug level to set.
239 *********************************************************************/
240 void set_debug_level(int debug_level)
242 debug = debug_level | LOG_LEVEL_MINIMUM;
246 /*********************************************************************
248 * Function : disable_logging
250 * Description : Disables logging.
256 *********************************************************************/
257 void disable_logging(void)
261 log_error(LOG_LEVEL_INFO,
262 "No logfile configured. Please enable it before reporting any problems.");
271 /*********************************************************************
273 * Function : init_error_log
275 * Description : Initializes the logging module to log to a file.
277 * XXX: should be renamed.
280 * 1 : prog_name = The program name.
281 * 2 : logfname = The logfile to (re)open.
285 *********************************************************************/
286 void init_error_log(const char *prog_name, const char *logfname)
290 assert(NULL != logfname);
294 if ((logfp != NULL) && (logfp != stderr))
296 log_error(LOG_LEVEL_INFO, "(Re-)Opening logfile \'%s\'", logfname);
299 /* set the designated log file */
300 fp = fopen(logfname, "a");
301 if ((NULL == fp) && (logfp != NULL))
304 * Some platforms (like OS/2) don't allow us to open
305 * the same file twice, therefore we give it another
306 * shot after closing the old file descriptor first.
308 * We don't do it right away because it prevents us
309 * from logging the "can't open logfile" message to
312 * XXX: this is a lame workaround and once the next
313 * release is out we should stop bothering reopening
314 * the logfile unless we have to.
316 * Currently we reopen it every time the config file
317 * has been reloaded, but actually we only have to
318 * reopen it if the file name changed or if the
319 * configuration reloas was caused by a SIGHUP.
321 log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. "
322 "Retrying after closing the old file descriptor first. If that "
323 "doesn't work, Privoxy will exit without being able to log a message.",
329 fp = fopen(logfname, "a");
334 log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
337 /* set logging to be completely unbuffered */
346 if (daemon_mode && (logfp == stderr))
348 if (dup2(1, 2) == -1)
351 * We only use fatal_error() to clear the pid
352 * file and to exit. Given that stderr has just
353 * been closed, the user will not see the error
356 fatal_error("Failed to reserve fd 2.");
363 show_version(prog_name);
367 } /* init_error_log */
370 /*********************************************************************
372 * Function : get_thread_id
374 * Description : Returns a number that is different for each thread.
376 * XXX: Should be moved elsewhere (miscutil.c?)
380 * Returns : thread_id
382 *********************************************************************/
383 static long get_thread_id(void)
385 long this_thread = 1; /* was: pthread_t this_thread;*/
389 APIRET ulrc; /* XXX: I have no clue what this does */
392 /* FIXME get current thread id */
393 #ifdef FEATURE_PTHREAD
394 this_thread = (long)pthread_self();
397 * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
398 * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
399 * pthread_t is supposed to be opaque... but it's fairly random, though, so
400 * we make it mostly presentable.
402 this_thread = abs(this_thread % 1000);
403 #endif /* def __MACH__ */
404 #elif defined(_WIN32)
405 this_thread = GetCurrentThreadId();
406 #elif defined(__OS2__)
407 ulrc = DosGetInfoBlocks(&ptib, NULL);
409 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
410 #endif /* def FEATURE_PTHREAD */
416 /*********************************************************************
418 * Function : get_log_timestamp
420 * Description : Generates the time stamp for the log message prefix.
423 * 1 : buffer = Storage buffer
424 * 2 : buffer_size = Size of storage buffer
426 * Returns : Number of written characters or 0 for error.
428 *********************************************************************/
429 static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
434 struct timeval tv_now; /* XXX: stupid name */
436 int msecs_length = 0;
438 gettimeofday(&tv_now, NULL);
439 msecs = tv_now.tv_usec / 1000;
442 #ifdef HAVE_LOCALTIME_R
443 tm_now = *localtime_r(&now, &tm_now);
444 #elif defined(MUTEX_LOCKS_AVAILABLE)
445 privoxy_mutex_lock(&localtime_mutex);
446 tm_now = *localtime(&now);
447 privoxy_mutex_unlock(&localtime_mutex);
449 tm_now = *localtime(&now);
452 length = strftime(buffer, buffer_size, "%b %d %H:%M:%S", &tm_now);
453 if (length > (size_t)0)
455 msecs_length = snprintf(buffer+length, buffer_size - length, ".%.3ld", msecs);
457 if (msecs_length > 0)
459 length += (size_t)msecs_length;
470 /*********************************************************************
472 * Function : get_clf_timestamp
474 * Description : Generates a Common Log Format time string.
477 * 1 : buffer = Storage buffer
478 * 2 : buffer_size = Size of storage buffer
480 * Returns : Number of written characters or 0 for error.
482 *********************************************************************/
483 static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
486 * Complex because not all OSs have tm_gmtoff or
487 * the %z field in strftime()
492 #ifdef HAVE_LOCALTIME_R
501 gmt = *gmtime_r(&now, &gmt);
502 #elif defined(MUTEX_LOCKS_AVAILABLE)
503 privoxy_mutex_lock(&gmtime_mutex);
505 privoxy_mutex_unlock(&gmtime_mutex);
509 #ifdef HAVE_LOCALTIME_R
510 tm_now = localtime_r(&now, &dummy);
511 #elif defined(MUTEX_LOCKS_AVAILABLE)
512 privoxy_mutex_lock(&localtime_mutex);
513 tm_now = localtime(&now);
514 privoxy_mutex_unlock(&localtime_mutex);
516 tm_now = localtime(&now);
518 days = tm_now->tm_yday - gmt.tm_yday;
519 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
520 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
522 length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
524 if (length > (size_t)0)
526 tz_length = snprintf(buffer+length, buffer_size-length,
527 "%+03d%02d", mins / 60, abs(mins) % 60);
531 length += (size_t)tz_length;
542 /*********************************************************************
544 * Function : get_log_level_string
546 * Description : Translates a numerical loglevel into a string.
549 * 1 : loglevel = LOG_LEVEL_FOO
551 * Returns : Log level string.
553 *********************************************************************/
554 static inline const char *get_log_level_string(int loglevel)
556 char *log_level_string = NULL;
558 assert(0 < loglevel);
562 case LOG_LEVEL_ERROR:
563 log_level_string = "Error";
565 case LOG_LEVEL_FATAL:
566 log_level_string = "Fatal error";
569 log_level_string = "Request";
571 case LOG_LEVEL_CONNECT:
572 log_level_string = "Connect";
575 log_level_string = "Writing";
577 case LOG_LEVEL_HEADER:
578 log_level_string = "Header";
581 log_level_string = "Info";
583 case LOG_LEVEL_RE_FILTER:
584 log_level_string = "Re-Filter";
586 #ifdef FEATURE_FORCE_LOAD
587 case LOG_LEVEL_FORCE:
588 log_level_string = "Force";
590 #endif /* def FEATURE_FORCE_LOAD */
591 case LOG_LEVEL_REDIRECTS:
592 log_level_string = "Redirect";
594 case LOG_LEVEL_DEANIMATE:
595 log_level_string = "Gif-Deanimate";
597 case LOG_LEVEL_CRUNCH:
598 log_level_string = "Crunch";
601 log_level_string = "CGI";
604 log_level_string = "Unknown log level";
607 assert(NULL != log_level_string);
609 return log_level_string;
613 /*********************************************************************
615 * Function : log_error
617 * Description : This is the error-reporting and logging function.
620 * 1 : loglevel = the type of message to be logged
621 * 2 : fmt = the main string we want logged, printf-like
622 * 3 : ... = arguments to be inserted in fmt (printf-like).
626 *********************************************************************/
627 void log_error(int loglevel, const char *fmt, ...)
631 static char *outbuf_save = NULL;
632 char tempbuf[BUFFER_SIZE];
634 const char * src = fmt;
638 * XXX: Make this a config option,
639 * why else do we allocate instead of using
642 size_t log_buffer_size = BUFFER_SIZE;
644 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
646 * Irrespective of debug setting, a GET/POST/CONNECT makes
647 * the taskbar icon animate. (There is an option to disable
648 * this but checking that is handled inside LogShowActivity()).
650 if ((loglevel == LOG_LEVEL_GPC) || (loglevel == LOG_LEVEL_CRUNCH))
654 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
657 * verify that the loglevel applies to current
658 * settings and that logging is enabled.
659 * Bail out otherwise.
661 if ((0 == (loglevel & debug))
667 if (loglevel == LOG_LEVEL_FATAL)
669 fatal_error("Fatal error. You're not supposed to"
670 "see this message. Please file a bug report.");
675 thread_id = get_thread_id();
676 get_log_timestamp(timestamp, sizeof(timestamp));
678 /* protect the whole function because of the static buffer (outbuf) */
681 if (NULL == outbuf_save)
683 outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
684 if (NULL == outbuf_save)
686 snprintf(tempbuf, sizeof(tempbuf),
687 "%s %08lx Fatal error: Out of memory in log_error().",
688 timestamp, thread_id);
689 fatal_error(tempbuf); /* Exit */
693 outbuf = outbuf_save;
696 * Memsetting the whole buffer to zero (in theory)
697 * makes things easier later on.
699 memset(outbuf, 0, log_buffer_size);
701 /* Add prefix for everything but Common Log Format messages */
702 if (loglevel != LOG_LEVEL_CLF)
704 length = (size_t)snprintf(outbuf, log_buffer_size, "%s %08lx %s: ",
705 timestamp, thread_id, get_log_level_string(loglevel));
708 /* get ready to scan var. args. */
711 /* build formatted message from fmt and var-args */
712 while ((*src) && (length < log_buffer_size-2))
714 const char *sval = NULL; /* %N string */
715 int ival; /* %N string length or an error code */
716 unsigned uval; /* %u value */
717 long lval; /* %l value */
718 unsigned long ulval; /* %ul value */
720 const char *format_string = tempbuf;
725 outbuf[length++] = ch;
727 * XXX: Only necessary on platforms where multiple threads
728 * can write to the buffer at the same time because we
729 * don't support mutexes (OS/2 for example).
731 outbuf[length] = '\0';
734 outbuf[length] = '\0';
742 ival = va_arg( ap, int );
743 snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
746 uval = va_arg( ap, unsigned );
747 snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
750 /* this is a modifier that must be followed by u, lu, or d */
754 lval = va_arg( ap, long );
755 snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
759 ulval = va_arg( ap, unsigned long );
760 snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
762 else if ((ch == 'l') && (*src == 'u'))
764 unsigned long long lluval = va_arg(ap, unsigned long long);
765 snprintf(tempbuf, sizeof(tempbuf), "%llu", lluval);
770 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
771 loglevel = LOG_LEVEL_FATAL;
776 * Note that char paramaters are converted to int, so we need to
777 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
779 tempbuf[0] = (char) va_arg(ap, int);
783 format_string = va_arg(ap, char *);
784 if (format_string == NULL)
786 format_string = "[null]";
791 * Non-standard: Print a counted unterminated string.
792 * Takes 2 parameters: int length, const char * string.
794 ival = va_arg(ap, int);
795 sval = va_arg(ap, char *);
798 format_string = "[null]";
804 /* That's ok (but stupid) */
810 * That's not ok (and even more stupid)
813 format_string = "[counted string lenght < 0]";
816 else if ((size_t)ival >= sizeof(tempbuf))
819 * String is too long, copy as much as possible.
820 * It will be further truncated later.
822 memcpy(tempbuf, sval, sizeof(tempbuf)-1);
823 tempbuf[sizeof(tempbuf)-1] = '\0';
827 memcpy(tempbuf, sval, (size_t) ival);
828 tempbuf[ival] = '\0';
832 /* Non-standard: Print error code from errno */
834 ival = WSAGetLastError();
835 format_string = w32_socket_strerr(ival, tempbuf);
840 format_string = os2_socket_strerr(ival, tempbuf);
845 format_string = strerror(ival);
847 #else /* ifndef _WIN32 */
850 format_string = strerror(ival);
851 #else /* ifndef HAVE_STRERROR */
852 format_string = NULL;
853 #endif /* ndef HAVE_STRERROR */
856 snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
858 #endif /* ndef _WIN32 */
861 /* Non-standard: Print a Common Log File timestamp */
862 get_clf_timestamp(tempbuf, sizeof(tempbuf));
865 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
866 loglevel = LOG_LEVEL_FATAL;
870 assert(length < log_buffer_size);
871 length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
873 if (length >= log_buffer_size-2)
875 static char warning[] = "... [too long, truncated]";
877 length = log_buffer_size - sizeof(warning) - 1;
878 length += strlcpy(outbuf + length, warning, log_buffer_size - length);
879 assert(length < log_buffer_size);
885 /* done with var. args */
888 assert(length < log_buffer_size);
889 length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
891 /* Some sanity checks */
892 if ((length >= log_buffer_size)
893 || (outbuf[log_buffer_size-1] != '\0')
894 || (outbuf[log_buffer_size] != '\0')
897 /* Repeat as assertions */
898 assert(length < log_buffer_size);
899 assert(outbuf[log_buffer_size-1] == '\0');
901 * outbuf's real size is log_buffer_size+1,
902 * so while this looks like an off-by-one,
903 * we're only checking our paranoia byte.
905 assert(outbuf[log_buffer_size] == '\0');
907 snprintf(outbuf, log_buffer_size,
908 "%s %08lx Fatal error: log_error()'s sanity checks failed."
909 "length: %d. Exiting.",
910 timestamp, thread_id, (int)length);
911 loglevel = LOG_LEVEL_FATAL;
916 * On Windows this is acceptable in case
917 * we are logging to the GUI window only.
919 assert(NULL != logfp);
922 if (loglevel == LOG_LEVEL_FATAL)
924 fatal_error(outbuf_save);
929 fputs(outbuf_save, logfp);
932 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
933 /* Write to display */
934 LogPutString(outbuf_save);
935 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
942 /*********************************************************************
944 * Function : jb_err_to_string
946 * Description : Translates JB_ERR_FOO codes into strings.
948 * XXX: the type of error codes is jb_err
949 * but the typedef'inition is currently not
950 * visible to all files that include errlog.h.
953 * 1 : error = a valid jb_err code
955 * Returns : A string with the jb_err translation
957 *********************************************************************/
958 const char *jb_err_to_string(int error)
963 return "Success, no error";
965 return "Out of memory";
966 case JB_ERR_CGI_PARAMS:
967 return "Missing or corrupt CGI parameters";
969 return "Error opening, reading or writing a file";
971 return "Parse error";
972 case JB_ERR_MODIFIED:
973 return "File has been modified outside of the CGI actions editor.";
974 case JB_ERR_COMPRESS:
975 return "(De)compression failure";
978 return "Unknown error";
981 return "Internal error";
985 /*********************************************************************
987 * Function : w32_socket_strerr
989 * Description : Translate the return value from WSAGetLastError()
993 * 1 : errcode = The return value from WSAGetLastError().
994 * 2 : tmp_buf = A temporary buffer that might be used to
997 * Returns : String representing the error code. This may be
998 * a global string constant or a string stored in
1001 *********************************************************************/
1002 static char *w32_socket_strerr(int errcode, char *tmp_buf)
1004 #define TEXT_FOR_ERROR(code,text) \
1005 if (errcode == code) \
1007 return #code " - " text; \
1010 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
1011 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
1012 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
1013 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
1014 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
1015 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
1016 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
1017 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
1018 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
1019 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
1020 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
1021 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
1022 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
1023 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
1024 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
1025 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
1026 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
1027 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
1028 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
1029 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
1030 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
1031 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
1032 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
1033 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
1034 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
1035 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
1036 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
1037 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
1038 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
1039 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
1040 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
1041 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
1042 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
1043 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
1044 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
1045 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
1046 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
1047 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
1048 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
1049 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
1050 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
1051 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
1053 * The following error codes are documented in the Microsoft WinSock
1054 * reference guide, but don't actually exist.
1056 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
1057 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
1058 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
1059 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
1060 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
1061 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
1062 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
1063 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
1064 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
1065 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
1068 sprintf(tmp_buf, "(error number %d)", errcode);
1071 #endif /* def _WIN32 */
1075 /*********************************************************************
1077 * Function : os2_socket_strerr
1079 * Description : Translate the return value from sock_errno()
1083 * 1 : errcode = The return value from sock_errno().
1084 * 2 : tmp_buf = A temporary buffer that might be used to
1087 * Returns : String representing the error code. This may be
1088 * a global string constant or a string stored in
1091 *********************************************************************/
1092 static char *os2_socket_strerr(int errcode, char *tmp_buf)
1094 #define TEXT_FOR_ERROR(code,text) \
1095 if (errcode == code) \
1097 return #code " - " text; \
1100 TEXT_FOR_ERROR(SOCEPERM , "Not owner.")
1101 TEXT_FOR_ERROR(SOCESRCH , "No such process.")
1102 TEXT_FOR_ERROR(SOCEINTR , "Interrupted system call.")
1103 TEXT_FOR_ERROR(SOCENXIO , "No such device or address.")
1104 TEXT_FOR_ERROR(SOCEBADF , "Bad file number.")
1105 TEXT_FOR_ERROR(SOCEACCES , "Permission denied.")
1106 TEXT_FOR_ERROR(SOCEFAULT , "Bad address.")
1107 TEXT_FOR_ERROR(SOCEINVAL , "Invalid argument.")
1108 TEXT_FOR_ERROR(SOCEMFILE , "Too many open files.")
1109 TEXT_FOR_ERROR(SOCEPIPE , "Broken pipe.")
1110 TEXT_FOR_ERROR(SOCEWOULDBLOCK , "Operation would block.")
1111 TEXT_FOR_ERROR(SOCEINPROGRESS , "Operation now in progress.")
1112 TEXT_FOR_ERROR(SOCEALREADY , "Operation already in progress.")
1113 TEXT_FOR_ERROR(SOCENOTSOCK , "Socket operation on non-socket.")
1114 TEXT_FOR_ERROR(SOCEDESTADDRREQ , "Destination address required.")
1115 TEXT_FOR_ERROR(SOCEMSGSIZE , "Message too long.")
1116 TEXT_FOR_ERROR(SOCEPROTOTYPE , "Protocol wrong type for socket.")
1117 TEXT_FOR_ERROR(SOCENOPROTOOPT , "Protocol not available.")
1118 TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
1119 TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
1120 TEXT_FOR_ERROR(SOCEOPNOTSUPP , "Operation not supported.")
1121 TEXT_FOR_ERROR(SOCEPFNOSUPPORT , "Protocol family not supported.")
1122 TEXT_FOR_ERROR(SOCEAFNOSUPPORT , "Address family not supported by protocol family.")
1123 TEXT_FOR_ERROR(SOCEADDRINUSE , "Address already in use.")
1124 TEXT_FOR_ERROR(SOCEADDRNOTAVAIL , "Can't assign requested address.")
1125 TEXT_FOR_ERROR(SOCENETDOWN , "Network is down.")
1126 TEXT_FOR_ERROR(SOCENETUNREACH , "Network is unreachable.")
1127 TEXT_FOR_ERROR(SOCENETRESET , "Network dropped connection on reset.")
1128 TEXT_FOR_ERROR(SOCECONNABORTED , "Software caused connection abort.")
1129 TEXT_FOR_ERROR(SOCECONNRESET , "Connection reset by peer.")
1130 TEXT_FOR_ERROR(SOCENOBUFS , "No buffer space available.")
1131 TEXT_FOR_ERROR(SOCEISCONN , "Socket is already connected.")
1132 TEXT_FOR_ERROR(SOCENOTCONN , "Socket is not connected.")
1133 TEXT_FOR_ERROR(SOCESHUTDOWN , "Can't send after socket shutdown.")
1134 TEXT_FOR_ERROR(SOCETOOMANYREFS , "Too many references: can't splice.")
1135 TEXT_FOR_ERROR(SOCETIMEDOUT , "Operation timed out.")
1136 TEXT_FOR_ERROR(SOCECONNREFUSED , "Connection refused.")
1137 TEXT_FOR_ERROR(SOCELOOP , "Too many levels of symbolic links.")
1138 TEXT_FOR_ERROR(SOCENAMETOOLONG , "File name too long.")
1139 TEXT_FOR_ERROR(SOCEHOSTDOWN , "Host is down.")
1140 TEXT_FOR_ERROR(SOCEHOSTUNREACH , "No route to host.")
1141 TEXT_FOR_ERROR(SOCENOTEMPTY , "Directory not empty.")
1142 TEXT_FOR_ERROR(SOCEOS2ERR , "OS/2 Error.")
1144 sprintf(tmp_buf, "(error number %d)", errcode);
1147 #endif /* def __OS2__ */