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
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* 
 * Access to syslog from Pike. 
 * 
 * Henrik Grubbström 1997-01-28 
 */ 
 
/* 
 * Includes 
 */ 
 
#include "global.h" 
 
#include "system_machine.h" 
#include "system.h" 
 
#ifdef HAVE_SYSLOG 
 
#include "interpret.h" 
#include "svalue.h" 
#include "stralloc.h" 
#include "threads.h" 
#include "module_support.h" 
#include "builtin_functions.h" 
 
#ifdef HAVE_SYSLOG_H 
#include <syslog.h> 
#elif defined(HAVE_SYS_SYSLOG_H) 
#include <sys/syslog.h> 
#endif 
 
#ifndef LOG_PID 
#define LOG_PID 0 
#endif 
#ifndef LOG_AUTH 
#define LOG_AUTH 0 
#endif 
#ifndef LOG_AUTHPRIV 
#define LOG_AUTHPRIV 0 
#endif 
#ifndef LOG_CRON 
#define LOG_CRON 0 
#endif 
#ifndef LOG_DAEMON 
#define LOG_DAEMON 0 
#endif 
#ifndef LOG_KERN 
#define LOG_KERN 0 
#endif 
#ifndef LOG_LOCAL0 
#define LOG_LOCAL0 0 
#endif 
#ifndef LOG_LOCAL1 
#define LOG_LOCAL1 0 
#endif 
#ifndef LOG_LOCAL2 
#define LOG_LOCAL2 0 
#endif 
#ifndef LOG_LOCAL3 
#define LOG_LOCAL3 0 
#endif 
#ifndef LOG_LOCAL4 
#define LOG_LOCAL4 0 
#endif 
#ifndef LOG_LOCAL5 
#define LOG_LOCAL5 0 
#endif 
#ifndef LOG_LOCAL6 
#define LOG_LOCAL6 0 
#endif 
#ifndef LOG_LOCAL7 
#define LOG_LOCAL7 0 
#endif 
#ifndef LOG_LPR 
#define LOG_LPR 0 
#endif 
#ifndef LOG_MAIL 
#define LOG_MAIL 0 
#endif 
#ifndef LOG_NEWS 
#define LOG_NEWS 0 
#endif 
#ifndef LOG_SYSLOG 
#define LOG_SYSLOG 0 
#endif 
#ifndef LOG_USER 
#define LOG_USER 0 
#endif 
#ifndef LOG_UUCP 
#define LOG_UUCP 0 
#endif 
#ifndef LOG_CONS 
#define LOG_CONS 0 
#endif 
#ifndef LOG_NDELAY 
#define LOG_NDELAY 0 
#endif 
#ifndef LOG_PERROR 
#define LOG_PERROR 0 
#endif 
 
/*! @module System 
 */ 
 
/*! @decl void openlog(string ident, int options, int facility) 
 *! 
 *! Initializes the connection to syslogd. 
 *! 
 *! @param ident. 
 *! The @[ident] argument specifies an identifier to tag all logentries 
 *! with. 
 *! 
 *! @param options 
 *! A bitfield specifying the behaviour of the message 
 *! logging. Valid options are: 
 *! 
 *! @int 
 *!   @value LOG_PID 
 *!     Log the process ID with each message. 
 *!   @value LOG_CONS 
 *!     Write messages to the console if they can't be sent to syslogd. 
 *!   @value LOG_NDELAY 
 *!     Open the connection to syslogd now and not later. 
 *!   @value LOG_NOWAIT 
 *!     Do not wait for subprocesses talking to syslogd. 
 *! @endint 
 *! 
 *! @param facility 
 *! Specifies what subsystem you want to log as. Valid 
 *! facilities are: 
 *! 
 *! @int 
 *!   @value LOG_AUTH 
 *!     Authorization subsystem 
 *!   @value LOG_AUTHPRIV 
 *! 
 *!   @value LOG_CRON 
 *!     Crontab subsystem 
 *!   @value LOG_DAEMON 
 *!     System daemons 
 *!   @value LOG_KERN 
 *!     Kernel subsystem (NOT USABLE) 
 *!   @value LOG_LOCAL 
 *!   @value LOG_LOCAL1 
 *!   @value LOG_LOCAL2 
 *!   @value LOG_LOCAL3 
 *!   @value LOG_LOCAL4 
 *!   @value LOG_LOCAL5 
 *!   @value LOG_LOCAL6 
 *!   @value LOG_LOCAL7 
 *!     For local use 
 *!   @value LOG_LPR 
 *!     Line printer spooling system 
 *!   @value LOG_MAIL 
 *!     Mail subsystem 
 *!   @value LOG_NEWS 
 *!     Network news subsystem 
 *!   @value LOG_SYSLOG 
 *! 
 *!   @value LOG_USER 
 *! 
 *!   @value LOG_UUCP 
 *!     UUCP subsystem 
 *! @endint 
 *! 
 *! @note 
 *! Only available on systems with syslog(3). 
 *! 
 *! @bugs 
 *! LOG_NOWAIT should probably always be specified. 
 *! 
 *! @seealso 
 *!   @[syslog], @[closelog] 
 */ 
void f_openlog(INT32 args) 
{ 
  char *ident; 
  INT_TYPE p_option, p_facility; 
  INT_TYPE option=0, facility=0; 
 
  get_all_args(NULL, args, "%s%i%i", &ident, &p_option, &p_facility); 
 
  if(p_option & (1<<0)) option |= LOG_CONS; 
  if(p_option & (1<<1)) option |= LOG_NDELAY; 
  if(p_option & (1<<2)) option |= LOG_PERROR; 
  if(p_option & (1<<3)) option |= LOG_PID; 
 
  if(p_facility & (1<<0)) facility |= LOG_AUTH; /* Don't use */ 
  if(p_facility & (1<<1)) facility |= LOG_AUTHPRIV; 
  if(p_facility & (1<<2)) facility |= LOG_CRON; 
  if(p_facility & (1<<3)) facility |= LOG_DAEMON; 
  if(p_facility & (1<<4)) facility |= LOG_KERN; 
  if(p_facility & (1<<5)) facility |= LOG_LOCAL0; 
  if(p_facility & (1<<6)) facility |= LOG_LOCAL1; 
  if(p_facility & (1<<7)) facility |= LOG_LOCAL2; 
  if(p_facility & (1<<8)) facility |= LOG_LOCAL3; 
  if(p_facility & (1<<9)) facility |= LOG_LOCAL4; 
  if(p_facility & (1<<10)) facility |= LOG_LOCAL5; 
  if(p_facility & (1<<11)) facility |= LOG_LOCAL6; 
  if(p_facility & (1<<12)) facility |= LOG_LOCAL7; 
  if(p_facility & (1<<13)) facility |= LOG_LPR; 
  if(p_facility & (1<<14)) facility |= LOG_MAIL; 
  if(p_facility & (1<<15)) facility |= LOG_NEWS; 
  if(p_facility & (1<<16)) facility |= LOG_SYSLOG; 
  if(p_facility & (1<<17)) facility |= LOG_USER; 
  if(p_facility & (1<<18)) facility |= LOG_UUCP; 
 
#ifdef LOG_NOWAIT 
  /* Don't let syslog wait for forked processes */ 
  option |= LOG_NOWAIT; 
#endif /* LOG_NOWAIT */ 
 
  THREADS_ALLOW(); 
 
  openlog(ident, option, facility); 
 
  THREADS_DISALLOW(); 
 
  pop_n_elems(args); 
} 
 
/*! @decl void syslog(int priority, string msg) 
 *! 
 *! Writes the message @[msg] to the log with the priorities in 
 *! @[priority]. 
 *! 
 *! @param priority 
 *!   Priority is a bit vector with the wanted priorities or:ed 
 *!   together. 
 *!   @int 
 *!     @value 0 
 *!       LOG_EMERG, system is unusable. 
 *!     @value 1 
 *!       LOG_ALERT, action must be taken immediately. 
 *!     @value 2 
 *!       LOG_CRIT, critical conditions. 
 *!     @value 3 
 *!       LOG_ERR, error conditions. 
 *!     @value 4 
 *!       LOG_WARNING, warnind conditions. 
 *!     @value 5 
 *!       LOG_NOTICE, normal, but significant, condition. 
 *!     @value 6 
 *!       LOG_INFO, informational message. 
 *!     @value 7 
 *!       LOG_DEBUG, debug-level message. 
 *!   @endint 
 */ 
void f_syslog(INT32 args) 
{ 
  struct pike_string *s; 
  INT_TYPE pri=0, i; 
 
  get_all_args(NULL, args, "%i%S", &i, &s); 
 
  if(i & (1<<0)) pri |= LOG_EMERG; 
  if(i & (1<<1)) pri |= LOG_ALERT; 
  if(i & (1<<2)) pri |= LOG_CRIT; 
  if(i & (1<<3)) pri |= LOG_ERR; 
  if(i & (1<<4)) pri |= LOG_WARNING; 
  if(i & (1<<5)) pri |= LOG_NOTICE; 
  if(i & (1<<6)) pri |= LOG_INFO; 
  if(i & (1<<7)) pri |= LOG_DEBUG; 
 
#ifndef MIGHT_HAVE_SYSLOG_RACES 
  /* glibc/linuxthreads has a race in syslog(2) that can make write, 
   * writev etc crash with the instruction pointer set to 0x1 when 
   * signals are delivered. Not releasing the interpreter lock here 
   * avoids that race most of the time but it's still not a 100% 
   * solution. 
   * 
   * C.f. the report "bug in thread support" filed by Balazs Scheidler 
   * in Oct 22, 2001 (http://sources.redhat.com/ml/libc-hacker/ 
   * 2001-10/msg00020.html). It's verified to still exist in glibc 
   * 2.2.93 (RedHat 8.0) and 2.3.2 (RedHat 9). */ 
  THREADS_ALLOW(); 
#endif 
 
  syslog(pri, "%s", s->str); 
 
#ifndef MIGHT_HAVE_SYSLOG_RACES 
  THREADS_DISALLOW(); 
#endif 
 
  pop_n_elems(args); 
} 
 
/*! @decl void closelog() 
 *! 
 *! @fixme 
 *!   Document this function. 
 */ 
void f_closelog(INT32 args) 
{ 
  closelog(); 
  pop_n_elems(args); 
} 
 
/*! @endmodule 
 */ 
 
#endif /* HAVE_SYSLOG */