1    	/*
2    	 * Copyright (c) 2006-2019 Red Hat, Inc.
3    	 *
4    	 * All rights reserved.
5    	 *
6    	 * Author: Christine Caulfield (ccaulfie@redhat.com)
7    	 * Author: Jan Friesse (jfriesse@redhat.com)
8    	 *
9    	 * This software licensed under BSD license, the text of which follows:
10   	 *
11   	 * Redistribution and use in source and binary forms, with or without
12   	 * modification, are permitted provided that the following conditions are met:
13   	 *
14   	 * - Redistributions of source code must retain the above copyright notice,
15   	 *   this list of conditions and the following disclaimer.
16   	 * - Redistributions in binary form must reproduce the above copyright notice,
17   	 *   this list of conditions and the following disclaimer in the documentation
18   	 *   and/or other materials provided with the distribution.
19   	 * - Neither the name of the MontaVista Software, Inc. nor the names of its
20   	 *   contributors may be used to endorse or promote products derived from this
21   	 *   software without specific prior written permission.
22   	 *
23   	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
24   	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   	 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26   	 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27   	 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28   	 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29   	 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30   	 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31   	 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32   	 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33   	 * THE POSSIBILITY OF SUCH DAMAGE.
34   	 */
35   	
36   	#include <config.h>
37   	
38   	#ifdef HAVE_ALLOCA_H
39   	#include <alloca.h>
40   	#endif
41   	#include <sys/types.h>
42   	#include <sys/socket.h>
43   	#include <sys/un.h>
44   	#include <sys/ioctl.h>
45   	#include <netinet/in.h>
46   	#include <sys/uio.h>
47   	#include <unistd.h>
48   	#include <fcntl.h>
49   	#include <stdlib.h>
50   	#include <stdio.h>
51   	#include <errno.h>
52   	#include <time.h>
53   	#include <assert.h>
54   	#include <arpa/inet.h>
55   	#include <sys/mman.h>
56   	
57   	#include <qb/qblist.h>
58   	#include <qb/qbmap.h>
59   	
60   	#include <corosync/corotypes.h>
61   	#include <qb/qbipc_common.h>
62   	#include <corosync/corodefs.h>
63   	#include <corosync/logsys.h>
64   	#include <corosync/coroapi.h>
65   	
66   	#include <corosync/cpg.h>
67   	#include <corosync/ipc_cpg.h>
68   	
69   	#ifndef MAP_ANONYMOUS
70   	#define MAP_ANONYMOUS MAP_ANON
71   	#endif
72   	
73   	#include "service.h"
74   	
75   	LOGSYS_DECLARE_SUBSYS ("CPG");
76   	
77   	#define GROUP_HASH_SIZE 32
78   	
79   	enum cpg_message_req_types {
80   		MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
81   		MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
82   		MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
83   		MESSAGE_REQ_EXEC_CPG_MCAST = 3,
84   		MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD = 4,
85   		MESSAGE_REQ_EXEC_CPG_DOWNLIST = 5,
86   		MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST = 6,
87   	};
88   	
89   	struct zcb_mapped {
90   		struct qb_list_head list;
91   		void *addr;
92   		size_t size;
93   	};
94   	/*
95   	 * state`		exec deliver
96   	 * match group name, pid -> if matched deliver for YES:
97   	 * XXX indicates impossible state
98   	 *
99   	 *			join			leave			mcast
100  	 * UNJOINED		XXX			XXX			NO
101  	 * LEAVE_STARTED	XXX			YES(unjoined_enter)	YES
102  	 * JOIN_STARTED		YES(join_started_enter)	XXX			NO
103  	 * JOIN_COMPLETED	XXX			NO			YES
104  	 *
105  	 * join_started_enter
106  	 * 	set JOIN_COMPLETED
107  	 *	add entry to process_info list
108  	 * unjoined_enter
109  	 *	set UNJOINED
110  	 *	delete entry from process_info list
111  	 *
112  	 *
113  	 *			library accept join error codes
114  	 * UNJOINED		YES(CS_OK) 			set JOIN_STARTED
115  	 * LEAVE_STARTED	NO(CS_ERR_BUSY)
116  	 * JOIN_STARTED		NO(CS_ERR_EXIST)
117  	 * JOIN_COMPlETED	NO(CS_ERR_EXIST)
118  	 *
119  	 *			library accept leave error codes
120  	 * UNJOINED		NO(CS_ERR_NOT_EXIST)
121  	 * LEAVE_STARTED	NO(CS_ERR_NOT_EXIST)
122  	 * JOIN_STARTED		NO(CS_ERR_BUSY)
123  	 * JOIN_COMPLETED	YES(CS_OK)			set LEAVE_STARTED
124  	 *
125  	 *			library accept mcast
126  	 * UNJOINED		NO(CS_ERR_NOT_EXIST)
127  	 * LEAVE_STARTED	NO(CS_ERR_NOT_EXIST)
128  	 * JOIN_STARTED		YES(CS_OK)
129  	 * JOIN_COMPLETED	YES(CS_OK)
130  	 */
131  	enum cpd_state {
132  		CPD_STATE_UNJOINED,
133  		CPD_STATE_LEAVE_STARTED,
134  		CPD_STATE_JOIN_STARTED,
135  		CPD_STATE_JOIN_COMPLETED
136  	};
137  	
138  	enum cpg_sync_state {
139  		CPGSYNC_DOWNLIST,
140  		CPGSYNC_JOINLIST
141  	};
142  	
143  	static struct qb_list_head joinlist_messages_head;
144  	
145  	struct cpg_pd {
146  		void *conn;
147  	 	mar_cpg_name_t group_name;
148  		uint32_t pid;
149  		enum cpd_state cpd_state;
150  		unsigned int flags;
151  		int initial_totem_conf_sent;
152  		uint64_t transition_counter; /* These two are used when sending fragmented messages */
153  		uint64_t initial_transition_counter;
154  		struct qb_list_head list;
155  		struct qb_list_head iteration_instance_list_head;
156  		struct qb_list_head zcb_mapped_list_head;
157  	};
158  	
159  	struct cpg_iteration_instance {
160  		hdb_handle_t handle;
161  		struct qb_list_head list;
162  		struct qb_list_head items_list_head; /* List of process_info */
163  		struct qb_list_head *current_pointer;
164  	};
165  	
166  	DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
167  	
168  	QB_LIST_DECLARE (cpg_pd_list_head);
169  	
170  	static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
171  	
172  	static unsigned int my_member_list_entries;
173  	
174  	static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
175  	
176  	static unsigned int my_old_member_list_entries = 0;
177  	
178  	static struct corosync_api_v1 *api = NULL;
179  	
180  	static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
181  	
182  	static mar_cpg_ring_id_t last_sync_ring_id;
183  	
184  	struct process_info {
185  		unsigned int nodeid;
186  		uint32_t pid;
187  		mar_cpg_name_t group;
188  		struct qb_list_head list; /* on the group_info members list */
189  	};
190  	QB_LIST_DECLARE (process_info_list_head);
191  	
192  	struct join_list_entry {
193  		uint32_t pid;
194  		mar_cpg_name_t group_name;
195  	};
196  	
197  	struct join_list_confchg_data {
198  		mar_cpg_name_t cpg_group;
199  		mar_cpg_address_t join_list[CPG_MEMBERS_MAX];
200  		int join_list_entries;
201  	};
202  	
203  	/*
204  	 * Service Interfaces required by service_message_handler struct
205  	 */
206  	static char *cpg_exec_init_fn (struct corosync_api_v1 *);
207  	
208  	static int cpg_lib_init_fn (void *conn);
209  	
210  	static int cpg_lib_exit_fn (void *conn);
211  	
212  	static void message_handler_req_exec_cpg_procjoin (
213  		const void *message,
214  		unsigned int nodeid);
215  	
216  	static void message_handler_req_exec_cpg_procleave (
217  		const void *message,
218  		unsigned int nodeid);
219  	
220  	static void message_handler_req_exec_cpg_joinlist (
221  		const void *message,
222  		unsigned int nodeid);
223  	
224  	static void message_handler_req_exec_cpg_mcast (
225  		const void *message,
226  		unsigned int nodeid);
227  	
228  	static void message_handler_req_exec_cpg_partial_mcast (
229  		const void *message,
230  		unsigned int nodeid);
231  	
232  	static void message_handler_req_exec_cpg_downlist_old (
233  		const void *message,
234  		unsigned int nodeid);
235  	
236  	static void message_handler_req_exec_cpg_downlist (
237  		const void *message,
238  		unsigned int nodeid);
239  	
240  	static void exec_cpg_procjoin_endian_convert (void *msg);
241  	
242  	static void exec_cpg_joinlist_endian_convert (void *msg);
243  	
244  	static void exec_cpg_mcast_endian_convert (void *msg);
245  	
246  	static void exec_cpg_partial_mcast_endian_convert (void *msg);
247  	
248  	static void exec_cpg_downlist_endian_convert_old (void *msg);
249  	
250  	static void exec_cpg_downlist_endian_convert (void *msg);
251  	
252  	static void message_handler_req_lib_cpg_join (void *conn, const void *message);
253  	
254  	static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
255  	
256  	static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
257  	
258  	static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
259  	
260  	static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message);
261  	
262  	static void message_handler_req_lib_cpg_membership (void *conn,
263  							    const void *message);
264  	
265  	static void message_handler_req_lib_cpg_local_get (void *conn,
266  							   const void *message);
267  	
268  	static void message_handler_req_lib_cpg_iteration_initialize (
269  		void *conn,
270  		const void *message);
271  	
272  	static void message_handler_req_lib_cpg_iteration_next (
273  		void *conn,
274  		const void *message);
275  	
276  	static void message_handler_req_lib_cpg_iteration_finalize (
277  		void *conn,
278  		const void *message);
279  	
280  	static void message_handler_req_lib_cpg_zc_alloc (
281  		void *conn,
282  		const void *message);
283  	
284  	static void message_handler_req_lib_cpg_zc_free (
285  		void *conn,
286  		const void *message);
287  	
288  	static void message_handler_req_lib_cpg_zc_execute (
289  		void *conn,
290  		const void *message);
291  	
292  	static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
293  	
294  	static int cpg_exec_send_downlist(void);
295  	
296  	static int cpg_exec_send_joinlist(void);
297  	
298  	static void downlist_inform_clients (void);
299  	
300  	static void joinlist_inform_clients (void);
301  	
302  	static void joinlist_messages_delete (void);
303  	
304  	static void cpg_sync_init (
305  		const unsigned int *trans_list,
306  		size_t trans_list_entries,
307  		const unsigned int *member_list,
308  		size_t member_list_entries,
309  		const struct memb_ring_id *ring_id);
310  	
311  	static int  cpg_sync_process (void);
312  	
313  	static void cpg_sync_activate (void);
314  	
315  	static void cpg_sync_abort (void);
316  	
317  	static void do_proc_join(
318  		const mar_cpg_name_t *name,
319  		uint32_t pid,
320  		unsigned int nodeid,
321  		int reason,
322  		qb_map_t *group_notify_map);
323  	
324  	static void do_proc_leave(
325  		const mar_cpg_name_t *name,
326  		uint32_t pid,
327  		unsigned int nodeid,
328  		int reason);
329  	
330  	static int notify_lib_totem_membership (
331  		void *conn,
332  		int member_list_entries,
333  		const unsigned int *member_list);
334  	
335  	static inline int zcb_all_free (
336  		struct cpg_pd *cpd);
337  	
338  	static char *cpg_print_group_name (
339  		const mar_cpg_name_t *group);
340  	
341  	/*
342  	 * Library Handler Definition
343  	 */
344  	static struct corosync_lib_handler cpg_lib_engine[] =
345  	{
346  		{ /* 0 - MESSAGE_REQ_CPG_JOIN */
347  			.lib_handler_fn				= message_handler_req_lib_cpg_join,
348  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
349  		},
350  		{ /* 1 - MESSAGE_REQ_CPG_LEAVE */
351  			.lib_handler_fn				= message_handler_req_lib_cpg_leave,
352  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
353  		},
354  		{ /* 2 - MESSAGE_REQ_CPG_MCAST */
355  			.lib_handler_fn				= message_handler_req_lib_cpg_mcast,
356  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
357  		},
358  		{ /* 3 - MESSAGE_REQ_CPG_MEMBERSHIP */
359  			.lib_handler_fn				= message_handler_req_lib_cpg_membership,
360  			.flow_control				= CS_LIB_FLOW_CONTROL_NOT_REQUIRED
361  		},
362  		{ /* 4 - MESSAGE_REQ_CPG_LOCAL_GET */
363  			.lib_handler_fn				= message_handler_req_lib_cpg_local_get,
364  			.flow_control				= CS_LIB_FLOW_CONTROL_NOT_REQUIRED
365  		},
366  		{ /* 5 - MESSAGE_REQ_CPG_ITERATIONINITIALIZE */
367  			.lib_handler_fn				= message_handler_req_lib_cpg_iteration_initialize,
368  			.flow_control				= CS_LIB_FLOW_CONTROL_NOT_REQUIRED
369  		},
370  		{ /* 6 - MESSAGE_REQ_CPG_ITERATIONNEXT */
371  			.lib_handler_fn				= message_handler_req_lib_cpg_iteration_next,
372  			.flow_control				= CS_LIB_FLOW_CONTROL_NOT_REQUIRED
373  		},
374  		{ /* 7 - MESSAGE_REQ_CPG_ITERATIONFINALIZE */
375  			.lib_handler_fn				= message_handler_req_lib_cpg_iteration_finalize,
376  			.flow_control				= CS_LIB_FLOW_CONTROL_NOT_REQUIRED
377  		},
378  		{ /* 8 - MESSAGE_REQ_CPG_FINALIZE */
379  			.lib_handler_fn				= message_handler_req_lib_cpg_finalize,
380  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
381  		},
382  		{ /* 9 */
383  			.lib_handler_fn				= message_handler_req_lib_cpg_zc_alloc,
384  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
385  		},
386  		{ /* 10 */
387  			.lib_handler_fn				= message_handler_req_lib_cpg_zc_free,
388  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
389  		},
390  		{ /* 11 */
391  			.lib_handler_fn				= message_handler_req_lib_cpg_zc_execute,
392  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
393  		},
394  		{ /* 12 */
395  			.lib_handler_fn				= message_handler_req_lib_cpg_partial_mcast,
396  			.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED
397  		},
398  	
399  	};
400  	
401  	static struct corosync_exec_handler cpg_exec_engine[] =
402  	{
403  		{ /* 0 - MESSAGE_REQ_EXEC_CPG_PROCJOIN */
404  			.exec_handler_fn	= message_handler_req_exec_cpg_procjoin,
405  			.exec_endian_convert_fn	= exec_cpg_procjoin_endian_convert
406  		},
407  		{ /* 1 - MESSAGE_REQ_EXEC_CPG_PROCLEAVE */
408  			.exec_handler_fn	= message_handler_req_exec_cpg_procleave,
409  			.exec_endian_convert_fn	= exec_cpg_procjoin_endian_convert
410  		},
411  		{ /* 2 - MESSAGE_REQ_EXEC_CPG_JOINLIST */
412  			.exec_handler_fn	= message_handler_req_exec_cpg_joinlist,
413  			.exec_endian_convert_fn	= exec_cpg_joinlist_endian_convert
414  		},
415  		{ /* 3 - MESSAGE_REQ_EXEC_CPG_MCAST */
416  			.exec_handler_fn	= message_handler_req_exec_cpg_mcast,
417  			.exec_endian_convert_fn	= exec_cpg_mcast_endian_convert
418  		},
419  		{ /* 4 - MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD */
420  			.exec_handler_fn	= message_handler_req_exec_cpg_downlist_old,
421  			.exec_endian_convert_fn	= exec_cpg_downlist_endian_convert_old
422  		},
423  		{ /* 5 - MESSAGE_REQ_EXEC_CPG_DOWNLIST */
424  			.exec_handler_fn	= message_handler_req_exec_cpg_downlist,
425  			.exec_endian_convert_fn	= exec_cpg_downlist_endian_convert
426  		},
427  		{ /* 6 - MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST */
428  			.exec_handler_fn	= message_handler_req_exec_cpg_partial_mcast,
429  			.exec_endian_convert_fn	= exec_cpg_partial_mcast_endian_convert
430  		},
431  	};
432  	
433  	struct corosync_service_engine cpg_service_engine = {
434  		.name				        = "corosync cluster closed process group service v1.01",
435  		.id					= CPG_SERVICE,
436  		.priority				= 1,
437  		.private_data_size			= sizeof (struct cpg_pd),
438  		.flow_control				= CS_LIB_FLOW_CONTROL_REQUIRED,
439  		.allow_inquorate			= CS_LIB_ALLOW_INQUORATE,
440  		.lib_init_fn				= cpg_lib_init_fn,
441  		.lib_exit_fn				= cpg_lib_exit_fn,
442  		.lib_engine				= cpg_lib_engine,
443  		.lib_engine_count			= sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
444  		.exec_init_fn				= cpg_exec_init_fn,
445  		.exec_dump_fn				= NULL,
446  		.exec_engine				= cpg_exec_engine,
447  		.exec_engine_count		        = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
448  		.sync_init                              = cpg_sync_init,
449  		.sync_process                           = cpg_sync_process,
450  		.sync_activate                          = cpg_sync_activate,
451  		.sync_abort                             = cpg_sync_abort
452  	};
453  	
454  	struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
455  	{
456  		return (&cpg_service_engine);
457  	}
458  	
459  	struct req_exec_cpg_procjoin {
460  		struct qb_ipc_request_header header __attribute__((aligned(8)));
461  		mar_cpg_name_t group_name __attribute__((aligned(8)));
462  		mar_uint32_t pid __attribute__((aligned(8)));
463  		mar_uint32_t reason __attribute__((aligned(8)));
464  	};
465  	
466  	struct req_exec_cpg_mcast {
467  		struct qb_ipc_request_header header __attribute__((aligned(8)));
468  		mar_cpg_name_t group_name __attribute__((aligned(8)));
469  		mar_uint32_t msglen __attribute__((aligned(8)));
470  		mar_uint32_t pid __attribute__((aligned(8)));
471  		mar_message_source_t source __attribute__((aligned(8)));
472  		mar_uint8_t message[] __attribute__((aligned(8)));
473  	};
474  	
475  	struct req_exec_cpg_partial_mcast {
476  		struct qb_ipc_request_header header __attribute__((aligned(8)));
477  		mar_cpg_name_t group_name __attribute__((aligned(8)));
478  		mar_uint32_t msglen __attribute__((aligned(8)));
479  		mar_uint32_t fraglen __attribute__((aligned(8)));
480  		mar_uint32_t pid __attribute__((aligned(8)));
481  		mar_uint32_t type __attribute__((aligned(8)));
482  		mar_message_source_t source __attribute__((aligned(8)));
483  		mar_uint8_t message[] __attribute__((aligned(8)));
484  	};
485  	
486  	struct req_exec_cpg_downlist_old {
487  		struct qb_ipc_request_header header __attribute__((aligned(8)));
488  		mar_uint32_t left_nodes __attribute__((aligned(8)));
489  		mar_uint32_t nodeids[PROCESSOR_COUNT_MAX]  __attribute__((aligned(8)));
490  	};
491  	
492  	struct req_exec_cpg_downlist {
493  		struct qb_ipc_request_header header __attribute__((aligned(8)));
494  		/* merge decisions */
495  		mar_uint32_t old_members __attribute__((aligned(8)));
496  		/* downlist below */
497  		mar_uint32_t left_nodes __attribute__((aligned(8)));
498  		mar_uint32_t nodeids[PROCESSOR_COUNT_MAX]  __attribute__((aligned(8)));
499  	};
500  	
501  	struct joinlist_msg {
502  		mar_uint32_t sender_nodeid;
503  		uint32_t pid;
504  		mar_cpg_name_t group_name;
505  		struct qb_list_head list;
506  	};
507  	
508  	static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
509  	
510  	/*
511  	 * Function print group name. It's not reentrant
512  	 */
513  	static char *cpg_print_group_name(const mar_cpg_name_t *group)
514  	{
515  		static char res[CPG_MAX_NAME_LENGTH * 4 + 1];
516  		int dest_pos = 0;
517  		char c;
518  		int i;
519  	
520  		for (i = 0; i < group->length; i++) {
521  			c = group->value[i];
522  	
523  			if (c >= ' ' && c < 0x7f && c != '\\') {
524  				res[dest_pos++] = c;
525  	                } else {
526  				if (c == '\\') {
527  					res[dest_pos++] = '\\';
528  					res[dest_pos++] = '\\';
529  				} else {
530  					snprintf(res + dest_pos, sizeof(res) - dest_pos, "\\x%02X", c);
531  					dest_pos += 4;
532  				}
533  			}
534  		}
535  		res[dest_pos] = 0;
536  	
537  		return (res);
538  	}
539  	
540  	static void cpg_sync_init (
541  		const unsigned int *trans_list,
542  		size_t trans_list_entries,
543  		const unsigned int *member_list,
544  		size_t member_list_entries,
545  		const struct memb_ring_id *ring_id)
546  	{
547  		int entries;
548  		int i, j;
549  		int found;
550  	
551  		my_sync_state = CPGSYNC_DOWNLIST;
552  	
553  		/*
554  		 * Drop joinlist messages left over from an aborted sync round. A
555  		 * stale message would replay a join for a node that may no longer be
556  		 * a member, with no leave ever following, and would also shield the
557  		 * entry from joinlist_remove_zombie_pi_entries(). Messages of the
558  		 * current round can only arrive after this init ran on all members.
559  		 */
560  		joinlist_messages_delete ();
561  	
562  		memcpy (my_member_list, member_list, member_list_entries *
563  			sizeof (unsigned int));
564  		my_member_list_entries = member_list_entries;
565  	
566  		last_sync_ring_id.nodeid = ring_id->nodeid;
567  		last_sync_ring_id.seq = ring_id->seq;
568  	
569  		entries = 0;
570  		/*
571  		 * Determine list of nodeids for downlist message
572  		 */
573  		for (i = 0; i < my_old_member_list_entries; i++) {
574  			found = 0;
575  			for (j = 0; j < trans_list_entries; j++) {
576  				if (my_old_member_list[i] == trans_list[j]) {
577  					found = 1;
578  					break;
579  				}
580  			}
581  			if (found == 0) {
582  				g_req_exec_cpg_downlist.nodeids[entries++] =
583  					my_old_member_list[i];
584  			}
585  		}
586  		g_req_exec_cpg_downlist.left_nodes = entries;
587  	}
588  	
589  	static int cpg_sync_process (void)
590  	{
591  		int res = -1;
592  	
593  		if (my_sync_state == CPGSYNC_DOWNLIST) {
594  			res = cpg_exec_send_downlist();
595  			if (res == -1) {
596  				return (-1);
597  			}
598  			my_sync_state = CPGSYNC_JOINLIST;
599  		}
600  		if (my_sync_state == CPGSYNC_JOINLIST) {
601  			res = cpg_exec_send_joinlist();
602  		}
603  		return (res);
604  	}
605  	
606  	static void cpg_sync_activate (void)
607  	{
608  		memcpy (my_old_member_list, my_member_list,
609  			my_member_list_entries * sizeof (unsigned int));
610  		my_old_member_list_entries = my_member_list_entries;
611  	
612  		downlist_inform_clients ();
613  	
614  		joinlist_inform_clients ();
615  	
616  		joinlist_messages_delete ();
617  	
618  		notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
619  	}
620  	
621  	static void cpg_sync_abort (void)
622  	{
623  	
624  		joinlist_messages_delete ();
625  	}
626  	
627  	static int notify_lib_totem_membership (
628  		void *conn,
629  		int member_list_entries,
630  		const unsigned int *member_list)
631  	{
632  		struct qb_list_head *iter;
633  		char *buf;
634  		int size;
635  		struct res_lib_cpg_totem_confchg_callback *res;
636  	
637  		size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
638  			sizeof(mar_uint32_t) * (member_list_entries);
639  		buf = alloca(size);
640  		if (!buf)
641  			return CS_ERR_LIBRARY;
642  	
643  		res = (struct res_lib_cpg_totem_confchg_callback *)buf;
644  		res->member_list_entries = member_list_entries;
645  		res->header.size = size;
646  		res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
647  		res->header.error = CS_OK;
648  	
649  		memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
650  		memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
651  	
652  		if (conn == NULL) {
653  			qb_list_for_each(iter, &cpg_pd_list_head) {
654  				struct cpg_pd *cpg_pd = qb_list_entry (iter, struct cpg_pd, list);
655  				api->ipc_dispatch_send (cpg_pd->conn, buf, size);
656  			}
657  		} else {
658  			api->ipc_dispatch_send (conn, buf, size);
659  		}
660  	
661  		return CS_OK;
662  	}
663  	
664  	/*
665  	 * Helper function for notify_lib_joinlist which prepares member_list using
666  	 * process_info_list with removed left_list items.
667  	 * member_list_entries - When not NULL it contains number of member_list entries
668  	 * member_list - When not NULL it is used as pointer to start of preallocated
669  	 *               array of members. Pointer is adjusted to the end of array on
670  	 *               exit.
671  	 */
672  	static void notify_lib_joinlist_fill_member_list(
673  		const mar_cpg_name_t *group_name,
674  		int left_list_entries,
675  		const mar_cpg_address_t *left_list,
676  		int *member_list_entries,
677  		mar_cpg_address_t **member_list)
678  	{
679  		struct qb_list_head *iter;
680  		int i;
681  	
682  		if (member_list_entries != NULL) {
683  			*member_list_entries = 0;
684  		}
685  	
686  		qb_list_for_each(iter, &process_info_list_head) {
687  			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
688  	
689  			if (mar_name_compare (&pi->group, group_name) == 0) {
690  				int in_left_list = 0;
691  	
692  				for (i = 0; i < left_list_entries; i++) {
693  					if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
694  						in_left_list = 1;
695  						break ;
696  					}
697  				}
698  	
699  				if (!in_left_list) {
700  					if (member_list_entries != NULL) {
701  						(*member_list_entries)++;
702  					}
703  	
704  					if (member_list != NULL) {
705  						(*member_list)->nodeid = pi->nodeid;
706  						(*member_list)->pid = pi->pid;
707  						(*member_list)->reason = CPG_REASON_UNDEFINED;
708  						(*member_list)++;
709  					}
710  				}
711  			}
712  		}
713  	}
714  	
715  	static int notify_lib_joinlist(
716  		const mar_cpg_name_t *group_name,
717  		int joined_list_entries,
718  		mar_cpg_address_t *joined_list,
719  		int left_list_entries,
720  		mar_cpg_address_t *left_list,
721  		int id)
722  	{
723  		int size;
724  		char *buf;
725  		struct qb_list_head *iter;
726  		int member_list_entries;
727  		struct res_lib_cpg_confchg_callback *res;
728  		mar_cpg_address_t *retgi;
729  		int i;
730  	
731  		/*
732  		 * Find size of member_list (use process_info_list but remove items in left_list)
733  		 */
734  		notify_lib_joinlist_fill_member_list(group_name, left_list_entries, left_list,
735  		    &member_list_entries, NULL);
736  	
737  		size = sizeof(struct res_lib_cpg_confchg_callback) +
738  			sizeof(mar_cpg_address_t) * (member_list_entries + left_list_entries + joined_list_entries);
739  		buf = alloca(size);
740  		if (!buf)
741  			return CS_ERR_LIBRARY;
742  	
743  		res = (struct res_lib_cpg_confchg_callback *)buf;
744  		res->joined_list_entries = joined_list_entries;
745  		res->left_list_entries = left_list_entries;
746  		res->member_list_entries = member_list_entries;
747  		retgi = res->member_list;
748  		res->header.size = size;
749  		res->header.id = id;
750  		res->header.error = CS_OK;
751  		memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
752  	
753  		/*
754  		 * Fill res->memberlist. Use process_info_list but remove items in left_list.
755  		 */
756  		notify_lib_joinlist_fill_member_list(group_name, left_list_entries, left_list,
757  		    NULL, &retgi);
758  	
759  		/*
760  		 * Fill res->left_list
761  		 */
762  		if (left_list_entries) {
763  			memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
764  			retgi += left_list_entries;
765  		}
766  	
767  		if (joined_list_entries) {
768  			/*
769  			 * Fill res->joined_list
770  			 */
771  			memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
772  			retgi += joined_list_entries;
773  	
774  			/*
775  			 * Update cpd_state for all local joined processes in group
776  			 */
777  			for (i = 0; i < joined_list_entries; i++) {
778  				if (joined_list[i].nodeid == api->totem_nodeid_get()) {
779  					qb_list_for_each(iter, &cpg_pd_list_head) {
780  						struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
781  						if (joined_list[i].pid == cpd->pid &&
782  						    mar_name_compare (&cpd->group_name, group_name) == 0) {
783  							cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
784  						}
785  					}
786  				}
787  			}
788  		}
789  	
790  		/*
791  		 * Send notification to all ipc clients joined in group_name
792  		 */
793  		qb_list_for_each(iter, &cpg_pd_list_head) {
794  			struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
795  			if (mar_name_compare (&cpd->group_name, group_name) == 0) {
796  				if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
797  					cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
798  	
799  					api->ipc_dispatch_send (cpd->conn, buf, size);
800  					cpd->transition_counter++;
801  				}
802  			}
803  		}
804  	
805  		if (left_list_entries) {
806  			/*
807  			 * Zero internal cpd state for all local processes leaving group
808  			 * (this loop is not strictly needed because left_list always either
809  			 *  contains exactly one process running on local node or more items
810  			 *  but none of them is running on local node)
811  			 */
812  			for (i = 0; i < joined_list_entries; i++) {
813  				if (left_list[i].nodeid == api->totem_nodeid_get() &&
814  				    left_list[i].reason == CONFCHG_CPG_REASON_LEAVE) {
815  					qb_list_for_each(iter, &cpg_pd_list_head) {
816  						struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
817  						if (left_list[i].pid == cpd->pid &&
818  						    mar_name_compare (&cpd->group_name, group_name) == 0) {
819  							cpd->pid = 0;
820  							memset (&cpd->group_name, 0, sizeof(cpd->group_name));
821  							cpd->cpd_state = CPD_STATE_UNJOINED;
822  						}
823  					}
824  				}
825  			}
826  		}
827  	
828  		/*
829  		 * Traverse thru cpds and send totem membership for cpd, where it is not send yet
830  		 */
831  		qb_list_for_each(iter, &cpg_pd_list_head) {
832  			struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
833  	
834  			if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
835  				cpd->initial_totem_conf_sent = 1;
836  	
837  				notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
838  			}
839  		}
840  	
841  		return CS_OK;
842  	}
843  	
844  	static void downlist_log(const char *msg, struct req_exec_cpg_downlist *dl)
845  	{
846  		log_printf (LOG_DEBUG,
847  			    "%s: members(old:%d left:%d)",
848  			    msg,
849  			    dl->old_members,
850  			    dl->left_nodes);
851  	}
852  	
853  	static void downlist_inform_clients (void)
854  	{
855  		struct qb_list_head *iter, *tmp_iter;
856  		struct process_info *left_pi;
857  		qb_map_t *group_map;
858  		struct cpg_name cpg_group;
859  		mar_cpg_name_t group;
860  		struct confchg_data{
861  			struct cpg_name cpg_group;
862  			mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
863  			int left_list_entries;
864  			struct qb_list_head  list;
865  		} *pcd;
866  		qb_map_iter_t *miter;
867  		int i, size;
868  	
869  		downlist_log("my downlist", &g_req_exec_cpg_downlist);
870  	
871  		group_map = qb_skiplist_create();
872  	
873  		/*
874  		 * only the cpg groups included in left nodes should receive
875  		 * confchg event, so we will collect these cpg groups and
876  		 * relative left_lists here.
877  		 */
878  		qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
879  			struct process_info *pi = qb_list_entry(iter, struct process_info, list);
880  	
881  			left_pi = NULL;
882  			for (i = 0; i < g_req_exec_cpg_downlist.left_nodes; i++) {
883  	
884  				if (pi->nodeid == g_req_exec_cpg_downlist.nodeids[i]) {
885  					left_pi = pi;
886  					break;
887  				}
888  			}
889  	
890  			if (left_pi) {
891  				marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
892  				cpg_group.value[cpg_group.length] = 0;
893  	
894  				pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
895  				if (pcd == NULL) {
896  					pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
897  					memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
898  					qb_map_put(group_map, pcd->cpg_group.value, pcd);
899  				}
900  				size = pcd->left_list_entries;
901  				pcd->left_list[size].nodeid = left_pi->nodeid;
902  				pcd->left_list[size].pid = left_pi->pid;
903  				pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
904  				pcd->left_list_entries++;
905  				qb_list_del (&left_pi->list);
906  				free (left_pi);
907  			}
908  		}
909  	
910  		/* send only one confchg event per cpg group */
911  		miter = qb_map_iter_create(group_map);
912  		while (qb_map_iter_next(miter, (void **)&pcd)) {
913  			marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
914  	
915  			log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
916  			for (i=0; i<pcd->left_list_entries; i++) {
917  				log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
918  					i, cpg_print_group_name(&group),
919  					(char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
920  					pcd->left_list[i].pid);
921  			}
922  	
923  			/* send confchg event */
924  			notify_lib_joinlist(&group,
925  				0, NULL,
926  				pcd->left_list_entries,
927  				pcd->left_list,
928  				MESSAGE_RES_CPG_CONFCHG_CALLBACK);
929  	
930  			free(pcd);
931  		}
932  		qb_map_iter_free(miter);
933  		qb_map_destroy(group_map);
934  	}
935  	
936  	/*
937  	 * Remove processes that might have left the group while we were suspended.
938  	 */
939  	static void joinlist_remove_zombie_pi_entries (void)
940  	{
941  		struct qb_list_head *pi_iter, *tmp_iter;
942  		struct qb_list_head *jl_iter;
943  		struct process_info *pi;
944  		struct joinlist_msg *stored_msg;
945  		int found;
946  	
947  		qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
948  			pi = qb_list_entry (pi_iter, struct process_info, list);
949  	
950  			/*
951  			 * Ignore local node
952  			 */
953  			if (pi->nodeid == api->totem_nodeid_get()) {
954  				continue ;
955  			}
956  	
957  			/*
958  			 * Try to find message in joinlist messages
959  			 */
960  			found = 0;
961  			qb_list_for_each(jl_iter, &joinlist_messages_head) {
962  				stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
963  	
964  				if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
965  					continue ;
966  				}
967  	
968  				if (pi->nodeid == stored_msg->sender_nodeid &&
969  				    pi->pid == stored_msg->pid &&
970  				    mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
971  					found = 1;
972  					break ;
973  				}
974  			}
975  	
976  			if (!found) {
977  				do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
978  			}
979  		}
980  	}
981  	
982  	static void joinlist_inform_clients (void)
983  	{
984  		struct joinlist_msg *stored_msg;
985  		struct qb_list_head *iter;
986  		unsigned int i;
987  		qb_map_t *group_notify_map;
988  		qb_map_iter_t *miter;
989  		struct join_list_confchg_data *jld;
990  	
991  		group_notify_map = qb_skiplist_create();
992  	
993  		i = 0;
994  		qb_list_for_each(iter, &joinlist_messages_head) {
995  			stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
996  	
997  			log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
998  				i++, cpg_print_group_name(&stored_msg->group_name),
999  				(char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
1000 				stored_msg->pid);
1001 	
1002 			/* Ignore our own messages */
1003 			if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
1004 				continue ;
1005 			}
1006 	
1007 			do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
1008 				CONFCHG_CPG_REASON_NODEUP, group_notify_map);
1009 		}
1010 	
1011 		miter = qb_map_iter_create(group_notify_map);
1012 		while (qb_map_iter_next(miter, (void **)&jld)) {
1013 			notify_lib_joinlist(&jld->cpg_group,
1014 					    jld->join_list_entries, jld->join_list,
1015 					    0, NULL,
1016 					    MESSAGE_RES_CPG_CONFCHG_CALLBACK);
1017 			free(jld);
1018 		}
1019 		qb_map_iter_free(miter);
1020 		qb_map_destroy(group_notify_map);
1021 	
1022 		joinlist_remove_zombie_pi_entries ();
1023 	}
1024 	
1025 	static void joinlist_messages_delete (void)
1026 	{
1027 		struct joinlist_msg *stored_msg;
1028 		struct qb_list_head *iter, *tmp_iter;
1029 	
1030 		qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
1031 			stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
1032 			qb_list_del (&stored_msg->list);
1033 			free (stored_msg);
1034 		}
1035 		qb_list_init (&joinlist_messages_head);
1036 	}
1037 	
1038 	static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
1039 	{
1040 		qb_list_init (&joinlist_messages_head);
1041 		api = corosync_api;
1042 		return (NULL);
1043 	}
1044 	
1045 	static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
1046 	{
1047 		struct qb_list_head *iter, *tmp_iter;
1048 		struct process_info *pi;
1049 	
1050 		qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
1051 			pi = qb_list_entry (iter, struct process_info, list);
1052 			qb_list_del (&pi->list);
1053 			free (pi);
1054 		}
1055 	
1056 		qb_list_del (&cpg_iteration_instance->list);
1057 		hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
1058 	}
1059 	
1060 	static void cpg_pd_finalize (struct cpg_pd *cpd)
1061 	{
1062 		struct qb_list_head *iter, *tmp_iter;
1063 		struct cpg_iteration_instance *cpii;
1064 	
1065 		zcb_all_free(cpd);
1066 		qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
1067 			cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
1068 	
1069 			cpg_iteration_instance_finalize (cpii);
1070 		}
1071 	
1072 		qb_list_del (&cpd->list);
1073 	}
1074 	
1075 	static int cpg_lib_exit_fn (void *conn)
1076 	{
1077 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1078 	
1079 		log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
1080 	
1081 		if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
1082 			cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
1083 					MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
1084 		}
1085 	
1086 		cpg_pd_finalize (cpd);
1087 	
1088 		api->ipc_refcnt_dec (conn);
1089 		return (0);
1090 	}
1091 	
1092 	static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
1093 	{
1094 		struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
1095 		struct iovec req_exec_cpg_iovec;
1096 		int result;
1097 	
1098 		memset(&req_exec_cpg_procjoin, 0, sizeof(req_exec_cpg_procjoin));
1099 	
1100 		memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
1101 		req_exec_cpg_procjoin.pid = pid;
1102 		req_exec_cpg_procjoin.reason = reason;
1103 	
1104 		req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
1105 		req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
1106 	
1107 		req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
1108 		req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
1109 	
1110 		result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
1111 	
1112 		return (result);
1113 	}
1114 	
1115 	/* Can byteswap join & leave messages */
1116 	static void exec_cpg_procjoin_endian_convert (void *msg)
1117 	{
1118 		struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
1119 	
1120 		req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
1121 		swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
1122 		req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
1123 	}
1124 	
1125 	static void exec_cpg_joinlist_endian_convert (void *msg_v)
1126 	{
1127 		char *msg = msg_v;
1128 		struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
1129 		struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
1130 	
1131 		swab_mar_int32_t (&res->size);
1132 	
1133 		while ((const char*)jle < msg + res->size) {
1134 			jle->pid = swab32(jle->pid);
1135 			swab_mar_cpg_name_t (&jle->group_name);
1136 			jle++;
1137 		}
1138 	}
1139 	
1140 	static void exec_cpg_downlist_endian_convert_old (void *msg)
1141 	{
1142 	}
1143 	
1144 	static void exec_cpg_downlist_endian_convert (void *msg)
1145 	{
1146 		struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
1147 		unsigned int i;
1148 	
1149 		req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
1150 		req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
1151 	
1152 		for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
1153 			req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
1154 		}
1155 	}
1156 	
1157 	
1158 	static void exec_cpg_mcast_endian_convert (void *msg)
1159 	{
1160 		struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
1161 	
1162 		swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
1163 		swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
1164 		req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
1165 		req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
1166 		swab_mar_message_source_t (&req_exec_cpg_mcast->source);
1167 	}
1168 	
1169 	static void exec_cpg_partial_mcast_endian_convert (void *msg)
1170 	{
1171 		struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
1172 	
1173 		swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
1174 		swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
1175 		req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
1176 		req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
1177 		req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
1178 		req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
1179 		swab_mar_message_source_t (&req_exec_cpg_mcast->source);
1180 	}
1181 	
1182 	static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
1183 		struct qb_list_head *iter;
1184 	
1185 		qb_list_for_each(iter, &process_info_list_head) {
1186 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1187 	
1188 			if (pi->pid == pid && pi->nodeid == nodeid &&
1189 				mar_name_compare (&pi->group, group_name) == 0) {
1190 					return pi;
1191 			}
1192 		}
1193 	
1194 		return NULL;
1195 	}
1196 	
1197 	static void do_proc_join(
1198 		const mar_cpg_name_t *name,
1199 		uint32_t pid,
1200 		unsigned int nodeid,
1201 		int reason,
1202 		qb_map_t *group_notify_map)
1203 	{
1204 		struct process_info *pi;
1205 		struct process_info *pi_entry;
1206 		mar_cpg_address_t notify_info;
1207 		struct qb_list_head *list;
1208 		struct qb_list_head *list_to_add = NULL;
1209 		int size;
1210 	
1211 		if (process_info_find (name, pid, nodeid) != NULL) {
1212 			return ;
1213 	 	}
1214 		pi = malloc (sizeof (struct process_info));
1215 		if (!pi) {
1216 			log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
1217 			return;
1218 		}
1219 		pi->nodeid = nodeid;
1220 		pi->pid = pid;
1221 		memcpy(&pi->group, name, sizeof(*name));
1222 		qb_list_init(&pi->list);
1223 	
1224 		/*
1225 		 * Insert new process in sorted order so synchronization works properly
1226 		 */
1227 		list_to_add = &process_info_list_head;
1228 		qb_list_for_each(list, &process_info_list_head) {
1229 			pi_entry = qb_list_entry(list, struct process_info, list);
1230 			if (pi_entry->nodeid > pi->nodeid ||
1231 				(pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
1232 	
1233 				break;
1234 			}
1235 			list_to_add = list;
1236 		}
1237 		qb_list_add (&pi->list, list_to_add);
1238 	
1239 		notify_info.pid = pi->pid;
1240 		notify_info.nodeid = nodeid;
1241 		notify_info.reason = reason;
1242 	
1243 		if (group_notify_map == NULL) {
1244 			notify_lib_joinlist(&pi->group,
1245 					    1, &notify_info,
1246 					    0, NULL,
1247 					    MESSAGE_RES_CPG_CONFCHG_CALLBACK);
1248 		} else {
1249 			struct join_list_confchg_data *jld = qb_map_get(group_notify_map, pi->group.value);
1250 			if (jld == NULL) {
1251 				jld = (struct join_list_confchg_data *)calloc(1, sizeof(struct join_list_confchg_data));
1252 				memcpy(&jld->cpg_group, &pi->group, sizeof(mar_cpg_name_t));
1253 				qb_map_put(group_notify_map, jld->cpg_group.value, jld);
1254 			}
1255 			size = jld->join_list_entries;
1256 			jld->join_list[size].nodeid = notify_info.nodeid;
1257 			jld->join_list[size].pid = notify_info.pid;
1258 			jld->join_list[size].reason = notify_info.reason;
1259 			jld->join_list_entries++;
1260 		}
1261 	}
1262 	
1263 	static void do_proc_leave(
1264 		const mar_cpg_name_t *name,
1265 		uint32_t pid,
1266 		unsigned int nodeid,
1267 		int reason)
1268 	{
1269 		struct process_info *pi;
1270 		struct qb_list_head *iter, *tmp_iter;
1271 		mar_cpg_address_t notify_info;
1272 	
1273 		notify_info.pid = pid;
1274 		notify_info.nodeid = nodeid;
1275 		notify_info.reason = reason;
1276 	
1277 		notify_lib_joinlist(name,
1278 			0, NULL,
1279 			1, &notify_info,
1280 			MESSAGE_RES_CPG_CONFCHG_CALLBACK);
1281 	
1282 		qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
1283 			pi = qb_list_entry(iter, struct process_info, list);
1284 	
1285 			if (pi->pid == pid && pi->nodeid == nodeid &&
1286 				mar_name_compare (&pi->group, name)==0) {
1287 				qb_list_del (&pi->list);
1288 				free (pi);
1289 			}
1290 		}
1291 	}
1292 	
1293 	static void message_handler_req_exec_cpg_downlist_old (
1294 		const void *message,
1295 		unsigned int nodeid)
1296 	{
1297 		log_printf (LOGSYS_LEVEL_DEBUG, "downlist OLD from node " CS_PRI_NODE_ID,
1298 			nodeid);
1299 	}
1300 	
1301 	static void message_handler_req_exec_cpg_downlist(
1302 		const void *message,
1303 		unsigned int nodeid)
1304 	{
1305 		const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
1306 	
1307 		log_printf (LOGSYS_LEVEL_DEBUG, "downlist left_list: %d received",
1308 				req_exec_cpg_downlist->left_nodes);
1309 	}
1310 	
1311 	
1312 	static void message_handler_req_exec_cpg_procjoin (
1313 		const void *message,
1314 		unsigned int nodeid)
1315 	{
1316 		const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
1317 	
1318 		log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
1319 			nodeid,
1320 			api->totem_ifaces_print(nodeid),
1321 			(unsigned int)req_exec_cpg_procjoin->pid);
1322 	
1323 		do_proc_join (&req_exec_cpg_procjoin->group_name,
1324 			req_exec_cpg_procjoin->pid, nodeid,
1325 			CONFCHG_CPG_REASON_JOIN, NULL);
1326 	}
1327 	
1328 	static void message_handler_req_exec_cpg_procleave (
1329 		const void *message,
1330 		unsigned int nodeid)
1331 	{
1332 		const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
1333 	
1334 		log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
1335 			nodeid,
1336 			api->totem_ifaces_print(nodeid),
1337 			(unsigned int)req_exec_cpg_procjoin->pid);
1338 	
1339 		do_proc_leave (&req_exec_cpg_procjoin->group_name,
1340 			req_exec_cpg_procjoin->pid, nodeid,
1341 			req_exec_cpg_procjoin->reason);
1342 	}
1343 	
1344 	
1345 	/* Got a proclist from another node */
1346 	static void message_handler_req_exec_cpg_joinlist (
1347 		const void *message_v,
1348 		unsigned int nodeid)
1349 	{
1350 		const char *message = message_v;
1351 		const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
1352 		const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
1353 		struct joinlist_msg *stored_msg;
1354 	
1355 		log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node " CS_PRI_NODE_ID,
1356 			nodeid);
1357 	
1358 		while ((const char*)jle < message + res->size) {
1359 			stored_msg = malloc (sizeof (struct joinlist_msg));
1360 			memset(stored_msg, 0, sizeof (struct joinlist_msg));
1361 			stored_msg->sender_nodeid = nodeid;
1362 			stored_msg->pid = jle->pid;
1363 			memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
1364 			qb_list_init (&stored_msg->list);
1365 			qb_list_add (&stored_msg->list, &joinlist_messages_head);
1366 			jle++;
1367 		}
1368 	}
1369 	
1370 	static void message_handler_req_exec_cpg_mcast (
1371 		const void *message,
1372 		unsigned int nodeid)
1373 	{
1374 		const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
1375 		struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
1376 		int msglen = req_exec_cpg_mcast->msglen;
1377 		struct qb_list_head *iter, *pi_iter, *tmp_iter;
1378 		struct cpg_pd *cpd;
1379 		struct iovec iovec[2];
1380 		int known_node = 0;
1381 	
1382 		res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
1383 		res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
1384 		res_lib_cpg_mcast.msglen = msglen;
1385 		res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
1386 		res_lib_cpg_mcast.nodeid = nodeid;
1387 	
1388 		memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
1389 			sizeof(mar_cpg_name_t));
1390 		iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
1391 		iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
1392 	
1393 		iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
1394 		iovec[1].iov_len = msglen;
1395 	
1396 		qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1397 			cpd = qb_list_entry(iter, struct cpg_pd, list);
1398 			if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
1399 				&& (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
1400 	
1401 				if (!known_node) {
1402 					/* Try to find, if we know the node */
1403 					qb_list_for_each(pi_iter, &process_info_list_head) {
1404 						struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
1405 	
1406 						if (pi->nodeid == nodeid &&
1407 							mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
1408 							known_node = 1;
1409 							break;
1410 						}
1411 					}
1412 				}
1413 	
1414 				if (!known_node) {
1415 					log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
1416 					return ;
1417 				}
1418 	
1419 				api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
1420 			}
1421 		}
1422 	}
1423 	
1424 	static void message_handler_req_exec_cpg_partial_mcast (
1425 		const void *message,
1426 		unsigned int nodeid)
1427 	{
1428 		const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
1429 		struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
1430 		int msglen = req_exec_cpg_mcast->fraglen;
1431 		struct qb_list_head *iter, *pi_iter, *tmp_iter;
1432 		struct cpg_pd *cpd;
1433 		struct iovec iovec[2];
1434 		int known_node = 0;
1435 	
1436 		log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node " CS_PRI_NODE_ID ", size = %d bytes\n", nodeid, msglen);
1437 	
1438 		res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
1439 		res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
1440 		res_lib_cpg_mcast.fraglen = msglen;
1441 		res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
1442 		res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
1443 		res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
1444 		res_lib_cpg_mcast.nodeid = nodeid;
1445 	
1446 		memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
1447 		       sizeof(mar_cpg_name_t));
1448 		iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
1449 		iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
1450 	
1451 		iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
1452 		iovec[1].iov_len = msglen;
1453 	
1454 		qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1455 			cpd = qb_list_entry(iter, struct cpg_pd, list);
1456 	
1457 			if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
1458 			    && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
1459 	
1460 				if (!known_node) {
1461 					/* Try to find, if we know the node */
1462 					qb_list_for_each(pi_iter, &process_info_list_head) {
1463 						struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
1464 	
1465 						if (pi->nodeid == nodeid &&
1466 						    mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
1467 							known_node = 1;
1468 							break;
1469 						}
1470 					}
1471 				}
1472 	
1473 				if (!known_node) {
1474 					log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
1475 					return ;
1476 				}
1477 	
1478 				api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
1479 			}
1480 		}
1481 	}
1482 	
1483 	
1484 	static int cpg_exec_send_downlist(void)
1485 	{
1486 		struct iovec iov;
1487 	
1488 		g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
1489 		g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
1490 	
1491 		g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
1492 	
1493 		iov.iov_base = (void *)&g_req_exec_cpg_downlist;
1494 		iov.iov_len = g_req_exec_cpg_downlist.header.size;
1495 	
1496 		return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
1497 	}
1498 	
1499 	static int cpg_exec_send_joinlist(void)
1500 	{
1501 		int count = 0;
1502 		struct qb_list_head *iter;
1503 		struct qb_ipc_response_header *res;
1504 		char *buf;
1505 		size_t buf_size;
1506 		struct join_list_entry *jle;
1507 		struct iovec req_exec_cpg_iovec;
1508 	
1509 		qb_list_for_each(iter, &process_info_list_head) {
1510 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1511 	
1512 	 		if (pi->nodeid == api->totem_nodeid_get ()) {
1513 	 			count++;
1514 			}
1515 		}
1516 	
1517 		/* Nothing to send */
1518 		if (!count)
1519 			return 0;
1520 	
1521 		buf_size = sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count;
1522 		buf = alloca(buf_size);
1523 		if (!buf) {
1524 			log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
1525 			return -1;
1526 		}
1527 		memset(buf, 0, buf_size);
1528 	
1529 		jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
1530 		res = (struct qb_ipc_response_header *)buf;
1531 	
1532 		qb_list_for_each(iter, &process_info_list_head) {
1533 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1534 	
1535 			if (pi->nodeid == api->totem_nodeid_get ()) {
1536 				memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
1537 				jle->pid = pi->pid;
1538 				jle++;
1539 			}
1540 		}
1541 	
1542 		res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
1543 		res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
1544 	
1545 		req_exec_cpg_iovec.iov_base = buf;
1546 		req_exec_cpg_iovec.iov_len = res->size;
1547 	
1548 		return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
1549 	}
1550 	
1551 	static int cpg_lib_init_fn (void *conn)
1552 	{
1553 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1554 		memset (cpd, 0, sizeof(struct cpg_pd));
1555 		cpd->conn = conn;
1556 		qb_list_add (&cpd->list, &cpg_pd_list_head);
1557 	
1558 		qb_list_init (&cpd->iteration_instance_list_head);
1559 		qb_list_init (&cpd->zcb_mapped_list_head);
1560 	
1561 		api->ipc_refcnt_inc (conn);
1562 		log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
1563 		return (0);
1564 	}
1565 	
1566 	/* Join message from the library */
1567 	static void message_handler_req_lib_cpg_join (void *conn, const void *message)
1568 	{
1569 		const struct req_lib_cpg_join *req_lib_cpg_join = message;
1570 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1571 		struct res_lib_cpg_join res_lib_cpg_join;
1572 		cs_error_t error = CS_OK;
1573 		struct qb_list_head *iter;
1574 	
1575 		/* Test, if we don't have same pid and group name joined */
1576 		qb_list_for_each(iter, &cpg_pd_list_head) {
1577 			struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
1578 	
1579 			if (cpd_item->pid == req_lib_cpg_join->pid &&
1580 				mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
1581 	
1582 				/* We have same pid and group name joined -> return error */
1583 				error = CS_ERR_EXIST;
1584 				goto response_send;
1585 			}
1586 		}
1587 	
1588 		/*
1589 		 * Same check must be done in process info list, because there may be not yet delivered
1590 		 * leave of client.
1591 		 */
1592 		qb_list_for_each(iter, &process_info_list_head) {
1593 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1594 	
1595 			if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
1596 			    mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
1597 				/* We have same pid and group name joined -> return error */
1598 				error = CS_ERR_TRY_AGAIN;
1599 				goto response_send;
1600 			}
1601 		}
1602 	
1603 		if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
1604 			error = CS_ERR_NAME_TOO_LONG;
1605 			goto response_send;
1606 		}
1607 	
1608 		switch (cpd->cpd_state) {
1609 		case CPD_STATE_UNJOINED:
1610 			error = CS_OK;
1611 			cpd->cpd_state = CPD_STATE_JOIN_STARTED;
1612 			cpd->pid = req_lib_cpg_join->pid;
1613 			cpd->flags = req_lib_cpg_join->flags;
1614 			memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
1615 				sizeof (cpd->group_name));
1616 	
1617 			cpg_node_joinleave_send (req_lib_cpg_join->pid,
1618 				&req_lib_cpg_join->group_name,
1619 				MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
1620 			break;
1621 		case CPD_STATE_LEAVE_STARTED:
1622 			error = CS_ERR_BUSY;
1623 			break;
1624 		case CPD_STATE_JOIN_STARTED:
1625 			error = CS_ERR_EXIST;
1626 			break;
1627 		case CPD_STATE_JOIN_COMPLETED:
1628 			error = CS_ERR_EXIST;
1629 			break;
1630 		}
1631 	
1632 	response_send:
1633 		res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
1634 	        res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
1635 	        res_lib_cpg_join.header.error = error;
1636 	        api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
1637 	}
1638 	
1639 	/* Leave message from the library */
1640 	static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
1641 	{
1642 		struct res_lib_cpg_leave res_lib_cpg_leave;
1643 		cs_error_t error = CS_OK;
1644 		struct req_lib_cpg_leave  *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
1645 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1646 	
1647 		log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
1648 	
1649 		switch (cpd->cpd_state) {
1650 		case CPD_STATE_UNJOINED:
1651 			error = CS_ERR_NOT_EXIST;
1652 			break;
1653 		case CPD_STATE_LEAVE_STARTED:
1654 			error = CS_ERR_NOT_EXIST;
1655 			break;
1656 		case CPD_STATE_JOIN_STARTED:
1657 			error = CS_ERR_BUSY;
1658 			break;
1659 		case CPD_STATE_JOIN_COMPLETED:
1660 			error = CS_OK;
1661 			cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
1662 			cpg_node_joinleave_send (req_lib_cpg_leave->pid,
1663 				&req_lib_cpg_leave->group_name,
1664 				MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
1665 				CONFCHG_CPG_REASON_LEAVE);
1666 			break;
1667 		}
1668 	
1669 		/* send return */
1670 		res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
1671 		res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
1672 		res_lib_cpg_leave.header.error = error;
1673 		api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
1674 	}
1675 	
1676 	/* Finalize message from library */
1677 	static void message_handler_req_lib_cpg_finalize (
1678 		void *conn,
1679 		const void *message)
1680 	{
1681 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1682 		struct res_lib_cpg_finalize res_lib_cpg_finalize;
1683 		cs_error_t error = CS_OK;
1684 	
1685 		log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
1686 	
1687 		/*
1688 		 * We will just remove cpd from list. After this call, connection will be
1689 		 * closed on lib side, and cpg_lib_exit_fn will be called
1690 		 */
1691 		qb_list_del (&cpd->list);
1692 		qb_list_init (&cpd->list);
1693 	
1694 		res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
1695 		res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
1696 		res_lib_cpg_finalize.header.error = error;
1697 	
1698 		api->ipc_response_send (conn, &res_lib_cpg_finalize,
1699 			sizeof (res_lib_cpg_finalize));
1700 	}
1701 	
1702 	static int
1703 	memory_map (
1704 		const char *path,
1705 		size_t bytes,
1706 		void **buf)
1707 	{
1708 		int32_t fd;
1709 		void *addr;
1710 		int32_t res;
1711 	
1712 		fd = open (path, O_RDWR, 0600);
1713 	
1714 		unlink (path);
1715 	
1716 		if (fd == -1) {
1717 			return (-1);
1718 		}
1719 	
1720 		res = ftruncate (fd, bytes);
1721 		if (res == -1) {
1722 			goto error_close_unlink;
1723 		}
1724 	
1725 		addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
1726 			MAP_SHARED, fd, 0);
1727 	
1728 		if (addr == MAP_FAILED) {
1729 			goto error_close_unlink;
1730 		}
1731 	#ifdef MADV_NOSYNC
1732 		madvise(addr, bytes, MADV_NOSYNC);
1733 	#endif
1734 	
1735 		res = close (fd);
1736 		if (res) {
1737 			munmap (addr, bytes);
1738 			return (-1);
1739 		}
1740 		*buf = addr;
1741 		return (0);
1742 	
1743 	error_close_unlink:
1744 		close (fd);
1745 		unlink(path);
1746 		return -1;
1747 	}
1748 	
1749 	static inline int zcb_alloc (
1750 		struct cpg_pd *cpd,
1751 		const char *path_to_file,
1752 		size_t size,
1753 		void **addr)
1754 	{
1755 		struct zcb_mapped *zcb_mapped;
1756 		unsigned int res;
1757 	
1758 		zcb_mapped = malloc (sizeof (struct zcb_mapped));
1759 		if (zcb_mapped == NULL) {
1760 			return (-1);
1761 		}
1762 	
1763 		res = memory_map (
1764 			path_to_file,
1765 			size,
1766 			addr);
1767 		if (res == -1) {
1768 			free (zcb_mapped);
1769 			return (-1);
1770 		}
1771 	
1772 		qb_list_init (&zcb_mapped->list);
1773 		zcb_mapped->addr = *addr;
1774 		zcb_mapped->size = size;
1775 		qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
1776 		return (0);
1777 	}
1778 	
1779 	
1780 	static inline int zcb_free (struct zcb_mapped *zcb_mapped)
1781 	{
1782 		int res;
1783 	
1784 		res = munmap (zcb_mapped->addr, zcb_mapped->size);
1785 		qb_list_del (&zcb_mapped->list);
1786 		free (zcb_mapped);
1787 		return (res);
1788 	}
1789 	
1790 	static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
1791 	{
1792 		struct qb_list_head *list, *tmp_iter;
1793 		struct zcb_mapped *zcb_mapped;
1794 		int res = 0;
1795 	
1796 		qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
1797 			zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
1798 	
1799 			if (zcb_mapped->addr == addr) {
1800 				res = zcb_free (zcb_mapped);
1801 				break;
1802 			}
1803 	
1804 		}
1805 		return (res);
1806 	}
1807 	
1808 	static inline int zcb_all_free (
1809 		struct cpg_pd *cpd)
1810 	{
1811 		struct qb_list_head *list, *tmp_iter;
1812 		struct zcb_mapped *zcb_mapped;
1813 	
1814 		qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
1815 			zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
1816 	
1817 			zcb_free (zcb_mapped);
1818 		}
1819 		return (0);
1820 	}
1821 	
1822 	union u {
1823 		uint64_t server_addr;
1824 		void *server_ptr;
1825 	};
1826 	
1827 	static uint64_t void2serveraddr (void *server_ptr)
1828 	{
1829 		union u u;
1830 	
(1) Event assign_union_field: The union field "server_ptr" of "u" is written.
Also see events: [inconsistent_union_field_access]
1831 		u.server_ptr = server_ptr;
CID (unavailable; MK=373a2bfb8d356c141d16a1aa1a84e9fe) (#1 of 1): Inconsistent C union access (INCONSISTENT_UNION_ACCESS):
(2) Event inconsistent_union_field_access: In "u.server_addr", the union field used: "server_addr" is inconsistent with the field most recently stored: "server_ptr".
Also see events: [assign_union_field]
1832 		return (u.server_addr);
1833 	}
1834 	
1835 	static void *serveraddr2void (uint64_t server_addr)
1836 	{
1837 		union u u;
1838 	
1839 		u.server_addr = server_addr;
1840 		return (u.server_ptr);
1841 	};
1842 	
1843 	static void message_handler_req_lib_cpg_zc_alloc (
1844 		void *conn,
1845 		const void *message)
1846 	{
1847 		mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
1848 		struct qb_ipc_response_header res_header;
1849 		void *addr = NULL;
1850 		struct coroipcs_zc_header *zc_header;
1851 		unsigned int res;
1852 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1853 	
1854 		log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
1855 	
1856 		res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
1857 			&addr);
1858 		assert(res == 0);
1859 	
1860 		zc_header = (struct coroipcs_zc_header *)addr;
1861 		zc_header->server_address = void2serveraddr(addr);
1862 	
1863 		res_header.size = sizeof (struct qb_ipc_response_header);
1864 		res_header.id = 0;
1865 		api->ipc_response_send (conn,
1866 			&res_header,
1867 			res_header.size);
1868 	}
1869 	
1870 	static void message_handler_req_lib_cpg_zc_free (
1871 		void *conn,
1872 		const void *message)
1873 	{
1874 		mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
1875 		struct qb_ipc_response_header res_header;
1876 		void *addr = NULL;
1877 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1878 	
1879 		log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
1880 	
1881 		addr = serveraddr2void (hdr->server_address);
1882 	
1883 		zcb_by_addr_free (cpd, addr);
1884 	
1885 		res_header.size = sizeof (struct qb_ipc_response_header);
1886 		res_header.id = 0;
1887 		api->ipc_response_send (
1888 			conn, &res_header,
1889 			res_header.size);
1890 	}
1891 	
1892 	/* Fragmented mcast message from the library */
1893 	static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
1894 	{
1895 		const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
1896 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1897 		mar_cpg_name_t group_name = cpd->group_name;
1898 	
1899 		struct iovec req_exec_cpg_iovec[2];
1900 		struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
1901 		struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
1902 		int msglen = req_lib_cpg_mcast->fraglen;
1903 		int result;
1904 		cs_error_t error = CS_ERR_NOT_EXIST;
1905 	
1906 		log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
1907 		log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
1908 	
1909 		switch (cpd->cpd_state) {
1910 		case CPD_STATE_UNJOINED:
1911 			error = CS_ERR_NOT_EXIST;
1912 			break;
1913 		case CPD_STATE_LEAVE_STARTED:
1914 			error = CS_ERR_NOT_EXIST;
1915 			break;
1916 		case CPD_STATE_JOIN_STARTED:
1917 			error = CS_OK;
1918 			break;
1919 		case CPD_STATE_JOIN_COMPLETED:
1920 			error = CS_OK;
1921 			break;
1922 		}
1923 	
1924 		res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
1925 		res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
1926 	
1927 		if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
1928 			cpd->initial_transition_counter = cpd->transition_counter;
1929 		}
1930 		if (cpd->transition_counter != cpd->initial_transition_counter) {
1931 			error = CS_ERR_INTERRUPT;
1932 		}
1933 	
1934 		if (error == CS_OK) {
1935 			req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
1936 			req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
1937 								       MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
1938 			req_exec_cpg_mcast.pid = cpd->pid;
1939 			req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
1940 			req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
1941 			req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
1942 			api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
1943 			memcpy(&req_exec_cpg_mcast.group_name, &group_name,
1944 			       sizeof(mar_cpg_name_t));
1945 	
1946 			req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
1947 			req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
1948 			req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
1949 			req_exec_cpg_iovec[1].iov_len = msglen;
1950 	
1951 			result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
1952 			assert(result == 0);
1953 		} else {
1954 			log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
1955 				   conn, group_name.value, cpd->cpd_state, error);
1956 		}
1957 	
1958 		res_lib_cpg_partial_send.header.error = error;
1959 		api->ipc_response_send (conn, &res_lib_cpg_partial_send,
1960 					sizeof (res_lib_cpg_partial_send));
1961 	}
1962 	
1963 	/* Mcast message from the library */
1964 	static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
1965 	{
1966 		const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
1967 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1968 		mar_cpg_name_t group_name = cpd->group_name;
1969 	
1970 		struct iovec req_exec_cpg_iovec[2];
1971 		struct req_exec_cpg_mcast req_exec_cpg_mcast;
1972 		int msglen = req_lib_cpg_mcast->msglen;
1973 		int result;
1974 		cs_error_t error = CS_ERR_NOT_EXIST;
1975 	
1976 		log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
1977 	
1978 		switch (cpd->cpd_state) {
1979 		case CPD_STATE_UNJOINED:
1980 			error = CS_ERR_NOT_EXIST;
1981 			break;
1982 		case CPD_STATE_LEAVE_STARTED:
1983 			error = CS_ERR_NOT_EXIST;
1984 			break;
1985 		case CPD_STATE_JOIN_STARTED:
1986 			error = CS_OK;
1987 			break;
1988 		case CPD_STATE_JOIN_COMPLETED:
1989 			error = CS_OK;
1990 			break;
1991 		}
1992 	
1993 		if (error == CS_OK) {
1994 			memset(&req_exec_cpg_mcast, 0, sizeof(req_exec_cpg_mcast));
1995 	
1996 			req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
1997 			req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
1998 				MESSAGE_REQ_EXEC_CPG_MCAST);
1999 			req_exec_cpg_mcast.pid = cpd->pid;
2000 			req_exec_cpg_mcast.msglen = msglen;
2001 			api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
2002 			memcpy(&req_exec_cpg_mcast.group_name, &group_name,
2003 				sizeof(mar_cpg_name_t));
2004 	
2005 			req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
2006 			req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
2007 			req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
2008 			req_exec_cpg_iovec[1].iov_len = msglen;
2009 	
2010 			result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
2011 			assert(result == 0);
2012 		} else {
2013 			log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
2014 				conn, group_name.value, cpd->cpd_state, error);
2015 		}
2016 	}
2017 	
2018 	static void message_handler_req_lib_cpg_zc_execute (
2019 		void *conn,
2020 		const void *message)
2021 	{
2022 		mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
2023 		struct qb_ipc_request_header *header;
2024 		struct res_lib_cpg_mcast res_lib_cpg_mcast;
2025 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
2026 		struct iovec req_exec_cpg_iovec[2];
2027 		struct req_exec_cpg_mcast req_exec_cpg_mcast;
2028 		struct req_lib_cpg_mcast *req_lib_cpg_mcast;
2029 		int result;
2030 		cs_error_t error = CS_ERR_NOT_EXIST;
2031 	
2032 		log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
2033 	
2034 		header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
2035 		req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
2036 	
2037 		switch (cpd->cpd_state) {
2038 		case CPD_STATE_UNJOINED:
2039 			error = CS_ERR_NOT_EXIST;
2040 			break;
2041 		case CPD_STATE_LEAVE_STARTED:
2042 			error = CS_ERR_NOT_EXIST;
2043 			break;
2044 		case CPD_STATE_JOIN_STARTED:
2045 			error = CS_OK;
2046 			break;
2047 		case CPD_STATE_JOIN_COMPLETED:
2048 			error = CS_OK;
2049 			break;
2050 		}
2051 	
2052 		res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
2053 		res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
2054 		if (error == CS_OK) {
2055 			req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
2056 			req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
2057 				MESSAGE_REQ_EXEC_CPG_MCAST);
2058 			req_exec_cpg_mcast.pid = cpd->pid;
2059 			req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
2060 			api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
2061 			memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
2062 				sizeof(mar_cpg_name_t));
2063 	
2064 			req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
2065 			req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
2066 			req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
2067 			req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
2068 	
2069 			result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
2070 			if (result == 0) {
2071 				res_lib_cpg_mcast.header.error = CS_OK;
2072 			} else {
2073 				res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
2074 			}
2075 		} else {
2076 			res_lib_cpg_mcast.header.error = error;
2077 		}
2078 	
2079 		api->ipc_response_send (conn, &res_lib_cpg_mcast,
2080 			sizeof (res_lib_cpg_mcast));
2081 	
2082 	}
2083 	
2084 	static void message_handler_req_lib_cpg_membership (void *conn,
2085 							    const void *message)
2086 	{
2087 		struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
2088 			(struct req_lib_cpg_membership_get *)message;
2089 		struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
2090 		struct qb_list_head *iter;
2091 		int member_count = 0;
2092 	
2093 		res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
2094 		res_lib_cpg_membership_get.header.error = CS_OK;
2095 		res_lib_cpg_membership_get.header.size =
2096 			sizeof (struct res_lib_cpg_membership_get);
2097 	
2098 		qb_list_for_each(iter, &process_info_list_head) {
2099 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
2100 			if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
2101 				res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
2102 				res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
2103 				member_count += 1;
2104 			}
2105 		}
2106 		res_lib_cpg_membership_get.member_count = member_count;
2107 	
2108 		api->ipc_response_send (conn, &res_lib_cpg_membership_get,
2109 			sizeof (res_lib_cpg_membership_get));
2110 	}
2111 	
2112 	static void message_handler_req_lib_cpg_local_get (void *conn,
2113 							   const void *message)
2114 	{
2115 		struct res_lib_cpg_local_get res_lib_cpg_local_get;
2116 	
2117 		res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
2118 		res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
2119 		res_lib_cpg_local_get.header.error = CS_OK;
2120 		res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
2121 	
2122 		api->ipc_response_send (conn, &res_lib_cpg_local_get,
2123 			sizeof (res_lib_cpg_local_get));
2124 	}
2125 	
2126 	static void message_handler_req_lib_cpg_iteration_initialize (
2127 		void *conn,
2128 		const void *message)
2129 	{
2130 		const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
2131 		struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
2132 		hdb_handle_t cpg_iteration_handle = 0;
2133 		struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
2134 		struct qb_list_head *iter, *iter2;
2135 		struct cpg_iteration_instance *cpg_iteration_instance;
2136 		cs_error_t error = CS_OK;
2137 		int res;
2138 	
2139 		log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
2140 	
2141 		/* Because between calling this function and *next can be some operations which will
2142 		 * change list, we must do full copy.
2143 		 */
2144 	
2145 		/*
2146 		 * Create new iteration instance
2147 		 */
2148 		res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
2149 				&cpg_iteration_handle);
2150 	
2151 		if (res != 0) {
2152 			error = CS_ERR_NO_MEMORY;
2153 			goto response_send;
2154 		}
2155 	
2156 		res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
2157 	
2158 		if (res != 0) {
2159 			error = CS_ERR_BAD_HANDLE;
2160 			goto error_destroy;
2161 		}
2162 	
2163 		qb_list_init (&cpg_iteration_instance->items_list_head);
2164 		cpg_iteration_instance->handle = cpg_iteration_handle;
2165 	
2166 		/*
2167 		 * Create copy of process_info list "grouped by" group name
2168 		 */
2169 		qb_list_for_each(iter, &process_info_list_head) {
2170 			struct process_info *pi = qb_list_entry (iter, struct process_info, list);
2171 			struct process_info *new_pi;
2172 	
2173 			if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
2174 				/*
2175 				 * Try to find processed group name in our list new list
2176 				 */
2177 				int found = 0;
2178 	
2179 	                        qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
2180 					 struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
2181 	
2182 					 if (mar_name_compare (&pi2->group, &pi->group) == 0) {
2183 						found = 1;
2184 						break;
2185 					 }
2186 				}
2187 	
2188 				if (found) {
2189 					/*
2190 					 * We have this name in list -> don't add
2191 					 */
2192 					continue ;
2193 				}
2194 			} else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
2195 				/*
2196 				 * Test pi group name with request
2197 				 */
2198 				if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
2199 					/*
2200 					 * Not same -> don't add
2201 					 */
2202 					continue ;
2203 			}
2204 	
2205 			new_pi = malloc (sizeof (struct process_info));
2206 			if (!new_pi) {
2207 				log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
2208 	
2209 				error = CS_ERR_NO_MEMORY;
2210 	
2211 				goto error_put_destroy;
2212 			}
2213 	
2214 			memcpy (new_pi, pi, sizeof (struct process_info));
2215 			qb_list_init (&new_pi->list);
2216 	
2217 			if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
2218 				/*
2219 				 * pid and nodeid -> undefined
2220 				 */
2221 				new_pi->pid = new_pi->nodeid = 0;
2222 			}
2223 	
2224 			/*
2225 			 * We will return list "grouped" by "group name", so try to find right place to add
2226 			 */
2227 			qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
2228 				struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
2229 	
2230 				if (mar_name_compare (&pi2->group, &pi->group) == 0) {
2231 					break;
2232 				}
2233 			}
2234 	
2235 			qb_list_add (&new_pi->list, iter2);
2236 		}
2237 	
2238 		/*
2239 		 * Now we have a full "grouped by" copy of process_info list
2240 		 */
2241 	
2242 		/*
2243 		 * Add instance to current cpd list
2244 		 */
2245 		qb_list_init (&cpg_iteration_instance->list);
2246 		qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
2247 	
2248 		cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
2249 	
2250 	error_put_destroy:
2251 		hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2252 	error_destroy:
2253 		if (error != CS_OK) {
2254 			hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2255 		}
2256 	
2257 	response_send:
2258 		res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
2259 		res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
2260 		res_lib_cpg_iterationinitialize.header.error = error;
2261 		res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
2262 	
2263 		api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
2264 			sizeof (res_lib_cpg_iterationinitialize));
2265 	}
2266 	
2267 	static void message_handler_req_lib_cpg_iteration_next (
2268 		void *conn,
2269 		const void *message)
2270 	{
2271 		const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
2272 		struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
2273 		struct cpg_iteration_instance *cpg_iteration_instance;
2274 		cs_error_t error = CS_OK;
2275 		int res;
2276 		struct process_info *pi;
2277 	
2278 		log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
2279 	
2280 		res = hdb_handle_get (&cpg_iteration_handle_t_db,
2281 				req_lib_cpg_iterationnext->iteration_handle,
2282 				(void *)&cpg_iteration_instance);
2283 	
2284 		if (res != 0) {
2285 			error = CS_ERR_LIBRARY;
2286 			goto error_exit;
2287 		}
2288 	
2289 		assert (cpg_iteration_instance);
2290 	
2291 		cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
2292 	
2293 		if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
2294 			error = CS_ERR_NO_SECTIONS;
2295 			goto error_put;
2296 		}
2297 	
2298 		pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
2299 	
2300 		/*
2301 		 * Copy iteration data
2302 		 */
2303 		res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
2304 		res_lib_cpg_iterationnext.description.pid = pi->pid;
2305 		memcpy (&res_lib_cpg_iterationnext.description.group,
2306 				&pi->group,
2307 				sizeof (mar_cpg_name_t));
2308 	
2309 	error_put:
2310 		hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
2311 	error_exit:
2312 		res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
2313 		res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
2314 		res_lib_cpg_iterationnext.header.error = error;
2315 	
2316 		api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
2317 			sizeof (res_lib_cpg_iterationnext));
2318 	}
2319 	
2320 	static void message_handler_req_lib_cpg_iteration_finalize (
2321 		void *conn,
2322 		const void *message)
2323 	{
2324 		const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
2325 		struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
2326 		struct cpg_iteration_instance *cpg_iteration_instance;
2327 		cs_error_t error = CS_OK;
2328 		int res;
2329 	
2330 		log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
2331 	
2332 		res = hdb_handle_get (&cpg_iteration_handle_t_db,
2333 				req_lib_cpg_iterationfinalize->iteration_handle,
2334 				(void *)&cpg_iteration_instance);
2335 	
2336 		if (res != 0) {
2337 			error = CS_ERR_LIBRARY;
2338 			goto error_exit;
2339 		}
2340 	
2341 		assert (cpg_iteration_instance);
2342 	
2343 		cpg_iteration_instance_finalize (cpg_iteration_instance);
2344 		hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
2345 	
2346 	error_exit:
2347 		res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
2348 		res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
2349 		res_lib_cpg_iterationfinalize.header.error = error;
2350 	
2351 		api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
2352 			sizeof (res_lib_cpg_iterationfinalize));
2353 	}
2354