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>
30 __FBSDID("$FreeBSD$");
43 #include <libcasper.h>
44 #include <libcasper_service.h>
46 #include "cap_syslog.h"
48 #define CAP_SYSLOG_LIMIT 2048
51 cap_syslog(cap_channel_t *chan, int pri, const char *fmt, ...)
56 cap_vsyslog(chan, pri, fmt, ap);
61 cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt, va_list ap)
64 char message[CAP_SYSLOG_LIMIT];
66 (void)vsnprintf(message, sizeof(message), fmt, ap);
68 nvl = nvlist_create(0);
69 nvlist_add_string(nvl, "cmd", "vsyslog");
70 nvlist_add_number(nvl, "priority", priority);
71 nvlist_add_string(nvl, "message", message);
72 nvl = cap_xfer_nvlist(chan, nvl);
80 cap_openlog(cap_channel_t *chan, const char *ident, int logopt, int facility)
84 nvl = nvlist_create(0);
85 nvlist_add_string(nvl, "cmd", "openlog");
87 nvlist_add_string(nvl, "ident", ident);
89 nvlist_add_number(nvl, "logopt", logopt);
90 nvlist_add_number(nvl, "facility", facility);
91 if (logopt & LOG_PERROR) {
92 nvlist_add_descriptor(nvl, "stderr", STDERR_FILENO);
94 nvl = cap_xfer_nvlist(chan, nvl);
102 cap_closelog(cap_channel_t *chan)
106 nvl = nvlist_create(0);
107 nvlist_add_string(nvl, "cmd", "closelog");
108 nvl = cap_xfer_nvlist(chan, nvl);
116 cap_setlogmask(cap_channel_t *chan, int maskpri)
121 nvl = nvlist_create(0);
122 nvlist_add_string(nvl, "cmd", "setlogmask");
123 nvlist_add_number(nvl, "maskpri", maskpri);
124 nvl = cap_xfer_nvlist(chan, nvl);
125 omask = nvlist_get_number(nvl, "omask");
137 static int prev_stderr = -1;
140 slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
141 nvlist_t *nvlout __unused)
144 syslog(nvlist_get_number(nvlin, "priority"), "%s",
145 nvlist_get_string(nvlin, "message"));
149 slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin,
150 nvlist_t *nvlout __unused)
156 ident = dnvlist_get_string(nvlin, "ident", NULL);
159 LogTag = strdup(ident);
162 logopt = nvlist_get_number(nvlin, "logopt");
163 if (logopt & LOG_PERROR) {
164 stderr_fd = dnvlist_get_descriptor(nvlin, "stderr", -1);
165 if (prev_stderr == -1)
166 prev_stderr = dup(STDERR_FILENO);
167 if (prev_stderr != -1)
168 (void)dup2(stderr_fd, STDERR_FILENO);
169 } else if (prev_stderr != -1) {
170 (void)dup2(prev_stderr, STDERR_FILENO);
174 openlog(LogTag, logopt, nvlist_get_number(nvlin, "facility"));
178 slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused,
179 nvlist_t *nvlout __unused)
187 if (prev_stderr != -1) {
188 (void)dup2(prev_stderr, STDERR_FILENO);
195 slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin,
200 omask = setlogmask(nvlist_get_number(nvlin, "maskpri"));
201 nvlist_add_number(nvlout, "omask", omask);
205 syslog_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
209 if (strcmp(cmd, "vsyslog") == 0) {
210 slog_vsyslog(limits, nvlin, nvlout);
211 } else if (strcmp(cmd, "openlog") == 0) {
212 slog_openlog(limits, nvlin, nvlout);
213 } else if (strcmp(cmd, "closelog") == 0) {
214 slog_closelog(limits, nvlin, nvlout);
215 } else if (strcmp(cmd, "setlogmask") == 0) {
216 slog_setlogmask(limits, nvlin, nvlout);
224 CREATE_SERVICE("system.syslog", NULL, syslog_command, 0);