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