summaryrefslogtreecommitdiffstats
path: root/vqsim/vq_object.c
blob: d832e10dfeebd7118d22a61795cca9ce400ce66f (plain)
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
/*
  This is a Votequorum object in the parent process. it's really just a conduit for the forked
  votequorum entity
*/

#include <qb/qblog.h>
#include <qb/qbloop.h>
#include <qb/qbipcc.h>
#include <netinet/in.h>

#include "../exec/votequorum.h"
#include "vqsim.h"

struct vq_instance
{
	int nodeid;
	int vq_socket;
	pid_t pid;
};

vq_object_t vq_create_instance(qb_loop_t *poll_loop, int nodeid)
{
	struct vq_instance *instance = malloc(sizeof(struct vq_instance));
	if (!instance) {
		return NULL;
	}

	instance->nodeid = nodeid;

	if (fork_new_instance(nodeid, &instance->vq_socket, &instance->pid)) {
		free(instance);
		return NULL;
	}

	return instance;
}

pid_t vq_get_pid(vq_object_t instance)
{
	struct vq_instance *vqi = instance;
	return vqi->pid;
}

void vq_quit(vq_object_t instance)
{
	struct vq_instance *vqi = instance;
	struct vqsim_msg_header msg;
	int res;

	msg.type = VQMSG_QUIT;
	msg.from_nodeid = 0;
	msg.param = 0;

	res = write(vqi->vq_socket, &msg, sizeof(msg));
	if (res <= 0) {
		perror("Quit write failed");
	}
}

int vq_quit_if_inquorate(vq_object_t instance)
{
	struct vq_instance *vqi = instance;
	struct vqsim_msg_header msg;
	int res;

	msg.type = VQMSG_QUORUMQUIT;
	msg.from_nodeid = 0;
	msg.param = 0;

	res = write(vqi->vq_socket, &msg, sizeof(msg));
	if (res <= 0) {
		perror("Quit write failed");
	}
	return 0;
}

int vq_set_nodelist(vq_object_t instance, struct memb_ring_id *ring_id, int *nodeids, int nodeids_entries)
{
	struct vq_instance *vqi = instance;
	char msgbuf[sizeof(int)*nodeids_entries + sizeof(struct vqsim_sync_msg)];
	struct vqsim_sync_msg *msg = (void*)msgbuf;
	int res;

	msg->header.type = VQMSG_SYNC;
	msg->header.from_nodeid = 0;
	msg->header.param = 0;
	msg->view_list_entries = nodeids_entries;
	memcpy(&msg->view_list, nodeids, nodeids_entries*sizeof(int));
	memcpy(&msg->ring_id, ring_id, sizeof(struct memb_ring_id));

	res = write(vqi->vq_socket, msgbuf, sizeof(msgbuf));
	if (res <= 0) {
		perror("Sync write failed");
		return -1;
	}
	return 0;
}

int vq_set_qdevice(vq_object_t instance, struct memb_ring_id *ring_id, int onoff)
{
	struct vq_instance *vqi = instance;
	struct vqsim_msg_header msg;
	int res;

	msg.type = VQMSG_QDEVICE;
	msg.from_nodeid = 0;
	msg.param = onoff;
	res = write(vqi->vq_socket, &msg, sizeof(msg));
	if (res <= 0) {
		perror("qdevice register write failed");
		return -1;
	}
	return 0;
}

int vq_get_parent_fd(vq_object_t instance)
{
	struct vq_instance *vqi = instance;

	return vqi->vq_socket;
}