2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
41 #include <libcasper.h>
42 #include <libcasper_service.h>
44 #include "cap_syslog.h"
46 #define CAP_SYSLOG_LIMIT 2048
49 cap_syslog(cap_channel_t *chan, int pri, const char *fmt, ...)
54 cap_vsyslog(chan, pri, fmt, ap);
59 cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt, va_list ap)
62 char message[CAP_SYSLOG_LIMIT];
64 (void)vsnprintf(message, sizeof(message), fmt, ap);
66 nvl = nvlist_create(0);
67 nvlist_add_string(nvl, "cmd", "vsyslog");
68 nvlist_add_number(nvl, "priority", priority);
69 nvlist_add_string(nvl, "message", message);
70 nvl = cap_xfer_nvlist(chan, nvl);
78 cap_openlog(cap_channel_t *chan, const char *ident, int logopt, int facility)
82 nvl = nvlist_create(0);
83 nvlist_add_string(nvl, "cmd", "openlog");
85 nvlist_add_string(nvl, "ident", ident);
87 nvlist_add_number(nvl, "logopt", logopt);
88 nvlist_add_number(nvl, "facility", facility);
89 if (logopt & LOG_PERROR) {
90 nvlist_add_descriptor(nvl, "stderr", STDERR_FILENO);
92 nvl = cap_xfer_nvlist(chan, nvl);
100 cap_closelog(cap_channel_t *chan)
104 nvl = nvlist_create(0);
105 nvlist_add_string(nvl, "cmd", "closelog");
106 nvl = cap_xfer_nvlist(chan, nvl);
114 cap_setlogmask(cap_channel_t *chan, int maskpri)
119 nvl = nvlist_create(0);
120 nvlist_add_string(nvl, "cmd", "setlogmask");
121 nvlist_add_number(nvl, "maskpri", maskpri);
122 nvl = cap_xfer_nvlist(chan, nvl);
123 omask = nvlist_get_number(nvl, "omask");
135 static int prev_stderr = -1;
138 slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
139 nvlist_t *nvlout __unused)
142 syslog(nvlist_get_number(nvlin, "priority"), "%s",
143 nvlist_get_string(nvlin, "message"));
147 slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
148 nvlist_t *nvlout __unused)
154 ident = dnvlist_get_string(nvlin, "ident", NULL);
157 LogTag = strdup(ident);
160 logopt = nvlist_get_number(nvlin, "logopt");
161 if (logopt & LOG_PERROR) {
162 stderr_fd = dnvlist_get_descriptor(nvlin, "stderr", -1);
163 if (prev_stderr == -1)
164 prev_stderr = dup(STDERR_FILENO);
165 if (prev_stderr != -1)
166 (void)dup2(stderr_fd, STDERR_FILENO);
167 } else if (prev_stderr != -1) {
168 (void)dup2(prev_stderr, STDERR_FILENO);
172 openlog(LogTag, logopt, nvlist_get_number(nvlin, "facility"));
176 slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused,
177 nvlist_t *nvlout __unused)
185 if (prev_stderr != -1) {
186 (void)dup2(prev_stderr, STDERR_FILENO);
193 slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin,
198 omask = setlogmask(nvlist_get_number(nvlin, "maskpri"));
199 nvlist_add_number(nvlout, "omask", omask);
203 syslog_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
207 if (strcmp(cmd, "vsyslog") == 0) {
208 slog_vsyslog(limits, nvlin, nvlout);
209 } else if (strcmp(cmd, "openlog") == 0) {
210 slog_openlog(limits, nvlin, nvlout);
211 } else if (strcmp(cmd, "closelog") == 0) {
212 slog_closelog(limits, nvlin, nvlout);
213 } else if (strcmp(cmd, "setlogmask") == 0) {
214 slog_setlogmask(limits, nvlin, nvlout);
222 CREATE_SERVICE("system.syslog", NULL, syslog_command, 0);