4 Copyright (C) Andrew Tridgell 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/time.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/blocking.h"
31 #include "lib/util/sys_rw.h"
32 #include "lib/util/time.h"
34 #include "ctdb_private.h"
35 #include "ctdb_client.h"
37 #include "common/common.h"
38 #include "common/logging.h"
40 struct ctdb_log_state
{
45 void (*logfn
)(const char *, uint16_t, void *);
49 /* Used by ctdb_set_child_logging() */
50 static struct ctdb_log_state
*log_state
;
52 /* Initialise logging */
53 bool ctdb_logging_init(TALLOC_CTX
*mem_ctx
, const char *logging
,
54 const char *debug_level
)
58 log_state
= talloc_zero(mem_ctx
, struct ctdb_log_state
);
59 if (log_state
== NULL
) {
63 ret
= logging_init(mem_ctx
, logging
, debug_level
, "ctdbd");
71 /* Note that do_debug always uses the global log state. */
72 static void write_to_log(struct ctdb_log_state
*log
,
73 const char *buf
, unsigned int len
)
75 if (script_log_level
<= DEBUGLEVEL
) {
76 if (log
!= NULL
&& log
->prefix
!= NULL
) {
77 dbgtext("%s: %*.*s\n", log
->prefix
, len
, len
, buf
);
79 dbgtext("%*.*s\n", len
, len
, buf
);
81 /* log it in the eventsystem as well */
82 if (log
&& log
->logfn
) {
83 log
->logfn(log
->buf
, len
, log
->logfn_private
);
89 called when log data comes in from a child process
91 static void ctdb_child_log_handler(struct tevent_context
*ev
,
92 struct tevent_fd
*fde
,
93 uint16_t flags
, void *private)
95 struct ctdb_log_state
*log
= talloc_get_type(private, struct ctdb_log_state
);
99 if (!(flags
& TEVENT_FD_READ
)) {
103 n
= sys_read(log
->pfd
, &log
->buf
[log
->buf_used
],
104 sizeof(log
->buf
) - log
->buf_used
);
108 if (log
!= log_state
) {
114 while (log
->buf_used
> 0 &&
115 (p
= memchr(log
->buf
, '\n', log
->buf_used
)) != NULL
) {
116 int n1
= (p
- log
->buf
)+1;
118 /* swallow \r from child processes */
119 if (n2
> 0 && log
->buf
[n2
-1] == '\r') {
122 write_to_log(log
, log
->buf
, n2
);
123 memmove(log
->buf
, p
+1, sizeof(log
->buf
) - n1
);
127 /* the buffer could have completely filled - unfortunately we have
128 no choice but to dump it out straight away */
129 if (log
->buf_used
== sizeof(log
->buf
)) {
130 write_to_log(log
, log
->buf
, log
->buf_used
);
135 static int log_context_destructor(struct ctdb_log_state
*log
)
137 /* Flush buffer in case it wasn't \n-terminated. */
138 if (log
->buf_used
> 0) {
139 write_to_log(log
, log
->buf
, log
->buf_used
);
145 * vfork + exec, redirecting child output to logging and specified callback.
147 struct ctdb_log_state
*ctdb_vfork_with_logging(TALLOC_CTX
*mem_ctx
,
148 struct ctdb_context
*ctdb
,
149 const char *log_prefix
,
152 const char **helper_argv
,
153 void (*logfn
)(const char *, uint16_t, void *),
154 void *logfn_private
, pid_t
*pid
)
157 struct ctdb_log_state
*log
;
158 struct tevent_fd
*fde
;
161 struct timeval before
;
164 log
= talloc_zero(mem_ctx
, struct ctdb_log_state
);
165 CTDB_NO_MEMORY_NULL(ctdb
, log
);
167 log
->prefix
= log_prefix
;
169 log
->logfn_private
= logfn_private
;
172 DEBUG(DEBUG_ERR
, (__location__
" Failed to setup pipe for child logging:"
173 " %s\n", strerror(errno
)));
177 argv
= talloc_array(mem_ctx
, char *, helper_argc
+ 2);
179 DEBUG(DEBUG_ERR
, (__location__
"Failed to allocate memory for helper\n"));
182 argv
[0] = discard_const(helper
);
183 argv
[1] = talloc_asprintf(argv
, "%d", p
[1]);
184 if (argv
[1] == NULL
) {
185 DEBUG(DEBUG_ERR
, (__location__
"Failed to allocate memory for helper\n"));
190 for (i
=0; i
<helper_argc
; i
++) {
191 argv
[i
+2] = discard_const(helper_argv
[i
]);
194 before
= timeval_current();
204 DEBUG(DEBUG_ERR
, (__location__
"vfork failed for helper process\n"));
209 delta_t
= timeval_elapsed(&before
);
211 DEBUG(DEBUG_WARNING
, ("vfork() took %lf seconds\n", delta_t
));
214 ctdb_track_child(ctdb
, *pid
);
217 set_close_on_exec(log
->pfd
);
218 talloc_set_destructor(log
, log_context_destructor
);
219 fde
= tevent_add_fd(ctdb
->ev
, log
, log
->pfd
, TEVENT_FD_READ
,
220 ctdb_child_log_handler
, log
);
221 tevent_fd_set_auto_close(fde
);
232 setup for logging of child process stdout
234 int ctdb_set_child_logging(struct ctdb_context
*ctdb
)
237 int old_stdout
, old_stderr
;
238 struct tevent_fd
*fde
;
240 /* setup a pipe to catch IO from subprocesses */
242 DEBUG(DEBUG_ERR
,(__location__
" Failed to setup for child logging pipe\n"));
246 /* We'll fail if stderr/stdout not already open; it's simpler. */
247 old_stdout
= dup(STDOUT_FILENO
);
248 if (old_stdout
< 0) {
249 DEBUG(DEBUG_ERR
, ("Failed to dup stdout for child logging\n"));
252 old_stderr
= dup(STDERR_FILENO
);
253 if (old_stderr
< 0) {
254 DEBUG(DEBUG_ERR
, ("Failed to dup stderr for child logging\n"));
258 if (dup2(p
[1], STDOUT_FILENO
) < 0 || dup2(p
[1], STDERR_FILENO
) < 0) {
259 int saved_errno
= errno
;
260 dup2(old_stdout
, STDOUT_FILENO
);
261 dup2(old_stderr
, STDERR_FILENO
);
268 printf(__location__
" dup2 failed: %s\n",
276 fde
= tevent_add_fd(ctdb
->ev
, log_state
, p
[0], TEVENT_FD_READ
,
277 ctdb_child_log_handler
, log_state
);
278 tevent_fd_set_auto_close(fde
);
280 log_state
->pfd
= p
[0];
282 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d for logging\n", p
[0]));