4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license from the top-level
9 * OPENSOLARIS.LICENSE or <http://opensource.org/licenses/CDDL-1.0>.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each file
14 * and include the License file from the top-level OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
24 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
36 #define ZED_LOG_MAX_ID_LEN 64
37 #define ZED_LOG_MAX_LOG_LEN 1024
43 char id[ZED_LOG_MAX_ID_LEN];
48 zed_log_init(const char *identity)
53 p = (p = strrchr(identity, '/')) ? p + 1 : identity;
54 strlcpy(_ctx.id, p, sizeof (_ctx.id));
71 * Create pipe for communicating daemonization status between the parent and
72 * child processes across the double-fork().
75 zed_log_pipe_open(void)
77 if ((_ctx.pipe_fd[0] != -1) || (_ctx.pipe_fd[1] != -1))
78 zed_log_die("Invalid use of zed_log_pipe_open in PID %d",
81 if (pipe(_ctx.pipe_fd) < 0)
82 zed_log_die("Failed to create daemonize pipe in PID %d: %s",
83 (int) getpid(), strerror(errno));
87 * Close the read-half of the daemonize pipe.
88 * This should be called by the child after fork()ing from the parent since
89 * the child will never read from this pipe.
92 zed_log_pipe_close_reads(void)
94 if (_ctx.pipe_fd[0] < 0)
96 "Invalid use of zed_log_pipe_close_reads in PID %d",
99 if (close(_ctx.pipe_fd[0]) < 0)
101 "Failed to close reads on daemonize pipe in PID %d: %s",
102 (int) getpid(), strerror(errno));
104 _ctx.pipe_fd[0] = -1;
108 * Close the write-half of the daemonize pipe.
109 * This should be called by the parent after fork()ing its child since the
110 * parent will never write to this pipe.
111 * This should also be called by the child once initialization is complete
112 * in order to signal the parent that it can safely exit.
115 zed_log_pipe_close_writes(void)
117 if (_ctx.pipe_fd[1] < 0)
119 "Invalid use of zed_log_pipe_close_writes in PID %d",
122 if (close(_ctx.pipe_fd[1]) < 0)
124 "Failed to close writes on daemonize pipe in PID %d: %s",
125 (int) getpid(), strerror(errno));
127 _ctx.pipe_fd[1] = -1;
131 * Block on reading from the daemonize pipe until signaled by the child
132 * (via zed_log_pipe_close_writes()) that initialization is complete.
133 * This should only be called by the parent while waiting to exit after
134 * fork()ing the child.
137 zed_log_pipe_wait(void)
142 if (_ctx.pipe_fd[0] < 0)
143 zed_log_die("Invalid use of zed_log_pipe_wait in PID %d",
147 n = read(_ctx.pipe_fd[0], &c, sizeof (c));
152 "Failed to read from daemonize pipe in PID %d: %s",
153 (int) getpid(), strerror(errno));
162 zed_log_stderr_open(int level)
169 zed_log_stderr_close(void)
175 zed_log_syslog_open(int facility)
177 const char *identity;
180 identity = (_ctx.id[0] == '\0') ? NULL : _ctx.id;
181 openlog(identity, LOG_NDELAY, facility);
185 zed_log_syslog_close(void)
192 _zed_log_aux(int priority, const char *fmt, va_list vargs)
194 char buf[ZED_LOG_MAX_LOG_LEN];
206 if (_ctx.id[0] != '\0') {
207 n = snprintf(p, len, "%s: ", _ctx.id);
208 if ((n < 0) || (n >= len)) {
216 if ((len > 0) && fmt) {
218 n = vsnprintf(p, len, fmt, vargs);
219 if ((n < 0) || (n >= len)) {
229 if (_ctx.do_syslog && syslogp)
230 syslog(priority, "%s", syslogp);
232 if (_ctx.do_stderr && priority <= _ctx.level)
233 fprintf(stderr, "%s\n", buf);
237 * Log a message at the given [priority] level specified by the printf-style
238 * format string [fmt].
241 zed_log_msg(int priority, const char *fmt, ...)
246 va_start(vargs, fmt);
247 _zed_log_aux(priority, fmt, vargs);
253 * Log a fatal error message specified by the printf-style format string [fmt].
256 zed_log_die(const char *fmt, ...)
261 va_start(vargs, fmt);
262 _zed_log_aux(LOG_ERR, fmt, vargs);