Remove mon_pipe for communicating from monitor to manager
[thirdparty/mdadm.git] / mdmon.c
CommitLineData
549e9569
NB
1
2/*
3 * md array manager.
4 * When md arrays have user-space managed metadata, this is the program
5 * that does the managing.
6 *
7 * Given one argument: the name of the array (e.g. /dev/md0) that is
8 * the container.
9 * We fork off a helper that runs high priority and mlocked. It responds to
10 * device failures and other events that might stop writeout, or that are
11 * trivial to deal with.
12 * The main thread then watches for new arrays being created in the container
13 * and starts monitoring them too ... along with a few other tasks.
14 *
15 * The main thread communicates with the priority thread by writing over
16 * a pipe.
17 * Separate programs can communicate with the main thread via Unix-domain
18 * socket.
19 * The two threads share address space and open file table.
20 *
21 */
22
23#ifndef _GNU_SOURCE
24#define _GNU_SOURCE
25#endif
26
27#include <unistd.h>
28#include <stdlib.h>
29#include <sys/stat.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <sys/mman.h>
33#include <stdio.h>
34#include <errno.h>
35#include <string.h>
36#include <fcntl.h>
b109d928 37#include <signal.h>
549e9569
NB
38
39#include <sched.h>
40
41#include "mdadm.h"
42#include "mdmon.h"
43
549e9569
NB
44struct active_array *discard_this;
45struct active_array *pending_discard;
3e70c845 46struct md_generic_cmd *active_cmd;
549e9569
NB
47
48int run_child(void *v)
49{
50 struct supertype *c = v;
1ed3f387
NB
51 sigset_t set;
52 /* SIGUSR is sent from child to parent, So child must block it */
53 sigemptyset(&set);
54 sigaddset(&set, SIGUSR1);
55 sigprocmask(SIG_BLOCK, &set, NULL);
56
549e9569
NB
57 do_monitor(c);
58 return 0;
59}
60
61int clone_monitor(struct supertype *container)
62{
549e9569
NB
63 static char stack[4096];
64 int rv;
65
3e70c845
DW
66 rv = pipe(container->mgr_pipe);
67 if (rv < 0)
68 return rv;
549e9569
NB
69
70 rv = clone(run_child, stack+4096-64,
71 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
72 container);
549e9569 73 if (rv < 0)
3e70c845
DW
74 goto err_clone;
75 else
549e9569 76 return rv;
3e70c845
DW
77
78 err_clone:
3e70c845
DW
79 close(container->mgr_pipe[0]);
80 close(container->mgr_pipe[1]);
81
82 return rv;
549e9569
NB
83}
84
85static struct superswitch *find_metadata_methods(char *vers)
86{
87 if (strcmp(vers, "ddf") == 0)
88 return &super_ddf;
5b65005f
DW
89 if (strcmp(vers, "imsm") == 0)
90 return &super_imsm;
549e9569
NB
91 return NULL;
92}
93
94
b109d928 95static int make_pidfile(char *devname, int o_excl)
549e9569
NB
96{
97 char path[100];
98 char pid[10];
99 int fd;
100 sprintf(path, "/var/run/mdadm/%s.pid", devname);
101
b109d928 102 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
549e9569
NB
103 if (fd < 0)
104 return -1;
105 sprintf(pid, "%d\n", getpid());
106 write(fd, pid, strlen(pid));
107 close(fd);
108 return 0;
109}
110
b109d928
DW
111static void try_kill_monitor(char *devname)
112{
113 char buf[100];
114 int fd;
115 pid_t pid;
116
117 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
118 fd = open(buf, O_RDONLY);
119 if (fd < 0)
120 return;
121
122 if (read(fd, buf, sizeof(buf)) < 0) {
123 close(fd);
124 return;
125 }
126
127 close(fd);
128 pid = strtoul(buf, NULL, 10);
129
130 /* kill this process if it is mdmon */
131 sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
132 fd = open(buf, O_RDONLY);
133 if (fd < 0)
134 return;
135
136 if (read(fd, buf, sizeof(buf)) < 0) {
137 close(fd);
138 return;
139 }
140
141 if (strstr(buf, "mdmon") != NULL)
142 kill(pid, SIGTERM);
143}
144
e0d6609f
NB
145void remove_pidfile(char *devname)
146{
147 char buf[100];
148
149 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
150 unlink(buf);
151}
152
549e9569
NB
153static int make_control_sock(char *devname)
154{
155 char path[100];
156 int sfd;
157 long fl;
158 struct sockaddr_un addr;
159
160 sprintf(path, "/var/run/mdadm/%s.sock", devname);
161 unlink(path);
162 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
163 if (sfd < 0)
164 return -1;
165
166 addr.sun_family = PF_LOCAL;
167 strcpy(addr.sun_path, path);
168 if (bind(sfd, &addr, sizeof(addr)) < 0) {
169 close(sfd);
170 return -1;
171 }
172 listen(sfd, 10);
173 fl = fcntl(sfd, F_GETFL, 0);
174 fl |= O_NONBLOCK;
175 fcntl(sfd, F_SETFL, fl);
176 return sfd;
177}
178
179int main(int argc, char *argv[])
180{
181 int mdfd;
549e9569
NB
182 struct mdinfo *mdi, *di;
183 struct supertype *container;
184 if (argc != 2) {
185 fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
186 exit(2);
187 }
188 mdfd = open(argv[1], O_RDWR);
189 if (mdfd < 0) {
190 fprintf(stderr, "md-manage: %s: %s\n", argv[1],
191 strerror(errno));
192 exit(1);
193 }
194 if (md_get_version(mdfd) < 0) {
195 fprintf(stderr, "md-manage: %s: Not an md device\n",
196 argv[1]);
197 exit(1);
198 }
199
200 /* hopefully it is a container - we'll check later */
201
202 container = malloc(sizeof(*container));
549e9569
NB
203 container->devnum = fd2devnum(mdfd);
204 container->devname = devnum2devname(container->devnum);
e0d6609f 205 container->device_name = argv[1];
549e9569
NB
206
207 /* If this fails, we hope it already exists */
208 mkdir("/var/run/mdadm", 0600);
209 /* pid file lives in /var/run/mdadm/mdXX.pid */
b109d928
DW
210 if (make_pidfile(container->devname, O_EXCL) < 0) {
211 if (ping_monitor(container->devname) == 0) {
212 fprintf(stderr, "mdmon: %s already managed\n",
213 container->devname);
214 exit(3);
215 } else {
216 /* cleanup the old monitor, this one is taking over */
217 try_kill_monitor(container->devname);
218 if (make_pidfile(container->devname, 0) < 0) {
219 fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
220 container->devname);
221 exit(3);
222 }
223 }
549e9569
NB
224 }
225
226 container->sock = make_control_sock(container->devname);
227 if (container->sock < 0) {
228 fprintf(stderr, "mdmon: Cannot create socket in /var/run/mdadm\n");
229 exit(3);
230 }
231 container->arrays = NULL;
232
233 mdi = sysfs_read(mdfd, container->devnum,
234 GET_VERSION|GET_LEVEL|GET_DEVS);
235
236 if (!mdi) {
237 fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
238 container->devname);
239 exit(3);
240 }
241 if (mdi->array.level != UnSet) {
242 fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
243 argv[1]);
244 exit(3);
245 }
246 if (mdi->array.major_version != -1 ||
247 mdi->array.minor_version != -2) {
248 fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
249 argv[1]);
250 exit(3);
251 }
252
253 container->ss = find_metadata_methods(mdi->text_version);
254 if (container->ss == NULL) {
255 fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
256 argv[1], mdi->text_version);
257 exit(3);
258 }
259
260 container->devs = NULL;
261 for (di = mdi->devs; di; di = di->next) {
262 struct mdinfo *cd = malloc(sizeof(*cd));
263 cd = di;
264 cd->next = container->devs;
265 container->devs = cd;
266 }
267 sysfs_free(mdi);
268
269
270 if (container->ss->load_super(container, mdfd, argv[1])) {
271 fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
272 argv[1]);
273 exit(3);
274 }
e0d6609f 275 close(mdfd);
5869a76c 276 close(mdfd);
549e9569
NB
277
278 mlockall(MCL_FUTURE);
279
3e70c845 280 if (clone_monitor(container) < 0) {
549e9569
NB
281 fprintf(stderr, "md-manage: failed to start monitor process: %s\n",
282 strerror(errno));
283 exit(2);
284 }
285
286 do_manager(container);
287
288 exit(0);
289}