Debian Codegraph @

membership/ccm/ccmmain.c

raw
1 /*
2 * ccm.c: Consensus Cluster Service Program
3 *
4 * Copyright (c) International Business Machines Corp., 2002
5 * Author: Ram Pai ([email protected])
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22 #include <lha_internal.h>
23 #include "ccm.h"
24 #include "ccmmisc.h"
25 #include <clplumbing/cl_signal.h>
26
27 #define SECOND 1000
28 #define OPTARGS "dv"
29
30 GMainLoop* mainloop = NULL;
31
32 /*
33 * hearbeat event source.
34 *
35 */
36 static gboolean hb_input_dispatch(IPC_Channel *, gpointer);
37 static void hb_input_destroy(gpointer);
38 static gboolean hb_timeout_dispatch(gpointer);
39 static gboolean
40 hb_input_dispatch(IPC_Channel * channel, gpointer user_data)
41 {
42 if (channel && (channel->ch_status == IPC_DISCONNECT)) {
43 ccm_log(LOG_ERR, "Lost connection to heartbeat service. Need to bail out.");
44 return FALSE;
45 }
46
47 return ccm_take_control(user_data);
48 }
49
50 static void
51 hb_input_destroy(gpointer user_data)
52 {
53 /* close connections to all the clients */
54 client_delete_all();
55 g_main_quit(mainloop);
56 return;
57 }
58
59 static gboolean
60 hb_timeout_dispatch(gpointer user_data)
61 {
62 if(debug_level > 0) {
63 ccm_check_memoryleak();
64 }
65 return hb_input_dispatch(0, user_data);
66 }
67
68
69 /*
70 * client messaging events sources...
71 *
72 */
73 static gboolean clntCh_input_dispatch(IPC_Channel *
74 , gpointer);
75 static void clntCh_input_destroy(gpointer );
76
77 static gboolean
78 clntCh_input_dispatch(IPC_Channel *client,
79 gpointer user_data)
80 {
81 if(client->ch_status == IPC_DISCONNECT){
82 ccm_log(LOG_INFO, "client (pid=%d) removed from ccm",
83 client->farside_pid);
84
85 client_delete(client);
86 return FALSE;
87 }
88 return TRUE; /* TOBEDONE */
89 }
90
91
92 static void
93 clntCh_input_destroy(gpointer user_data)
94 {
95 return;
96 }
97
98
99
100 /*
101 * client connection events source..
102 *
103 */
104 static gboolean waitCh_input_dispatch(IPC_Channel *, gpointer);
105 static void waitCh_input_destroy(gpointer);
106
107 static gboolean
108 waitCh_input_dispatch(IPC_Channel *newclient, gpointer user_data)
109 {
110 client_add(newclient);
111
112 /* Some ccm clients may not consume our messages fast enough,
113 * but will have serious trouble if we kick them out for that reason.
114 * Worse, still, if send_message() "live locks" on one client, we won't
115 * be able to drain requests from other clients, possibly causing them
116 * to close their connection to us, or even causing the heartbeat MCP
117 * to kick _us_ out. Kick out the ccm! We really want to avoid that.
118 * Chose a "generous" queue length.
119 * And consider kicking out clients that still manage to have their
120 * queue fill up. (TOBEDONE)
121 */
122 newclient->ops->set_send_qlen(newclient, 1024);
123
124 G_main_add_IPC_Channel(G_PRIORITY_LOW, newclient, FALSE,
125 clntCh_input_dispatch, newclient,
126 clntCh_input_destroy);
127 return TRUE;
128 }
129
130 static void
131 waitCh_input_destroy(gpointer user_data)
132 {
133 IPC_WaitConnection *wait_ch =
134 (IPC_WaitConnection *)user_data;
135
136 wait_ch->ops->destroy(wait_ch);
137 return;
138 }
139
140 static IPC_WaitConnection *
141 wait_channel_init(void)
142 {
143 IPC_WaitConnection *wait_ch;
144 mode_t mask;
145 char path[] = IPC_PATH_ATTR;
146 char ccmfifo[] = CCMFIFO;
147 char domainsocket[] = IPC_DOMAIN_SOCKET;
148
149 GHashTable * attrs = g_hash_table_new(g_str_hash,g_str_equal);
150 g_hash_table_insert(attrs, path, ccmfifo);
151
152 mask = umask(0);
153 wait_ch = ipc_wait_conn_constructor(domainsocket, attrs);
154 if (wait_ch == NULL){
155 cl_perror("Can't create wait channel");
156 exit(1);
157 }
158 mask = umask(mask);
159
160 g_hash_table_destroy(attrs);
161
162 return wait_ch;
163 }
164
165 static void
166 usage(const char *cmd)
167 {
168 fprintf(stderr, "\nUsage: %s [-v]\n", cmd);
169 }
170
171
172 /* */
173 /* debug facilitator. */
174 /* */
175 static void
176 usr_signal_handler(int signum)
177 {
178 switch(signum) {
179 case SIGUSR1:
180 debug_level++;
181 break;
182
183 case SIGUSR2:
184 debug_level--;
185 if (debug_level < 0) {
186 debug_level = 0;
187 }
188 break;
189 }
190
191 if(debug_level > 0){
192 cl_log_enable_stderr(TRUE);
193 } else {
194 cl_log_enable_stderr(FALSE);
195 }
196 ccm_log(LOG_INFO, "set debug_level to %d", debug_level);
197
198 }
199
200 static gboolean
201 ccm_shutdone(int sig, gpointer userdata)
202 {
203 ccm_t* ccm = (ccm_t*)userdata;
204 ccm_info_t * info = (ccm_info_t*)ccm->info;
205
206 ccm_log(LOG_INFO, "received SIGTERM, going to shut down");
207 if (info == NULL){
208 ccm_log(LOG_ERR, "ccm_shutdone: invalid arguments");
209 return FALSE;
210 }
211
212 ccm_reset(info);
213 g_main_quit(mainloop);
214
215 return TRUE;
216 }
217
218 /* */
219 /* The main function! */
220 /* */
221 int
222 main(int argc, char **argv)
223 {
224 IPC_WaitConnection *wait_ch;
225
226 char *cmdname;
227 char *tmp_cmdname;
228 int flag;
229 ccm_t* ccm;
230
231 tmp_cmdname = g_strdup(argv[0]);
232 if ((cmdname = strrchr(tmp_cmdname, '/')) != NULL) {
233 ++cmdname;
234 } else {
235 cmdname = tmp_cmdname;
236 }
237 cl_log_set_entity(cmdname);
238 cl_log_set_facility(HA_LOG_FACILITY);
239 while ((flag = getopt(argc, argv, OPTARGS)) != EOF) {
240 switch(flag) {
241 case 'v': /* Debug mode, more logs*/
242 ++debug_level;
243 break;
244 default:
245 usage(cmdname);
246 return 1;
247 }
248 }
249 if(debug_level > 0)
250 cl_log_enable_stderr(TRUE);
251
252
253 CL_SIGNAL(SIGUSR1, usr_signal_handler);
254 CL_SIGNAL(SIGUSR2, usr_signal_handler);
255 CL_IGNORE_SIG(SIGPIPE);
256
257 cl_inherit_logging_environment(0);
258
259 /* initialize the client tracking system */
260 client_init();
261
262 /*
263 * heartbeat is the main source of events.
264 * This source must be listened
265 * at high priority
266 */
267 ccm = ccm_initialize();
268 if(ccm == NULL){
269 ccm_log(LOG_ERR, "Initialization failed. Exit");
270 exit(1);
271 }
272
273 G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM,
274 ccm_shutdone, ccm, NULL);
275 /* we want hb_input_dispatch to be called when some input is
276 * pending on the heartbeat fd, and every 1 second
277 */
278 G_main_add_IPC_Channel(G_PRIORITY_HIGH, ccm_get_ipcchan(ccm),
279 FALSE, hb_input_dispatch, ccm, hb_input_destroy);
280 Gmain_timeout_add_full(G_PRIORITY_HIGH, SECOND, hb_timeout_dispatch,
281 ccm, hb_input_destroy);
282
283 /* the clients wait channel is the other source of events.
284 * This source delivers the clients connection events.
285 * listen to this source at a relatively lower priority.
286 */
287 wait_ch = wait_channel_init();
288 G_main_add_IPC_WaitConnection(G_PRIORITY_LOW, wait_ch, NULL,
289 FALSE, waitCh_input_dispatch, wait_ch,
290 waitCh_input_destroy);
291
292 mainloop = g_main_loop_new(NULL, FALSE);
293 g_main_run(mainloop);
294 g_main_destroy(mainloop);
295
296 g_free(tmp_cmdname);
297 /*this program should never terminate,unless killed*/
298 return(1);
299 }