ctdb-daemon: Add ctdb_vfork_exec()
[kamenim/samba-autobuild/.git] / ctdb / server / 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 "replace.h"
21 #include "system/wait.h"
22 #include "system/network.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26
27 #include "lib/util/debug.h"
28 #include "lib/util/time.h"
29
30 #include "ctdb_private.h"
31 #include "ctdb_client.h"
32
33 #include "common/rb_tree.h"
34 #include "common/system.h"
35 #include "common/common.h"
36 #include "common/logging.h"
37
38 void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid)
39 {
40         char *process;
41
42         /* Only CTDB main daemon should track child processes */
43         if (getpid() != ctdb->ctdbd_pid) {
44                 return;
45         }
46
47         process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
48         trbt_insert32(ctdb->child_processes, pid, process);
49 }
50
51 /*
52  * This function forks a child process and drops the realtime 
53  * scheduler for the child process.
54  */
55 pid_t ctdb_fork(struct ctdb_context *ctdb)
56 {
57         pid_t pid;
58         struct timeval before;
59         double delta_t;
60
61         before = timeval_current();
62
63         pid = fork();
64         if (pid == -1) {
65                 DEBUG(DEBUG_ERR,
66                       (__location__ " fork() failed (%s)\n", strerror(errno)));
67                 return -1;
68         }
69         if (pid == 0) {
70                 /* Close the Unix Domain socket and the TCP socket.
71                  * This ensures that none of the child processes will
72                  * look like the main daemon when it is not running.
73                  * tevent needs to be stopped before closing sockets.
74                  */
75                 if (ctdb->ev != NULL) {
76                         talloc_free(ctdb->ev);
77                         ctdb->ev = NULL;
78                 }
79                 if (ctdb->daemon.sd != -1) {
80                         close(ctdb->daemon.sd);
81                         ctdb->daemon.sd = -1;
82                 }
83                 if (ctdb->methods != NULL && ctdb->methods->shutdown != NULL) {
84                         ctdb->methods->shutdown(ctdb);
85                 }
86
87                 /* The child does not need to be realtime */
88                 if (ctdb->do_setsched) {
89                         reset_scheduler();
90                 }
91                 ctdb->can_send_controls = false;
92
93                 return 0;
94         }
95
96         delta_t = timeval_elapsed(&before);
97         if (delta_t > 3.0) {
98                 DEBUG(DEBUG_WARNING, ("fork() took %lf seconds\n", delta_t));
99         }
100
101         ctdb_track_child(ctdb, pid);
102         return pid;
103 }
104
105 /*
106  * vfork + exec
107  */
108 pid_t ctdb_vfork_exec(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
109                       const char *helper, int helper_argc,
110                       const char **helper_argv)
111 {
112         pid_t pid;
113         struct timeval before;
114         double delta_t;
115         char **argv;
116         int i;
117
118         argv = talloc_array(mem_ctx, char *, helper_argc + 1);
119         if (argv == NULL) {
120                 DEBUG(DEBUG_ERR, ("Memory allocation error\n"));
121                 return -1;
122         }
123
124         argv[0] = discard_const(helper);
125         for (i=0; i<helper_argc; i++) {
126                 argv[i+1] = discard_const(helper_argv[i]);
127         }
128
129         before = timeval_current();
130
131         pid = vfork();
132         if (pid == -1) {
133                 DEBUG(DEBUG_ERR, ("vfork() failed (%s)\n", strerror(errno)));
134                 return -1;
135         }
136
137         if (pid == 0) {
138                 execv(helper, argv);
139                 _exit(1);
140         }
141
142         delta_t = timeval_elapsed(&before);
143         if (delta_t > 3.0) {
144                 DEBUG(DEBUG_WARNING, ("vfork() took %lf seconds\n", delta_t));
145         }
146
147         ctdb_track_child(ctdb, pid);
148         return pid;
149 }
150
151 static void ctdb_sigchld_handler(struct tevent_context *ev,
152         struct tevent_signal *te, int signum, int count,
153         void *dont_care, 
154         void *private_data)
155 {
156         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
157         int status;
158         pid_t pid = -1;
159
160         while (pid != 0) {
161                 pid = waitpid(-1, &status, WNOHANG);
162                 if (pid == -1) {
163                         DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
164                         return;
165                 }
166                 if (pid > 0) {
167                         char *process;
168
169                         if (getpid() != ctdb->ctdbd_pid) {
170                                 continue;
171                         }
172
173                         process = trbt_lookup32(ctdb->child_processes, pid);
174                         if (process == NULL) {
175                                 DEBUG(DEBUG_ERR,("Got SIGCHLD from pid:%d we didn not spawn with ctdb_fork\n", pid));
176                         }
177
178                         DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d %s\n", (int)pid, process));
179                         talloc_free(process);
180                 }
181         }
182 }
183
184
185 struct tevent_signal *
186 ctdb_init_sigchld(struct ctdb_context *ctdb)
187 {
188         struct tevent_signal *se;
189
190         ctdb->child_processes = trbt_create(ctdb, 0);
191
192         se = tevent_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, ctdb_sigchld_handler, ctdb);
193         return se;
194 }
195
196 int
197 ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum)
198 {
199         char *process;
200
201         if (signum == 0) {
202                 return kill(pid, signum);
203         }
204
205         if (getpid() != ctdb->ctdbd_pid) {
206                 return kill(pid, signum);
207         }
208
209         process = trbt_lookup32(ctdb->child_processes, pid);
210         if (process == NULL) {
211                 DEBUG(DEBUG_ERR,("ctdb_kill: trying to kill(%d, %d) a process that does not exist\n", pid, signum));
212                 return 0;
213         }
214
215         return kill(pid, signum);
216 }