initscript: If CTDB doesn't become ready, print a message before killing
[obnox/ctdb.git] / common / ctdb_fork.c
1 /* 
2    functions to track and manage processes
3
4    Copyright (C) Ronnie Sahlberg 2012
5
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include "system/wait.h"
22 #include "../include/ctdb_client.h"
23 #include "../include/ctdb_private.h"
24 #include "../common/rb_tree.h"
25
26 static bool is_child = false;
27
28 void ctdb_set_child_info(TALLOC_CTX *mem_ctx, const char *child_name_fmt, ...)
29 {
30         is_child = true;
31         if (child_name_fmt != NULL) {
32                 va_list ap;
33                 char *t;
34
35                 va_start(ap, child_name_fmt);
36                 t = talloc_vasprintf(mem_ctx, child_name_fmt, ap);
37                 debug_extra = talloc_asprintf(mem_ctx, "%s:", t);
38                 talloc_free(t);
39                 va_end(ap);
40         }
41 }
42
43 bool ctdb_is_child_process(void)
44 {
45         return is_child;
46 }
47
48 /*
49  * This function forks a child process and drops the realtime 
50  * scheduler for the child process.
51  */
52 pid_t ctdb_fork_no_free_ringbuffer(struct ctdb_context *ctdb)
53 {
54         pid_t pid;
55         char *process;
56
57         pid = fork();
58         if (pid == -1) {
59                 return -1;
60         }
61         if (pid == 0) {
62                 ctdb_set_child_info(ctdb, NULL);
63
64                 /* Close the Unix Domain socket and the TCP socket.
65                  * This ensures that none of the child processes will
66                  * look like the main daemon when it is not running.
67                  * tevent needs to be stopped before closing sockets.
68                  */
69                 if (ctdb->ev != NULL) {
70                         talloc_free(ctdb->ev);
71                         ctdb->ev = NULL;
72                 }
73                 if (ctdb->daemon.sd != -1) {
74                         close(ctdb->daemon.sd);
75                         ctdb->daemon.sd = -1;
76                 }
77                 if (ctdb->methods != NULL) {
78                         ctdb->methods->shutdown(ctdb);
79                 }
80
81                 /* The child does not need to be realtime */
82                 if (ctdb->do_setsched) {
83                         ctdb_restore_scheduler(ctdb);
84                 }
85                 ctdb->can_send_controls = false;
86
87                 return 0;
88         }
89
90         if (getpid() != ctdb->ctdbd_pid) {
91                 return pid;
92         }
93
94         process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
95         trbt_insert32(ctdb->child_processes, pid, process);
96
97         return pid;
98 }
99
100 pid_t ctdb_fork(struct ctdb_context *ctdb)
101 {
102         pid_t pid;
103
104         pid = ctdb_fork_no_free_ringbuffer(ctdb);
105         if (pid == 0) {
106                 ctdb_log_ringbuffer_free();
107         }
108
109         return pid;
110 }
111
112
113 static void ctdb_sigchld_handler(struct tevent_context *ev,
114         struct tevent_signal *te, int signum, int count,
115         void *dont_care, 
116         void *private_data)
117 {
118         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
119         int status;
120         pid_t pid = -1;
121
122         while (pid != 0) {
123                 pid = waitpid(-1, &status, WNOHANG);
124                 if (pid == -1) {
125                         DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
126                         return;
127                 }
128                 if (pid > 0) {
129                         char *process;
130
131                         if (getpid() != ctdb->ctdbd_pid) {
132                                 continue;
133                         }
134
135                         process = trbt_lookup32(ctdb->child_processes, pid);
136                         if (process == NULL) {
137                                 DEBUG(DEBUG_ERR,("Got SIGCHLD from pid:%d we didn not spawn with ctdb_fork\n", pid));
138                         }
139
140                         DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d %s\n", (int)pid, process));
141                         talloc_free(process);
142                 }
143         }
144 }
145
146
147 struct tevent_signal *
148 ctdb_init_sigchld(struct ctdb_context *ctdb)
149 {
150         struct tevent_signal *se;
151
152         ctdb->child_processes = trbt_create(ctdb, 0);
153
154         se = tevent_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, ctdb_sigchld_handler, ctdb);
155         return se;
156 }
157
158 int
159 ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum)
160 {
161         char *process;
162
163         if (signum == 0) {
164                 return kill(pid, signum);
165         }
166
167         if (getpid() != ctdb->ctdbd_pid) {
168                 return kill(pid, signum);
169         }
170
171         process = trbt_lookup32(ctdb->child_processes, pid);
172         if (process == NULL) {
173                 DEBUG(DEBUG_ERR,("ctdb_kill: trying to kill(%d, %d) a process that does not exist\n", pid, signum));
174                 return 0;
175         }
176
177         return kill(pid, signum);
178 }