1*b819cea2SGordon Ross /* 2*b819cea2SGordon Ross * This file and its contents are supplied under the terms of the 3*b819cea2SGordon Ross * Common Development and Distribution License ("CDDL"), version 1.0. 4*b819cea2SGordon Ross * You may only use this file in accordance with the terms of version 5*b819cea2SGordon Ross * 1.0 of the CDDL. 6*b819cea2SGordon Ross * 7*b819cea2SGordon Ross * A full copy of the text of the CDDL should have accompanied this 8*b819cea2SGordon Ross * source. A copy of the CDDL is also available via the Internet at 9*b819cea2SGordon Ross * http://www.illumos.org/license/CDDL. 10*b819cea2SGordon Ross */ 11*b819cea2SGordon Ross 12*b819cea2SGordon Ross /* 13*b819cea2SGordon Ross * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 14*b819cea2SGordon Ross */ 15*b819cea2SGordon Ross 16*b819cea2SGordon Ross #include <stdio.h> 17*b819cea2SGordon Ross #include <stdarg.h> 18*b819cea2SGordon Ross #include <errno.h> 19*b819cea2SGordon Ross #include <string.h> 20*b819cea2SGordon Ross #include <syslog.h> 21*b819cea2SGordon Ross 22*b819cea2SGordon Ross #include <smbsrv/smbinfo.h> 23*b819cea2SGordon Ross #include <smbsrv/smb_ioctl.h> 24*b819cea2SGordon Ross #include "smbd.h" 25*b819cea2SGordon Ross 26*b819cea2SGordon Ross #define CBUFSIZ 26 /* ctime(3c) */ 27*b819cea2SGordon Ross 28*b819cea2SGordon Ross static const char *pri_name[LOG_DEBUG+1] = { 29*b819cea2SGordon Ross "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" 30*b819cea2SGordon Ross }; 31*b819cea2SGordon Ross 32*b819cea2SGordon Ross static void 33*b819cea2SGordon Ross smb_svc_log(int pri, const char *fmt, va_list ap) 34*b819cea2SGordon Ross { 35*b819cea2SGordon Ross static time_t prev_ts; 36*b819cea2SGordon Ross char fbuf[SMBD_LOG_MSGSIZE]; 37*b819cea2SGordon Ross char cbuf[CBUFSIZ]; 38*b819cea2SGordon Ross char *newfmt; 39*b819cea2SGordon Ross time_t ts; 40*b819cea2SGordon Ross int save_errno = errno; 41*b819cea2SGordon Ross 42*b819cea2SGordon Ross pri &= LOG_PRIMASK; 43*b819cea2SGordon Ross if (smbd.s_debug == 0 && pri == LOG_DEBUG) 44*b819cea2SGordon Ross return; 45*b819cea2SGordon Ross 46*b819cea2SGordon Ross ts = time(NULL); 47*b819cea2SGordon Ross if (prev_ts != ts) { 48*b819cea2SGordon Ross prev_ts = ts; 49*b819cea2SGordon Ross /* NB: cbuf has \n */ 50*b819cea2SGordon Ross (void) fprintf(stdout, "@ %s", 51*b819cea2SGordon Ross ctime_r(&ts, cbuf, sizeof (cbuf))); 52*b819cea2SGordon Ross } 53*b819cea2SGordon Ross 54*b819cea2SGordon Ross newfmt = smb_syslog_fmt_m(fbuf, sizeof (fbuf), fmt, save_errno); 55*b819cea2SGordon Ross 56*b819cea2SGordon Ross flockfile(stdout); 57*b819cea2SGordon Ross (void) fprintf(stdout, "smbd.%s: ", pri_name[pri]); 58*b819cea2SGordon Ross /* LINTED E_SEC_PRINTF_VAR_FMT */ 59*b819cea2SGordon Ross (void) vfprintf(stdout, newfmt, ap); 60*b819cea2SGordon Ross (void) fprintf(stdout, "\n"); 61*b819cea2SGordon Ross funlockfile(stdout); 62*b819cea2SGordon Ross 63*b819cea2SGordon Ross (void) fflush(stdout); 64*b819cea2SGordon Ross } 65*b819cea2SGordon Ross 66*b819cea2SGordon Ross /* 67*b819cea2SGordon Ross * Provide a replacement for libsmb:smb_vsyslog() that prints messages 68*b819cea2SGordon Ross * both to the normal sysloc(3c), and to stdout, which ends up in: 69*b819cea2SGordon Ross * /var/svc/log/network-smb-server:default.log 70*b819cea2SGordon Ross * It's much easier to follow debug messages in the service log. 71*b819cea2SGordon Ross */ 72*b819cea2SGordon Ross void 73*b819cea2SGordon Ross smb_vsyslog(int pri, const char *fmt, va_list ap) 74*b819cea2SGordon Ross { 75*b819cea2SGordon Ross va_list tap; 76*b819cea2SGordon Ross 77*b819cea2SGordon Ross va_copy(tap, ap); 78*b819cea2SGordon Ross smb_svc_log(pri, fmt, tap); 79*b819cea2SGordon Ross va_end(tap); 80*b819cea2SGordon Ross 81*b819cea2SGordon Ross vsyslog(pri, fmt, ap); 82*b819cea2SGordon Ross } 83*b819cea2SGordon Ross 84*b819cea2SGordon Ross /* 85*b819cea2SGordon Ross * An override for libsmb:smb_trace(). As the comment there says: 86*b819cea2SGordon Ross * 87*b819cea2SGordon Ross * This function is designed to be used with dtrace, i.e. see: 88*b819cea2SGordon Ross * usr/src/cmd/smbsrv/dtrace/smbd-all.d 89*b819cea2SGordon Ross * 90*b819cea2SGordon Ross * Outside of dtrace, the messages passed to this function usually 91*b819cea2SGordon Ross * lack sufficient context to be useful, so don't log them. 92*b819cea2SGordon Ross * However, if you insist, set debug >= 3 and this will log them. 93*b819cea2SGordon Ross */ 94*b819cea2SGordon Ross void 95*b819cea2SGordon Ross smb_trace(const char *s) 96*b819cea2SGordon Ross { 97*b819cea2SGordon Ross if (smbd.s_debug >= 3) 98*b819cea2SGordon Ross (void) fprintf(stdout, "smbd.trace: %s\n", s); 99*b819cea2SGordon Ross } 100