summaryrefslogtreecommitdiffstats
path: root/msg.c
blob: 013bcb99e71fa7e4c2a6bb21f012444507478517 (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
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
/*
 * Copyright (C) 2008 Intel Corporation
 *
 * 	mdmon socket / message handling
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "mdadm.h"
#include "mdmon.h"

static const __u32 start_magic = 0x5a5aa5a5;
static const __u32 end_magic = 0xa5a55a5a;

static int send_buf(int fd, const void* buf, int len, int tmo)
{
	fd_set set;
	int rv;
	struct timeval timeout = {tmo, 0};
	struct timeval *ptmo = tmo ? &timeout : NULL;

	while (len) {
		FD_ZERO(&set);
		FD_SET(fd, &set);
		rv = select(fd+1, NULL, &set, NULL, ptmo);
		if (rv <= 0)
			return -1;
		rv = write(fd, buf, len);
		if (rv <= 0)
			return -1;
		len -= rv;
		buf += rv;
	}
	return 0;
}

static int recv_buf(int fd, void* buf, int len, int tmo)
{
	fd_set set;
	int rv;
	struct timeval timeout = {tmo, 0};
	struct timeval *ptmo = tmo ? &timeout : NULL;

	while (len) {
		FD_ZERO(&set);
		FD_SET(fd, &set);
		rv = select(fd+1, &set, NULL, NULL, ptmo);
		if (rv <= 0)
			return -1;
		rv = read(fd, buf, len);
		if (rv <= 0)
			return -1;
		len -= rv;
		buf += rv;
	}
	return 0;
}


int send_message(int fd, struct metadata_update *msg, int tmo)
{
	__u32 len = msg->len;
	int rv;

	rv = send_buf(fd, &start_magic, 4, tmo);
	rv = rv ?: send_buf(fd, &len, 4, tmo);
	if (len)
		rv = rv ?: send_buf(fd, msg->buf, msg->len, tmo);
	rv = send_buf(fd, &end_magic, 4, tmo);

	return rv;
}

int receive_message(int fd, struct metadata_update *msg, int tmo)
{
	__u32 magic;
	__u32 len;
	int rv;

	rv = recv_buf(fd, &magic, 4, tmo);
	if (rv < 0 || magic != start_magic)
		return -1;
	rv = recv_buf(fd, &len, 4, tmo);
	if (rv < 0 || len > MSG_MAX_LEN)
		return -1;
	if (len) {
		msg->buf = malloc(len);
		if (msg->buf == NULL)
			return -1;
		rv = recv_buf(fd, msg->buf, len, tmo);
		if (rv < 0) {
			free(msg->buf);
			return -1;
		}
	} else
		msg->buf = NULL;
	rv = recv_buf(fd, &magic, 4, tmo);
	if (rv < 0 || magic != end_magic) {
		free(msg->buf);
		return -1;
	}
	msg->len = len;
	return 0;
}

int ack(int fd, int tmo)
{
	struct metadata_update msg = { .len = 0 };

	return send_message(fd, &msg, tmo);
}

int wait_reply(int fd, int tmo)
{
	struct metadata_update msg;
	return receive_message(fd, &msg, tmo);
}

int connect_monitor(char *devname)
{
	char path[100];
	int sfd;
	long fl;
	struct sockaddr_un addr;
	int pos;
	char *c;

	pos = sprintf(path, "/var/run/mdadm/");
	if (is_subarray(devname)) {
		devname++;
		c = strchr(devname, '/');
		if (!c)
			return -1;
		snprintf(&path[pos], c - devname + 1, "%s", devname);
		pos += c - devname;
	} else
		pos += sprintf(&path[pos], "%s", devname);
	sprintf(&path[pos], ".sock");

	sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sfd < 0)
		return -1;

	addr.sun_family = PF_LOCAL;
	strcpy(addr.sun_path, path);
	if (connect(sfd, &addr, sizeof(addr)) < 0) {
		close(sfd);
		return -1;
	}

	fl = fcntl(sfd, F_GETFL, 0);
	fl |= O_NONBLOCK;
	fcntl(sfd, F_SETFL, fl);

	return sfd;
}

int ping_monitor(char *devname)
{
	int sfd = connect_monitor(devname);
	int err = 0;

	if (sfd < 0)
		return sfd;

	/* try to ping existing socket */
	if (ack(sfd, 20) != 0)
		err = -1;

	/* check the reply */
	if (!err && wait_reply(sfd, 20) != 0)
		err = -1;

	close(sfd);
	return err;
}