File: ipc_bridge.c

package info (click to toggle)
pacemaker 1.0.9.1%2Bhg15626-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,668 kB
  • ctags: 5,645
  • sloc: xml: 87,444; ansic: 57,853; python: 11,479; sh: 5,255; makefile: 663; perl: 387; sed: 262
file content (225 lines) | stat: -rw-r--r-- 5,554 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
 * 
 * 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 2.1 of the License, or (at your option) any later version.
 * 
 * This software 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>

#include <crm_internal.h>
#include <crm/crm.h>
#include <crm/common/ipc.h>
#include <glib/ghash.h>

#include <crm/ais.h>

int num_clients = 0;
GHashTable *ipc_client_list = NULL;
gboolean ais_shutdown_flag = FALSE;

typedef struct ais_client_s 
{
		char  *id;
		char  *name;
		char  *callback_id;

		const char  *channel_name;

		IPC_Channel *channel;
		GCHSource   *source;
		unsigned long num_calls;
} ais_client_t;


static void
ais_ipc_connection_destroy(gpointer user_data)
{
	ais_client_t *ais_client = user_data;
	
	if(ais_client == NULL) {
		return;
	}

	if(ais_client->source != NULL) {
		crm_debug_4("Deleting %s (%p) from mainloop",
			    ais_client->name, ais_client->source);
		G_main_del_IPC_Channel(ais_client->source); 
		ais_client->source = NULL;
	}
	
	crm_debug_3("Destroying %s (%p)", ais_client->name, user_data);
	num_clients--;
	crm_debug("Num unfree'd clients: %d", num_clients);
	crm_free(ais_client->name);
	crm_free(ais_client->callback_id);
	crm_free(ais_client->id);
	crm_free(ais_client);
	crm_debug_4("Freed the cib client");

	return;
}

static gboolean
ais_process_disconnect(IPC_Channel *channel, ais_client_t *ais_client)
{
	if (channel == NULL) {
		CRM_DEV_ASSERT(ais_client == NULL);
		
	} else if (ais_client == NULL) {
		crm_err("No client");
		
	} else {
		CRM_DEV_ASSERT(channel->ch_status != IPC_CONNECT);
		crm_debug_2("Cleaning up after client disconnect: %s/%s/%s",
			    crm_str(ais_client->name),
			    ais_client->channel_name,
			    ais_client->id);
		
		if(ais_client->id != NULL) {
			if(!g_hash_table_remove(ipc_client_list, ais_client->id)) {
				crm_err("Client %s not found in the hashtable",
					ais_client->name);
			}
		}		
	}

	if(ais_shutdown_flag && g_hash_table_size(ipc_client_list) == 0) {
		crm_info("All clients disconnected...");
		exit(0);
	}
	
	return FALSE;
}

static gboolean
ais_ipc_callback(IPC_Channel *channel, gpointer user_data)
{
	int lpc = 0;
	xmlNode *op_request = NULL;
	gboolean keep_channel = TRUE;
	ais_client_t *ais_client = user_data;
	
	if(ais_client == NULL) {
		crm_err("Receieved call from unknown source. Discarding.");
		return FALSE;
	}

	if(ais_client->name == NULL) {
		ais_client->name = crm_itoa(channel->farside_pid);
	}
	if(ais_client->id == NULL) {
		ais_client->id = crm_strdup(ais_client->name);
		g_hash_table_insert(ipc_client_list, ais_client->id, ais_client);
	}
	
	crm_err("Callback for %s on %s channel",
		    ais_client->id, ais_client->channel_name);

	while(IPC_ISRCONN(channel)) {
		if(channel->ops->is_message_pending(channel) == 0) {
			break;
		}

		op_request = msgfromIPC_noauth(channel);
		if (op_request == NULL) {
			perror("Receive failure:");
			break;
		}

		lpc++;
		crm_assert_failed = FALSE;

		ha_msg_add(op_request, "client-id", ais_client->id);
		ha_msg_add(op_request, "client-name", ais_client->name);
		crm_log_xml(LOG_ERR, "Client[inbound]", op_request);
		
/* 		cib_common_callback_worker( */
/* 			op_request, cib_client, force_synchronous, privileged); */

		crm_msg_del(op_request);

		if(channel->ch_status == IPC_CONNECT) {
			break;
		}
	}

	crm_debug_2("Processed %d messages", lpc);

	if(channel->ch_status != IPC_CONNECT) {
		crm_debug_2("Client disconnected");
		keep_channel = ais_process_disconnect(channel, ais_client);	
	}

	return keep_channel;
}

gboolean
ais_client_connect(IPC_Channel *channel, gpointer user_data)
{
	const char *channel_name = user_data;
	ais_client_t *new_client = NULL;
	crm_debug_3("Connecting channel");

	if (channel == NULL) {
		crm_err("Channel was NULL");

	} else if (channel->ch_status != IPC_CONNECT) {
		crm_err("Channel was disconnected");
		
	} else if(channel_name == NULL) {
		crm_err("user_data must contain channel name");
		
	} else if(ais_shutdown_flag) {
		crm_info("Ignoring new client [%d] during shutdown",
			channel->farside_pid);
		
	} else {
		crm_malloc0(new_client, sizeof(ais_client_t));
		num_clients++;
		new_client->channel = channel;
		new_client->channel_name = channel_name;

		crm_debug_3("Created channel %p for channel %s",
			  new_client, new_client->channel_name);

		channel->ops->set_recv_qlen(channel, 500);
		channel->ops->set_send_qlen(channel, 500);

		new_client->source = G_main_add_IPC_Channel(
			G_PRIORITY_DEFAULT, channel, FALSE, ais_ipc_callback,
			new_client, ais_ipc_connection_destroy);

		crm_debug_3("Channel %s connected for client %s",
			    new_client->channel_name, new_client->id);
	}

	if(new_client == NULL) {
		return FALSE;
	}
	return TRUE;
}