1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba debug functions
|
---|
4 | Copyright (C) Andrew Tridgell 2003
|
---|
5 | Copyright (C) James J Myers 2003
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "system/filesys.h"
|
---|
23 | #include "system/time.h"
|
---|
24 | #include "dynconfig/dynconfig.h"
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @file
|
---|
28 | * @brief Debug logging
|
---|
29 | **/
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * this global variable determines what messages are printed
|
---|
33 | */
|
---|
34 | int _debug_level = 0;
|
---|
35 | _PUBLIC_ int *debug_level = &_debug_level;
|
---|
36 | static int debug_all_class_hack = 1;
|
---|
37 | int *DEBUGLEVEL_CLASS = &debug_all_class_hack; /* For samba 3 */
|
---|
38 | static bool debug_all_class_isset_hack = true;
|
---|
39 | bool *DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack; /* For samba 3 */
|
---|
40 | XFILE *dbf = NULL; /* For Samba 3*/
|
---|
41 |
|
---|
42 | /* the registered mutex handlers */
|
---|
43 | static struct {
|
---|
44 | const char *name;
|
---|
45 | struct debug_ops ops;
|
---|
46 | } debug_handlers;
|
---|
47 |
|
---|
48 | /* state variables for the debug system */
|
---|
49 | static struct {
|
---|
50 | int fd;
|
---|
51 | enum debug_logtype logtype;
|
---|
52 | const char *prog_name;
|
---|
53 | bool reopening_logs;
|
---|
54 | } state;
|
---|
55 |
|
---|
56 | static bool reopen_logs_scheduled;
|
---|
57 | static bool check_reopen_logs(void)
|
---|
58 | {
|
---|
59 | if (state.fd == 0 || reopen_logs_scheduled) {
|
---|
60 | reopen_logs_scheduled = false;
|
---|
61 | reopen_logs();
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (state.fd <= 0)
|
---|
65 | return false;
|
---|
66 |
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | _PUBLIC_ void debug_schedule_reopen_logs(void)
|
---|
71 | {
|
---|
72 | reopen_logs_scheduled = true;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static void log_timestring(int level, const char *location, const char *func)
|
---|
76 | {
|
---|
77 | char *t = NULL;
|
---|
78 | char *s = NULL;
|
---|
79 |
|
---|
80 | if (!check_reopen_logs()) return;
|
---|
81 |
|
---|
82 | if (state.logtype != DEBUG_FILE) return;
|
---|
83 |
|
---|
84 | t = timestring(NULL, time(NULL));
|
---|
85 | if (!t) return;
|
---|
86 |
|
---|
87 | asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
|
---|
88 | talloc_free(t);
|
---|
89 | if (!s) return;
|
---|
90 |
|
---|
91 | write(state.fd, s, strlen(s));
|
---|
92 | free(s);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | the backend for debug messages. Note that the DEBUG() macro has already
|
---|
97 | ensured that the log level has been met before this is called
|
---|
98 | */
|
---|
99 | _PUBLIC_ void dbghdr(int level, const char *location, const char *func)
|
---|
100 | {
|
---|
101 | log_timestring(level, location, func);
|
---|
102 | log_task_id();
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | _PUBLIC_ void dbghdrclass(int level, int dclass, const char *location, const char *func)
|
---|
107 | {
|
---|
108 | /* Simple wrapper, Samba 4 doesn't do debug classes */
|
---|
109 | dbghdr(level, location, func);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | the backend for debug messages. Note that the DEBUG() macro has already
|
---|
114 | ensured that the log level has been met before this is called
|
---|
115 |
|
---|
116 | @note You should never have to call this function directly. Call the DEBUG()
|
---|
117 | macro instead.
|
---|
118 | */
|
---|
119 | _PUBLIC_ void dbgtext(const char *format, ...)
|
---|
120 | {
|
---|
121 | va_list ap;
|
---|
122 | char *s = NULL;
|
---|
123 |
|
---|
124 | if (!check_reopen_logs()) return;
|
---|
125 |
|
---|
126 | va_start(ap, format);
|
---|
127 | vasprintf(&s, format, ap);
|
---|
128 | va_end(ap);
|
---|
129 |
|
---|
130 | write(state.fd, s, strlen(s));
|
---|
131 | free(s);
|
---|
132 | }
|
---|
133 |
|
---|
134 | _PUBLIC_ const char *logfile = NULL;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | reopen the log file (usually called because the log file name might have changed)
|
---|
138 | */
|
---|
139 | _PUBLIC_ void reopen_logs(void)
|
---|
140 | {
|
---|
141 | char *fname = NULL;
|
---|
142 | int old_fd = state.fd;
|
---|
143 | if (state.reopening_logs) {
|
---|
144 | return;
|
---|
145 | }
|
---|
146 |
|
---|
147 | switch (state.logtype) {
|
---|
148 | case DEBUG_STDOUT:
|
---|
149 | state.fd = 1;
|
---|
150 | break;
|
---|
151 |
|
---|
152 | case DEBUG_STDERR:
|
---|
153 | state.fd = 2;
|
---|
154 | break;
|
---|
155 |
|
---|
156 | case DEBUG_FILE:
|
---|
157 | state.reopening_logs = true;
|
---|
158 | if (logfile && (*logfile) == '/') {
|
---|
159 | fname = strdup(logfile);
|
---|
160 | } else {
|
---|
161 | asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
|
---|
162 | }
|
---|
163 | if (fname) {
|
---|
164 | int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
|
---|
165 | if (newfd == -1) {
|
---|
166 | DEBUG(1, ("Failed to open new logfile: %s\n", fname));
|
---|
167 | old_fd = -1;
|
---|
168 | } else {
|
---|
169 | state.fd = newfd;
|
---|
170 | }
|
---|
171 | free(fname);
|
---|
172 | } else {
|
---|
173 | DEBUG(1, ("Failed to find name for file-based logfile!\n"));
|
---|
174 | }
|
---|
175 | state.reopening_logs = false;
|
---|
176 |
|
---|
177 | break;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (old_fd > 2) {
|
---|
181 | close(old_fd);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | control the name of the logfile and whether logging will be to stdout, stderr
|
---|
187 | or a file
|
---|
188 | */
|
---|
189 | _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
|
---|
190 | {
|
---|
191 | if (state.logtype < new_logtype) {
|
---|
192 | state.logtype = new_logtype;
|
---|
193 | }
|
---|
194 | if (prog_name) {
|
---|
195 | state.prog_name = prog_name;
|
---|
196 | }
|
---|
197 | reopen_logs();
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | Just run logging to stdout for this program
|
---|
202 | */
|
---|
203 | _PUBLIC_ void setup_logging_stdout(void)
|
---|
204 | {
|
---|
205 | setup_logging(NULL, DEBUG_STDOUT);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | return a string constant containing n tabs
|
---|
210 | no more than 10 tabs are returned
|
---|
211 | */
|
---|
212 | _PUBLIC_ const char *do_debug_tab(int n)
|
---|
213 | {
|
---|
214 | const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t",
|
---|
215 | "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t",
|
---|
216 | "\t\t\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t"};
|
---|
217 | return tabs[MIN(n, 10)];
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | log suspicious usage - print comments and backtrace
|
---|
223 | */
|
---|
224 | _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
|
---|
225 | {
|
---|
226 | if (!debug_handlers.ops.log_suspicious_usage) return;
|
---|
227 |
|
---|
228 | debug_handlers.ops.log_suspicious_usage(from, info);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | print suspicious usage - print comments and backtrace
|
---|
234 | */
|
---|
235 | _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
|
---|
236 | {
|
---|
237 | if (!debug_handlers.ops.print_suspicious_usage) return;
|
---|
238 |
|
---|
239 | debug_handlers.ops.print_suspicious_usage(from, info);
|
---|
240 | }
|
---|
241 |
|
---|
242 | _PUBLIC_ uint32_t get_task_id(void)
|
---|
243 | {
|
---|
244 | if (debug_handlers.ops.get_task_id) {
|
---|
245 | return debug_handlers.ops.get_task_id();
|
---|
246 | }
|
---|
247 | return getpid();
|
---|
248 | }
|
---|
249 |
|
---|
250 | _PUBLIC_ void log_task_id(void)
|
---|
251 | {
|
---|
252 | if (!debug_handlers.ops.log_task_id) return;
|
---|
253 |
|
---|
254 | if (!check_reopen_logs()) return;
|
---|
255 |
|
---|
256 | debug_handlers.ops.log_task_id(state.fd);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | register a set of debug handlers.
|
---|
261 | */
|
---|
262 | _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
|
---|
263 | {
|
---|
264 | debug_handlers.name = name;
|
---|
265 | debug_handlers.ops = *ops;
|
---|
266 | }
|
---|