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