s3: VFS: vfs_snapper: Make chflags return errno = EROFS on a shadow copy path.
[Samba.git] / ctdb / server / ctdb_logging.c
blob8af787c189fa47d63cd501bb8ef0fb8cc44931e0
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 const char *prefix;
42 int fd, pfd;
43 char buf[1024];
44 uint16_t buf_used;
45 void (*logfn)(const char *, uint16_t, void *);
46 void *logfn_private;
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)
56 int ret;
58 log_state = talloc_zero(mem_ctx, struct ctdb_log_state);
59 if (log_state == NULL) {
60 return false;
63 ret = logging_init(mem_ctx, logging, debug_level, "ctdbd");
64 if (ret != 0) {
65 return false;
68 return true;
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);
78 } else {
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);
96 char *p;
97 int n;
99 if (!(flags & TEVENT_FD_READ)) {
100 return;
103 n = sys_read(log->pfd, &log->buf[log->buf_used],
104 sizeof(log->buf) - log->buf_used);
105 if (n > 0) {
106 log->buf_used += n;
107 } else if (n == 0) {
108 if (log != log_state) {
109 talloc_free(log);
111 return;
114 while (log->buf_used > 0 &&
115 (p = memchr(log->buf, '\n', log->buf_used)) != NULL) {
116 int n1 = (p - log->buf)+1;
117 int n2 = n1 - 1;
118 /* swallow \r from child processes */
119 if (n2 > 0 && log->buf[n2-1] == '\r') {
120 n2--;
122 write_to_log(log, log->buf, n2);
123 memmove(log->buf, p+1, sizeof(log->buf) - n1);
124 log->buf_used -= 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);
131 log->buf_used = 0;
136 setup for logging of child process stdout
138 int ctdb_set_child_logging(struct ctdb_context *ctdb)
140 int p[2];
141 int old_stdout, old_stderr;
142 struct tevent_fd *fde;
144 /* setup a pipe to catch IO from subprocesses */
145 if (pipe(p) != 0) {
146 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
147 return -1;
150 /* We'll fail if stderr/stdout not already open; it's simpler. */
151 old_stdout = dup(STDOUT_FILENO);
152 if (old_stdout < 0) {
153 DEBUG(DEBUG_ERR, ("Failed to dup stdout for child logging\n"));
154 return -1;
156 old_stderr = dup(STDERR_FILENO);
157 if (old_stderr < 0) {
158 DEBUG(DEBUG_ERR, ("Failed to dup stderr for child logging\n"));
159 close(old_stdout);
160 return -1;
162 if (dup2(p[1], STDOUT_FILENO) < 0 || dup2(p[1], STDERR_FILENO) < 0) {
163 int saved_errno = errno;
164 dup2(old_stdout, STDOUT_FILENO);
165 dup2(old_stderr, STDERR_FILENO);
166 close(old_stdout);
167 close(old_stderr);
168 close(p[0]);
169 close(p[1]);
170 errno = saved_errno;
172 printf(__location__ " dup2 failed: %s\n",
173 strerror(errno));
174 return -1;
176 close(p[1]);
177 close(old_stdout);
178 close(old_stderr);
180 fde = tevent_add_fd(ctdb->ev, log_state, p[0], TEVENT_FD_READ,
181 ctdb_child_log_handler, log_state);
182 tevent_fd_set_auto_close(fde);
184 log_state->pfd = p[0];
186 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
188 return 0;