Introduce syslog service for Casper.

syslog in libc secretly reconnects to the daemon.
Another issue is that we don't have any information from openlog(3) if we
succeeded to open log or not so we don't know if we are ready
to enter cabability mode.
Because all of that we decided we need a syslog service for Caspser.

Reviewed by:	bapt@
Differential Revision:	https://reviews.freebsd.org/D12824
This commit is contained in:
Mariusz Zaborski 2017-11-12 08:34:25 +00:00
parent 3d7342941b
commit 7b4fce76cc
6 changed files with 282 additions and 0 deletions

View File

@ -7,6 +7,7 @@ SUBDIR+= cap_grp
SUBDIR+= cap_pwd
SUBDIR+= cap_random
SUBDIR+= cap_sysctl
SUBDIR+= cap_syslog
SUBDIR.${MK_TESTS}+= tests

View File

@ -0,0 +1,24 @@
# $FreeBSD$
SHLIBDIR?= /lib/casper
.include <src.opts.mk>
PACKAGE=libcasper
SHLIB_MAJOR= 0
INCSDIR?= ${INCLUDEDIR}/casper
.if ${MK_CASPER} != "no"
SHLIB= cap_syslog
SRCS= cap_syslog.c
.endif
INCS= cap_syslog.h
LIBADD= nv
CFLAGS+=-I${.CURDIR}
.include <bsd.lib.mk>

View File

@ -0,0 +1,199 @@
/*-
* Copyright (c) 2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/dnv.h>
#include <sys/nv.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <libcasper.h>
#include <libcasper_service.h>
#include "cap_syslog.h"
#define CAP_SYSLOG_LIMIT 2048
void
cap_syslog(cap_channel_t *chan, int pri, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
cap_vsyslog(chan, pri, fmt, ap);
va_end(ap);
}
void
cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt, va_list ap)
{
nvlist_t *nvl;
char message[CAP_SYSLOG_LIMIT];
(void)vsnprintf(message, sizeof(message), fmt, ap);
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", "vsyslog");
nvlist_add_number(nvl, "priority", priority);
nvlist_add_string(nvl, "message", message);
nvl = cap_xfer_nvlist(chan, nvl, 0);
if (nvl == NULL) {
return;
}
nvlist_destroy(nvl);
}
void
cap_openlog(cap_channel_t *chan, const char *ident, int logopt, int facility)
{
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", "openlog");
if (ident != NULL) {
nvlist_add_string(nvl, "ident", ident);
}
nvlist_add_number(nvl, "logopt", logopt);
nvlist_add_number(nvl, "facility", facility);
nvl = cap_xfer_nvlist(chan, nvl, 0);
if (nvl == NULL) {
return;
}
nvlist_destroy(nvl);
}
void
cap_closelog(cap_channel_t *chan)
{
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", "closelog");
nvl = cap_xfer_nvlist(chan, nvl, 0);
if (nvl == NULL) {
return;
}
nvlist_destroy(nvl);
}
int
cap_setlogmask(cap_channel_t *chan, int maskpri)
{
nvlist_t *nvl;
int omask;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", "setlogmask");
nvlist_add_number(nvl, "maskpri", maskpri);
nvl = cap_xfer_nvlist(chan, nvl, 0);
omask = nvlist_get_number(nvl, "omask");
nvlist_destroy(nvl);
return (omask);
}
/*
* Service functions.
*/
static char *LogTag;
static void
slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
nvlist_t *nvlout __unused)
{
syslog(nvlist_get_number(nvlin, "priority"), "%s",
nvlist_get_string(nvlin, "message"));
}
static void
slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
nvlist_t *nvlout __unused)
{
const char *ident;
ident = dnvlist_get_string(nvlin, "ident", NULL);
if (ident != NULL) {
free(LogTag);
LogTag = strdup(ident);
}
openlog(LogTag, nvlist_get_number(nvlin, "logopt"),
nvlist_get_number(nvlin, "facility"));
}
static void
slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused,
nvlist_t *nvlout __unused)
{
closelog();
free(LogTag);
LogTag = NULL;
}
static void
slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin,
nvlist_t *nvlout)
{
int omask;
omask = setlogmask(nvlist_get_number(nvlin, "maskpri"));
nvlist_add_number(nvlout, "omask", omask);
}
static int
syslog_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
nvlist_t *nvlout)
{
if (strcmp(cmd, "vsyslog") == 0) {
slog_vsyslog(limits, nvlin, nvlout);
} else if (strcmp(cmd, "openlog") == 0) {
slog_openlog(limits, nvlin, nvlout);
} else if (strcmp(cmd, "closelog") == 0) {
slog_closelog(limits, nvlin, nvlout);
} else if (strcmp(cmd, "setlogmask") == 0) {
slog_setlogmask(limits, nvlin, nvlout);
} else {
return (EINVAL);
}
return (0);
}
CREATE_SERVICE("system.syslog", NULL, syslog_command, 0);

View File

@ -0,0 +1,54 @@
/*-
* Copyright (c) 2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _CAP_SYSLOG_H_
#define _CAP_SYSLOG_H_
#ifdef WITH_CASPER
void cap_syslog(cap_channel_t *chan, int pri,
const char *fmt, ...) __printflike(3, 4);
void cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt,
va_list ap) __printflike(3, 0);
void cap_openlog(cap_channel_t *chan, const char *ident, int logopt,
int facility);
void cap_closelog(cap_channel_t *chan);
int cap_setlogmask(cap_channel_t *chan, int maskpri);
#else
#define cap_syslog(chan, pri, ...) syslog(pri, __VA_ARGS__)
#define cap_vsyslog(chan, pri, fmt, ap) vsyslog(pri, fmt, ap)
#define cap_openlog(chan, ident, logopt, facility) \
openlog(ident, logopt, facility)
#define cap_closelog(chan) closelog()
#define cap_setlogmask(chan, maskpri) setlogmask(maskpri)
#endif /* !WITH_CASPER */
#endif /* !_CAP_SYSLOG_H_ */

View File

@ -38,6 +38,7 @@ LIBCAP_GRP?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_grp.a
LIBCAP_PWD?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_pwd.a
LIBCAP_RANDOM?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_random.a
LIBCAP_SYSCTL?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_sysctl.a
LIBCAP_SYSLOG?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_syslog.a
LIBCASPER?= ${LIBDESTDIR}${LIBDIR_BASE}/libcasper.a
LIBCOMPAT?= ${LIBDESTDIR}${LIBDIR_BASE}/libcompat.a
LIBCOMPILER_RT?=${LIBDESTDIR}${LIBDIR_BASE}/libcompiler_rt.a

View File

@ -78,6 +78,7 @@ _LIBRARIES= \
cap_pwd \
cap_random \
cap_sysctl \
cap_syslog \
com_err \
compiler_rt \
crypt \
@ -239,6 +240,7 @@ _DP_cap_grp= nv
_DP_cap_pwd= nv
_DP_cap_random= nv
_DP_cap_sysctl= nv
_DP_cap_syslog= nv
_DP_pjdlog= util
_DP_opie= md
_DP_usb= pthread
@ -530,6 +532,7 @@ LIBCAP_GRPDIR= ${OBJTOP}/lib/libcasper/services/cap_grp
LIBCAP_PWDDIR= ${OBJTOP}/lib/libcasper/services/cap_pwd
LIBCAP_RANDOMDIR= ${OBJTOP}/lib/libcasper/services/cap_random
LIBCAP_SYSCTLDIR= ${OBJTOP}/lib/libcasper/services/cap_sysctl
LIBCAP_SYSLOGDIR= ${OBJTOP}/lib/libcasper/services/cap_syslog
LIBBSDXMLDIR= ${OBJTOP}/lib/libexpat
LIBKVMDIR= ${OBJTOP}/lib/libkvm
LIBPTHREADDIR= ${OBJTOP}/lib/libthr