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