]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
Remove mon_pipe for communicating from monitor to manager
[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 *discard_this;
45 struct active_array *pending_discard;
46 struct md_generic_cmd *active_cmd;
47
48 int run_child(void *v)
49 {
50 struct supertype *c = v;
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
57 do_monitor(c);
58 return 0;
59 }
60
61 int clone_monitor(struct supertype *container)
62 {
63 static char stack[4096];
64 int rv;
65
66 rv = pipe(container->mgr_pipe);
67 if (rv < 0)
68 return rv;
69
70 rv = clone(run_child, stack+4096-64,
71 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
72 container);
73 if (rv < 0)
74 goto err_clone;
75 else
76 return rv;
77
78 err_clone:
79 close(container->mgr_pipe[0]);
80 close(container->mgr_pipe[1]);
81
82 return rv;
83 }
84
85 static struct superswitch *find_metadata_methods(char *vers)
86 {
87 if (strcmp(vers, "ddf") == 0)
88 return &super_ddf;
89 if (strcmp(vers, "imsm") == 0)
90 return &super_imsm;
91 return NULL;
92 }
93
94
95 static int make_pidfile(char *devname, int o_excl)
96 {
97 char path[100];
98 char pid[10];
99 int fd;
100 sprintf(path, "/var/run/mdadm/%s.pid", devname);
101
102 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
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
111 static 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
145 void remove_pidfile(char *devname)
146 {
147 char buf[100];
148
149 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
150 unlink(buf);
151 }
152
153 static 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
179 int main(int argc, char *argv[])
180 {
181 int mdfd;
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));
203 container->devnum = fd2devnum(mdfd);
204 container->devname = devnum2devname(container->devnum);
205 container->device_name = argv[1];
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 */
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 }
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 }
275 close(mdfd);
276 close(mdfd);
277
278 mlockall(MCL_FUTURE);
279
280 if (clone_monitor(container) < 0) {
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 }