3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 #define __INSIDE_CYGWIN_NET__
10 #define USE_SYS_TYPES_FD_SET
18 #include <sys/socket.h>
29 #define CYGWIN_LOG_NAME L"Cygwin"
33 wchar_t *process_ident
;
37 } syslog_globals
= { NULL
, 0, 0, LOG_UPTO (LOG_DEBUG
) };
39 /* openlog: save the passed args. Don't open the system log or /dev/log yet. */
41 openlog (const char *ident
, int logopt
, int facility
)
43 wchar_t *new_ident
= NULL
;
45 debug_printf ("openlog called with (%s, %d, %d)",
46 ident
? ident
: "<NULL>", logopt
, facility
);
50 sys_mbstowcs_alloc (&new_ident
, HEAP_NOTHEAP
, ident
);
52 debug_printf ("failed to allocate memory for "
53 "syslog_globals.process_ident");
56 wchar_t *old_ident
= syslog_globals
.process_ident
;
57 syslog_globals
.process_ident
= new_ident
;
62 syslog_globals
.process_logopt
= logopt
;
63 syslog_globals
.process_facility
= facility
;
66 /* setlogmask: set the log priority mask and return previous mask.
67 If maskpri is zero, just return previous. */
69 setlogmask (int maskpri
)
72 return syslog_globals
.process_logmask
;
74 int old_mask
= syslog_globals
.process_logmask
;
75 syslog_globals
.process_logmask
= maskpri
;
80 /* Private class used to handle formatting of syslog message
81 It is named pass_handler because it does a two-pass handling of log
82 strings. The first pass counts the length of the string, and the second
83 one builds the string. */
94 /* Explicitly disallow copies */
95 pass_handler (const pass_handler
&);
96 pass_handler
& operator = (const pass_handler
&);
102 int initialize (int);
104 int print (const char *,...);
105 int print_va (const char *, va_list);
106 char *get_message () const { return message_
; }
107 void set_message (char *s
) { message_
= s
; *message_
= '\0'; }
110 pass_handler::pass_handler () : fp_ (0), message_ (0), total_len_ (0)
115 pass_handler::~pass_handler ()
121 pass_handler::shutdown ()
131 pass_handler::initialize (int pass_number
)
135 return total_len_
+ 1;
137 fp_
= fopen ("/dev/null", "wb");
141 debug_printf ("failed to open /dev/null");
149 pass_handler::print (const char *fmt
, ...)
153 int ret
= print_va (fmt
, ap
);
159 pass_handler::print_va (const char *fmt
, va_list list
)
163 int len
= vfprintf (fp_
, fmt
, list
);
169 else if (message_
!= NULL
)
171 char *printpos
= &message_
[strlen (message_
)];
172 vsprintf (printpos
, fmt
, list
);
175 debug_printf ("FAILURE ! fp_ and message_ both 0!! ");
179 static NO_COPY muto try_connect_guard
;
186 static int syslogd_sock
= -1;
187 extern "C" int cygwin_socket (int, int, int);
188 extern "C" int cygwin_connect (int, const struct sockaddr
*, int);
189 extern int get_inet_addr_local (const struct sockaddr
*, int,
190 struct sockaddr_storage
*, int *,
191 int * = NULL
, int * = NULL
);
197 struct sockaddr_un sun
;
198 struct sockaddr_storage sst
;
201 if (syslogd_inited
!= not_inited
&& syslogd_sock
>= 0)
202 close (syslogd_sock
);
203 syslogd_inited
= inited_failed
;
205 sun
.sun_family
= AF_LOCAL
;
206 strncpy (sun
.sun_path
, _PATH_LOG
, sizeof sun
.sun_path
);
207 if (get_inet_addr_local ((struct sockaddr
*) &sun
, sizeof sun
,
210 if ((fd
= cygwin_socket (AF_LOCAL
, type
| SOCK_CLOEXEC
, 0)) < 0)
212 if (cygwin_connect (fd
, (struct sockaddr
*) &sun
, sizeof sun
) == 0)
214 if (type
== SOCK_DGRAM
)
219 * As soon as AF_LOCAL sockets are using pipes, this code has to
223 /* connect on a dgram socket always succeeds. We still don't know
224 if syslogd is actually listening. */
226 PMIB_UDPTABLE tab
= (PMIB_UDPTABLE
) tp
.w_get ();
229 struct sockaddr_in
*sa
= (struct sockaddr_in
*) &sst
;
231 if (GetUdpTable (tab
, &size
, FALSE
) == NO_ERROR
)
233 for (DWORD i
= 0; i
< tab
->dwNumEntries
; ++i
)
234 if (tab
->table
[i
].dwLocalAddr
== sa
->sin_addr
.s_addr
235 && tab
->table
[i
].dwLocalPort
== sa
->sin_port
)
242 /* No syslogd is listening. */
248 syslogd_inited
= type
== SOCK_DGRAM
? inited_dgram
: inited_stream
;
256 debug_printf ("found /dev/log, fd = %d, type = %s",
257 fd
, syslogd_inited
== inited_stream
? "STREAM" : "DGRAM");
262 try_connect_syslogd (int priority
, const char *msg
, size_t len
)
266 try_connect_guard
.init ("try_connect_guard")->acquire ();
267 if (syslogd_inited
== not_inited
)
269 if (syslogd_inited
!= inited_failed
)
272 sprintf (pribuf
, "<%d>", priority
);
275 { pribuf
, strlen (pribuf
) },
276 { (char *) msg
, len
}
279 ret
= writev (syslogd_sock
, iv
, 2);
280 /* If the syslog daemon has been restarted and /dev/log was
281 a stream socket, the connection is broken. In this case,
282 try to reopen the socket and try again. */
283 if (ret
< 0 && syslogd_inited
== inited_stream
)
286 if (syslogd_sock
>= 0)
287 ret
= writev (syslogd_sock
, iv
, 2);
289 /* If write fails and LOG_CONS is set, return failure to vsyslog so
290 it falls back to the usual logging method for this OS. */
291 if (ret
>= 0 || !(syslog_globals
.process_logopt
& LOG_CONS
))
294 try_connect_guard
.release ();
298 /* syslog: creates the log message and writes to /dev/log, or to the
299 NT system log if /dev/log isn't available.
301 FIXME. WinNT system log messages don't look pretty, but in order to
302 fix this we have to embed resources in the code and tell the NT
303 registry where we are, blech (what happens if we move ?). We could,
304 however, add the resources in Cygwin and always point to that. */
307 vsyslog (int priority
, const char *message
, va_list ap
)
309 debug_printf ("%y %s", priority
, message
);
310 /* If the priority fails the current mask, reject */
311 if ((LOG_MASK (LOG_PRI (priority
)) & syslog_globals
.process_logmask
) == 0)
313 debug_printf ("failing message %y due to priority mask %y",
314 priority
, syslog_globals
.process_logmask
);
318 /* Set default facility to LOG_USER if not yet set via openlog. */
319 if (!syslog_globals
.process_facility
)
320 syslog_globals
.process_facility
= LOG_USER
;
322 /* Add default facility if not in the given priority. */
323 if (!(priority
& LOG_FACMASK
))
324 priority
|= syslog_globals
.process_facility
;
326 /* Translate %m in the message to error text */
327 char *errtext
= strerror (get_errno ());
328 int errlen
= strlen (errtext
);
331 for (const char *cp
= message
; *cp
; cp
++)
332 if (*cp
== '%' && cp
[1] == 'm')
335 char *newmessage
= (char *) alloca (strlen (message
) +
336 (errlen
* numfound
) + 1);
338 if (newmessage
== NULL
)
340 debug_printf ("failed to allocate newmessage");
344 char *dst
= newmessage
;
345 for (const char *cp2
= message
; *cp2
; cp2
++)
346 if (*cp2
== '%' && cp2
[1] == 'm')
349 strcpy (dst
, errtext
);
357 message
= newmessage
;
359 /* Work out the priority type - we ignore the facility for now.. */
361 switch (LOG_PRI (priority
))
367 eventType
= EVENTLOG_ERROR_TYPE
;
370 eventType
= EVENTLOG_WARNING_TYPE
;
375 eventType
= EVENTLOG_INFORMATION_TYPE
;
378 eventType
= EVENTLOG_ERROR_TYPE
;
382 /* We need to know how long the buffer needs to be.
383 The only legal way I can see of doing this is to
384 do a vfprintf to /dev/null, and count the bytes
385 output, then do it again to a malloc'ed string. This
386 is ugly, slow, but prevents core dumps :-).
389 for (int pass_number
= 0; pass_number
< 2; ++pass_number
)
391 int n
= pass
.initialize (pass_number
);
395 pass
.set_message ((char *) alloca (n
));
397 /* Deal with ident_string */
398 if (syslog_globals
.process_ident
!= NULL
)
400 if (pass
.print ("%ls: ", syslog_globals
.process_ident
) == -1)
403 if (syslog_globals
.process_logopt
& LOG_PID
)
405 if (pass
.print ("PID %u: ", getpid ()) == -1)
409 /* Print out the variable part */
410 if (pass
.print_va (message
, ap
) == -1)
414 char *total_msg
= pass
.get_message ();
415 size_t len
= strlen (total_msg
);
416 if (len
!= 0 && (total_msg
[len
- 1] == '\n'))
417 total_msg
[--len
] = '\0';
419 if (syslog_globals
.process_logopt
& LOG_PERROR
)
421 write (STDERR_FILENO
, total_msg
, len
);
422 write (STDERR_FILENO
, "\n", 1);
426 if ((fd
= try_connect_syslogd (priority
, total_msg
, len
+ 1)) < 0)
428 /* If syslogd isn't present, open the event log and send the message */
431 hEventSrc
= RegisterEventSourceW (NULL
, syslog_globals
.process_ident
434 debug_printf ("RegisterEventSourceW, %E");
437 wchar_t *msg_strings
[1];
439 msg_strings
[0] = tp
.w_get ();
440 sys_mbstowcs (msg_strings
[0], NT_MAX_PATH
, total_msg
);
441 if (!ReportEventW (hEventSrc
, eventType
, 0, 0, cygheap
->user
.sid (),
442 1, 0, (const wchar_t **) msg_strings
, NULL
))
443 debug_printf ("ReportEventW, %E");
444 DeregisterEventSource (hEventSrc
);
450 syslog (int priority
, const char *message
, ...)
453 va_start (ap
, message
);
454 vsyslog (priority
, message
, ap
);
461 try_connect_guard
.init ("try_connect_guard")->acquire ();
462 if (syslogd_inited
!= not_inited
&& syslogd_sock
>= 0)
464 close (syslogd_sock
);
466 syslogd_inited
= not_inited
;
468 try_connect_guard
.release ();