1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <syslog.h>
21 #include <sys/strlog.h>
22 
23 #include <smbsrv/smbinfo.h>
24 #include <smbsrv/smb_ioctl.h>
25 #include "smbd.h"
26 
27 #include <libfakekernel/fakekernel.h>
28 
29 static const char *pri_name[LOG_DEBUG+1] = {
30 	"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"
31 };
32 
33 
34 /*
35  * Provide a replacement for libsmb:smb_vsyslog() that just
36  * prints the messages to stdout for "fksmbd" debugging.
37  */
38 void
smb_vsyslog(int pri,const char * fmt,va_list ap)39 smb_vsyslog(int pri, const char *fmt, va_list ap)
40 {
41 	int save_errno = errno;
42 	char buf[SMBD_LOG_MSGSIZE];
43 	char *newfmt;
44 
45 	pri &= LOG_PRIMASK;
46 
47 	if (smbd.s_debug == 0 && pri > LOG_INFO)
48 		return;
49 
50 	newfmt = smb_syslog_fmt_m(buf, sizeof (buf), fmt, save_errno);
51 
52 	flockfile(stdout);
53 	(void) fprintf(stdout, "fksmbd.%s: ", pri_name[pri]);
54 	/* LINTED E_SEC_PRINTF_VAR_FMT */
55 	(void) vfprintf(stdout, newfmt, ap);
56 	(void) fprintf(stdout, "\n");
57 	funlockfile(stdout);
58 
59 	(void) fflush(stdout);
60 }
61 
62 /*
63  * Provide a real function (one that prints something) to replace
64  * the stub in libfakekernel.  This prints cmn_err() messages.
65  */
66 void
fakekernel_putlog(char * msg,size_t len,int flags)67 fakekernel_putlog(char *msg, size_t len, int flags)
68 {
69 
70 	/*
71 	 * [CE_CONT, CE_NOTE, CE_WARN, CE_PANIC] maps to
72 	 * [SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL]
73 	 */
74 	if (smbd.s_debug == 0 && (flags & SL_NOTE))
75 		return;
76 	(void) fwrite(msg, 1, len, stdout);
77 	(void) fflush(stdout);
78 }
79 
80 /*
81  * Initialization function called at the start of fksmbd:main().
82  * Call an empty function in both of libfksmbsrv, libfakekernel,
83  * just to force them to load so we can set breakpoints in them
84  * without debugger forceload tricks.  This also avoids elfchk
85  * complaints from libfakekernel, which we don't call directly
86  * except for here.
87  */
88 void
fksmbd_init(void)89 fksmbd_init(void)
90 {
91 	fksmbsrv_drv_load();
92 	fakekernel_init();
93 }
94