1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $
5 * Purpose : Log errors to a designated destination in an elegant,
8 * Copyright : Written by and Copyright (C) 2001-2014 the
9 * Privoxy team. http://www.privoxy.org/
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 *********************************************************************/
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 */
77 #ifdef FEATURE_EXTERNAL_FILTERS
78 #include "jbsockets.h"
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)
150 fputs(error_message, logfp);
153 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
155 /* Skip timestamp and thread id for the message box. */
156 const char *box_message = strstr(error_message, "Fatal error");
157 if (NULL == box_message)
159 /* Shouldn't happen but ... */
160 box_message = error_message;
162 MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
163 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
165 /* Cleanup - remove taskbar icon etc. */
168 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
181 /*********************************************************************
183 * Function : show_version
185 * Description : Logs the Privoxy version and the program name.
188 * 1 : prog_name = The program name.
192 *********************************************************************/
193 void show_version(const char *prog_name)
195 log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
196 if (prog_name != NULL)
198 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
203 /*********************************************************************
205 * Function : init_log_module
207 * Description : Initializes the logging module to log to stderr.
208 * Can only be called while stderr hasn't been closed
209 * yet and is only supposed to be called once.
212 * 1 : prog_name = The program name.
216 *********************************************************************/
217 void init_log_module(void)
222 set_debug_level(debug);
226 /*********************************************************************
228 * Function : set_debug_level
230 * Description : Sets the debug level to the provided value
231 * plus LOG_LEVEL_MINIMUM.
233 * XXX: we should only use the LOG_LEVEL_MINIMUM
234 * until the first time the configuration file has
237 * Parameters : 1: debug_level = The debug level to set.
241 *********************************************************************/
242 void set_debug_level(int debug_level)
245 if (LOG_LEVEL_STFU == debug_level)
247 debug = LOG_LEVEL_STFU;
249 if (LOG_LEVEL_STFU == debug)
255 debug = debug_level | LOG_LEVEL_MINIMUM;
259 /*********************************************************************
261 * Function : debug_level_is_enabled
263 * Description : Checks if a certain debug level is enabled.
265 * Parameters : 1: debug_level = The debug level to check.
269 *********************************************************************/
270 int debug_level_is_enabled(int debug_level)
272 return (0 != (debug & debug_level));
276 /*********************************************************************
278 * Function : disable_logging
280 * Description : Disables logging.
286 *********************************************************************/
287 void disable_logging(void)
291 log_error(LOG_LEVEL_INFO,
292 "No logfile configured. Please enable it before reporting any problems.");
301 /*********************************************************************
303 * Function : init_error_log
305 * Description : Initializes the logging module to log to a file.
307 * XXX: should be renamed.
310 * 1 : prog_name = The program name.
311 * 2 : logfname = The logfile to (re)open.
315 *********************************************************************/
316 void init_error_log(const char *prog_name, const char *logfname)
320 assert(NULL != logfname);
324 if ((logfp != NULL) && (logfp != stderr))
326 log_error(LOG_LEVEL_INFO, "(Re-)Opening logfile \'%s\'", logfname);
329 /* set the designated log file */
330 fp = fopen(logfname, "a");
331 if ((NULL == fp) && (logfp != NULL))
334 * Some platforms (like OS/2) don't allow us to open
335 * the same file twice, therefore we give it another
336 * shot after closing the old file descriptor first.
338 * We don't do it right away because it prevents us
339 * from logging the "can't open logfile" message to
342 * XXX: this is a lame workaround and once the next
343 * release is out we should stop bothering reopening
344 * the logfile unless we have to.
346 * Currently we reopen it every time the config file
347 * has been reloaded, but actually we only have to
348 * reopen it if the file name changed or if the
349 * configuration reload was caused by a SIGHUP.
351 log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. "
352 "Retrying after closing the old file descriptor first. If that "
353 "doesn't work, Privoxy will exit without being able to log a message.",
359 fp = fopen(logfname, "a");
364 log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
367 #ifdef FEATURE_EXTERNAL_FILTERS
368 mark_socket_for_close_on_execute(3);
371 /* set logging to be completely unbuffered */
380 if (daemon_mode && (logfp == stderr))
382 if (dup2(1, 2) == -1)
385 * We only use fatal_error() to clear the pid
386 * file and to exit. Given that stderr has just
387 * been closed, the user will not see the error
390 fatal_error("Failed to reserve fd 2.");
397 show_version(prog_name);
401 } /* init_error_log */
404 /*********************************************************************
406 * Function : get_thread_id
408 * Description : Returns a number that is different for each thread.
410 * XXX: Should be moved elsewhere (miscutil.c?)
414 * Returns : thread_id
416 *********************************************************************/
417 static long get_thread_id(void)
423 APIRET ulrc; /* XXX: I have no clue what this does */
426 /* FIXME get current thread id */
427 #ifdef FEATURE_PTHREAD
428 this_thread = (long)pthread_self();
431 * Mac OSX (and perhaps other Mach instances) doesn't have a unique
432 * value at the lowest order 4 bytes of pthread_self()'s return value, a pthread_t,
433 * so trim the three lowest-order bytes from the value (16^3).
435 this_thread = this_thread / 4096;
436 #endif /* def __MACH__ */
437 #elif defined(_WIN32)
438 this_thread = GetCurrentThreadId();
439 #elif defined(__OS2__)
440 ulrc = DosGetInfoBlocks(&ptib, NULL);
442 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
444 /* Forking instead of threading. */
446 #endif /* def FEATURE_PTHREAD */
452 /*********************************************************************
454 * Function : get_log_timestamp
456 * Description : Generates the time stamp for the log message prefix.
459 * 1 : buffer = Storage buffer
460 * 2 : buffer_size = Size of storage buffer
462 * Returns : Number of written characters or 0 for error.
464 *********************************************************************/
465 static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
470 struct timeval tv_now; /* XXX: stupid name */
472 int msecs_length = 0;
474 gettimeofday(&tv_now, NULL);
475 msecs = tv_now.tv_usec / 1000;
478 #ifdef HAVE_LOCALTIME_R
479 tm_now = *localtime_r(&now, &tm_now);
480 #elif defined(MUTEX_LOCKS_AVAILABLE)
481 privoxy_mutex_lock(&localtime_mutex);
482 tm_now = *localtime(&now);
483 privoxy_mutex_unlock(&localtime_mutex);
485 tm_now = *localtime(&now);
488 length = strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &tm_now);
489 if (length > (size_t)0)
491 msecs_length = snprintf(buffer+length, buffer_size - length, ".%.3ld", msecs);
493 if (msecs_length > 0)
495 length += (size_t)msecs_length;
506 /*********************************************************************
508 * Function : get_clf_timestamp
510 * Description : Generates a Common Log Format time string.
513 * 1 : buffer = Storage buffer
514 * 2 : buffer_size = Size of storage buffer
516 * Returns : Number of written characters or 0 for error.
518 *********************************************************************/
519 static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
522 * Complex because not all OSs have tm_gmtoff or
523 * the %z field in strftime()
528 #ifdef HAVE_LOCALTIME_R
537 gmt = *gmtime_r(&now, &gmt);
538 #elif defined(MUTEX_LOCKS_AVAILABLE)
539 privoxy_mutex_lock(&gmtime_mutex);
541 privoxy_mutex_unlock(&gmtime_mutex);
545 #ifdef HAVE_LOCALTIME_R
546 tm_now = localtime_r(&now, &dummy);
547 #elif defined(MUTEX_LOCKS_AVAILABLE)
548 privoxy_mutex_lock(&localtime_mutex);
549 tm_now = localtime(&now);
550 privoxy_mutex_unlock(&localtime_mutex);
552 tm_now = localtime(&now);
554 days = tm_now->tm_yday - gmt.tm_yday;
555 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
556 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
558 length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
560 if (length > (size_t)0)
562 tz_length = snprintf(buffer+length, buffer_size-length,
563 "%+03d%02d", mins / 60, abs(mins) % 60);
567 length += (size_t)tz_length;
578 /*********************************************************************
580 * Function : get_log_level_string
582 * Description : Translates a numerical loglevel into a string.
585 * 1 : loglevel = LOG_LEVEL_FOO
587 * Returns : Log level string.
589 *********************************************************************/
590 static inline const char *get_log_level_string(int loglevel)
592 char *log_level_string = NULL;
594 assert(0 < loglevel);
598 case LOG_LEVEL_ERROR:
599 log_level_string = "Error";
601 case LOG_LEVEL_FATAL:
602 log_level_string = "Fatal error";
605 log_level_string = "Request";
607 case LOG_LEVEL_CONNECT:
608 log_level_string = "Connect";
610 case LOG_LEVEL_WRITING:
611 log_level_string = "Writing";
613 case LOG_LEVEL_RECEIVED:
614 log_level_string = "Received";
616 case LOG_LEVEL_HEADER:
617 log_level_string = "Header";
620 log_level_string = "Info";
622 case LOG_LEVEL_RE_FILTER:
623 log_level_string = "Re-Filter";
625 #ifdef FEATURE_FORCE_LOAD
626 case LOG_LEVEL_FORCE:
627 log_level_string = "Force";
629 #endif /* def FEATURE_FORCE_LOAD */
630 case LOG_LEVEL_REDIRECTS:
631 log_level_string = "Redirect";
633 case LOG_LEVEL_DEANIMATE:
634 log_level_string = "Gif-Deanimate";
636 case LOG_LEVEL_CRUNCH:
637 log_level_string = "Crunch";
640 log_level_string = "CGI";
642 case LOG_LEVEL_ACTIONS:
643 log_level_string = "Actions";
646 log_level_string = "Unknown log level";
649 assert(NULL != log_level_string);
651 return log_level_string;
655 #define LOG_BUFFER_SIZE BUFFER_SIZE
656 /*********************************************************************
658 * Function : log_error
660 * Description : This is the error-reporting and logging function.
663 * 1 : loglevel = the type of message to be logged
664 * 2 : fmt = the main string we want logged, printf-like
665 * 3 : ... = arguments to be inserted in fmt (printf-like).
669 *********************************************************************/
670 void log_error(int loglevel, const char *fmt, ...)
673 char outbuf[LOG_BUFFER_SIZE+1];
674 char tempbuf[LOG_BUFFER_SIZE];
676 const char * src = fmt;
679 const 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))
705 if (debug == LOG_LEVEL_STFU)
710 if (loglevel == LOG_LEVEL_FATAL)
712 fatal_error("Fatal error. You're not supposed to"
713 "see this message. Please file a bug report.");
718 thread_id = get_thread_id();
719 get_log_timestamp(timestamp, sizeof(timestamp));
722 * Memsetting the whole buffer to zero (in theory)
723 * makes things easier later on.
725 memset(outbuf, 0, sizeof(outbuf));
727 /* Add prefix for everything but Common Log Format messages */
728 if (loglevel != LOG_LEVEL_CLF)
730 length = (size_t)snprintf(outbuf, log_buffer_size, "%s %08lx %s: ",
731 timestamp, thread_id, get_log_level_string(loglevel));
734 /* get ready to scan var. args. */
737 /* build formatted message from fmt and var-args */
738 while ((*src) && (length < log_buffer_size-2))
740 const char *sval = NULL; /* %N string */
741 int ival; /* %N string length or an error code */
742 unsigned uval; /* %u value */
743 long lval; /* %l value */
744 unsigned long ulval; /* %ul value */
746 const char *format_string = tempbuf;
751 outbuf[length++] = ch;
753 * XXX: Only necessary on platforms where multiple threads
754 * can write to the buffer at the same time because we
755 * don't support mutexes (OS/2 for example).
757 outbuf[length] = '\0';
760 outbuf[length] = '\0';
768 ival = va_arg(ap, int);
769 snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
772 uval = va_arg(ap, unsigned);
773 snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
776 /* this is a modifier that must be followed by u, lu, or d */
780 lval = va_arg(ap, long);
781 snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
785 ulval = va_arg(ap, unsigned long);
786 snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
788 else if ((ch == 'l') && (*src == 'u'))
790 unsigned long long lluval = va_arg(ap, unsigned long long);
791 snprintf(tempbuf, sizeof(tempbuf), "%llu", lluval);
796 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
797 loglevel = LOG_LEVEL_FATAL;
802 * Note that char paramaters are converted to int, so we need to
803 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
805 tempbuf[0] = (char) va_arg(ap, int);
809 format_string = va_arg(ap, char *);
810 if (format_string == NULL)
812 format_string = "[null]";
817 * Non-standard: Print a counted unterminated string,
818 * replacing unprintable bytes with their hex value.
819 * Takes 2 parameters: int length, const char * string.
821 ival = va_arg(ap, int);
823 sval = va_arg(ap, char *);
824 assert(sval != NULL);
826 while ((ival-- > 0) && (length < log_buffer_size - 6))
828 if (isprint((int)*sval) && (*sval != '\\'))
830 outbuf[length++] = *sval;
831 outbuf[length] = '\0';
835 int ret = snprintf(outbuf + length,
836 log_buffer_size - length - 2, "\\x%.2x", (unsigned char)*sval);
843 * XXX: In case of printable characters at the end of
844 * the %N string, we're not using the whole buffer.
846 format_string = (length < log_buffer_size - 6) ? "" : "[too long]";
849 /* Non-standard: Print error code from errno */
851 ival = WSAGetLastError();
852 format_string = w32_socket_strerr(ival, tempbuf);
857 format_string = os2_socket_strerr(ival, tempbuf);
862 format_string = strerror(ival);
864 #else /* ifndef _WIN32 */
867 format_string = strerror(ival);
868 #else /* ifndef HAVE_STRERROR */
869 format_string = NULL;
870 #endif /* ndef HAVE_STRERROR */
873 snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
875 #endif /* ndef _WIN32 */
878 /* Non-standard: Print a Common Log File timestamp */
879 get_clf_timestamp(tempbuf, sizeof(tempbuf));
882 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
883 loglevel = LOG_LEVEL_FATAL;
887 assert(length < log_buffer_size);
888 length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
890 if (length >= log_buffer_size-2)
892 static const char warning[] = "... [too long, truncated]";
894 length = log_buffer_size - sizeof(warning) - 1;
895 length += strlcpy(outbuf + length, warning, log_buffer_size - length);
896 assert(length < log_buffer_size);
902 /* done with var. args */
905 assert(length < log_buffer_size);
906 length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
908 /* Some sanity checks */
909 if ((length >= log_buffer_size)
910 || (outbuf[log_buffer_size-1] != '\0')
911 || (outbuf[log_buffer_size] != '\0')
914 /* Repeat as assertions */
915 assert(length < log_buffer_size);
916 assert(outbuf[log_buffer_size-1] == '\0');
918 * outbuf's real size is log_buffer_size+1,
919 * so while this looks like an off-by-one,
920 * we're only checking our paranoia byte.
922 assert(outbuf[log_buffer_size] == '\0');
924 snprintf(outbuf, log_buffer_size,
925 "%s %08lx Fatal error: log_error()'s sanity checks failed."
926 "length: %d. Exiting.",
927 timestamp, thread_id, (int)length);
928 loglevel = LOG_LEVEL_FATAL;
933 * On Windows this is acceptable in case
934 * we are logging to the GUI window only.
936 assert(NULL != logfp);
941 if (loglevel == LOG_LEVEL_FATAL)
948 fputs(outbuf, logfp);
951 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
952 /* Write to display */
953 LogPutString(outbuf);
954 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
961 /*********************************************************************
963 * Function : jb_err_to_string
965 * Description : Translates JB_ERR_FOO codes into strings.
967 * XXX: the type of error codes is jb_err
968 * but the typedef'inition is currently not
969 * visible to all files that include errlog.h.
972 * 1 : jb_error = a valid jb_err code
974 * Returns : A string with the jb_err translation
976 *********************************************************************/
977 const char *jb_err_to_string(jb_err jb_error)
982 return "Success, no error";
984 return "Out of memory";
985 case JB_ERR_CGI_PARAMS:
986 return "Missing or corrupt CGI parameters";
988 return "Error opening, reading or writing a file";
990 return "Parse error";
991 case JB_ERR_MODIFIED:
992 return "File has been modified outside of the CGI actions editor.";
993 case JB_ERR_COMPRESS:
994 return "(De)compression failure";
997 return "Internal error";
1001 /*********************************************************************
1003 * Function : w32_socket_strerr
1005 * Description : Translate the return value from WSAGetLastError()
1009 * 1 : errcode = The return value from WSAGetLastError().
1010 * 2 : tmp_buf = A temporary buffer that might be used to
1013 * Returns : String representing the error code. This may be
1014 * a global string constant or a string stored in
1017 *********************************************************************/
1018 static char *w32_socket_strerr(int errcode, char *tmp_buf)
1020 #define TEXT_FOR_ERROR(code,text) \
1021 if (errcode == code) \
1023 return #code " - " text; \
1026 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
1027 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
1028 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
1029 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
1030 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
1031 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
1032 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
1033 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
1034 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
1035 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
1036 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
1037 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
1038 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
1039 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
1040 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
1041 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
1042 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
1043 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
1044 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
1045 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
1046 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
1047 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
1048 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
1049 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
1050 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
1051 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
1052 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
1053 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
1054 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
1055 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
1056 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
1057 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
1058 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
1059 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
1060 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
1061 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
1062 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
1063 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
1064 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
1065 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
1066 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
1067 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
1069 * The following error codes are documented in the Microsoft WinSock
1070 * reference guide, but don't actually exist.
1072 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
1073 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
1074 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
1075 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
1076 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
1077 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
1078 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
1079 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
1080 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
1081 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
1084 sprintf(tmp_buf, "(error number %d)", errcode);
1087 #endif /* def _WIN32 */
1091 /*********************************************************************
1093 * Function : os2_socket_strerr
1095 * Description : Translate the return value from sock_errno()
1099 * 1 : errcode = The return value from sock_errno().
1100 * 2 : tmp_buf = A temporary buffer that might be used to
1103 * Returns : String representing the error code. This may be
1104 * a global string constant or a string stored in
1107 *********************************************************************/
1108 static char *os2_socket_strerr(int errcode, char *tmp_buf)
1110 #define TEXT_FOR_ERROR(code,text) \
1111 if (errcode == code) \
1113 return #code " - " text; \
1116 TEXT_FOR_ERROR(SOCEPERM , "Not owner.")
1117 TEXT_FOR_ERROR(SOCESRCH , "No such process.")
1118 TEXT_FOR_ERROR(SOCEINTR , "Interrupted system call.")
1119 TEXT_FOR_ERROR(SOCENXIO , "No such device or address.")
1120 TEXT_FOR_ERROR(SOCEBADF , "Bad file number.")
1121 TEXT_FOR_ERROR(SOCEACCES , "Permission denied.")
1122 TEXT_FOR_ERROR(SOCEFAULT , "Bad address.")
1123 TEXT_FOR_ERROR(SOCEINVAL , "Invalid argument.")
1124 TEXT_FOR_ERROR(SOCEMFILE , "Too many open files.")
1125 TEXT_FOR_ERROR(SOCEPIPE , "Broken pipe.")
1126 TEXT_FOR_ERROR(SOCEWOULDBLOCK , "Operation would block.")
1127 TEXT_FOR_ERROR(SOCEINPROGRESS , "Operation now in progress.")
1128 TEXT_FOR_ERROR(SOCEALREADY , "Operation already in progress.")
1129 TEXT_FOR_ERROR(SOCENOTSOCK , "Socket operation on non-socket.")
1130 TEXT_FOR_ERROR(SOCEDESTADDRREQ , "Destination address required.")
1131 TEXT_FOR_ERROR(SOCEMSGSIZE , "Message too long.")
1132 TEXT_FOR_ERROR(SOCEPROTOTYPE , "Protocol wrong type for socket.")
1133 TEXT_FOR_ERROR(SOCENOPROTOOPT , "Protocol not available.")
1134 TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
1135 TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
1136 TEXT_FOR_ERROR(SOCEOPNOTSUPP , "Operation not supported.")
1137 TEXT_FOR_ERROR(SOCEPFNOSUPPORT , "Protocol family not supported.")
1138 TEXT_FOR_ERROR(SOCEAFNOSUPPORT , "Address family not supported by protocol family.")
1139 TEXT_FOR_ERROR(SOCEADDRINUSE , "Address already in use.")
1140 TEXT_FOR_ERROR(SOCEADDRNOTAVAIL , "Can't assign requested address.")
1141 TEXT_FOR_ERROR(SOCENETDOWN , "Network is down.")
1142 TEXT_FOR_ERROR(SOCENETUNREACH , "Network is unreachable.")
1143 TEXT_FOR_ERROR(SOCENETRESET , "Network dropped connection on reset.")
1144 TEXT_FOR_ERROR(SOCECONNABORTED , "Software caused connection abort.")
1145 TEXT_FOR_ERROR(SOCECONNRESET , "Connection reset by peer.")
1146 TEXT_FOR_ERROR(SOCENOBUFS , "No buffer space available.")
1147 TEXT_FOR_ERROR(SOCEISCONN , "Socket is already connected.")
1148 TEXT_FOR_ERROR(SOCENOTCONN , "Socket is not connected.")
1149 TEXT_FOR_ERROR(SOCESHUTDOWN , "Can't send after socket shutdown.")
1150 TEXT_FOR_ERROR(SOCETOOMANYREFS , "Too many references: can't splice.")
1151 TEXT_FOR_ERROR(SOCETIMEDOUT , "Operation timed out.")
1152 TEXT_FOR_ERROR(SOCECONNREFUSED , "Connection refused.")
1153 TEXT_FOR_ERROR(SOCELOOP , "Too many levels of symbolic links.")
1154 TEXT_FOR_ERROR(SOCENAMETOOLONG , "File name too long.")
1155 TEXT_FOR_ERROR(SOCEHOSTDOWN , "Host is down.")
1156 TEXT_FOR_ERROR(SOCEHOSTUNREACH , "No route to host.")
1157 TEXT_FOR_ERROR(SOCENOTEMPTY , "Directory not empty.")
1158 TEXT_FOR_ERROR(SOCEOS2ERR , "OS/2 Error.")
1160 sprintf(tmp_buf, "(error number %d)", errcode);
1163 #endif /* def __OS2__ */