1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Edward Tomasz Napierala under sponsorship 8 * from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <errno.h> 37 #include <stdarg.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <syslog.h> 42 #include <vis.h> 43 44 #include "common.h" 45 46 static int log_level = 0; 47 static char *peer_name = NULL; 48 static char *peer_addr = NULL; 49 50 #define MSGBUF_LEN 1024 51 52 void 53 log_init(int level) 54 { 55 56 log_level = level; 57 openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON); 58 } 59 60 void 61 log_set_peer_name(const char *name) 62 { 63 64 /* 65 * XXX: Turn it into assertion? 66 */ 67 if (peer_name != NULL) 68 log_errx(1, "%s called twice", __func__); 69 if (peer_addr == NULL) 70 log_errx(1, "%s called before log_set_peer_addr", __func__); 71 72 peer_name = checked_strdup(name); 73 } 74 75 void 76 log_set_peer_addr(const char *addr) 77 { 78 79 /* 80 * XXX: Turn it into assertion? 81 */ 82 if (peer_addr != NULL) 83 log_errx(1, "%s called twice", __func__); 84 85 peer_addr = checked_strdup(addr); 86 } 87 88 static void 89 log_common(int priority, int log_errno, const char *fmt, va_list ap) 90 { 91 static char msgbuf[MSGBUF_LEN]; 92 static char msgbuf_strvised[MSGBUF_LEN * 4 + 1]; 93 char *errstr; 94 int ret; 95 96 ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); 97 if (ret < 0) { 98 fprintf(stderr, "%s: snprintf failed", getprogname()); 99 syslog(LOG_CRIT, "snprintf failed"); 100 exit(1); 101 } 102 103 ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL); 104 if (ret < 0) { 105 fprintf(stderr, "%s: strnvis failed", getprogname()); 106 syslog(LOG_CRIT, "strnvis failed"); 107 exit(1); 108 } 109 110 if (log_errno == -1) { 111 if (peer_name != NULL) { 112 fprintf(stderr, "%s: %s (%s): %s\n", getprogname(), 113 peer_addr, peer_name, msgbuf_strvised); 114 syslog(priority, "%s (%s): %s", 115 peer_addr, peer_name, msgbuf_strvised); 116 } else if (peer_addr != NULL) { 117 fprintf(stderr, "%s: %s: %s\n", getprogname(), 118 peer_addr, msgbuf_strvised); 119 syslog(priority, "%s: %s", 120 peer_addr, msgbuf_strvised); 121 } else { 122 fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised); 123 syslog(priority, "%s", msgbuf_strvised); 124 } 125 126 } else { 127 errstr = strerror(log_errno); 128 129 if (peer_name != NULL) { 130 fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(), 131 peer_addr, peer_name, msgbuf_strvised, errstr); 132 syslog(priority, "%s (%s): %s: %s", 133 peer_addr, peer_name, msgbuf_strvised, errstr); 134 } else if (peer_addr != NULL) { 135 fprintf(stderr, "%s: %s: %s: %s\n", getprogname(), 136 peer_addr, msgbuf_strvised, errstr); 137 syslog(priority, "%s: %s: %s", 138 peer_addr, msgbuf_strvised, errstr); 139 } else { 140 fprintf(stderr, "%s: %s: %s\n", getprogname(), 141 msgbuf_strvised, errstr); 142 syslog(priority, "%s: %s", 143 msgbuf_strvised, errstr); 144 } 145 } 146 } 147 148 void 149 log_err(int eval, const char *fmt, ...) 150 { 151 va_list ap; 152 153 va_start(ap, fmt); 154 log_common(LOG_CRIT, errno, fmt, ap); 155 va_end(ap); 156 157 exit(eval); 158 } 159 160 void 161 log_errx(int eval, const char *fmt, ...) 162 { 163 va_list ap; 164 165 va_start(ap, fmt); 166 log_common(LOG_CRIT, -1, fmt, ap); 167 va_end(ap); 168 169 exit(eval); 170 } 171 172 void 173 log_warn(const char *fmt, ...) 174 { 175 va_list ap; 176 177 va_start(ap, fmt); 178 log_common(LOG_WARNING, errno, fmt, ap); 179 va_end(ap); 180 } 181 182 void 183 log_warnx(const char *fmt, ...) 184 { 185 va_list ap; 186 187 va_start(ap, fmt); 188 log_common(LOG_WARNING, -1, fmt, ap); 189 va_end(ap); 190 } 191 192 void 193 log_debugx(const char *fmt, ...) 194 { 195 va_list ap; 196 197 if (log_level == 0) 198 return; 199 200 va_start(ap, fmt); 201 log_common(LOG_DEBUG, -1, fmt, ap); 202 va_end(ap); 203 } 204