]> git.ipfire.org Git - thirdparty/mdadm.git/blob - msg.c
Factor common code into new "start_mdmon".
[thirdparty/mdadm.git] / msg.c
1 /*
2 * Copyright (C) 2008 Intel Corporation
3 *
4 * mdmon socket / message handling
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include "mdadm.h"
32 #include "mdmon.h"
33
34 static const __u32 start_magic = 0x5a5aa5a5;
35 static const __u32 end_magic = 0xa5a55a5a;
36
37 static int send_buf(int fd, const void* buf, int len, int tmo)
38 {
39 fd_set set;
40 int rv;
41 struct timeval timeout = {tmo, 0};
42 struct timeval *ptmo = tmo ? &timeout : NULL;
43
44 while (len) {
45 FD_ZERO(&set);
46 FD_SET(fd, &set);
47 rv = select(fd+1, NULL, &set, NULL, ptmo);
48 if (rv <= 0)
49 return -1;
50 rv = write(fd, buf, len);
51 if (rv <= 0)
52 return -1;
53 len -= rv;
54 buf += rv;
55 }
56 return 0;
57 }
58
59 static int recv_buf(int fd, void* buf, int len, int tmo)
60 {
61 fd_set set;
62 int rv;
63 struct timeval timeout = {tmo, 0};
64 struct timeval *ptmo = tmo ? &timeout : NULL;
65
66 while (len) {
67 FD_ZERO(&set);
68 FD_SET(fd, &set);
69 rv = select(fd+1, &set, NULL, NULL, ptmo);
70 if (rv <= 0)
71 return -1;
72 rv = read(fd, buf, len);
73 if (rv <= 0)
74 return -1;
75 len -= rv;
76 buf += rv;
77 }
78 return 0;
79 }
80
81
82 int send_message(int fd, struct metadata_update *msg, int tmo)
83 {
84 __u32 len = msg->len;
85 int rv;
86
87 rv = send_buf(fd, &start_magic, 4, tmo);
88 rv = rv ?: send_buf(fd, &len, 4, tmo);
89 if (len)
90 rv = rv ?: send_buf(fd, msg->buf, msg->len, tmo);
91 rv = send_buf(fd, &end_magic, 4, tmo);
92
93 return rv;
94 }
95
96 int receive_message(int fd, struct metadata_update *msg, int tmo)
97 {
98 __u32 magic;
99 __u32 len;
100 int rv;
101
102 rv = recv_buf(fd, &magic, 4, tmo);
103 if (rv < 0 || magic != start_magic)
104 return -1;
105 rv = recv_buf(fd, &len, 4, tmo);
106 if (rv < 0 || len > MSG_MAX_LEN)
107 return -1;
108 if (len) {
109 msg->buf = malloc(len);
110 if (msg->buf == NULL)
111 return -1;
112 rv = recv_buf(fd, msg->buf, len, tmo);
113 if (rv < 0) {
114 free(msg->buf);
115 return -1;
116 }
117 } else
118 msg->buf = NULL;
119 rv = recv_buf(fd, &magic, 4, tmo);
120 if (rv < 0 || magic != end_magic) {
121 free(msg->buf);
122 return -1;
123 }
124 msg->len = len;
125 return 0;
126 }
127
128 int ack(int fd, int tmo)
129 {
130 struct metadata_update msg = { .len = 0 };
131
132 return send_message(fd, &msg, tmo);
133 }
134
135 int wait_reply(int fd, int tmo)
136 {
137 struct metadata_update msg;
138 return receive_message(fd, &msg, tmo);
139 }
140
141 int connect_monitor(char *devname)
142 {
143 char path[100];
144 int sfd;
145 long fl;
146 struct sockaddr_un addr;
147
148 sprintf(path, "/var/run/mdadm/%s.sock", devname);
149 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
150 if (sfd < 0)
151 return -1;
152
153 addr.sun_family = PF_LOCAL;
154 strcpy(addr.sun_path, path);
155 if (connect(sfd, &addr, sizeof(addr)) < 0) {
156 close(sfd);
157 return -1;
158 }
159
160 fl = fcntl(sfd, F_GETFL, 0);
161 fl |= O_NONBLOCK;
162 fcntl(sfd, F_SETFL, fl);
163
164 return sfd;
165 }
166
167 int ping_monitor(char *devname)
168 {
169 int sfd = connect_monitor(devname);
170 int err = 0;
171
172 if (sfd < 0)
173 return sfd;
174
175 /* try to ping existing socket */
176 if (ack(sfd, 0) != 0)
177 err = -1;
178
179 /* check the reply */
180 if (!err && wait_reply(sfd, 0) != 0)
181 err = -1;
182
183 close(sfd);
184 return err;
185 }