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