1 const char errlog_rcs[] = "$Id: errlog.c,v 1.54 2007/09/22 16:15:34 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-2007 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.
36 * Revision 1.54 2007/09/22 16:15:34 fabiankeil
37 * - Let it compile with pcc.
38 * - Move our includes below system includes to prevent macro conflicts.
40 * Revision 1.53 2007/08/05 13:53:14 fabiankeil
41 * #1763173 from Stefan Huehner: declare some more functions
42 * static and use void instead of empty parameter lists.
44 * Revision 1.52 2007/07/14 07:28:47 fabiankeil
45 * Add translation function for JB_ERR_FOO codes.
47 * Revision 1.51 2007/05/11 11:51:34 fabiankeil
48 * Fix a type mismatch warning.
50 * Revision 1.50 2007/04/11 10:55:44 fabiankeil
51 * Enforce some assertions that could be triggered
52 * on mingw32 and other systems where we use threads
55 * Revision 1.49 2007/04/08 16:44:15 fabiankeil
56 * We need <sys/time.h> for gettimeofday(), not <time.h>.
58 * Revision 1.48 2007/03/31 13:33:28 fabiankeil
59 * Add alternative log_error() with timestamps
60 * that contain milliseconds and without using
61 * strcpy(), strcat() or sprintf().
63 * Revision 1.47 2006/11/28 15:25:15 fabiankeil
64 * Only unlink the pidfile if it's actually used.
66 * Revision 1.46 2006/11/13 19:05:51 fabiankeil
67 * Make pthread mutex locking more generic. Instead of
68 * checking for OSX and OpenBSD, check for FEATURE_PTHREAD
69 * and use mutex locking unless there is an _r function
70 * available. Better safe than sorry.
72 * Fixes "./configure --disable-pthread" and should result
73 * in less threading-related problems on pthread-using platforms,
74 * but it still doesn't fix BR#1122404.
76 * Revision 1.45 2006/08/21 11:15:54 david__schmidt
77 * MS Visual C++ build updates
79 * Revision 1.44 2006/08/18 16:03:16 david__schmidt
80 * Tweak for OS/2 build happiness.
82 * Revision 1.43 2006/08/03 02:46:41 david__schmidt
83 * Incorporate Fabian Keil's patch work:
84 * http://www.fabiankeil.de/sourcecode/privoxy/
86 * Revision 1.42 2006/07/18 14:48:46 david__schmidt
87 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
88 * with what was really the latest development (the v_3_0_branch branch)
90 * Revision 1.40.2.4 2005/04/03 20:10:50 david__schmidt
91 * Thanks to Jindrich Makovicka for a race condition fix for the log
92 * file. The race condition remains for non-pthread implementations.
93 * Reference patch #1175720.
95 * Revision 1.40.2.3 2003/03/07 03:41:04 david__schmidt
96 * Wrapping all *_r functions (the non-_r versions of them) with mutex
97 * semaphores for OSX. Hopefully this will take care of all of those pesky
100 * Revision 1.40.2.2 2002/09/28 00:30:57 david__schmidt
101 * Update error logging to give sane values for thread IDs on Mach kernels.
102 * It's still a hack, but at least it looks farily normal. We print the
103 * absolute value of the first 4 bytes of the pthread_t modded with 1000.
105 * Revision 1.40.2.1 2002/09/25 12:47:42 oes
106 * Make log_error safe against NULL string arguments
108 * Revision 1.40 2002/05/22 01:27:27 david__schmidt
110 * Add os2_socket_strerr mirroring w32_socket_strerr.
112 * Revision 1.39 2002/04/03 17:15:27 gliptak
113 * zero padding thread ids in log
115 * Revision 1.38 2002/03/31 17:18:59 jongfoster
116 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
118 * Revision 1.37 2002/03/27 14:32:43 david__schmidt
119 * More compiler warning message maintenance
121 * Revision 1.36 2002/03/26 22:29:54 swa
122 * we have a new homepage!
124 * Revision 1.35 2002/03/24 15:23:33 jongfoster
127 * Revision 1.34 2002/03/24 13:25:43 swa
128 * name change related issues
130 * Revision 1.33 2002/03/13 00:27:04 jongfoster
133 * Revision 1.32 2002/03/07 03:46:17 oes
134 * Fixed compiler warnings
136 * Revision 1.31 2002/03/06 23:02:57 jongfoster
139 * Revision 1.30 2002/03/05 22:43:45 david__schmidt
140 * - Better error reporting on OS/2
141 * - Fix double-slash comment (oops)
143 * Revision 1.29 2002/03/04 23:45:13 jongfoster
144 * Printing thread ID if using Win32 native threads
146 * Revision 1.28 2002/03/04 17:59:59 oes
147 * Deleted deletePidFile(), cosmetics
149 * Revision 1.27 2002/03/04 02:08:01 david__schmidt
150 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
152 * Revision 1.26 2002/01/09 19:05:45 steudten
153 * Fix big memory leak.
155 * Revision 1.25 2002/01/09 14:32:08 oes
156 * Added support for gmtime_r and localtime_r.
158 * Revision 1.24 2001/12/30 14:07:32 steudten
159 * - Add signal handling (unix)
160 * - Add SIGHUP handler (unix)
161 * - Add creation of pidfile (unix)
162 * - Add action 'top' in rc file (RH)
163 * - Add entry 'SIGNALS' to manpage
164 * - Add exit message to logfile (unix)
166 * Revision 1.23 2001/11/07 00:02:13 steudten
167 * Add line number in error output for lineparsing for
168 * actionsfile and configfile.
169 * Special handling for CLF added.
171 * Revision 1.22 2001/11/05 23:43:05 steudten
172 * Add time+date to log files.
174 * Revision 1.21 2001/10/25 03:40:47 david__schmidt
175 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
176 * threads to call select() simultaneously. So, it's time to do a real, live,
177 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
178 * (native). Both versions will work, but using __OS2__ offers multi-threading.
180 * Revision 1.20 2001/09/16 23:04:34 jongfoster
183 * Revision 1.19 2001/09/13 20:08:06 jongfoster
184 * Adding support for LOG_LEVEL_CGI
186 * Revision 1.18 2001/09/10 11:27:24 oes
187 * Declaration of w32_socket_strerr now conditional
189 * Revision 1.17 2001/09/10 10:17:13 oes
190 * Removed unused variable; Fixed sprintf format
192 * Revision 1.16 2001/07/30 22:08:36 jongfoster
193 * Tidying up #defines:
194 * - All feature #defines are now of the form FEATURE_xxx
195 * - Permanently turned off WIN_GUI_EDIT
196 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
198 * Revision 1.15 2001/07/29 17:41:10 jongfoster
199 * Now prints thread ID for each message (pthreads only)
201 * Revision 1.14 2001/07/19 19:03:48 haroon
202 * - Added case for LOG_LEVEL_POPUPS
204 * Revision 1.13 2001/07/13 13:58:58 oes
205 * - Added case for LOG_LEVEL_DEANIMATE
206 * - Removed all #ifdef PCRS
208 * Revision 1.12 2001/06/09 10:55:28 jongfoster
209 * Changing BUFSIZ ==> BUFFER_SIZE
211 * Revision 1.11 2001/06/01 18:14:49 jongfoster
212 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
213 * in config.h if appropriate) rather than the NO_STRERR macro.
215 * Revision 1.10 2001/05/29 11:52:21 oes
216 * Conditional compilation of w32_socket_error
218 * Revision 1.9 2001/05/28 16:15:17 jongfoster
219 * Improved reporting of errors under Win32.
221 * Revision 1.8 2001/05/26 17:25:14 jongfoster
222 * Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
224 * Revision 1.7 2001/05/26 15:21:28 jongfoster
225 * Activity animation in Win32 GUI now works even if debug==0
227 * Revision 1.6 2001/05/25 21:55:08 jongfoster
228 * Now cleans up properly on FATAL (removes taskbar icon etc)
230 * Revision 1.5 2001/05/22 18:46:04 oes
232 * - Enabled filtering banners by size rather than URL
233 * by adding patterns that replace all standard banner
234 * sizes with the "Junkbuster" gif to the re_filterfile
236 * - Enabled filtering WebBugs by providing a pattern
237 * which kills all 1x1 images
239 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
240 * which is selected by the (nonstandard and therefore
241 * capital) letter 'U' in the option string.
242 * It causes the quantifiers to be ungreedy by default.
243 * Appending a ? turns back to greedy (!).
245 * - Added a new interceptor ijb-send-banner, which
246 * sends back the "Junkbuster" gif. Without imagelist or
247 * MSIE detection support, or if tinygif = 1, or the
248 * URL isn't recognized as an imageurl, a lame HTML
249 * explanation is sent instead.
251 * - Added new feature, which permits blocking remote
252 * script redirects and firing back a local redirect
254 * The feature is conditionally compiled, i.e. it
255 * can be disabled with --disable-fast-redirects,
256 * plus it must be activated by a "fast-redirects"
257 * line in the config file, has its own log level
258 * and of course wants to be displayed by show-proxy-args
259 * Note: Boy, all the #ifdefs in 1001 locations and
260 * all the fumbling with configure.in and acconfig.h
261 * were *way* more work than the feature itself :-(
263 * - Because a generic redirect template was needed for
264 * this, tinygif = 3 now uses the same.
266 * - Moved GIFs, and other static HTTP response templates
271 * - Removed some >400 CRs again (Jon, you really worked
274 * Revision 1.4 2001/05/21 19:32:54 jongfoster
275 * Added another #ifdef _WIN_CONSOLE
277 * Revision 1.3 2001/05/20 01:11:40 jongfoster
278 * Added support for LOG_LEVEL_FATAL
279 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
280 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
282 * Revision 1.2 2001/05/17 22:42:01 oes
283 * - Cleaned CRLF's from the sources and related files
284 * - Repaired logging for REF and FRC
286 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
287 * Initial import of version 2.9.3 source tree
290 *********************************************************************/
299 #include "miscutil.h"
301 #if defined(HAVE_STRLCPY) && defined(HAVE_GETTIMEOFDAY)
302 #define USE_NEW_LOG_ERROR
303 /* For gettimeofday() */
304 #include <sys/time.h>
305 #endif /* defined(HAVE_STRLCPY) && defined(HAVE_GETTIMEOFDAY) */
307 #if !defined(_WIN32) && !defined(__OS2__)
309 #endif /* !defined(_WIN32) && !defined(__OS2__) */
321 #endif /* ndef _WIN_CONSOLE */
322 #endif /* def _WIN32 */
324 #define inline __inline
325 #endif /* def _MSC_VER */
328 #include <sys/socket.h> /* For sock_errno */
337 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
341 * LOG_LEVEL_FATAL cannot be turned off. (There are
342 * some exceptional situations where we need to get a
343 * message to the user).
345 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
347 /* where to log (default: stderr) */
348 static FILE *logfp = NULL;
350 /* logging detail level. */
351 int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
353 /* static functions */
354 static void fatal_error(const char * error_message);
356 static char *w32_socket_strerr(int errcode, char *tmp_buf);
359 static char *os2_socket_strerr(int errcode, char *tmp_buf);
362 #ifdef FEATURE_PTHREAD
363 static inline void lock_logfile(void)
365 pthread_mutex_lock(&log_mutex);
367 static inline void unlock_logfile(void)
369 pthread_mutex_unlock(&log_mutex);
371 static inline void lock_loginit(void)
373 pthread_mutex_lock(&log_init_mutex);
375 static inline void unlock_loginit(void)
377 pthread_mutex_unlock(&log_init_mutex);
379 #else /* ! FEATURE_PTHREAD */
381 * FIXME we need a cross-platform locking mechanism.
382 * The locking/unlocking functions below should be
383 * fleshed out for non-pthread implementations.
385 static inline void lock_logfile() {}
386 static inline void unlock_logfile() {}
387 static inline void lock_loginit() {}
388 static inline void unlock_loginit() {}
391 /*********************************************************************
393 * Function : fatal_error
395 * Description : Displays a fatal error to standard error (or, on
396 * a WIN32 GUI, to a dialog box), and exits
397 * JunkBuster with status code 1.
400 * 1 : error_message = The error message to display.
402 * Returns : Does not return.
404 *********************************************************************/
405 static void fatal_error(const char * error_message)
407 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
408 MessageBox(g_hwndLogFrame, error_message, "Privoxy Error",
409 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
411 /* Cleanup - remove taskbar icon etc. */
414 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
415 fputs(error_message, logfp);
416 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
429 /*********************************************************************
431 * Function : show_version
433 * Description : Logs the Privoxy version and the program name.
436 * 1 : prog_name = The program name.
440 *********************************************************************/
441 static void show_version(const char *prog_name)
443 log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
444 if (prog_name != NULL)
446 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
451 /*********************************************************************
453 * Function : init_log_module
455 * Description : Initializes the logging module to log to stderr.
456 * Can only be called while stderr hasn't been closed
457 * yet and is only supposed to be called once.
460 * 1 : prog_name = The program name.
464 *********************************************************************/
465 void init_log_module(const char *prog_name)
470 set_debug_level(debug);
471 show_version(prog_name);
475 /*********************************************************************
477 * Function : set_debug_level
479 * Description : Sets the debug level to the provided value
480 * plus LOG_LEVEL_MINIMUM.
482 * XXX: we should only use the LOG_LEVEL_MINIMUM
483 * until the frist time the configuration file has
486 * Parameters : 1: debug_level = The debug level to set.
490 *********************************************************************/
491 void set_debug_level(int debug_level)
493 debug = debug_level | LOG_LEVEL_MINIMUM;
497 /*********************************************************************
499 * Function : disable_logging
501 * Description : Disables logging.
507 *********************************************************************/
508 void disable_logging(void)
520 /*********************************************************************
522 * Function : init_error_log
524 * Description : Initializes the logging module to log to a file.
526 * XXX: should be renamed.
529 * 1 : prog_name = The program name.
530 * 2 : logfname = The logfile name, or NULL for stderr.
531 * 3 : debuglevel = The debugging level.
535 *********************************************************************/
536 void init_error_log(const char *prog_name, const char *logfname)
540 assert(NULL != logfname);
545 if ((logfp != NULL) && (logfp != stderr))
547 log_error(LOG_LEVEL_INFO, "(Re-)Open logfile \'%s\'", logfname ? logfname : "none");
550 /* set the designated log file */
551 fp = fopen(logfname, "a");
554 log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
557 /* set logging to be completely unbuffered */
567 show_version(prog_name);
571 } /* init_error_log */
574 #if defined(USE_NEW_LOG_ERROR)
576 * Use an alternative log_error version and its helper functions.
578 * The code is mostly copied from the old log_error
579 * function but it's divided into multiple functions,
580 * doesn't use strcpy, strcat or sprintf and compiles
581 * with gcc43 without warnings (on FreeBSD that is).
583 * It should behave like the old log_error function with
586 * - too long format strings are logged truncated instead
587 * of completely replaced with the truncation warning,
588 * - its time stamps contain milliseconds.
591 /*********************************************************************
593 * Function : get_thread_id
595 * Description : Returns a number that is different for each thread.
597 * XXX: Should be moved elsewhere (miscutil.c?)
601 * Returns : thread_id
603 *********************************************************************/
604 static long get_thread_id(void)
606 long this_thread = 1; /* was: pthread_t this_thread;*/
610 APIRET ulrc; /* XXX: I have no clue what this does */
613 /* FIXME get current thread id */
614 #ifdef FEATURE_PTHREAD
615 this_thread = (long)pthread_self();
618 * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
619 * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
620 * pthread_t is supposed to be opaque... but it's fairly random, though, so
621 * we make it mostly presentable.
623 this_thread = abs(this_thread % 1000);
624 #endif /* def __MACH__ */
625 #elif defined(_WIN32)
626 this_thread = GetCurrentThreadId();
627 #elif defined(__OS2__)
628 ulrc = DosGetInfoBlocks(&ptib, NULL);
630 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
631 #endif /* def FEATURE_PTHREAD */
637 /*********************************************************************
639 * Function : get_log_timestamp
641 * Description : Generates the time stamp for the log message prefix.
644 * 1 : buffer = Storage buffer
645 * 2 : buffer_size = Size of storage buffer
647 * Returns : Number of written characters or 0 for error.
649 *********************************************************************/
650 static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
655 struct timeval tv_now; /* XXX: stupid name */
657 int msecs_length = 0;
659 gettimeofday(&tv_now, NULL);
660 msecs = tv_now.tv_usec / 1000;
664 #ifdef HAVE_LOCALTIME_R
665 tm_now = *localtime_r(&now, &tm_now);
666 #elif FEATURE_PTHREAD
667 pthread_mutex_lock(&localtime_mutex);
668 tm_now = *localtime(&now);
669 pthread_mutex_unlock(&localtime_mutex);
671 tm_now = *localtime(&now);
674 length = strftime(buffer, buffer_size, "%b %d %H:%M:%S", &tm_now);
677 msecs_length = snprintf(buffer+length, buffer_size - length, ".%.3ld", msecs);
679 if (msecs_length > 0)
681 length += (size_t)msecs_length;
692 /*********************************************************************
694 * Function : get_clf_timestamp
696 * Description : Generates a Common Log Format time string.
699 * 1 : buffer = Storage buffer
700 * 2 : buffer_size = Size of storage buffer
702 * Returns : Number of written characters or 0 for error.
704 *********************************************************************/
705 static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
708 * Complex because not all OSs have tm_gmtoff or
709 * the %z field in strftime()
714 #ifdef HAVE_LOCALTIME_R
723 gmt = *gmtime_r(&now, &gmt);
724 #elif FEATURE_PTHREAD
725 pthread_mutex_lock(&gmtime_mutex);
727 pthread_mutex_unlock(&gmtime_mutex);
731 #ifdef HAVE_LOCALTIME_R
732 tm_now = localtime_r(&now, &dummy);
733 #elif FEATURE_PTHREAD
734 pthread_mutex_lock(&localtime_mutex);
735 tm_now = localtime(&now);
736 pthread_mutex_unlock(&localtime_mutex);
738 tm_now = localtime(&now);
740 days = tm_now->tm_yday - gmt.tm_yday;
741 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
742 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
744 length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
748 tz_length = snprintf(buffer+length, buffer_size-length,
749 "%+03d%02d", mins / 60, abs(mins) % 60);
753 length += (size_t)tz_length;
764 /*********************************************************************
766 * Function : get_log_level_string
768 * Description : Translates a numerical loglevel into a string.
771 * 1 : loglevel = LOG_LEVEL_FOO
773 * Returns : Log level string.
775 *********************************************************************/
776 static inline const char *get_log_level_string(int loglevel)
778 char *log_level_string = NULL;
780 assert(0 < loglevel);
784 case LOG_LEVEL_ERROR:
785 log_level_string = "Error";
787 case LOG_LEVEL_FATAL:
788 log_level_string = "Fatal error";
791 log_level_string = "Request";
793 case LOG_LEVEL_CONNECT:
794 log_level_string = "Connect";
797 log_level_string = "Writing";
799 case LOG_LEVEL_HEADER:
800 log_level_string = "Header";
803 log_level_string = "Info";
805 case LOG_LEVEL_RE_FILTER:
806 log_level_string = "Re-Filter";
808 #ifdef FEATURE_FORCE_LOAD
809 case LOG_LEVEL_FORCE:
810 log_level_string = "Force";
812 #endif /* def FEATURE_FORCE_LOAD */
813 #ifdef FEATURE_FAST_REDIRECTS
814 case LOG_LEVEL_REDIRECTS:
815 log_level_string = "Redirect";
817 #endif /* def FEATURE_FAST_REDIRECTS */
818 case LOG_LEVEL_DEANIMATE:
819 log_level_string = "Gif-Deanimate";
821 #ifdef FEATURE_KILL_POPUPS
822 case LOG_LEVEL_POPUPS:
823 log_level_string = "Kill-Popups";
825 #endif /* def FEATURE_KILL_POPUPS */
827 log_level_string = "CGI";
830 log_level_string = "Unknown log level";
833 assert(NULL != log_level_string);
835 return log_level_string;
839 /*********************************************************************
841 * Function : log_error
843 * Description : This is the error-reporting and logging function.
846 * 1 : loglevel = the type of message to be logged
847 * 2 : fmt = the main string we want logged, printf-like
848 * 3 : ... = arguments to be inserted in fmt (printf-like).
852 *********************************************************************/
853 void log_error(int loglevel, const char *fmt, ...)
857 static char *outbuf_save = NULL;
858 char tempbuf[BUFFER_SIZE];
860 const char * src = fmt;
864 * XXX: Make this a config option,
865 * why else do we allocate instead of using
868 size_t log_buffer_size = BUFFER_SIZE;
870 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
872 * Irrespective of debug setting, a GET/POST/CONNECT makes
873 * the taskbar icon animate. (There is an option to disable
874 * this but checking that is handled inside LogShowActivity()).
876 if (loglevel == LOG_LEVEL_GPC)
880 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
883 * verify that the loglevel applies to current
884 * settings and that logging is enabled.
885 * Bail out otherwise.
887 if ((0 == (loglevel & debug)) || (logfp == NULL))
892 thread_id = get_thread_id();
893 get_log_timestamp(timestamp, sizeof(timestamp));
895 /* protect the whole function because of the static buffer (outbuf) */
898 if (NULL == outbuf_save)
900 outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
901 if (NULL == outbuf_save)
903 snprintf(tempbuf, sizeof(tempbuf),
904 "%s Privoxy(%08lx) Fatal error: log_error() failed to allocate buffer memory.\n"
905 "\nExiting.", timestamp, thread_id);
906 assert(NULL != logfp);
907 fputs(tempbuf, logfp);
909 fatal_error(tempbuf); /* Exit */
912 outbuf = outbuf_save;
915 * Memsetting the whole buffer to zero (in theory)
916 * makes things easier later on.
918 memset(outbuf, 0, log_buffer_size);
920 /* Add prefix for everything but Common Log Format messages */
921 if (loglevel != LOG_LEVEL_CLF)
923 length = (size_t)snprintf(outbuf, log_buffer_size, "%s Privoxy(%08lx) %s: ",
924 timestamp, thread_id, get_log_level_string(loglevel));
927 /* get ready to scan var. args. */
930 /* build formatted message from fmt and var-args */
931 while ((*src) && (length < log_buffer_size-2))
933 const char *sval = NULL; /* %N string */
934 int ival; /* %N string length or an error code */
935 unsigned uval; /* %u value */
936 long lval; /* %l value */
937 unsigned long ulval; /* %ul value */
939 const char *format_string = tempbuf;
944 outbuf[length++] = ch;
946 * XXX: Only necessary on platforms which don't use pthread
947 * mutexes (mingw32 for example), where multiple threads can
948 * write to the buffer at the same time.
950 outbuf[length] = '\0';
953 outbuf[length] = '\0';
961 ival = va_arg( ap, int );
962 snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
965 uval = va_arg( ap, unsigned );
966 snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
969 /* this is a modifier that must be followed by u or d */
973 lval = va_arg( ap, long );
974 snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
978 ulval = va_arg( ap, unsigned long );
979 snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
983 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
984 loglevel = LOG_LEVEL_FATAL;
989 * Note that char paramaters are converted to int, so we need to
990 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
992 tempbuf[0] = (char) va_arg(ap, int);
996 format_string = va_arg(ap, char *);
997 if (format_string == NULL)
999 format_string = "[null]";
1004 * Non-standard: Print a counted unterminated string.
1005 * Takes 2 parameters: int length, const char * string.
1007 ival = va_arg(ap, int);
1008 sval = va_arg(ap, char *);
1011 format_string = "[null]";
1017 /* That's ok (but stupid) */
1023 * That's not ok (and even more stupid)
1026 format_string = "[counted string lenght < 0]";
1029 else if (ival >= sizeof(tempbuf))
1032 * String is too long, copy as much as possible.
1033 * It will be further truncated later.
1035 memcpy(tempbuf, sval, sizeof(tempbuf)-1);
1036 tempbuf[sizeof(tempbuf)-1] = '\0';
1040 memcpy(tempbuf, sval, (size_t) ival);
1041 tempbuf[ival] = '\0';
1045 /* Non-standard: Print error code from errno */
1047 ival = WSAGetLastError();
1048 format_string = w32_socket_strerr(ival, tempbuf);
1050 ival = sock_errno();
1053 format_string = os2_socket_strerr(ival, tempbuf);
1058 format_string = strerror(ival);
1060 #else /* ifndef _WIN32 */
1062 #ifdef HAVE_STRERROR
1063 format_string = strerror(ival);
1064 #else /* ifndef HAVE_STRERROR */
1065 format_string = NULL;
1066 #endif /* ndef HAVE_STRERROR */
1069 snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
1071 #endif /* ndef _WIN32 */
1074 /* Non-standard: Print a Common Log File timestamp */
1075 get_clf_timestamp(tempbuf, sizeof(tempbuf));
1078 snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
1079 loglevel = LOG_LEVEL_FATAL;
1083 assert(length < log_buffer_size);
1084 length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
1086 if (length >= log_buffer_size-2)
1088 static char warning[] = "... [too long, truncated]";
1090 length = log_buffer_size - sizeof(warning) - 1;
1091 length += strlcpy(outbuf + length, warning, log_buffer_size - length);
1092 assert(length < log_buffer_size);
1096 } /* for( p ... ) */
1098 /* done with var. args */
1101 assert(length < log_buffer_size);
1102 length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
1104 /* Some sanity checks */
1105 if (!(length < log_buffer_size)
1106 || !(outbuf[log_buffer_size-1] == '\0')
1107 || !(outbuf[log_buffer_size] == '\0')
1110 /* Repeat as assertions */
1111 assert(length < log_buffer_size);
1112 assert(outbuf[log_buffer_size-1] == '\0');
1114 * outbuf's real size is log_buffer_size+1,
1115 * so while this looks like an off-by-one,
1116 * we're only checking our paranoia byte.
1118 assert(outbuf[log_buffer_size] == '\0');
1120 snprintf(outbuf, log_buffer_size,
1121 "%s Privoxy(%08lx) Fatal error: log_error()'s sanity checks failed. length: %d\n"
1122 "Exiting.", timestamp, thread_id, (int)length);
1123 loglevel = LOG_LEVEL_FATAL;
1126 assert(NULL != logfp);
1128 if (loglevel == LOG_LEVEL_FATAL)
1130 fatal_error(outbuf_save);
1131 /* Never get here */
1133 fputs(outbuf_save, logfp);
1137 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
1138 /* Write to display */
1139 LogPutString(outbuf_save);
1140 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
1144 #else /* use traditional log_error version */
1146 /*********************************************************************
1148 * Function : log_error
1150 * Description : This is the error-reporting and logging function.
1153 * 1 : loglevel = the type of message to be logged
1154 * 2 : fmt = the main string we want logged, printf-like
1155 * 3 : ... = arguments to be inserted in fmt (printf-like).
1159 *********************************************************************/
1160 void log_error(int loglevel, const char *fmt, ...)
1164 static char *outbuf_save = NULL;
1165 const char * src = fmt;
1167 long this_thread = 1; /* was: pthread_t this_thread;*/
1171 #endif /* __OS2__ */
1173 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
1175 * Irrespective of debug setting, a GET/POST/CONNECT makes
1176 * the taskbar icon animate. (There is an option to disable
1177 * this but checking that is handled inside LogShowActivity()).
1179 if (loglevel == LOG_LEVEL_GPC)
1183 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
1185 /* verify if loglevel applies to current settings and bail out if negative */
1186 if ((loglevel & debug) == 0)
1191 /* protect the whole function because of the static buffer (outbuf) */
1194 /* FIXME get current thread id */
1195 #ifdef FEATURE_PTHREAD
1196 this_thread = (long)pthread_self();
1199 * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
1200 * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
1201 * pthread_t is supposed to be opaque... but it's fairly random, though, so
1202 * we make it mostly presentable.
1204 this_thread = abs(this_thread % 1000);
1205 #endif /* def __MACH__ */
1206 #elif defined(_WIN32)
1207 this_thread = GetCurrentThreadId();
1208 #elif defined(__OS2__)
1209 ulrc = DosGetInfoBlocks(&ptib, NULL);
1211 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
1212 #endif /* def FEATURE_PTHREAD */
1216 outbuf_save = outbuf = (char*)malloc(BUFFER_SIZE);
1217 if (NULL == outbuf_save)
1219 fatal_error("Privoxy failed to allocate log buffer.");
1222 outbuf = outbuf_save;
1226 * Write timestamp into tempbuf.
1228 * Complex because not all OSs have tm_gmtoff or
1229 * the %z field in strftime()
1234 #ifdef HAVE_LOCALTIME_R
1235 tm_now = *localtime_r(&now, &tm_now);
1236 #elif FEATURE_PTHREAD
1237 pthread_mutex_lock(&localtime_mutex);
1238 tm_now = *localtime (&now);
1239 pthread_mutex_unlock(&localtime_mutex);
1241 tm_now = *localtime (&now);
1243 strftime(outbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1244 outbuf += strlen( outbuf );
1248 case LOG_LEVEL_ERROR:
1249 outc = sprintf(outbuf, "Privoxy(%08lx) Error: ", this_thread);
1251 case LOG_LEVEL_FATAL:
1252 outc = sprintf(outbuf, "Privoxy(%08lx) Fatal error: ", this_thread);
1255 outc = sprintf(outbuf, "Privoxy(%08lx) Request: ", this_thread);
1257 case LOG_LEVEL_CONNECT:
1258 outc = sprintf(outbuf, "Privoxy(%08lx) Connect: ", this_thread);
1261 outc = sprintf(outbuf, "Privoxy(%08lx) Writing: ", this_thread);
1263 case LOG_LEVEL_HEADER:
1264 outc = sprintf(outbuf, "Privoxy(%08lx) Header: ", this_thread);
1266 case LOG_LEVEL_INFO:
1267 outc = sprintf(outbuf, "Privoxy(%08lx) Info: ", this_thread);
1269 case LOG_LEVEL_RE_FILTER:
1270 outc = sprintf(outbuf, "Privoxy(%08lx) Re-Filter: ", this_thread);
1272 #ifdef FEATURE_FORCE_LOAD
1273 case LOG_LEVEL_FORCE:
1274 outc = sprintf(outbuf, "Privoxy(%08lx) Force: ", this_thread);
1276 #endif /* def FEATURE_FORCE_LOAD */
1277 #ifdef FEATURE_FAST_REDIRECTS
1278 case LOG_LEVEL_REDIRECTS:
1279 outc = sprintf(outbuf, "Privoxy(%08lx) Redirect: ", this_thread);
1281 #endif /* def FEATURE_FAST_REDIRECTS */
1282 case LOG_LEVEL_DEANIMATE:
1283 outc = sprintf(outbuf, "Privoxy(%08lx) Gif-Deanimate: ", this_thread);
1286 outbuf = outbuf_save;
1290 #ifdef FEATURE_KILL_POPUPS
1291 case LOG_LEVEL_POPUPS:
1292 outc = sprintf(outbuf, "Privoxy(%08lx) Kill-Popups: ", this_thread);
1294 #endif /* def FEATURE_KILL_POPUPS */
1296 outc = sprintf(outbuf, "Privoxy(%08lx) CGI: ", this_thread);
1299 outc = sprintf(outbuf, "Privoxy(%08lx) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
1303 /* get ready to scan var. args. */
1304 va_start( ap, fmt );
1306 /* build formatted message from fmt and var-args */
1307 while ((*src) && (outc < BUFFER_SIZE-2))
1309 char tempbuf[BUFFER_SIZE];
1314 unsigned long ulval;
1321 outbuf[outc++] = ch;
1328 outbuf[outc++] = '%';
1331 ival = va_arg( ap, int );
1333 outc += sprintf(tempbuf, "%d", ival);
1334 if (outc < BUFFER_SIZE-1)
1336 strcpy(outbuf + oldoutc, tempbuf);
1340 outbuf[oldoutc] = '\0';
1344 uval = va_arg( ap, unsigned );
1346 outc += sprintf(tempbuf, "%u", uval);
1347 if (outc < BUFFER_SIZE-1)
1349 strcpy(outbuf + oldoutc, tempbuf);
1353 outbuf[oldoutc] = '\0';
1357 /* this is a modifier that must be followed by u or d */
1361 lval = va_arg( ap, long );
1363 outc += sprintf(tempbuf, "%ld", lval);
1367 ulval = va_arg( ap, unsigned long );
1369 outc += sprintf(tempbuf, "%lu", ulval);
1374 sprintf(outbuf, "Privoxy(%08lx) Error: log_error(): Bad format string:\n"
1376 "Exiting.", this_thread, fmt);
1381 fputs(outbuf, logfp);
1382 fatal_error(outbuf);
1383 /* Never get here */
1386 if (outc < BUFFER_SIZE-1)
1388 strcpy(outbuf + oldoutc, tempbuf);
1392 outbuf[oldoutc] = '\0';
1397 * Note that char paramaters are converted to int, so we need to
1398 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
1400 outbuf[outc++] = (char) va_arg( ap, int );
1403 sval = va_arg( ap, char * );
1409 outc += strlen(sval);
1410 if (outc < BUFFER_SIZE-1)
1412 strcpy(outbuf + oldoutc, sval);
1416 outbuf[oldoutc] = '\0';
1420 /* Non-standard: Print a counted string. Takes 2 parameters:
1421 * int length, const char * string
1423 ival = va_arg( ap, int );
1424 sval = va_arg( ap, char * );
1435 if (outc < BUFFER_SIZE-1)
1437 memcpy(outbuf + oldoutc, sval, (size_t) ival);
1441 outbuf[oldoutc] = '\0';
1445 /* Non-standard: Print error code from errno */
1447 ival = WSAGetLastError();
1448 sval = w32_socket_strerr(ival, tempbuf);
1450 ival = sock_errno();
1452 sval = os2_socket_strerr(ival, tempbuf);
1456 sval = strerror(ival);
1458 #else /* ifndef _WIN32 */
1460 #ifdef HAVE_STRERROR
1461 sval = strerror(ival);
1462 #else /* ifndef HAVE_STRERROR */
1464 #endif /* ndef HAVE_STRERROR */
1467 sprintf(tempbuf, "(errno = %d)", ival);
1470 #endif /* ndef _WIN32 */
1472 outc += strlen(sval);
1473 if (outc < BUFFER_SIZE-1)
1475 strcpy(outbuf + oldoutc, sval);
1479 outbuf[oldoutc] = '\0';
1483 /* Non-standard: Print a Common Log File timestamp */
1486 * Write timestamp into tempbuf.
1488 * Complex because not all OSs have tm_gmtoff or
1489 * the %z field in strftime()
1494 #ifdef HAVE_LOCALTIME_R
1497 int days, hrs, mins;
1499 #ifdef HAVE_GMTIME_R
1500 gmt = *gmtime_r(&now, &gmt);
1501 #elif FEATURE_PTHREAD
1502 pthread_mutex_lock(&gmtime_mutex);
1503 gmt = *gmtime(&now);
1504 pthread_mutex_unlock(&gmtime_mutex);
1506 gmt = *gmtime(&now);
1508 #ifdef HAVE_LOCALTIME_R
1509 tm_now = localtime_r(&now, &dummy);
1510 #elif FEATURE_PTHREAD
1511 pthread_mutex_lock(&localtime_mutex);
1512 tm_now = localtime (&now);
1513 pthread_mutex_unlock(&localtime_mutex);
1515 tm_now = localtime (&now);
1517 days = tm_now->tm_yday - gmt.tm_yday;
1518 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
1519 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
1520 strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now);
1521 sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60);
1524 outc += strlen(tempbuf);
1525 if (outc < BUFFER_SIZE-1)
1527 strcpy(outbuf + oldoutc, tempbuf);
1531 outbuf[oldoutc] = '\0';
1535 sprintf(outbuf, "Privoxy(%08lx) Error: log_error(): Bad format string:\n"
1537 "Exiting.", this_thread, fmt);
1542 fputs(outbuf_save, logfp);
1544 fatal_error(outbuf_save);
1545 /* Never get here */
1550 } /* for( p ... ) */
1552 /* done with var. args */
1555 if (outc >= BUFFER_SIZE-2)
1557 /* insufficient room for newline and trailing null. */
1559 static const char warning[] = "... [too long, truncated]\n";
1561 if (outc < BUFFER_SIZE)
1563 /* Need to add terminating null in this case. */
1564 outbuf[outc] = '\0';
1567 /* Truncate output */
1568 outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
1570 /* Append warning */
1571 strcat(outbuf, warning);
1575 /* Add terminating newline and null */
1576 outbuf[outc++] = '\n';
1577 outbuf[outc] = '\0';
1580 /* deal with glibc stupidity - it won't let you initialize logfp */
1586 fputs(outbuf_save, logfp);
1588 if (loglevel == LOG_LEVEL_FATAL)
1590 fatal_error(outbuf_save);
1591 /* Never get here */
1596 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
1597 /* Write to display */
1598 LogPutString(outbuf_save);
1599 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
1602 #endif /* defined(USE_NEW_LOG_ERROR) */
1605 /*********************************************************************
1607 * Function : jb_err_to_string
1609 * Description : Translates JB_ERR_FOO codes into strings.
1611 * XXX: the type of error codes is jb_err
1612 * but the typedef'inition is currently not
1613 * visible to all files that include errlog.h.
1616 * 1 : error = a valid jb_err code
1618 * Returns : A string with the jb_err translation
1620 *********************************************************************/
1621 const char *jb_err_to_string(int error)
1626 return "Success, no error";
1628 return "Out of memory";
1629 case JB_ERR_CGI_PARAMS:
1630 return "Missing or corrupt CGI parameters";
1632 return "Error opening, reading or writing a file";
1634 return "Parse error";
1635 case JB_ERR_MODIFIED:
1636 return "File has been modified outside of the CGI actions editor.";
1637 case JB_ERR_COMPRESS:
1638 return "(De)compression failure";
1641 return "Unknown error";
1644 return "Internal error";
1648 /*********************************************************************
1650 * Function : w32_socket_strerr
1652 * Description : Translate the return value from WSAGetLastError()
1656 * 1 : errcode = The return value from WSAGetLastError().
1657 * 2 : tmp_buf = A temporary buffer that might be used to
1660 * Returns : String representing the error code. This may be
1661 * a global string constant or a string stored in
1664 *********************************************************************/
1665 static char *w32_socket_strerr(int errcode, char *tmp_buf)
1667 #define TEXT_FOR_ERROR(code,text) \
1668 if (errcode == code) \
1670 return #code " - " text; \
1673 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
1674 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
1675 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
1676 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
1677 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
1678 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
1679 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
1680 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
1681 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
1682 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
1683 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
1684 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
1685 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
1686 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
1687 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
1688 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
1689 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
1690 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
1691 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
1692 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
1693 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
1694 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
1695 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
1696 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
1697 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
1698 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
1699 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
1700 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
1701 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
1702 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
1703 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
1704 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
1705 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
1706 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
1707 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
1708 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
1709 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
1710 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
1711 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
1712 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
1713 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
1714 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
1716 * The following error codes are documented in the Microsoft WinSock
1717 * reference guide, but don't actually exist.
1719 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
1720 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
1721 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
1722 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
1723 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
1724 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
1725 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
1726 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
1727 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
1728 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
1731 sprintf(tmp_buf, "(error number %d)", errcode);
1734 #endif /* def _WIN32 */
1738 /*********************************************************************
1740 * Function : os2_socket_strerr
1742 * Description : Translate the return value from sock_errno()
1746 * 1 : errcode = The return value from sock_errno().
1747 * 2 : tmp_buf = A temporary buffer that might be used to
1750 * Returns : String representing the error code. This may be
1751 * a global string constant or a string stored in
1754 *********************************************************************/
1755 static char *os2_socket_strerr(int errcode, char *tmp_buf)
1757 #define TEXT_FOR_ERROR(code,text) \
1758 if (errcode == code) \
1760 return #code " - " text; \
1763 TEXT_FOR_ERROR(SOCEPERM , "Not owner.")
1764 TEXT_FOR_ERROR(SOCESRCH , "No such process.")
1765 TEXT_FOR_ERROR(SOCEINTR , "Interrupted system call.")
1766 TEXT_FOR_ERROR(SOCENXIO , "No such device or address.")
1767 TEXT_FOR_ERROR(SOCEBADF , "Bad file number.")
1768 TEXT_FOR_ERROR(SOCEACCES , "Permission denied.")
1769 TEXT_FOR_ERROR(SOCEFAULT , "Bad address.")
1770 TEXT_FOR_ERROR(SOCEINVAL , "Invalid argument.")
1771 TEXT_FOR_ERROR(SOCEMFILE , "Too many open files.")
1772 TEXT_FOR_ERROR(SOCEPIPE , "Broken pipe.")
1773 TEXT_FOR_ERROR(SOCEWOULDBLOCK , "Operation would block.")
1774 TEXT_FOR_ERROR(SOCEINPROGRESS , "Operation now in progress.")
1775 TEXT_FOR_ERROR(SOCEALREADY , "Operation already in progress.")
1776 TEXT_FOR_ERROR(SOCENOTSOCK , "Socket operation on non-socket.")
1777 TEXT_FOR_ERROR(SOCEDESTADDRREQ , "Destination address required.")
1778 TEXT_FOR_ERROR(SOCEMSGSIZE , "Message too long.")
1779 TEXT_FOR_ERROR(SOCEPROTOTYPE , "Protocol wrong type for socket.")
1780 TEXT_FOR_ERROR(SOCENOPROTOOPT , "Protocol not available.")
1781 TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
1782 TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
1783 TEXT_FOR_ERROR(SOCEOPNOTSUPP , "Operation not supported.")
1784 TEXT_FOR_ERROR(SOCEPFNOSUPPORT , "Protocol family not supported.")
1785 TEXT_FOR_ERROR(SOCEAFNOSUPPORT , "Address family not supported by protocol family.")
1786 TEXT_FOR_ERROR(SOCEADDRINUSE , "Address already in use.")
1787 TEXT_FOR_ERROR(SOCEADDRNOTAVAIL , "Can't assign requested address.")
1788 TEXT_FOR_ERROR(SOCENETDOWN , "Network is down.")
1789 TEXT_FOR_ERROR(SOCENETUNREACH , "Network is unreachable.")
1790 TEXT_FOR_ERROR(SOCENETRESET , "Network dropped connection on reset.")
1791 TEXT_FOR_ERROR(SOCECONNABORTED , "Software caused connection abort.")
1792 TEXT_FOR_ERROR(SOCECONNRESET , "Connection reset by peer.")
1793 TEXT_FOR_ERROR(SOCENOBUFS , "No buffer space available.")
1794 TEXT_FOR_ERROR(SOCEISCONN , "Socket is already connected.")
1795 TEXT_FOR_ERROR(SOCENOTCONN , "Socket is not connected.")
1796 TEXT_FOR_ERROR(SOCESHUTDOWN , "Can't send after socket shutdown.")
1797 TEXT_FOR_ERROR(SOCETOOMANYREFS , "Too many references: can't splice.")
1798 TEXT_FOR_ERROR(SOCETIMEDOUT , "Operation timed out.")
1799 TEXT_FOR_ERROR(SOCECONNREFUSED , "Connection refused.")
1800 TEXT_FOR_ERROR(SOCELOOP , "Too many levels of symbolic links.")
1801 TEXT_FOR_ERROR(SOCENAMETOOLONG , "File name too long.")
1802 TEXT_FOR_ERROR(SOCEHOSTDOWN , "Host is down.")
1803 TEXT_FOR_ERROR(SOCEHOSTUNREACH , "No route to host.")
1804 TEXT_FOR_ERROR(SOCENOTEMPTY , "Directory not empty.")
1805 TEXT_FOR_ERROR(SOCEOS2ERR , "OS/2 Error.")
1807 sprintf(tmp_buf, "(error number %d)", errcode);
1810 #endif /* def __OS2__ */