summaryrefslogtreecommitdiff
path: root/usr/src/cmd/smbsrv/smbd/smbd_syslog.c
blob: 46389ce226fee35de08640669f763368b5300230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
 */

#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>

#include <smbsrv/smbinfo.h>
#include <smbsrv/smb_ioctl.h>
#include "smbd.h"

#define	CBUFSIZ 26	/* ctime(3c) */

static const char *pri_name[LOG_DEBUG+1] = {
	"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"
};

static void
smb_svc_log(int pri, const char *fmt, va_list ap)
{
	static time_t prev_ts;
	char fbuf[SMBD_LOG_MSGSIZE];
	char cbuf[CBUFSIZ];
	char *newfmt;
	time_t ts;
	int save_errno = errno;

	pri &= LOG_PRIMASK;
	if (smbd.s_debug == 0 && pri == LOG_DEBUG)
		return;

	ts = time(NULL);
	if (prev_ts != ts) {
		prev_ts = ts;
		/* NB: cbuf has \n */
		(void) fprintf(stdout, "@ %s",
		    ctime_r(&ts, cbuf, sizeof (cbuf)));
	}

	newfmt = smb_syslog_fmt_m(fbuf, sizeof (fbuf), fmt, save_errno);

	flockfile(stdout);
	(void) fprintf(stdout, "smbd.%s: ", pri_name[pri]);
	/* LINTED E_SEC_PRINTF_VAR_FMT */
	(void) vfprintf(stdout, newfmt, ap);
	(void) fprintf(stdout, "\n");
	funlockfile(stdout);

	(void) fflush(stdout);
}

/*
 * Provide a replacement for libsmb:smb_vsyslog() that prints messages
 * both to the normal sysloc(3c), and to stdout, which ends up in:
 *  /var/svc/log/network-smb-server:default.log
 * It's much easier to follow debug messages in the service log.
 */
void
smb_vsyslog(int pri, const char *fmt, va_list ap)
{
	va_list tap;

	va_copy(tap, ap);
	smb_svc_log(pri, fmt, tap);
	va_end(tap);

	vsyslog(pri, fmt, ap);
}

/*
 * An override for libsmb:smb_trace().  As the comment there says:
 *
 * This function is designed to be used with dtrace, i.e. see:
 * usr/src/cmd/smbsrv/dtrace/smbd-all.d
 *
 * Outside of dtrace, the messages passed to this function usually
 * lack sufficient context to be useful, so don't log them.
 * However, if you insist, set debug >= 3 and this will log them.
 */
void
smb_trace(const char *s)
{
	if (smbd.s_debug >= 3)
		(void) fprintf(stdout, "smbd.trace: %s\n", s);
}