1 /* $NetBSD: refuse_log.c,v 1.2 2022/01/29 00:03:41 tnn Exp $ */
2
3 /*
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if !defined(lint)
34 __RCSID("$NetBSD: refuse_log.c,v 1.2 2022/01/29 00:03:41 tnn Exp $");
35 #endif /* !lint */
36
37 #include <assert.h>
38 #include <fuse_log.h>
39 #if defined(MULTITHREADED_REFUSE)
40 # include <pthread.h>
41 #endif
42 #include <stdio.h>
43
44 static void __printflike(2, 0)
default_log_func(enum fuse_log_level level,const char * fmt,va_list ap)45 default_log_func(enum fuse_log_level level __attribute__((__unused__)),
46 const char *fmt, va_list ap) {
47 /* This function needs to be thread-safe. Calling vfprintf(3)
48 * should be okay because POSIX mandates locking FILE* objects
49 * internally. */
50 vfprintf(stderr, fmt, ap);
51 }
52
53 #if defined(MULTITHREADED_REFUSE)
54 static pthread_mutex_t log_func_mutex = PTHREAD_MUTEX_INITIALIZER;
55 #endif
56 static fuse_log_func_t log_func = default_log_func;
57
58 void
fuse_set_log_func(fuse_log_func_t func)59 fuse_set_log_func(fuse_log_func_t func) {
60 #if defined(MULTITHREADED_REFUSE)
61 /* What we really need here is merely a memory barrier, but
62 * locking a mutex is the easiest way to achieve that. */
63 int rv;
64
65 rv = pthread_mutex_lock(&log_func_mutex);
66 assert(rv == 0);
67 #endif
68
69 if (func)
70 log_func = func;
71 else
72 log_func = default_log_func;
73
74 #if defined(MULTITHREADED_REFUSE)
75 rv = pthread_mutex_unlock(&log_func_mutex);
76 assert(rv == 0);
77 #endif
78 }
79
80 void
fuse_log(enum fuse_log_level level,const char * fmt,...)81 fuse_log(enum fuse_log_level level, const char *fmt, ...) {
82 va_list ap;
83 #if defined(MULTITHREADED_REFUSE)
84 /* What we really need here is merely a memory barrier, but
85 * locking a mutex is the easiest way to achieve that. */
86 int rv;
87
88 rv = pthread_mutex_lock(&log_func_mutex);
89 assert(rv == 0);
90 #endif
91
92 va_start(ap, fmt);
93 log_func(level, fmt, ap);
94 va_end(ap);
95
96 #if defined(MULTITHREADED_REFUSE)
97 rv = pthread_mutex_unlock(&log_func_mutex);
98 assert(rv == 0);
99 #endif
100 }
101