mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Implement syslog (#136)
This commit is contained in:
parent
83abd68029
commit
6682013d12
50 changed files with 535 additions and 89 deletions
|
@ -1,2 +1,15 @@
|
|||
.include "o/libc/nt/codegen.inc"
|
||||
.imp advapi32,__imp_DeregisterEventSource,DeregisterEventSource,1239
|
||||
|
||||
.text.windows
|
||||
DeregisterEventSource:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
mov %rdi,%rcx
|
||||
sub $32,%rsp
|
||||
call *__imp_DeregisterEventSource(%rip)
|
||||
leave
|
||||
ret
|
||||
.endfn DeregisterEventSource,globl
|
||||
.previous
|
||||
|
|
|
@ -1,2 +1,12 @@
|
|||
.include "o/libc/nt/codegen.inc"
|
||||
.imp advapi32,__imp_RegisterEventSourceA,RegisterEventSourceA,1686
|
||||
|
||||
.text.windows
|
||||
RegisterEventSourceA:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
mov __imp_RegisterEventSourceA(%rip),%rax
|
||||
jmp __sysv2nt
|
||||
.endfn RegisterEventSourceA,globl
|
||||
.previous
|
||||
|
|
|
@ -1,2 +1,12 @@
|
|||
.include "o/libc/nt/codegen.inc"
|
||||
.imp advapi32,__imp_RegisterEventSourceW,RegisterEventSourceW,1687
|
||||
|
||||
.text.windows
|
||||
RegisterEventSource:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
mov __imp_RegisterEventSourceW(%rip),%rax
|
||||
jmp __sysv2nt
|
||||
.endfn RegisterEventSource,globl
|
||||
.previous
|
||||
|
|
|
@ -1,2 +1,12 @@
|
|||
.include "o/libc/nt/codegen.inc"
|
||||
.imp advapi32,__imp_ReportEventA,ReportEventA,1704
|
||||
|
||||
.text.windows
|
||||
ReportEventA:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
mov __imp_ReportEventA(%rip),%rax
|
||||
jmp __sysv2nt10
|
||||
.endfn ReportEventA,globl
|
||||
.previous
|
||||
|
|
|
@ -1,2 +1,12 @@
|
|||
.include "o/libc/nt/codegen.inc"
|
||||
.imp advapi32,__imp_ReportEventW,ReportEventW,1705
|
||||
|
||||
.text.windows
|
||||
ReportEvent:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
mov __imp_ReportEventW(%rip),%rax
|
||||
jmp __sysv2nt10
|
||||
.endfn ReportEvent,globl
|
||||
.previous
|
||||
|
|
|
@ -39,6 +39,19 @@ bool32 GetCursorPos(struct NtPoint *lpPoint);
|
|||
int64_t SendMessage(int64_t hWnd, uint32_t Msg, uint64_t wParam,
|
||||
int64_t lParam);
|
||||
|
||||
#define EVENTLOG_SUCCESS 0x00000000
|
||||
#define EVENTLOG_ERROR_TYPE 0x00000001
|
||||
#define EVENTLOG_WARNING_TYPE 0x00000002
|
||||
#define EVENTLOG_INFORMATION_TYPE 0x00000004
|
||||
#define EVENTLOG_AUDIT_SUCCESS 0x00000008
|
||||
#define EVENTLOG_AUDIT_FAILURE 0x00000010
|
||||
|
||||
int32_t ReportEventA(int64_t handle, uint16_t wType, uint16_t wCategory,
|
||||
uint32_t dwEventID, const char *lpUserId, uint16_t wNumStrings,
|
||||
uint32_t dwDataSize, const char **lpStrings, void **lpRawData);
|
||||
int64_t RegisterEventSourceA(const char *lpUNCServerName, const char *lpSourceName);
|
||||
int32_t DeregisterEventSource(uint64_t handle);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_NT_EVENTS_H_ */
|
||||
|
|
|
@ -1214,7 +1214,7 @@ imp 'DeleteUmsThreadContext' DeleteUmsThreadContext kernel32 284
|
|||
imp 'DeleteVolumeMountPointA' DeleteVolumeMountPointA kernel32 285
|
||||
imp 'DeleteVolumeMountPoint' DeleteVolumeMountPointW kernel32 0 # KernelBase
|
||||
imp 'DequeueUmsCompletionListItems' DequeueUmsCompletionListItems kernel32 287
|
||||
imp 'DeregisterEventSource' DeregisterEventSource advapi32 1239
|
||||
imp 'DeregisterEventSource' DeregisterEventSource advapi32 1239 1
|
||||
imp 'DeregisterShellHookWindow' DeregisterShellHookWindow user32 1682
|
||||
imp 'DeriveCapabilitySidsFromName' DeriveCapabilitySidsFromName KernelBase 263
|
||||
imp 'DescribePixelFormat' DescribePixelFormat gdi32 1388
|
||||
|
@ -4652,8 +4652,8 @@ imp 'RegisterDManipHook' RegisterDManipHook user32 2254
|
|||
imp 'RegisterDeviceNotificationA' RegisterDeviceNotificationA user32 2255
|
||||
imp 'RegisterDeviceNotification' RegisterDeviceNotificationW user32 2256
|
||||
imp 'RegisterErrorReportingDialog' RegisterErrorReportingDialog user32 2257
|
||||
imp 'RegisterEventSourceA' RegisterEventSourceA advapi32 1686
|
||||
imp 'RegisterEventSource' RegisterEventSourceW advapi32 1687
|
||||
imp 'RegisterEventSourceA' RegisterEventSourceA advapi32 1686 2
|
||||
imp 'RegisterEventSource' RegisterEventSourceW advapi32 1687 2
|
||||
imp 'RegisterFrostWindow' RegisterFrostWindow user32 2258
|
||||
imp 'RegisterGPNotificationInternal' RegisterGPNotificationInternal KernelBase 1385
|
||||
imp 'RegisterGhostWindow' RegisterGhostWindow user32 2259
|
||||
|
@ -4737,8 +4737,8 @@ imp 'ReplacePartitionUnit' ReplacePartitionUnit kernel32 1217
|
|||
imp 'ReplaceTextA' ReplaceTextA comdlg32 124 1
|
||||
imp 'ReplaceText' ReplaceTextW comdlg32 125 1
|
||||
imp 'ReplyMessage' ReplyMessage user32 2288
|
||||
imp 'ReportEventA' ReportEventA advapi32 1704
|
||||
imp 'ReportEvent' ReportEventW advapi32 1705
|
||||
imp 'ReportEventA' ReportEventA advapi32 1704 9
|
||||
imp 'ReportEvent' ReportEventW advapi32 1705 9
|
||||
imp 'ReportInertia' ReportInertia user32 2551
|
||||
imp 'RequestDeviceWakeup' RequestDeviceWakeup kernel32 1218
|
||||
imp 'RequestWakeupLatency' RequestWakeupLatency kernel32 1219
|
||||
|
|
|
@ -41,10 +41,10 @@ int bind(int fd, const void *addr, uint32_t addrsize) {
|
|||
if (!IsBsd()) {
|
||||
return sys_bind(fd, addr, addrsize);
|
||||
} else {
|
||||
struct sockaddr_in_bsd addr2;
|
||||
assert(sizeof(struct sockaddr_in) == sizeof(struct sockaddr_in_bsd));
|
||||
memcpy(&addr2, addr, sizeof(struct sockaddr_in));
|
||||
sockaddr2bsd(&addr2);
|
||||
char addr2[sizeof(struct sockaddr_un_bsd)]; /* sockaddr_un_bsd is the largest */
|
||||
assert(addrsize <= sizeof(addr2));
|
||||
memcpy(&addr2, addr, addrsize);
|
||||
sockaddr2bsd(&addr2[0]);
|
||||
return sys_bind(fd, &addr2, addrsize);
|
||||
}
|
||||
} else if (__isfdkind(fd, kFdSocket)) {
|
||||
|
|
|
@ -23,14 +23,13 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
int sys_connect(int fd, const void *addr, uint32_t addrsize) {
|
||||
if (addrsize != sizeof(struct sockaddr_in)) return einval();
|
||||
if (!IsBsd()) {
|
||||
return __sys_connect(fd, addr, addrsize);
|
||||
} else {
|
||||
struct sockaddr_in_bsd addr2;
|
||||
assert(sizeof(struct sockaddr_in) == sizeof(struct sockaddr_in_bsd));
|
||||
memcpy(&addr2, addr, sizeof(struct sockaddr_in));
|
||||
sockaddr2bsd(&addr2);
|
||||
return sys_connect(fd, &addr2, addrsize);
|
||||
char addr2[sizeof(struct sockaddr_un_bsd)]; /* sockaddr_un_bsd is the largest */
|
||||
assert(addrsize <= sizeof(addr2));
|
||||
memcpy(&addr2, addr, addrsize);
|
||||
sockaddr2bsd(&addr2[0]);
|
||||
return __sys_connect(fd, &addr2, addrsize);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,12 @@ struct msghdr_bsd {
|
|||
uint32_t msg_flags; /* « different type */
|
||||
};
|
||||
|
||||
struct sockaddr_un_bsd {
|
||||
uint8_t sun_len; /* sockaddr len including NUL on freebsd but excluding it on openbsd/xnu */
|
||||
uint8_t sun_family;
|
||||
char sun_path[108];
|
||||
};
|
||||
|
||||
struct SockFd {
|
||||
int family;
|
||||
int type;
|
||||
|
|
|
@ -47,16 +47,15 @@
|
|||
*/
|
||||
ssize_t sendto(int fd, const void *buf, size_t size, uint32_t flags,
|
||||
const void *opt_addr, uint32_t addrsize) {
|
||||
assert(sizeof(struct sockaddr_in) == sizeof(struct sockaddr_in_bsd));
|
||||
if (!IsWindows()) {
|
||||
if (!IsBsd() || !opt_addr) {
|
||||
return sys_sendto(fd, buf, size, flags, opt_addr, addrsize);
|
||||
} else {
|
||||
struct sockaddr_in_bsd addr2;
|
||||
if (addrsize != sizeof(addr2)) return einval();
|
||||
memcpy(&addr2, opt_addr, sizeof(struct sockaddr_in));
|
||||
sockaddr2bsd(&addr2);
|
||||
return sys_sendto(fd, buf, size, flags, &addr2, addrsize);
|
||||
char addr2[sizeof(struct sockaddr_un_bsd)]; /* sockaddr_un_bsd is the largest */
|
||||
if (addrsize > sizeof(addr2)) return einval();
|
||||
memcpy(&addr2, opt_addr, addrsize);
|
||||
sockaddr2bsd(&addr2[0]);
|
||||
return sys_sendto(fd, buf, size, flags, &addr2[0], addrsize);
|
||||
}
|
||||
} else if (__isfdkind(fd, kFdSocket)) {
|
||||
return sys_sendto_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, flags,
|
||||
|
|
|
@ -40,6 +40,11 @@ struct sockaddr_in { /* Linux+NT ABI */
|
|||
uint8_t sin_zero[8];
|
||||
};
|
||||
|
||||
struct sockaddr_un {
|
||||
uint16_t sun_family; /* AF_UNIX */
|
||||
char sun_path[108];/* path */
|
||||
};
|
||||
|
||||
struct sockaddr_storage {
|
||||
union {
|
||||
uint16_t ss_family;
|
||||
|
|
|
@ -24,8 +24,11 @@ LIBC_SOCK_A_DIRECTDEPS = \
|
|||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_MEM \
|
||||
LIBC_TIME \
|
||||
LIBC_STR \
|
||||
LIBC_UNICODE \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_ADVAPI32 \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_MSWSOCK \
|
||||
LIBC_NT_NTDLL \
|
||||
|
|
343
libc/sock/syslog.c
Normal file
343
libc/sock/syslog.c
Normal file
|
@ -0,0 +1,343 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or 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 THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/sysv/consts/log.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/runtime/valist.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/time/struct/tm.h"
|
||||
#include "libc/time/time.h"
|
||||
#include "libc/calls/weirdtypes.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/syslog.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/events.h"
|
||||
|
||||
/* Note: log_facility should be initialized with LOG_USER by default,
|
||||
* but since LOG_USER is not a constant value, we cannot initialize it
|
||||
* here. Set it to -1 to force the public functions to call __initlog()
|
||||
* for the first time.
|
||||
*/
|
||||
static int log_facility = -1;
|
||||
static char log_ident[32];
|
||||
static int log_opt;
|
||||
static int log_mask;
|
||||
static uint16_t log_id; /* Used for Windows EvtID */
|
||||
static int64_t log_fd = -1;
|
||||
|
||||
|
||||
static const struct sockaddr_un log_addr = {
|
||||
AF_UNIX,
|
||||
"/dev/log"
|
||||
};
|
||||
|
||||
|
||||
static void __initlog() {
|
||||
log_ident[0] = '\0';
|
||||
log_opt = LOG_ODELAY;
|
||||
log_facility = LOG_USER;
|
||||
log_mask = LOG_PRI(0xff); // Initially use max verbosity
|
||||
log_fd = -1;
|
||||
}
|
||||
|
||||
forceinline int is_lost_conn(int e) {
|
||||
return (e==ECONNREFUSED) ||
|
||||
(e==ECONNRESET) ||
|
||||
(e==ENOTCONN) ||
|
||||
(e==EPIPE);
|
||||
}
|
||||
|
||||
static void __openlog() {
|
||||
if (IsWindows()) {
|
||||
log_fd = RegisterEventSourceA(NULL, log_ident);
|
||||
} else {
|
||||
log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
|
||||
if (log_fd >= 0) {
|
||||
int rc = connect(log_fd, (void *)&log_addr, sizeof(log_addr));
|
||||
if (rc < 0) {
|
||||
printf("ERR: connect(openlog) failed: %s (errno=%d)\n", strerror(errno), errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a log message which will be distributed by syslogd
|
||||
*
|
||||
* Note: no errors are reported if an error occurred while logging
|
||||
* the message.
|
||||
*
|
||||
* @param priority is a bitmask containing the facility value and
|
||||
* the level value. If no facility value is ORed into priority,
|
||||
* then the default value set by openlog() is used.
|
||||
* it set to NULL, the program name is used.
|
||||
* Level is one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
|
||||
* LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
|
||||
* @param message the format of the message to be processed by vprintf()
|
||||
* @param ap the va_list of arguments to be applied to the message
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void vsyslog(int priority, const char *message, va_list ap) {
|
||||
char timebuf[16]; /* Store formatted time */
|
||||
time_t now;
|
||||
struct tm tm;
|
||||
char buf[1024];
|
||||
int errno_save = errno; /* Save current errno */
|
||||
int pid;
|
||||
int l, l2;
|
||||
int hlen; /* If LOG_CONS is specified, use to store the point in
|
||||
* the header message after the timestamp */
|
||||
|
||||
if (log_fd < 0) __openlog();
|
||||
|
||||
if (!(priority & LOG_FACMASK)) priority |= log_facility;
|
||||
|
||||
/* Build the time string */
|
||||
now = time(NULL);
|
||||
gmtime_r(&now, &tm);
|
||||
strftime(timebuf, sizeof(timebuf), "%b %e %T", &tm);
|
||||
|
||||
pid = (log_opt & LOG_PID) ? getpid() : 0;
|
||||
/* This is a clever trick to optionally include "[<pid>]"
|
||||
* only if pid != 0. When pid==0, the while "[%.0d]" is skipped:
|
||||
* %s%.0d%s -> String, pidValue, String
|
||||
* Each of those %s:
|
||||
* - if pid == 0 -> !pid is true (1), so "[" + 1 points to the
|
||||
* NULL terminator after the "[".
|
||||
* - if pid != 0 -> !pid is false (0), so the string printed is
|
||||
* the "[".
|
||||
*/
|
||||
l = snprintf(buf, sizeof(buf), "<%d>%s ", priority, timebuf);
|
||||
hlen = l;
|
||||
l += snprintf(buf+l, sizeof(buf)-l, "%s%s%.0d%s: ", log_ident, "["+!pid, pid, "]"+!pid);
|
||||
errno = errno_save;
|
||||
|
||||
/* Append user message */
|
||||
l2 = vsnprintf(buf+l, sizeof(buf)-l, message, ap);
|
||||
if (l2 >= 0) {
|
||||
if (l2 >= sizeof(buf) - l) {
|
||||
l = sizeof(buf) - 1;
|
||||
} else {
|
||||
l += l2;
|
||||
}
|
||||
if (buf[l-1] != '\n') {
|
||||
buf[l++] = '\n';
|
||||
}
|
||||
if (!IsWindows()) {
|
||||
/* Unix:
|
||||
* - First try to send it to syslogd
|
||||
* - If fails and LOG_CONS is provided, writes to /dev/console
|
||||
*/
|
||||
|
||||
#if 0
|
||||
if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
|
||||
|| connect(log_fd, (void *)&log_addr, sizeof(log_addr)) < 0
|
||||
|| send(log_fd, buf, l, 0) < 0)
|
||||
&& (log_opt & LOG_CONS)) {
|
||||
int fd = open("/dev/console", O_WRONLY|O_NOCTTY);
|
||||
if (fd >= 0) {
|
||||
dprintf(fd, "%.*s", l-hlen, buf+hlen);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
#else
|
||||
int rc = send(log_fd, buf, l, 0);
|
||||
if (rc < 0) {
|
||||
printf("ERR: send(1) failed: %s (errno=%d)\n", strerror(errno), errno);
|
||||
if (!is_lost_conn(errno)) {
|
||||
rc = connect(log_fd, (void *)&log_addr, sizeof(log_addr));
|
||||
if (rc < 0) {
|
||||
printf("ERR: connect(syslog) failed: %s (errno=%d)\n", strerror(errno), errno);
|
||||
} else {
|
||||
rc = send(log_fd, buf, l, 0);
|
||||
if (rc < 0) {
|
||||
printf("ERR: send(2) failed: %s (errno=%d)\n", strerror(errno), errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
uint16_t evtType;
|
||||
uint32_t evtID;
|
||||
const char *bufArr[] = { &buf[hlen] }; /* Only print the message without time*/
|
||||
/* Windows only have 3 usable event types
|
||||
* Event ID are not supported
|
||||
* For more information on message types and event IDs, see:
|
||||
* https://docs.microsoft.com/en-us/windows/win32/eventlog/event-identifiers
|
||||
*/
|
||||
priority &= LOG_PRIMASK; // Remove facility from the priority field
|
||||
if (priority == LOG_EMERG ||
|
||||
priority == LOG_ALERT ||
|
||||
priority == LOG_CRIT) {
|
||||
evtType = EVENTLOG_ERROR_TYPE;
|
||||
evtID = 0xe << 28 | (LOG_USER) << 16;
|
||||
} else if (priority == LOG_WARNING) {
|
||||
evtType = EVENTLOG_WARNING_TYPE;
|
||||
evtID = 0xa << 28 | (LOG_USER) << 16;
|
||||
} else {
|
||||
/* LOG_NOTICE, LOG_INFO, LOG_DEBUG */
|
||||
evtType = EVENTLOG_INFORMATION_TYPE;
|
||||
evtID = 0x6 << 28 | (LOG_USER) << 16;
|
||||
}
|
||||
ReportEventA(log_fd,
|
||||
evtType, /* Derived from priority */
|
||||
0 /* Category unsupported */,
|
||||
evtID, /* Unsupported */
|
||||
NULL, /* User SID */
|
||||
1, /* Number of strings */
|
||||
0, /* Raw data size */
|
||||
bufArr, /* String(s) */
|
||||
NULL /* Arguments */ );
|
||||
++log_id;
|
||||
}
|
||||
|
||||
if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets log priority mask
|
||||
*
|
||||
* Modifies the log priority mask that determines which calls to
|
||||
* syslog() may be logged.
|
||||
* Log priority values are LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
|
||||
* LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG.
|
||||
*
|
||||
* @param mask the new priority mask to use by syslog()
|
||||
* @return the previous log priority mask
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int setlogmask(int maskpri) {
|
||||
int ret;
|
||||
if (log_facility == -1) {
|
||||
__initlog();
|
||||
}
|
||||
ret = log_mask;
|
||||
if (maskpri) log_mask = LOG_PRI(maskpri);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Opens a connection to the system logger
|
||||
*
|
||||
* Calling this function before calling syslog() is optional and
|
||||
* only allow customizing the identity, options and facility of
|
||||
* the messages logged.
|
||||
*
|
||||
* @param ident a string that prepends every logged message. If
|
||||
* it set to NULL, the program name is used.
|
||||
* @param opt specifies flags which control the operation of openlog().
|
||||
* Only the following flags are supported:
|
||||
* LOG_CONS = Write directly to the system console if there is
|
||||
* an error while sending to the system logger.
|
||||
* LOG_NDELAY = Open the connection with the system logger
|
||||
* immediately instead of waiting for the first
|
||||
* message to be logged.
|
||||
* LOG_ODELAY = The converse of LOG_NDELAY.
|
||||
* LOG_PERROR = Also log the message to stderr.
|
||||
* LOG_PID = Include the caller's PID with each message
|
||||
* @param facility specifies the default facitlity value that defines
|
||||
* what kind of program is logging the message.
|
||||
* Possible values are: LOG_AUTH, LOG_AUTHPRIV,
|
||||
* LOG_CRON, LOG_DAEMON, LOG_FTP, LOG_LOCAL0 through LOG_LOCAL7,
|
||||
* LOG_LPR, LOG_MAIL, LOG_NEWS, LOG_SYSLOG, LOG_USER (default),
|
||||
* LOG_UUCP.
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void openlog(const char *ident, int opt, int facility) {
|
||||
size_t n;
|
||||
|
||||
if (log_facility == -1) {
|
||||
__initlog();
|
||||
}
|
||||
if (!ident) {
|
||||
ident = program_invocation_short_name;
|
||||
}
|
||||
n = strnlen(ident, sizeof(log_ident) - 1);
|
||||
memcpy(log_ident, ident, n);
|
||||
log_ident[n] = 0;
|
||||
log_opt = opt;
|
||||
log_facility = facility;
|
||||
log_id = 0;
|
||||
|
||||
if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a log message which will be distributed by syslogd
|
||||
*
|
||||
* Note: no errors are reported if an error occurred while logging
|
||||
* the message.
|
||||
*
|
||||
* @param priority is a bitmask containing the facility value and
|
||||
* the level value. If no facility value is ORed into priority,
|
||||
* then the default value set by openlog() is used.
|
||||
* it set to NULL, the program name is used.
|
||||
* Level is one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
|
||||
* LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
|
||||
* @param message the message formatted using the same rules as printf()
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void syslog(int priority, const char *message, ...) {
|
||||
va_list ap;
|
||||
if (log_facility == -1) {
|
||||
__initlog();
|
||||
}
|
||||
if (LOG_PRI(priority) <= log_mask) {
|
||||
va_start(ap, message);
|
||||
vsyslog(priority, message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the file descriptor being used to write to the system logger
|
||||
*
|
||||
* Use of closelog is optional
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void closelog(void) {
|
||||
if (log_facility == -1) {
|
||||
__initlog();
|
||||
}
|
||||
if (log_fd == -1) {
|
||||
return;
|
||||
}
|
||||
if (IsWindows()) {
|
||||
DeregisterEventSource(log_fd);
|
||||
} else {
|
||||
close(log_fd);
|
||||
}
|
||||
log_fd = -1;
|
||||
}
|
||||
|
||||
|
18
libc/sock/syslog.h
Normal file
18
libc/sock/syslog.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_SOCK_SYSLOG_H_
|
||||
#define COSMOPOLITAN_LIBC_SOCK_SYSLOG_H_
|
||||
|
||||
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define LOG_PRI(p) ((p) & LOG_PRIMASK)
|
||||
|
||||
int setlogmask(int);
|
||||
void openlog(const char *, int, int);
|
||||
void syslog(int, const char *, ...);
|
||||
void closelog(void);
|
||||
void vsyslog(int, const char *, va_list);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_SOCK_SYSLOG_H_ */
|
|
@ -22,7 +22,7 @@ dir=libc/sysv/consts
|
|||
# The Fifth Bell System, Community Edition
|
||||
# » catalogue of carnage
|
||||
#
|
||||
# group name GNU/Systemd XNU's Not UNIX FreeBSD OpenBSD NetBSD XENIX Commentary
|
||||
# group name GNU/Systemd XNU's Not UNIX FreeBSD OpenBSD NetBSD Windows Commentary
|
||||
syscon errno ENOSYS 38 78 78 78 78 1 # bsd consensus & kNtErrorInvalidFunction
|
||||
syscon errno EPERM 1 1 1 1 1 12 # unix consensus & kNtErrorInvalidAccess (should be kNtErrorNotOwner but is that mutex only??)
|
||||
syscon errno ENOENT 2 2 2 2 2 2 # unix consensus & kNtErrorFileNotFound
|
||||
|
@ -842,41 +842,41 @@ syscon pr PR_SET_PTRACER 0x59616d61 0 0 0 0 0
|
|||
|
||||
syscon log LOG_EMERG 0 0 0 0 0 0 # consensus
|
||||
syscon log LOG_KERN 0 0 0 0 0 0 # consensus
|
||||
syscon log LOG_ALERT 1 1 1 1 1 0 # unix consensus
|
||||
syscon log LOG_PID 1 1 1 1 1 0 # unix consensus
|
||||
syscon log LOG_CONS 2 2 2 2 2 0 # unix consensus
|
||||
syscon log LOG_CRIT 2 2 2 2 2 0 # unix consensus
|
||||
syscon log LOG_ERR 3 3 3 3 3 0 # unix consensus
|
||||
syscon log LOG_ODELAY 4 4 4 4 4 0 # unix consensus
|
||||
syscon log LOG_WARNING 4 4 4 4 4 0 # unix consensus
|
||||
syscon log LOG_NOTICE 5 5 5 5 5 0 # unix consensus
|
||||
syscon log LOG_INFO 6 6 6 6 6 0 # unix consensus
|
||||
syscon log LOG_DEBUG 7 7 7 7 7 0 # unix consensus
|
||||
syscon log LOG_PRIMASK 7 7 7 7 7 0 # unix consensus
|
||||
syscon log LOG_NDELAY 8 8 8 8 8 0 # unix consensus
|
||||
syscon log LOG_USER 8 8 8 8 8 0 # unix consensus
|
||||
syscon log LOG_MAIL 0x10 0x10 0x10 0x10 0x10 0 # unix consensus
|
||||
syscon log LOG_NOWAIT 0x10 0x10 0x10 0x10 0x10 0 # unix consensus
|
||||
syscon log LOG_DAEMON 24 24 24 24 24 0 # unix consensus
|
||||
syscon log LOG_NFACILITIES 24 25 24 24 24 0
|
||||
syscon log LOG_AUTH 0x20 0x20 0x20 0x20 0x20 0 # unix consensus
|
||||
syscon log LOG_PERROR 0x20 0x20 0x20 0x20 0x20 0 # unix consensus
|
||||
syscon log LOG_SYSLOG 40 40 40 40 40 0 # unix consensus
|
||||
syscon log LOG_LPR 48 48 48 48 48 0 # unix consensus
|
||||
syscon log LOG_NEWS 56 56 56 56 56 0 # unix consensus
|
||||
syscon log LOG_UUCP 0x40 0x40 0x40 0x40 0x40 0 # unix consensus
|
||||
syscon log LOG_CRON 72 72 72 72 72 0 # unix consensus
|
||||
syscon log LOG_ALERT 1 1 1 1 1 1 # unix consensus
|
||||
syscon log LOG_PID 1 1 1 1 1 1 # unix consensus
|
||||
syscon log LOG_CONS 2 2 2 2 2 2 # unix consensus
|
||||
syscon log LOG_CRIT 2 2 2 2 2 2 # unix consensus
|
||||
syscon log LOG_ERR 3 3 3 3 3 3 # unix consensus
|
||||
syscon log LOG_ODELAY 4 4 4 4 4 4 # unix consensus
|
||||
syscon log LOG_WARNING 4 4 4 4 4 4 # unix consensus
|
||||
syscon log LOG_NOTICE 5 5 5 5 5 5 # unix consensus
|
||||
syscon log LOG_INFO 6 6 6 6 6 6 # unix consensus
|
||||
syscon log LOG_DEBUG 7 7 7 7 7 7 # unix consensus
|
||||
syscon log LOG_PRIMASK 7 7 7 7 7 7 # unix consensus
|
||||
syscon log LOG_NDELAY 8 8 8 8 8 8 # unix consensus
|
||||
syscon log LOG_USER 8 8 8 8 8 8 # unix consensus
|
||||
syscon log LOG_MAIL 0x10 0x10 0x10 0x10 0x10 0x10 # unix consensus
|
||||
syscon log LOG_NOWAIT 0x10 0x10 0x10 0x10 0x10 0x10 # unix consensus
|
||||
syscon log LOG_DAEMON 24 24 24 24 24 24 # unix consensus
|
||||
syscon log LOG_NFACILITIES 24 25 24 24 24 24
|
||||
syscon log LOG_AUTH 0x20 0x20 0x20 0x20 0x20 0x20 # unix consensus
|
||||
syscon log LOG_PERROR 0x20 0x20 0x20 0x20 0x20 0x20 # unix consensus
|
||||
syscon log LOG_SYSLOG 40 40 40 40 40 40 # unix consensus
|
||||
syscon log LOG_LPR 48 48 48 48 48 48 # unix consensus
|
||||
syscon log LOG_NEWS 56 56 56 56 56 56 # unix consensus
|
||||
syscon log LOG_UUCP 0x40 0x40 0x40 0x40 0x40 40 # unix consensus
|
||||
syscon log LOG_CRON 72 72 72 72 72 72 # unix consensus
|
||||
syscon log LOG_SELECT 76 0 0 0 0 0
|
||||
syscon log LOG_SENSE 77 0 0 0 0 0
|
||||
syscon log LOG_LOCAL0 0x80 0x80 0x80 0x80 0x80 0 # unix consensus
|
||||
syscon log LOG_LOCAL1 136 136 136 136 136 0 # unix consensus
|
||||
syscon log LOG_LOCAL2 144 144 144 144 144 0 # unix consensus
|
||||
syscon log LOG_LOCAL3 152 152 152 152 152 0 # unix consensus
|
||||
syscon log LOG_LOCAL4 160 160 160 160 160 0 # unix consensus
|
||||
syscon log LOG_LOCAL5 168 168 168 168 168 0 # unix consensus
|
||||
syscon log LOG_LOCAL6 176 176 176 176 176 0 # unix consensus
|
||||
syscon log LOG_LOCAL7 184 184 184 184 184 0 # unix consensus
|
||||
syscon log LOG_FACMASK 0x03f8 0x03f8 0x03f8 0x03f8 0x03f8 0 # unix consensus
|
||||
syscon log LOG_LOCAL0 0x80 0x80 0x80 0x80 0x80 0x80 # unix consensus
|
||||
syscon log LOG_LOCAL1 136 136 136 136 136 136 # unix consensus
|
||||
syscon log LOG_LOCAL2 144 144 144 144 144 144 # unix consensus
|
||||
syscon log LOG_LOCAL3 152 152 152 152 152 152 # unix consensus
|
||||
syscon log LOG_LOCAL4 160 160 160 160 160 160 # unix consensus
|
||||
syscon log LOG_LOCAL5 168 168 168 168 168 168 # unix consensus
|
||||
syscon log LOG_LOCAL6 176 176 176 176 176 176 # unix consensus
|
||||
syscon log LOG_LOCAL7 184 184 184 184 184 184 # unix consensus
|
||||
syscon log LOG_FACMASK 0x03f8 0x03f8 0x03f8 0x03f8 0x03f8 0x03f8 # unix consensus
|
||||
|
||||
syscon sg SG_DXFER_TO_FROM_DEV -4 0 0 0 0 0
|
||||
syscon sg SG_DXFER_FROM_DEV -3 0 0 0 0 0
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_ALERT,1,1,1,1,1,0
|
||||
.syscon log,LOG_ALERT,1,1,1,1,1,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_AUTH,0x20,0x20,0x20,0x20,0x20,0
|
||||
.syscon log,LOG_AUTH,0x20,0x20,0x20,0x20,0x20,0x20
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_CONS,2,2,2,2,2,0
|
||||
.syscon log,LOG_CONS,2,2,2,2,2,2
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_CRIT,2,2,2,2,2,0
|
||||
.syscon log,LOG_CRIT,2,2,2,2,2,2
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_CRON,72,72,72,72,72,0
|
||||
.syscon log,LOG_CRON,72,72,72,72,72,72
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_DAEMON,24,24,24,24,24,0
|
||||
.syscon log,LOG_DAEMON,24,24,24,24,24,24
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_DEBUG,7,7,7,7,7,0
|
||||
.syscon log,LOG_DEBUG,7,7,7,7,7,7
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_ERR,3,3,3,3,3,0
|
||||
.syscon log,LOG_ERR,3,3,3,3,3,3
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_FACMASK,0x03f8,0x03f8,0x03f8,0x03f8,0x03f8,0
|
||||
.syscon log,LOG_FACMASK,0x03f8,0x03f8,0x03f8,0x03f8,0x03f8,0x03f8
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_INFO,6,6,6,6,6,0
|
||||
.syscon log,LOG_INFO,6,6,6,6,6,6
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL0,0x80,0x80,0x80,0x80,0x80,0
|
||||
.syscon log,LOG_LOCAL0,0x80,0x80,0x80,0x80,0x80,0x80
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL1,136,136,136,136,136,0
|
||||
.syscon log,LOG_LOCAL1,136,136,136,136,136,136
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL2,144,144,144,144,144,0
|
||||
.syscon log,LOG_LOCAL2,144,144,144,144,144,144
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL3,152,152,152,152,152,0
|
||||
.syscon log,LOG_LOCAL3,152,152,152,152,152,152
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL4,160,160,160,160,160,0
|
||||
.syscon log,LOG_LOCAL4,160,160,160,160,160,160
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL5,168,168,168,168,168,0
|
||||
.syscon log,LOG_LOCAL5,168,168,168,168,168,168
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL6,176,176,176,176,176,0
|
||||
.syscon log,LOG_LOCAL6,176,176,176,176,176,176
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LOCAL7,184,184,184,184,184,0
|
||||
.syscon log,LOG_LOCAL7,184,184,184,184,184,184
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_LPR,48,48,48,48,48,0
|
||||
.syscon log,LOG_LPR,48,48,48,48,48,48
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_MAIL,0x10,0x10,0x10,0x10,0x10,0
|
||||
.syscon log,LOG_MAIL,0x10,0x10,0x10,0x10,0x10,0x10
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_NDELAY,8,8,8,8,8,0
|
||||
.syscon log,LOG_NDELAY,8,8,8,8,8,8
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_NEWS,56,56,56,56,56,0
|
||||
.syscon log,LOG_NEWS,56,56,56,56,56,56
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_NFACILITIES,24,25,24,24,24,0
|
||||
.syscon log,LOG_NFACILITIES,24,25,24,24,24,24
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_NOTICE,5,5,5,5,5,0
|
||||
.syscon log,LOG_NOTICE,5,5,5,5,5,5
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_NOWAIT,0x10,0x10,0x10,0x10,0x10,0
|
||||
.syscon log,LOG_NOWAIT,0x10,0x10,0x10,0x10,0x10,0x10
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_ODELAY,4,4,4,4,4,0
|
||||
.syscon log,LOG_ODELAY,4,4,4,4,4,4
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_PERROR,0x20,0x20,0x20,0x20,0x20,0
|
||||
.syscon log,LOG_PERROR,0x20,0x20,0x20,0x20,0x20,0x20
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_PID,1,1,1,1,1,0
|
||||
.syscon log,LOG_PID,1,1,1,1,1,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_PRIMASK,7,7,7,7,7,0
|
||||
.syscon log,LOG_PRIMASK,7,7,7,7,7,7
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_SYSLOG,40,40,40,40,40,0
|
||||
.syscon log,LOG_SYSLOG,40,40,40,40,40,40
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_USER,8,8,8,8,8,0
|
||||
.syscon log,LOG_USER,8,8,8,8,8,8
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_UUCP,0x40,0x40,0x40,0x40,0x40,0
|
||||
.syscon log,LOG_UUCP,0x40,0x40,0x40,0x40,0x40,40
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon log,LOG_WARNING,4,4,4,4,4,0
|
||||
.syscon log,LOG_WARNING,4,4,4,4,4,4
|
||||
|
|
|
@ -40,6 +40,13 @@
|
|||
#define LOG_UUCP SYMBOLIC(LOG_UUCP)
|
||||
#define LOG_WARNING SYMBOLIC(LOG_WARNING)
|
||||
|
||||
/*
|
||||
* arguments to setlogmask.
|
||||
*/
|
||||
#define LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
|
||||
#define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1) /* all priorities through pri */
|
||||
|
||||
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
|
|
Loading…
Reference in a new issue