summaryrefslogtreecommitdiffstats
path: root/lib/base/ereport.cpp
blob: 3454d01f95e4d6e54aef3f0abb6c51691caabb9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/*
 * ereport.c:  Records transactions, reports errors to administrators, etc.
 * 
 * Rob McCool
 */


#include "private/pprio.h" /* for nspr20 binary release */
#include "netsite.h"
#include "file.h"      /* system_fopenWA, system_write_atomic */
#include "pblock.h"
#include "session.h"
#include "util.h"      /* util_vsprintf */
#include "ereport.h"

#include "base/dbtbase.h"

#include <stdarg.h>
#include <stdio.h>      /* vsprintf */
#include <string.h>     /* strcpy */
#include <time.h>       /* localtime */

#ifdef XP_UNIX
#include <syslog.h>     /* error logging to syslog */

static SYS_FILE _error_fd;
#else /* WIN32 */
#include <nt/regparms.h>
#include <nt/messages.h>
#include "eventlog.h"

static SYS_FILE _error_fd;
#endif /* XP_UNIX */

static PRBool _ereport_initialized = PR_FALSE;


NSAPI_PUBLIC SYS_FILE ereport_getfd(void) { return _error_fd; }



/* ----------------------------- ereport_init ----------------------------- */


NSAPI_PUBLIC char *ereport_init(char *err_fn, char *email, 
                                PASSWD pwuser, char *version)
{
    char err[MAGNUS_ERROR_LEN];
    SYS_FILE new_fd;

#ifdef XP_UNIX
    if(!strcmp(err_fn, "SYSLOG")) {
#ifdef NET_SSL
        openlog("secure-httpd", LOG_PID, LOG_DAEMON);
#else
        openlog("httpd", LOG_PID, LOG_DAEMON);
#endif
	_error_fd = PR_ImportFile(ERRORS_TO_SYSLOG);

        _ereport_initialized = PR_TRUE;
        return NULL;
    }
#endif /* XP_UNIX */
    if( (new_fd = system_fopenWA(err_fn)) == SYS_ERROR_FD) {
        util_snprintf(err, MAGNUS_ERROR_LEN, "can't open error log %s (%s)", err_fn,
                     system_errmsg());
#ifdef XP_UNIX
	_error_fd = PR_ImportFile(STDERR_FILENO);
#else
	_error_fd = PR_ImportFile(NULL);
#endif
        return STRDUP(err);
    }
    _error_fd = new_fd;

#ifdef XP_UNIX
    if(pwuser)
        chown(err_fn, pwuser->pw_uid, pwuser->pw_gid);
#endif /* XP_UNIX */

    _ereport_initialized = PR_TRUE;

    ereport(LOG_INFORM, XP_GetAdminStr(DBT_successfulServerStartup_));
    ereport(LOG_INFORM, XP_GetAdminStr(DBT_SBS_), MAGNUS_VERSION_STRING, BUILD_NUM);
    if (strcasecmp(BUILD_NUM, version)) {
        ereport(LOG_WARN, XP_GetAdminStr(DBT_netscapeExecutableAndSharedLibra_));
        ereport(LOG_WARN, XP_GetAdminStr(DBT_executableVersionIsS_), version);
        ereport(LOG_WARN, XP_GetAdminStr(DBT_sharedLibraryVersionIsS_), BUILD_NUM);

    }

    /* Initialize thread-specific error handling */
    system_errmsg_init();

    return NULL;
}



/* -------------------------- ereport_terminate --------------------------- */


NSAPI_PUBLIC void ereport_terminate(void)
{
    if (!_ereport_initialized)
        return;

#ifdef XP_UNIX
    ereport(LOG_INFORM, XP_GetAdminStr(DBT_errorReportingShuttingDown_));
    if(PR_FileDesc2NativeHandle(_error_fd) != ERRORS_TO_SYSLOG)
        system_fclose(_error_fd);
    else
        closelog();
#else /* WIN32 */
    if(PR_FileDesc2NativeHandle(_error_fd) != NULL)
        system_fclose(_error_fd);
#endif /* XP_UNIX */

}


/* ------------------------------- ereport -------------------------------- */


static int degree_msg[] = {
                            DBT_warning_,
                            DBT_config_,
                            DBT_security_,
                            DBT_failure_,
                            DBT_catastrophe_,
                            DBT_info_,
                            DBT_verbose_
                          };

#ifdef XP_UNIX
static int degree_syslog[] = {
    LOG_WARNING, LOG_ERR, LOG_NOTICE, LOG_ALERT, LOG_CRIT, LOG_INFO, LOG_INFO
};
#endif/* XP_UNIX */


NSAPI_PUBLIC int ereport_v(int degree, char *fmt, va_list args)
{
    char errstr[MAX_ERROR_LEN];
    int pos = 0;
    struct tm *tms, tmss;
    time_t t;
#ifdef MCC_PROXY
    char *p;
    int i;
#endif /* MCC_PROXY */

    if (!_ereport_initialized)
        return IO_OKAY;

#ifdef XP_UNIX
    if(PR_FileDesc2NativeHandle(_error_fd) != ERRORS_TO_SYSLOG) {
#endif /* XP_UNIX */
        t = time(NULL);
        tms = system_localtime(&t, &tmss);
        util_strftime(errstr, ERR_TIMEFMT, tms);
        pos = strlen(errstr);

        pos += util_snprintf(&errstr[pos], MAX_ERROR_LEN - pos, " %s: ",
                            XP_GetAdminStr(degree_msg[degree]));
#ifdef XP_UNIX 
    }
#endif /* XP_UNIX */

    pos += util_vsnprintf(&errstr[pos], sizeof(errstr) - pos, fmt, args);

    pos += util_snprintf(&errstr[pos], sizeof(errstr) - pos, ENDLINE);

#ifdef MCC_PROXY
    /* Thanx to netlib, the proxy sometimes generates multiline err msgs */
    for(p=errstr, i=pos-1; i>0; i--, p++) {
	if (*p == '\n' || *p == '\r') *p = ' ';
    }
#endif /* MCC_PROXY */

#if defined XP_UNIX
    if(PR_FileDesc2NativeHandle(_error_fd) != ERRORS_TO_SYSLOG)
        return system_fwrite_atomic(_error_fd, errstr, pos);
    syslog(degree_syslog[degree], errstr);
    return IO_OKAY;
#elif defined XP_WIN32 /* XP_WIN32 */
	if(PR_FileDesc2NativeHandle(_error_fd) != NULL)
	{
#ifdef MCC_HTTPD
	/* also write to NT Event Log if error is serious */
		switch (degree) 
		{
         case LOG_MISCONFIG:
         case LOG_SECURITY:
         case LOG_CATASTROPHE:
           LogErrorEvent(NULL, EVENTLOG_ERROR_TYPE, 
                         0, MSG_BAD_PARAMETER, 
                         errstr, NULL);
           break;
			default:
           break;
		}
#endif
		return system_fwrite_atomic(_error_fd, errstr, pos);
	}
	else { /* log to the EventLogger */
                /* Write to the event logger... */
		switch (degree) {
                  case LOG_WARN:
                    LogErrorEvent(NULL, EVENTLOG_WARNING_TYPE, 
                                  0, MSG_BAD_PARAMETER, 
                                  errstr, NULL);
                    break;
                  case LOG_MISCONFIG:
                  case LOG_SECURITY:
                  case LOG_FAILURE:
                  case LOG_CATASTROPHE:
                    LogErrorEvent(NULL, EVENTLOG_ERROR_TYPE, 
                                  0, MSG_BAD_PARAMETER, 
                                  errstr, NULL);
                    break;

                  case LOG_INFORM:
                  default:
                    LogErrorEvent(NULL, EVENTLOG_INFORMATION_TYPE, 
                                  0, MSG_BAD_PARAMETER, 
                                  errstr, NULL);
                    break;
		}
		return (IO_OKAY);
	}
#endif /* XP_WIN32 */
	
}

NSAPI_PUBLIC int ereport(int degree, char *fmt, ...)
{
    va_list args;
    int rv;
    va_start(args, fmt);
    rv = ereport_v(degree, fmt, args);
    va_end(args);
    return rv;
}