File: error-handler.c

package info (click to toggle)
dhcpcd 0.70-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 276 kB
  • ctags: 270
  • sloc: ansic: 2,300; sh: 391; makefile: 93
file content (169 lines) | stat: -rw-r--r-- 3,100 bytes parent folder | download | duplicates (3)
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
/* $Id: error-handler.c,v 0.2 1997/08/30 18:00:52 yoichi v0_70 $
 *
 * dhcpcd - DHCP client daemon -
 * Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
 *
 * Dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include "error-handler.h"

#define MAXLINE 1024

int DebugFlag;

static void	errorPrint(int flag, const char *fmt, va_list ap);
static void	sendLog(int flag, int priority, const char *fmt, va_list ap);


static void
errorPrint(int flag, const char *fmt, va_list ap)
{
	int		errnoOrg;
	char	buf[MAXLINE];

	errnoOrg = errno;
	vsprintf(buf, fmt, ap);
	if ( flag ) {
		sprintf(buf+strlen(buf), ": %s", strerror(errnoOrg));
	}
	strcat(buf, "\n");
	fflush(stdout);			/* in case stdout and stderr are the same */
	fputs(buf, stderr);
	fflush(NULL);			/* flushes all stdio output streams */
}

void
errSysExit(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	errorPrint(1, fmt, ap);
	va_end(ap);
	exit(1);
}

void
errSysRet(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	errorPrint(1, fmt, ap);
	va_end(ap);
}

void
errQuit(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	errorPrint(0, fmt, ap);
	va_end(ap);
	exit(1);
}


void
errMsg(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	errorPrint(0, fmt, ap);
	va_end(ap);
}

static void
sendLog(int flag, int priority, const char *fmt, va_list ap)
{
	int  errnoOrg;
	char buf[MAXLINE];

	errnoOrg = errno;
	vsprintf(buf, fmt, ap);
	if ( flag ) {
		sprintf(buf+strlen(buf), ": %s", strerror(errnoOrg));
	}
	strcat(buf, "\n");
	if ( DebugFlag ) {
		fflush(stdout);
		fputs(buf, stderr);
		fflush(NULL);
	} else {
		syslog(priority, buf);
	}
}

void
logOpen(const char *ident, int option, int facility)
{
	if ( !DebugFlag ) {
		openlog(ident, option, facility);
	}
}

void
logSysExit(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	sendLog(1, LOG_ERR, fmt, ap);
	va_end(ap);
	closelog();
	exit(2);
}

void
logSysRet(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	sendLog(1, LOG_ERR, fmt, ap);
	va_end(ap);
}

void
logQuit(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	sendLog(0, LOG_ERR, fmt, ap);
	va_end(ap);
	closelog();
	exit(2);
}

void
logRet(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	sendLog(0, LOG_ERR, fmt, ap);
	va_end(ap);
}