s3:ntlm_auth: make logs more consistent with length check
[Samba.git] / ctdb / server / ctdb_logging.c
blob1da26b5534c76e05d28b263e691439e70fd51d82
1 /*
2 ctdb logging code
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/time.h"
25 #include <talloc.h>
26 #include <tevent.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 {
41 int fd, pfd;
42 char buf[1024];
43 uint16_t buf_used;
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)
53 int ret;
55 log_state = talloc_zero(mem_ctx, struct ctdb_log_state);
56 if (log_state == NULL) {
57 return false;
60 ret = logging_init(mem_ctx, logging, debug_level, "ctdbd");
61 if (ret != 0) {
62 return false;
65 return true;
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);
81 char *p;
82 int n;
84 if (!(flags & TEVENT_FD_READ)) {
85 return;
88 n = sys_read(log->pfd, &log->buf[log->buf_used],
89 sizeof(log->buf) - log->buf_used);
90 if (n > 0) {
91 log->buf_used += n;
92 } else if (n == 0) {
93 if (log != log_state) {
94 talloc_free(log);
96 return;
99 while (log->buf_used > 0 &&
100 (p = memchr(log->buf, '\n', log->buf_used)) != NULL) {
101 int n1 = (p - log->buf)+1;
102 int n2 = n1 - 1;
103 /* swallow \r from child processes */
104 if (n2 > 0 && log->buf[n2-1] == '\r') {
105 n2--;
107 write_to_log(log->buf, n2);
108 memmove(log->buf, p+1, sizeof(log->buf) - n1);
109 log->buf_used -= 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);
116 log->buf_used = 0;
121 setup for logging of child process stdout
123 int ctdb_set_child_logging(struct ctdb_context *ctdb)
125 int p[2];
126 int old_stdout, old_stderr;
127 struct tevent_fd *fde;
129 /* setup a pipe to catch IO from subprocesses */
130 if (pipe(p) != 0) {
131 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
132 return -1;
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"));
139 return -1;
141 old_stderr = dup(STDERR_FILENO);
142 if (old_stderr < 0) {
143 DEBUG(DEBUG_ERR, ("Failed to dup stderr for child logging\n"));
144 close(old_stdout);
145 return -1;
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);
151 close(old_stdout);
152 close(old_stderr);
153 close(p[0]);
154 close(p[1]);
155 errno = saved_errno;
157 printf(__location__ " dup2 failed: %s\n",
158 strerror(errno));
159 return -1;
161 close(p[1]);
162 close(old_stdout);
163 close(old_stderr);
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]));
173 return 0;