ctdb-build: Remove duplicate socket_wrapper library
[Samba.git] / ctdb / common / ctdb_fork.c
blobaa9bcf01bc4926f038133760acdb522dbd7eb0ae
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 "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"
26 static bool is_child = false;
28 void ctdb_set_child_info(TALLOC_CTX *mem_ctx, const char *child_name_fmt, ...)
30 is_child = true;
31 if (child_name_fmt != NULL) {
32 va_list ap;
33 char *t;
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);
43 bool ctdb_is_child_process(void)
45 return is_child;
48 void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid)
50 char *process;
52 /* Only CTDB main daemon should track child processes */
53 if (getpid() != ctdb->ctdbd_pid) {
54 return;
57 process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
58 trbt_insert32(ctdb->child_processes, pid, process);
62 * This function forks a child process and drops the realtime
63 * scheduler for the child process.
65 pid_t ctdb_fork_no_free_ringbuffer(struct ctdb_context *ctdb)
67 pid_t pid;
69 pid = fork();
70 if (pid == -1) {
71 return -1;
73 if (pid == 0) {
74 ctdb_set_child_info(ctdb, NULL);
76 /* Close the Unix Domain socket and the TCP socket.
77 * This ensures that none of the child processes will
78 * look like the main daemon when it is not running.
79 * tevent needs to be stopped before closing sockets.
81 if (ctdb->ev != NULL) {
82 talloc_free(ctdb->ev);
83 ctdb->ev = NULL;
85 if (ctdb->daemon.sd != -1) {
86 close(ctdb->daemon.sd);
87 ctdb->daemon.sd = -1;
89 if (ctdb->methods != NULL) {
90 ctdb->methods->shutdown(ctdb);
93 /* The child does not need to be realtime */
94 if (ctdb->do_setsched) {
95 reset_scheduler();
97 ctdb->can_send_controls = false;
99 return 0;
102 ctdb_track_child(ctdb, pid);
103 return pid;
106 pid_t ctdb_fork(struct ctdb_context *ctdb)
108 pid_t pid;
110 pid = ctdb_fork_no_free_ringbuffer(ctdb);
111 if (pid == 0) {
112 ctdb_log_ringbuffer_free();
115 return pid;
119 static void ctdb_sigchld_handler(struct tevent_context *ev,
120 struct tevent_signal *te, int signum, int count,
121 void *dont_care,
122 void *private_data)
124 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
125 int status;
126 pid_t pid = -1;
128 while (pid != 0) {
129 pid = waitpid(-1, &status, WNOHANG);
130 if (pid == -1) {
131 DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
132 return;
134 if (pid > 0) {
135 char *process;
137 if (getpid() != ctdb->ctdbd_pid) {
138 continue;
141 process = trbt_lookup32(ctdb->child_processes, pid);
142 if (process == NULL) {
143 DEBUG(DEBUG_ERR,("Got SIGCHLD from pid:%d we didn not spawn with ctdb_fork\n", pid));
146 DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d %s\n", (int)pid, process));
147 talloc_free(process);
153 struct tevent_signal *
154 ctdb_init_sigchld(struct ctdb_context *ctdb)
156 struct tevent_signal *se;
158 ctdb->child_processes = trbt_create(ctdb, 0);
160 se = tevent_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, ctdb_sigchld_handler, ctdb);
161 return se;
165 ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum)
167 char *process;
169 if (signum == 0) {
170 return kill(pid, signum);
173 if (getpid() != ctdb->ctdbd_pid) {
174 return kill(pid, signum);
177 process = trbt_lookup32(ctdb->child_processes, pid);
178 if (process == NULL) {
179 DEBUG(DEBUG_ERR,("ctdb_kill: trying to kill(%d, %d) a process that does not exist\n", pid, signum));
180 return 0;
183 return kill(pid, signum);