2 * This file is part of the ZFS Event Daemon (ZED).
4 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
6 * Refer to the OpenZFS git commit log for authoritative copyright attribution.
8 * The contents of this file are subject to the terms of the
9 * Common Development and Distribution License Version 1.0 (CDDL-1.0).
10 * You can obtain a copy of the license from the top-level file
11 * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>.
12 * You may not use this file except in compliance with the license.
21 #include <sys/types.h>
26 #define ZED_LOG_MAX_LOG_LEN 1024
37 * Initialize the logging subsystem.
40 zed_log_init(const char *identity
)
43 const char *p
= strrchr(identity
, '/');
44 _ctx
.identity
= (p
!= NULL
) ? p
+ 1 : identity
;
53 * Shutdown the logging subsystem.
58 zed_log_stderr_close();
59 zed_log_syslog_close();
63 * Create pipe for communicating daemonization status between the parent and
64 * child processes across the double-fork().
67 zed_log_pipe_open(void)
69 if ((_ctx
.pipe_fd
[0] != -1) || (_ctx
.pipe_fd
[1] != -1))
70 zed_log_die("Invalid use of zed_log_pipe_open in PID %d",
73 if (pipe(_ctx
.pipe_fd
) < 0)
74 zed_log_die("Failed to create daemonize pipe in PID %d: %s",
75 (int)getpid(), strerror(errno
));
79 * Close the read-half of the daemonize pipe.
81 * This should be called by the child after fork()ing from the parent since
82 * the child will never read from this pipe.
85 zed_log_pipe_close_reads(void)
87 if (_ctx
.pipe_fd
[0] < 0)
89 "Invalid use of zed_log_pipe_close_reads in PID %d",
92 if (close(_ctx
.pipe_fd
[0]) < 0)
94 "Failed to close reads on daemonize pipe in PID %d: %s",
95 (int)getpid(), strerror(errno
));
101 * Close the write-half of the daemonize pipe.
103 * This should be called by the parent after fork()ing its child since the
104 * parent will never write to this pipe.
106 * This should also be called by the child once initialization is complete
107 * in order to signal the parent that it can safely exit.
110 zed_log_pipe_close_writes(void)
112 if (_ctx
.pipe_fd
[1] < 0)
114 "Invalid use of zed_log_pipe_close_writes in PID %d",
117 if (close(_ctx
.pipe_fd
[1]) < 0)
119 "Failed to close writes on daemonize pipe in PID %d: %s",
120 (int)getpid(), strerror(errno
));
122 _ctx
.pipe_fd
[1] = -1;
126 * Block on reading from the daemonize pipe until signaled by the child
127 * (via zed_log_pipe_close_writes()) that initialization is complete.
129 * This should only be called by the parent while waiting to exit after
130 * fork()ing the child.
133 zed_log_pipe_wait(void)
138 if (_ctx
.pipe_fd
[0] < 0)
139 zed_log_die("Invalid use of zed_log_pipe_wait in PID %d",
143 n
= read(_ctx
.pipe_fd
[0], &c
, sizeof (c
));
148 "Failed to read from daemonize pipe in PID %d: %s",
149 (int)getpid(), strerror(errno
));
158 * Start logging messages at the syslog [priority] level or higher to stderr.
159 * Refer to syslog(3) for valid priority values.
162 zed_log_stderr_open(int priority
)
165 _ctx
.priority
= priority
;
169 * Stop logging messages to stderr.
172 zed_log_stderr_close(void)
179 * Start logging messages to syslog.
180 * Refer to syslog(3) for valid option/facility values.
183 zed_log_syslog_open(int facility
)
186 openlog(_ctx
.identity
, LOG_NDELAY
| LOG_PID
, facility
);
190 * Stop logging messages to syslog.
193 zed_log_syslog_close(void)
195 if (_ctx
.do_syslog
) {
202 * Auxiliary function to log a message to syslog and/or stderr.
205 _zed_log_aux(int priority
, const char *fmt
, va_list vargs
)
207 char buf
[ZED_LOG_MAX_LOG_LEN
];
213 n
= vsnprintf(buf
, sizeof (buf
), fmt
, vargs
);
214 if ((n
< 0) || (n
>= sizeof (buf
))) {
215 buf
[sizeof (buf
) - 2] = '+';
216 buf
[sizeof (buf
) - 1] = '\0';
220 syslog(priority
, "%s", buf
);
222 if (_ctx
.do_stderr
&& (priority
<= _ctx
.priority
))
223 fprintf(stderr
, "%s\n", buf
);
227 * Log a message at the given [priority] level specified by the printf-style
228 * format string [fmt].
231 zed_log_msg(int priority
, const char *fmt
, ...)
236 va_start(vargs
, fmt
);
237 _zed_log_aux(priority
, fmt
, vargs
);
243 * Log a fatal error message specified by the printf-style format string [fmt].
246 zed_log_die(const char *fmt
, ...)
251 va_start(vargs
, fmt
);
252 _zed_log_aux(LOG_ERR
, fmt
, vargs
);