File: error_msg.c

package info (click to toggle)
dcc 1.2.74-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,552 kB
  • ctags: 4,041
  • sloc: ansic: 41,034; perl: 2,310; sh: 2,186; makefile: 224
file content (228 lines) | stat: -rw-r--r-- 4,810 bytes parent folder | download | duplicates (2)
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
/* Distributed Checksum Clearinghouse
 *
 * Copyright (c) 2005 by Rhyolite Software
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Rhyolite Software DCC 1.2.74-1.28 $Revision$
 */

#include "dcc_defs.h"
#include "dcc_paths.h"
#ifndef DCC_WIN32
#include <syslog.h>
#endif

extern void dcc_syslog_lock(void);
extern void dcc_syslog_unlock(void);

u_char dcc_no_syslog;

int dcc_error_priority = LOG_ERR | LOG_MAIL;
int dcc_trace_priority = LOG_NOTICE | LOG_MAIL;


/* commonly used, but not thread safe */
int dcc_ex_code = EX_UNAVAILABLE;


DCC_PATH dcc_progname;

#ifdef HAVE___PROGNAME
extern const char *__progname;
#endif

void
dcc_syslog_init(u_char use_syslog,
		const char *argv0 UATTRIB, const char *suffix)
{
	const char *p;
#ifndef DCC_WIN32
	struct stat sb;
	int fd;
#endif

	/* Solaris defaults to "syslog" with a null identification string,
	 * but does not seem to have __progname set by crt0. */
#undef GOT_PROGNAME
#ifdef HAVE_GETPROGNAME
	p = getprogname();
#	define GOT_PROGNAME
#endif
#if defined(HAVE___PROGNAME) && !defined(GOT_PROGNAME)
	p = __progname;
#	define GOT_PROGNAME
#endif
#ifndef GOT_PROGNAME
	p = strrchr(argv0, '/');
	if (!p)
		p = argv0;
	else
		++p;
#endif
	snprintf(dcc_progname, sizeof(dcc_progname), "%s%s",
		 p, suffix ? suffix : "");

#ifdef DCC_WIN32
	dcc_no_syslog = 1;
#else
	/* Don't wait for the console if somehow we must use it,
	 * because that messes up dccm. */
#ifndef LOG_NOWAIT
#define LOG_NOWAIT 0
#endif
	openlog(dcc_progname, LOG_PID | LOG_NOWAIT, LOG_MAIL);
	dcc_no_syslog = !use_syslog;

	/* ensure that stdout and stderr exist so that when we open
	 * database or other files, we don't get file descriptor 1 or 2
	 * and then later write error messages to them. */
	if (0 > fstat(STDOUT_FILENO, &sb) && errno == EBADF) {
		fd = open(_PATH_DEVNULL, 0, O_RDWR);
		if (fd < 0)
			return;
		if (fd != STDOUT_FILENO) {
			dup2(fd, STDOUT_FILENO);
			close(fd);
		}
	}
	if (0 > fstat(STDERR_FILENO, &sb) && errno == EBADF) {
		fd = open(_PATH_DEVNULL, 0, O_RDWR);
		if (fd < 0)
			return;
		if (fd != STDERR_FILENO) {
			dup2(fd, STDERR_FILENO);
			close(fd);
		}
	}
#endif /* DCC_WIN32 */
}



void
dcc_vfatal_msg(const char *p, va_list args)
{
	char sbuf[200];
	int i;
	COPY_DCC_ARGS2();

	fflush(stdout);			/* keep stderr and stdout straight */
	vfprintf(stderr, p, DCC_ARGS2);
	fputs("; fatal error\n", stderr);
	fflush(stderr);
	END_DCC_ARGS2();

	if (dcc_no_syslog)
		return;

	dcc_syslog_lock();
	/* try to write the message with the "fatal error" addition as
	 * a single message */
	i = vsnprintf(sbuf, sizeof(sbuf), p, args);
	if (i < ISZ(sbuf)-ISZ("fatal error")) {
		syslog(dcc_error_priority, "%s; fatal error", sbuf);
	} else {
		vsyslog(dcc_error_priority, p, args);
		syslog(dcc_error_priority, "fatal error");
	}
	dcc_syslog_unlock();
}



void
dcc_verror_msg(const char *p, va_list args)
{
	COPY_DCC_ARGS2();

	fflush(stdout);			/* keep stderr and stdout straight */
	vfprintf(stderr, p, DCC_ARGS2);
	fputc('\n', stderr);
	END_DCC_ARGS2();

	if (dcc_no_syslog)
		return;
	dcc_syslog_lock();
	vsyslog(dcc_error_priority, p, args);
	dcc_syslog_unlock();
}



void PATTRIB(1,2)
dcc_error_msg(const char *p, ...)
{
	va_list args;

	va_start(args, p);
	dcc_verror_msg(p, args);
	va_end(args);
}



void
dcc_vtrace_msg(const char *p, va_list args)
{
	COPY_DCC_ARGS2();

	fflush(stdout);			/* keep stderr and stdout straight */
	vfprintf(stderr, p, DCC_ARGS2);
	fputc('\n', stderr);
	END_DCC_ARGS2();

	if (dcc_no_syslog)
		return;
	dcc_syslog_lock();
	vsyslog(dcc_trace_priority, p, args);
	dcc_syslog_unlock();
}



void PATTRIB(1,2)
dcc_trace_msg(const char *p, ...)
{
	va_list args;

	va_start(args, p);
	dcc_vtrace_msg(p, args);
	va_end(args);
}



void
dcc_vpemsg(int ex_code, DCC_EMSG emsg, const char *msg, va_list args)
{
	if (!emsg) {
		dcc_verror_msg(msg, args);
	} else {
		dcc_ex_code = ex_code;
		vsnprintf(emsg, sizeof(DCC_EMSG), msg, args);
	}
}



void PATTRIB(3,4)
dcc_pemsg(int ex_code, DCC_EMSG emsg, const char *msg, ...)
{
	va_list args;

	va_start(args, msg);
	dcc_vpemsg(ex_code, emsg, msg, args);
	va_end(args);
}