ctdb-daemon: Move switch_from_server_to_client() to ctdb_daemon.c
[Samba.git] / ctdb / common / ctdb_fork.c
blob46b0a13a7d99f144d3b7422f945982ad582169ba
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"
29 #include "ctdb_logging.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"
37 void ctdb_set_child_info(TALLOC_CTX *mem_ctx, const char *child_name_fmt, ...)
39 if (child_name_fmt != NULL) {
40 va_list ap;
41 char *t;
43 va_start(ap, child_name_fmt);
44 t = talloc_vasprintf(mem_ctx, child_name_fmt, ap);
45 debug_extra = talloc_asprintf(mem_ctx, "%s:", t);
46 talloc_free(t);
47 va_end(ap);
51 void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid)
53 char *process;
55 /* Only CTDB main daemon should track child processes */
56 if (getpid() != ctdb->ctdbd_pid) {
57 return;
60 process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
61 trbt_insert32(ctdb->child_processes, pid, process);
65 * This function forks a child process and drops the realtime
66 * scheduler for the child process.
68 pid_t ctdb_fork(struct ctdb_context *ctdb)
70 pid_t pid;
72 pid = fork();
73 if (pid == -1) {
74 return -1;
76 if (pid == 0) {
77 ctdb_set_child_info(ctdb, NULL);
79 /* Close the Unix Domain socket and the TCP socket.
80 * This ensures that none of the child processes will
81 * look like the main daemon when it is not running.
82 * tevent needs to be stopped before closing sockets.
84 if (ctdb->ev != NULL) {
85 talloc_free(ctdb->ev);
86 ctdb->ev = NULL;
88 if (ctdb->daemon.sd != -1) {
89 close(ctdb->daemon.sd);
90 ctdb->daemon.sd = -1;
92 if (ctdb->methods != NULL) {
93 ctdb->methods->shutdown(ctdb);
96 /* The child does not need to be realtime */
97 if (ctdb->do_setsched) {
98 reset_scheduler();
100 ctdb->can_send_controls = false;
102 return 0;
105 ctdb_track_child(ctdb, pid);
106 return pid;
109 static void ctdb_sigchld_handler(struct tevent_context *ev,
110 struct tevent_signal *te, int signum, int count,
111 void *dont_care,
112 void *private_data)
114 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
115 int status;
116 pid_t pid = -1;
118 while (pid != 0) {
119 pid = waitpid(-1, &status, WNOHANG);
120 if (pid == -1) {
121 DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
122 return;
124 if (pid > 0) {
125 char *process;
127 if (getpid() != ctdb->ctdbd_pid) {
128 continue;
131 process = trbt_lookup32(ctdb->child_processes, pid);
132 if (process == NULL) {
133 DEBUG(DEBUG_ERR,("Got SIGCHLD from pid:%d we didn not spawn with ctdb_fork\n", pid));
136 DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d %s\n", (int)pid, process));
137 talloc_free(process);
143 struct tevent_signal *
144 ctdb_init_sigchld(struct ctdb_context *ctdb)
146 struct tevent_signal *se;
148 ctdb->child_processes = trbt_create(ctdb, 0);
150 se = tevent_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, ctdb_sigchld_handler, ctdb);
151 return se;
155 ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum)
157 char *process;
159 if (signum == 0) {
160 return kill(pid, signum);
163 if (getpid() != ctdb->ctdbd_pid) {
164 return kill(pid, signum);
167 process = trbt_lookup32(ctdb->child_processes, pid);
168 if (process == NULL) {
169 DEBUG(DEBUG_ERR,("ctdb_kill: trying to kill(%d, %d) a process that does not exist\n", pid, signum));
170 return 0;
173 return kill(pid, signum);