]>
git.proxmox.com Git - mirror_zfs.git/blob - cmd/zed/zed_log.c
2 * This file is part of the ZFS Event Daemon (ZED)
3 * for ZFS on Linux (ZoL) <http://zfsonlinux.org/>.
4 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
6 * Refer to the ZoL 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.
24 #define ZED_LOG_MAX_LOG_LEN 1024
35 * Initialize the logging subsystem.
38 zed_log_init(const char *identity
)
41 const char *p
= strrchr(identity
, '/');
42 _ctx
.identity
= (p
!= NULL
) ? p
+ 1 : identity
;
51 * Shutdown the logging subsystem.
56 zed_log_stderr_close();
57 zed_log_syslog_close();
61 * Create pipe for communicating daemonization status between the parent and
62 * child processes across the double-fork().
65 zed_log_pipe_open(void)
67 if ((_ctx
.pipe_fd
[0] != -1) || (_ctx
.pipe_fd
[1] != -1))
68 zed_log_die("Invalid use of zed_log_pipe_open in PID %d",
71 if (pipe(_ctx
.pipe_fd
) < 0)
72 zed_log_die("Failed to create daemonize pipe in PID %d: %s",
73 (int)getpid(), strerror(errno
));
77 * Close the read-half of the daemonize pipe.
79 * This should be called by the child after fork()ing from the parent since
80 * the child will never read from this pipe.
83 zed_log_pipe_close_reads(void)
85 if (_ctx
.pipe_fd
[0] < 0)
87 "Invalid use of zed_log_pipe_close_reads in PID %d",
90 if (close(_ctx
.pipe_fd
[0]) < 0)
92 "Failed to close reads on daemonize pipe in PID %d: %s",
93 (int)getpid(), strerror(errno
));
99 * Close the write-half of the daemonize pipe.
101 * This should be called by the parent after fork()ing its child since the
102 * parent will never write to this pipe.
104 * This should also be called by the child once initialization is complete
105 * in order to signal the parent that it can safely exit.
108 zed_log_pipe_close_writes(void)
110 if (_ctx
.pipe_fd
[1] < 0)
112 "Invalid use of zed_log_pipe_close_writes in PID %d",
115 if (close(_ctx
.pipe_fd
[1]) < 0)
117 "Failed to close writes on daemonize pipe in PID %d: %s",
118 (int)getpid(), strerror(errno
));
120 _ctx
.pipe_fd
[1] = -1;
124 * Block on reading from the daemonize pipe until signaled by the child
125 * (via zed_log_pipe_close_writes()) that initialization is complete.
127 * This should only be called by the parent while waiting to exit after
128 * fork()ing the child.
131 zed_log_pipe_wait(void)
136 if (_ctx
.pipe_fd
[0] < 0)
137 zed_log_die("Invalid use of zed_log_pipe_wait in PID %d",
141 n
= read(_ctx
.pipe_fd
[0], &c
, sizeof (c
));
146 "Failed to read from daemonize pipe in PID %d: %s",
147 (int)getpid(), strerror(errno
));
156 * Start logging messages at the syslog [priority] level or higher to stderr.
157 * Refer to syslog(3) for valid priority values.
160 zed_log_stderr_open(int priority
)
163 _ctx
.priority
= priority
;
167 * Stop logging messages to stderr.
170 zed_log_stderr_close(void)
177 * Start logging messages to syslog.
178 * Refer to syslog(3) for valid option/facility values.
181 zed_log_syslog_open(int facility
)
184 openlog(_ctx
.identity
, LOG_NDELAY
| LOG_PID
, facility
);
188 * Stop logging messages to syslog.
191 zed_log_syslog_close(void)
193 if (_ctx
.do_syslog
) {
200 * Auxiliary function to log a message to syslog and/or stderr.
203 _zed_log_aux(int priority
, const char *fmt
, va_list vargs
)
205 char buf
[ZED_LOG_MAX_LOG_LEN
];
211 n
= vsnprintf(buf
, sizeof (buf
), fmt
, vargs
);
212 if ((n
< 0) || (n
>= sizeof (buf
))) {
213 buf
[sizeof (buf
) - 2] = '+';
214 buf
[sizeof (buf
) - 1] = '\0';
218 syslog(priority
, "%s", buf
);
220 if (_ctx
.do_stderr
&& (priority
<= _ctx
.priority
))
221 fprintf(stderr
, "%s\n", buf
);
225 * Log a message at the given [priority] level specified by the printf-style
226 * format string [fmt].
229 zed_log_msg(int priority
, const char *fmt
, ...)
234 va_start(vargs
, fmt
);
235 _zed_log_aux(priority
, fmt
, vargs
);
241 * Log a fatal error message specified by the printf-style format string [fmt].
244 zed_log_die(const char *fmt
, ...)
249 va_start(vargs
, fmt
);
250 _zed_log_aux(LOG_ERR
, fmt
, vargs
);