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
{
46 /* Used by ctdb_set_child_logging() */
47 static struct ctdb_log_state
*log_state
;
49 /* Initialise logging */
50 bool ctdb_logging_init(TALLOC_CTX
*mem_ctx
, const char *logging
,
51 const char *debug_level
)
55 log_state
= talloc_zero(mem_ctx
, struct ctdb_log_state
);
56 if (log_state
== NULL
) {
60 ret
= logging_init(mem_ctx
, logging
, debug_level
, "ctdbd");
68 static void write_to_log(const char *buf
, unsigned int len
)
70 DEBUG(script_log_level
, ("%*.*s\n", len
, len
, buf
));
74 called when log data comes in from a child process
76 static void ctdb_child_log_handler(struct tevent_context
*ev
,
77 struct tevent_fd
*fde
,
78 uint16_t flags
, void *private)
80 struct ctdb_log_state
*log
= talloc_get_type(private, struct ctdb_log_state
);
84 if (!(flags
& TEVENT_FD_READ
)) {
88 n
= sys_read(log
->pfd
, &log
->buf
[log
->buf_used
],
89 sizeof(log
->buf
) - log
->buf_used
);
93 if (log
!= log_state
) {
99 while (log
->buf_used
> 0 &&
100 (p
= memchr(log
->buf
, '\n', log
->buf_used
)) != NULL
) {
101 int n1
= (p
- log
->buf
)+1;
103 /* swallow \r from child processes */
104 if (n2
> 0 && log
->buf
[n2
-1] == '\r') {
107 write_to_log(log
->buf
, n2
);
108 memmove(log
->buf
, p
+1, sizeof(log
->buf
) - n1
);
112 /* the buffer could have completely filled - unfortunately we have
113 no choice but to dump it out straight away */
114 if (log
->buf_used
== sizeof(log
->buf
)) {
115 write_to_log(log
->buf
, log
->buf_used
);
121 setup for logging of child process stdout
123 int ctdb_set_child_logging(struct ctdb_context
*ctdb
)
126 int old_stdout
, old_stderr
;
127 struct tevent_fd
*fde
;
129 /* setup a pipe to catch IO from subprocesses */
131 DEBUG(DEBUG_ERR
,(__location__
" Failed to setup for child logging pipe\n"));
135 /* We'll fail if stderr/stdout not already open; it's simpler. */
136 old_stdout
= dup(STDOUT_FILENO
);
137 if (old_stdout
< 0) {
138 DEBUG(DEBUG_ERR
, ("Failed to dup stdout for child logging\n"));
141 old_stderr
= dup(STDERR_FILENO
);
142 if (old_stderr
< 0) {
143 DEBUG(DEBUG_ERR
, ("Failed to dup stderr for child logging\n"));
147 if (dup2(p
[1], STDOUT_FILENO
) < 0 || dup2(p
[1], STDERR_FILENO
) < 0) {
148 int saved_errno
= errno
;
149 dup2(old_stdout
, STDOUT_FILENO
);
150 dup2(old_stderr
, STDERR_FILENO
);
157 printf(__location__
" dup2 failed: %s\n",
165 fde
= tevent_add_fd(ctdb
->ev
, log_state
, p
[0], TEVENT_FD_READ
,
166 ctdb_child_log_handler
, log_state
);
167 tevent_fd_set_auto_close(fde
);
169 log_state
->pfd
= p
[0];
171 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d for logging\n", p
[0]));