-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarm.c
235 lines (201 loc) · 5.56 KB
/
alarm.c
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
/*
* The Spread Toolkit.
*
* The contents of this file are subject to the Spread Open-Source
* License, Version 1.0 (the ``License''); you may not use
* this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.spread.org/license/
*
* or in the file ``license.txt'' found in this distribution.
*
* Software distributed under the License is distributed on an AS IS basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Creators of Spread are:
* Yair Amir, Michal Miskin-Amir, Jonathan Stanton.
*
* Copyright (C) 1993-2001 Spread Concepts LLC <[email protected]>
*
* All Rights Reserved.
*
* Major Contributor(s):
* ---------------
* Dan Schoenblum [email protected] - Java Interface Developer.
* John Schultz [email protected] - contribution to process group membership.
* Theo Schlossnagle [email protected] - Perl library and Skiplists.
*
*/
/* 2001-08-08 Theo Schlossnagle -- removed dependency on arch.h */
/* 2001-10-10 Aashima Munjal -- modified to add ability to write to syslog for wackamole */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "config.h"
#ifdef HAVE_GOOD_VARGS
#include <stdarg.h>
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
static int syslog_flag = 0;
/* We are on Linux.. for now. And have goo varargs. */
#define HAVE_GOOD_VARGS
#include "alarm.h"
static int32 wack_alarm_mask = PRINT | EXIT ;
static char *wack_alarm_timestamp_format = NULL;
static const char *DEFAULT_TIMESTAMP_FORMAT="[%a %d %b %Y %H:%M:%S]";
static int wack_alarmInteractiveProgram = FALSE;
#ifdef HAVE_GOOD_VARGS
/* Probably should work on all platforms, but just in case, I leave it to the
developers...
*/
void wack_alarm( int32 mask, char *message, ...)
{
char *s;
if ( wack_alarm_mask & mask )
{
va_list ap;
if ( wack_alarm_timestamp_format )
{
char timestamp[42];
struct tm *tm_now;
time_t time_now;
size_t length;
time_now = time(NULL);
tm_now = localtime(&time_now);
length = strftime(timestamp, 40,
wack_alarm_timestamp_format, tm_now);
timestamp[length] = ' ';
#ifdef HAVE_SYSLOG_H
if(syslog_flag)
syslog(LOG_NOTICE, timestamp);
else
#endif
fwrite(timestamp, length+1, sizeof(char), stdout);
}
va_start(ap,message);
#ifdef HAVE_SYSLOG_H
if(syslog_flag){
int len = strlen(message)+100;
s = malloc(len);/*estimation*/
vsnprintf(s,len,message, ap);
syslog(LOG_NOTICE, s);
free(s);
}
else
#endif
{
vprintf(message, ap);
putchar('\n');
}
va_end(ap);
}
if ( EXIT & mask )
{
exit( 0 );
}
}
#else
void wack_alarm( int32 mask, char *message,
void *ptr1, void *ptr2, void *ptr3, void *ptr4,
void *ptr5, void *ptr6, void *ptr7, void *ptr8,
void *ptr9, void *ptr10, void*ptr11, void *ptr12,
void *ptr13, void *ptr14, void *ptr15, void *ptr16,
void *ptr17, void *ptr18, void *ptr19, void *ptr20){
if ( wack_alarm_mask & mask )
{
if ( wack_alarm_timestamp_format )
{
char timestamp[42];
struct tm *tm_now;
time_t time_now;
size_t length;
time_now = time(NULL);
tm_now = localtime(&time_now);
length = strftime(timestamp, 40,
wack_alarm_timestamp_format, tm_now);
timestamp[length] = ' ';
if(syslog_flag)
syslog(LOG_NOTICE, timestamp);
else
fwrite(timestamp, length+1, sizeof(char), stdout);
}
if(syslog_flag){
syslog(LOG_NOTICE, message, ptr1, ptr2, ptr3, ptr4, ptr5, ptr6,
ptr7, ptr8,ptr9,ptr10, ptr11, ptr12, ptr13, ptr14, ptr15,
ptr16, ptr17, ptr18, ptr19, ptr20 );
}
else
printf(message, ptr1, ptr2, ptr3, ptr4, ptr5, ptr6, ptr7, ptr8, ptr9,
ptr10, ptr11, ptr12, ptr13, ptr14, ptr15, ptr16, ptr17, ptr18,
ptr19, ptr20 );
putchar('\n');
}
if ( EXIT & mask )
{
perror("errno say:");
exit( 0 );
}
}
#endif /* HAVE_GOOD_VARGS */
void wack_alarm_enable_syslog(char *ident){
#ifdef HAVE_SYSLOG_H
openlog(ident,LOG_PID, LOG_DAEMON);
syslog_flag = 1;
#endif
}
void wack_alarm_set_interactive(void)
{
wack_alarmInteractiveProgram = TRUE;
}
int wack_alarm_get_interactive(void)
{
return(wack_alarmInteractiveProgram);
}
void wack_alarm_set_output(char *filename) {
FILE *newfile;
newfile = freopen(filename, "a", stdout);
if ( NULL == newfile ) {
perror("failed to open file for stdout");
}
newfile = freopen(filename, "a", stderr);
if ( NULL == newfile ) {
perror("failed to open file for stderr");
}
setvbuf(stderr, (char *)0, _IONBF, 0);
setvbuf(stdout, (char *)0, _IONBF, 0);
}
void wack_alarm_enable_timestamp(char *format)
{
static char _local_timestamp[40];
if(format)
strncpy(_local_timestamp, format, 40);
else
strncpy(_local_timestamp, DEFAULT_TIMESTAMP_FORMAT, 40);
wack_alarm_timestamp_format = _local_timestamp;
}
void wack_alarm_disable_timestamp(void)
{
wack_alarm_timestamp_format = NULL;
}
void wack_alarm_set(int32 mask)
{
wack_alarm_mask = wack_alarm_mask | mask;
}
void wack_alarm_clear(int32 mask)
{
wack_alarm_mask = wack_alarm_mask & ~mask;
}
int32 wack_alarm_get(void)
{
return(wack_alarm_mask);
}