ctdb: Change the ctdb_vfork_exec prototype to const char*const*
[samba4-gss.git] / ctdb / server / ctdb_fork.c
blob8f3e0896b32536081318317fb3f7a020e242202c
1 /*
2 functions to track and manage processes
4 Copyright (C) Ronnie Sahlberg 2012
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/wait.h"
22 #include "system/network.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/util/debug.h"
28 #include "lib/util/time.h"
30 #include "ctdb_private.h"
31 #include "ctdb_client.h"
33 #include "common/rb_tree.h"
34 #include "common/system.h"
35 #include "common/common.h"
36 #include "common/logging.h"
38 void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid)
40 char *process;
42 /* Only CTDB main daemon should track child processes */
43 if (getpid() != ctdb->ctdbd_pid) {
44 return;
47 process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
48 trbt_insert32(ctdb->child_processes, pid, process);
52 * This function forks a child process and drops the realtime
53 * scheduler for the child process.
55 pid_t ctdb_fork(struct ctdb_context *ctdb)
57 pid_t pid;
58 struct timeval before;
59 double delta_t;
61 before = timeval_current();
63 pid = fork();
64 if (pid == -1) {
65 DEBUG(DEBUG_ERR,
66 (__location__ " fork() failed (%s)\n", strerror(errno)));
67 return -1;
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.
75 if (ctdb->ev != NULL) {
76 talloc_free(ctdb->ev);
77 ctdb->ev = NULL;
79 if (ctdb->daemon.sd != -1) {
80 close(ctdb->daemon.sd);
81 ctdb->daemon.sd = -1;
83 if (ctdb->methods != NULL && ctdb->methods->shutdown != NULL) {
84 ctdb->methods->shutdown(ctdb);
87 /* The child does not need to be realtime */
88 if (ctdb->do_setsched) {
89 reset_scheduler();
91 ctdb->can_send_controls = false;
93 return 0;
96 delta_t = timeval_elapsed(&before);
97 if (delta_t > 3.0) {
98 DEBUG(DEBUG_WARNING, ("fork() took %lf seconds\n", delta_t));
101 ctdb_track_child(ctdb, pid);
102 return pid;
106 * vfork + exec
108 pid_t ctdb_vfork_exec(TALLOC_CTX *mem_ctx,
109 struct ctdb_context *ctdb,
110 const char *helper,
111 int helper_argc,
112 const char *const *helper_argv)
114 pid_t pid;
115 struct timeval before;
116 double delta_t;
117 char **argv;
118 int i;
120 argv = talloc_array(mem_ctx, char *, helper_argc + 1);
121 if (argv == NULL) {
122 DEBUG(DEBUG_ERR, ("Memory allocation error\n"));
123 return -1;
126 argv[0] = discard_const(helper);
127 for (i=0; i<helper_argc; i++) {
128 argv[i+1] = discard_const(helper_argv[i]);
131 before = timeval_current();
133 pid = vfork();
134 if (pid == -1) {
135 DEBUG(DEBUG_ERR, ("vfork() failed (%s)\n", strerror(errno)));
136 return -1;
139 if (pid == 0) {
140 execv(helper, argv);
141 _exit(1);
144 delta_t = timeval_elapsed(&before);
145 if (delta_t > 3.0) {
146 DEBUG(DEBUG_WARNING, ("vfork() took %lf seconds\n", delta_t));
149 ctdb_track_child(ctdb, pid);
150 return pid;
153 static void ctdb_sigchld_handler(struct tevent_context *ev,
154 struct tevent_signal *te, int signum, int count,
155 void *dont_care,
156 void *private_data)
158 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
159 int status;
160 pid_t pid = -1;
162 while (pid != 0) {
163 pid = waitpid(-1, &status, WNOHANG);
164 if (pid == -1) {
165 DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
166 return;
168 if (pid > 0) {
169 char *process;
171 if (getpid() != ctdb->ctdbd_pid) {
172 continue;
175 process = trbt_lookup32(ctdb->child_processes, pid);
176 if (process == NULL) {
177 DEBUG(DEBUG_ERR,("Got SIGCHLD from pid:%d we didn not spawn with ctdb_fork\n", pid));
180 DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d %s\n", (int)pid, process));
181 talloc_free(process);
187 struct tevent_signal *
188 ctdb_init_sigchld(struct ctdb_context *ctdb)
190 struct tevent_signal *se;
192 ctdb->child_processes = trbt_create(ctdb, 0);
194 se = tevent_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, ctdb_sigchld_handler, ctdb);
195 return se;
199 ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum)
201 char *process;
203 if (signum == 0) {
204 return kill(pid, signum);
207 if (getpid() != ctdb->ctdbd_pid) {
208 return kill(pid, signum);
211 process = trbt_lookup32(ctdb->child_processes, pid);
212 if (process == NULL) {
213 DEBUG(DEBUG_ERR,("ctdb_kill: trying to kill(%d, %d) a process that does not exist\n", pid, signum));
214 return 0;
217 return kill(pid, signum);