]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
add infrastructure to receive higher order commands, like remove_device
[thirdparty/mdadm.git] / mdmon.c
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>
37 #include <signal.h>
38
39 #include <sched.h>
40
41 #include "mdadm.h"
42 #include "mdmon.h"
43
44 struct active_array *array_list;
45 struct active_array *discard_this;
46 struct active_array *pending_discard;
47 struct md_generic_cmd *active_cmd;
48
49 int run_child(void *v)
50 {
51 struct supertype *c = v;
52 do_monitor(c);
53 return 0;
54 }
55
56 int clone_monitor(struct supertype *container)
57 {
58 static char stack[4096];
59 int rv;
60
61 rv = pipe(container->mgr_pipe);
62 if (rv < 0)
63 return rv;
64 rv = pipe(container->mon_pipe);
65 if (rv < 0)
66 goto err_mon_pipe;
67
68 rv = clone(run_child, stack+4096-64,
69 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
70 container);
71 if (rv < 0)
72 goto err_clone;
73 else
74 return rv;
75
76 err_clone:
77 close(container->mon_pipe[0]);
78 close(container->mon_pipe[1]);
79 err_mon_pipe:
80 close(container->mgr_pipe[0]);
81 close(container->mgr_pipe[1]);
82
83 return rv;
84 }
85
86 static struct superswitch *find_metadata_methods(char *vers)
87 {
88 if (strcmp(vers, "ddf") == 0)
89 return &super_ddf;
90 return NULL;
91 }
92
93
94 static int make_pidfile(char *devname, int o_excl)
95 {
96 char path[100];
97 char pid[10];
98 int fd;
99 sprintf(path, "/var/run/mdadm/%s.pid", devname);
100
101 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
102 if (fd < 0)
103 return -1;
104 sprintf(pid, "%d\n", getpid());
105 write(fd, pid, strlen(pid));
106 close(fd);
107 return 0;
108 }
109
110 static void try_kill_monitor(char *devname)
111 {
112 char buf[100];
113 int fd;
114 pid_t pid;
115
116 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
117 fd = open(buf, O_RDONLY);
118 if (fd < 0)
119 return;
120
121 if (read(fd, buf, sizeof(buf)) < 0) {
122 close(fd);
123 return;
124 }
125
126 close(fd);
127 pid = strtoul(buf, NULL, 10);
128
129 /* kill this process if it is mdmon */
130 sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
131 fd = open(buf, O_RDONLY);
132 if (fd < 0)
133 return;
134
135 if (read(fd, buf, sizeof(buf)) < 0) {
136 close(fd);
137 return;
138 }
139
140 if (strstr(buf, "mdmon") != NULL)
141 kill(pid, SIGTERM);
142 }
143
144 static int make_control_sock(char *devname)
145 {
146 char path[100];
147 int sfd;
148 long fl;
149 struct sockaddr_un addr;
150
151 sprintf(path, "/var/run/mdadm/%s.sock", devname);
152 unlink(path);
153 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
154 if (sfd < 0)
155 return -1;
156
157 addr.sun_family = PF_LOCAL;
158 strcpy(addr.sun_path, path);
159 if (bind(sfd, &addr, sizeof(addr)) < 0) {
160 close(sfd);
161 return -1;
162 }
163 listen(sfd, 10);
164 fl = fcntl(sfd, F_GETFL, 0);
165 fl |= O_NONBLOCK;
166 fcntl(sfd, F_SETFL, fl);
167 return sfd;
168 }
169
170 int main(int argc, char *argv[])
171 {
172 int mdfd;
173 struct mdinfo *mdi, *di;
174 struct supertype *container;
175 if (argc != 2) {
176 fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
177 exit(2);
178 }
179 mdfd = open(argv[1], O_RDWR);
180 if (mdfd < 0) {
181 fprintf(stderr, "md-manage: %s: %s\n", argv[1],
182 strerror(errno));
183 exit(1);
184 }
185 if (md_get_version(mdfd) < 0) {
186 fprintf(stderr, "md-manage: %s: Not an md device\n",
187 argv[1]);
188 exit(1);
189 }
190
191 /* hopefully it is a container - we'll check later */
192
193 container = malloc(sizeof(*container));
194 container->devfd = mdfd;
195 container->devnum = fd2devnum(mdfd);
196 container->devname = devnum2devname(container->devnum);
197
198 /* If this fails, we hope it already exists */
199 mkdir("/var/run/mdadm", 0600);
200 /* pid file lives in /var/run/mdadm/mdXX.pid */
201 if (make_pidfile(container->devname, O_EXCL) < 0) {
202 if (ping_monitor(container->devname) == 0) {
203 fprintf(stderr, "mdmon: %s already managed\n",
204 container->devname);
205 exit(3);
206 } else {
207 /* cleanup the old monitor, this one is taking over */
208 try_kill_monitor(container->devname);
209 if (make_pidfile(container->devname, 0) < 0) {
210 fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
211 container->devname);
212 exit(3);
213 }
214 }
215 }
216
217 container->sock = make_control_sock(container->devname);
218 if (container->sock < 0) {
219 fprintf(stderr, "mdmon: Cannot create socket in /var/run/mdadm\n");
220 exit(3);
221 }
222 container->arrays = NULL;
223
224 mdi = sysfs_read(mdfd, container->devnum,
225 GET_VERSION|GET_LEVEL|GET_DEVS);
226
227 if (!mdi) {
228 fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
229 container->devname);
230 exit(3);
231 }
232 if (mdi->array.level != UnSet) {
233 fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
234 argv[1]);
235 exit(3);
236 }
237 if (mdi->array.major_version != -1 ||
238 mdi->array.minor_version != -2) {
239 fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
240 argv[1]);
241 exit(3);
242 }
243
244 container->ss = find_metadata_methods(mdi->text_version);
245 if (container->ss == NULL) {
246 fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
247 argv[1], mdi->text_version);
248 exit(3);
249 }
250
251 container->devs = NULL;
252 for (di = mdi->devs; di; di = di->next) {
253 struct mdinfo *cd = malloc(sizeof(*cd));
254 cd = di;
255 cd->next = container->devs;
256 container->devs = cd;
257 }
258 sysfs_free(mdi);
259
260
261 if (container->ss->load_super(container, mdfd, argv[1])) {
262 fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
263 argv[1]);
264 exit(3);
265 }
266
267
268 mlockall(MCL_FUTURE);
269
270 if (clone_monitor(container) < 0) {
271 fprintf(stderr, "md-manage: failed to start monitor process: %s\n",
272 strerror(errno));
273 exit(2);
274 }
275
276 do_manager(container);
277
278 exit(0);
279 }