Changeset View
Standalone View
lib/libcasper/services/cap_syslog/cap_syslog.c
Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | cap_openlog(cap_channel_t *chan, const char *ident, int logopt, int facility) | ||||
nvl = nvlist_create(0); | nvl = nvlist_create(0); | ||||
nvlist_add_string(nvl, "cmd", "openlog"); | nvlist_add_string(nvl, "cmd", "openlog"); | ||||
if (ident != NULL) { | if (ident != NULL) { | ||||
nvlist_add_string(nvl, "ident", ident); | nvlist_add_string(nvl, "ident", ident); | ||||
} | } | ||||
nvlist_add_number(nvl, "logopt", logopt); | nvlist_add_number(nvl, "logopt", logopt); | ||||
nvlist_add_number(nvl, "facility", facility); | nvlist_add_number(nvl, "facility", facility); | ||||
if (logopt & LOG_PERROR) { | |||||
oshogbo: This is incorrect according to style 9. | |||||
nvlist_add_descriptor(nvl, "stderr", STDERR_FILENO); | |||||
} | |||||
nvl = cap_xfer_nvlist(chan, nvl); | nvl = cap_xfer_nvlist(chan, nvl); | ||||
if (nvl == NULL) { | if (nvl == NULL) { | ||||
return; | return; | ||||
} | } | ||||
nvlist_destroy(nvl); | nvlist_destroy(nvl); | ||||
} | } | ||||
void | void | ||||
Show All 27 Lines | cap_setlogmask(cap_channel_t *chan, int maskpri) | ||||
return (omask); | return (omask); | ||||
} | } | ||||
/* | /* | ||||
* Service functions. | * Service functions. | ||||
*/ | */ | ||||
static char *LogTag; | static char *LogTag; | ||||
static int prev_stderr = -1; | |||||
static void | static void | ||||
slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin, | slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin, | ||||
nvlist_t *nvlout __unused) | nvlist_t *nvlout __unused) | ||||
{ | { | ||||
syslog(nvlist_get_number(nvlin, "priority"), "%s", | syslog(nvlist_get_number(nvlin, "priority"), "%s", | ||||
nvlist_get_string(nvlin, "message")); | nvlist_get_string(nvlin, "message")); | ||||
} | } | ||||
static void | static void | ||||
slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin, | slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin, | ||||
nvlist_t *nvlout __unused) | nvlist_t *nvlout __unused) | ||||
{ | { | ||||
const char *ident; | const char *ident; | ||||
uint64_t logopt; | |||||
int stderr_fd; | |||||
ident = dnvlist_get_string(nvlin, "ident", NULL); | ident = dnvlist_get_string(nvlin, "ident", NULL); | ||||
if (ident != NULL) { | if (ident != NULL) { | ||||
free(LogTag); | free(LogTag); | ||||
LogTag = strdup(ident); | LogTag = strdup(ident); | ||||
} | } | ||||
openlog(LogTag, nvlist_get_number(nvlin, "logopt"), | logopt = nvlist_get_number(nvlin, "logopt"); | ||||
nvlist_get_number(nvlin, "facility")); | if (logopt & LOG_PERROR) { | ||||
stderr_fd = dnvlist_get_descriptor(nvlin, "stderr", -1); | |||||
Not Done Inline ActionsI think dnvlist_take_descriptor() would be more appropriate. The _get flavour dup()s the copy in the nvlist itself and returns that, so _take avoids an extra system call. markj: I think dnvlist_take_descriptor() would be more appropriate. The _get flavour dup()s the copy… | |||||
Done Inline ActionsI thought nvlist_add_descriptor() was the only one that does any dup'ing? jah: I thought nvlist_add_descriptor() was the only one that does any dup'ing?
The manpage at least… | |||||
Not Done Inline ActionsSorry, you're right. markj: Sorry, you're right. | |||||
if (stderr_fd != -1) { | |||||
prev_stderr = dup(STDERR_FILENO); | |||||
dup2(stderr_fd, STDERR_FILENO); | |||||
} | } | ||||
Not Done Inline ActionsI'd suggest casting the return value of dup() and dup2() to void to indicate that we're intentionally ignoring the return values. markj: I'd suggest casting the return value of dup() and dup2() to void to indicate that we're… | |||||
} else if (prev_stderr != -1) { | |||||
dup2(prev_stderr, STDERR_FILENO); | |||||
Not Done Inline ActionsDose it work? oshogbo: Dose it work?
While changing those descriptors you may change internal Casper descriptors. This… | |||||
Not Done Inline ActionsOh sorry I forgot that Casper reserve those for you. Doing /dev/null on them. Sorry LGTM. oshogbo: Oh sorry I forgot that Casper reserve those for you. Doing /dev/null on them. Sorry LGTM. | |||||
close(prev_stderr); | |||||
prev_stderr = -1; | |||||
} | |||||
openlog(LogTag, logopt, nvlist_get_number(nvlin, "facility")); | |||||
} | |||||
static void | static void | ||||
slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused, | slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused, | ||||
nvlist_t *nvlout __unused) | nvlist_t *nvlout __unused) | ||||
{ | { | ||||
closelog(); | closelog(); | ||||
free(LogTag); | free(LogTag); | ||||
LogTag = NULL; | LogTag = NULL; | ||||
if (prev_stderr != -1) { | |||||
dup2(prev_stderr, STDERR_FILENO); | |||||
close(prev_stderr); | |||||
prev_stderr = -1; | |||||
} | } | ||||
} | |||||
static void | static void | ||||
slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin, | slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin, | ||||
nvlist_t *nvlout) | nvlist_t *nvlout) | ||||
{ | { | ||||
int omask; | int omask; | ||||
omask = setlogmask(nvlist_get_number(nvlin, "maskpri")); | omask = setlogmask(nvlist_get_number(nvlin, "maskpri")); | ||||
Show All 15 Lines | if (strcmp(cmd, "vsyslog") == 0) { | ||||
slog_setlogmask(limits, nvlin, nvlout); | slog_setlogmask(limits, nvlin, nvlout); | ||||
} else { | } else { | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
return (0); | return (0); | ||||
} | } | ||||
CREATE_SERVICE("system.syslog", NULL, syslog_command, CASPER_SERVICE_STDIO); | CREATE_SERVICE("system.syslog", NULL, syslog_command, 0); |
This is incorrect according to style 9.