]>
Commit | Line | Data |
---|---|---|
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 |
44 | struct active_array *discard_this; |
45 | struct active_array *pending_discard; | |
3e70c845 | 46 | struct md_generic_cmd *active_cmd; |
549e9569 NB |
47 | |
48 | int 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 | ||
61 | int 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 | ||
91 | static 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 | 99 | static 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 |
115 | static 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 | ||
e0d6609f NB |
149 | void remove_pidfile(char *devname) |
150 | { | |
151 | char buf[100]; | |
152 | ||
153 | sprintf(buf, "/var/run/mdadm/%s.pid", devname); | |
154 | unlink(buf); | |
155 | } | |
156 | ||
549e9569 NB |
157 | static int make_control_sock(char *devname) |
158 | { | |
159 | char path[100]; | |
160 | int sfd; | |
161 | long fl; | |
162 | struct sockaddr_un addr; | |
163 | ||
164 | sprintf(path, "/var/run/mdadm/%s.sock", devname); | |
165 | unlink(path); | |
166 | sfd = socket(PF_LOCAL, SOCK_STREAM, 0); | |
167 | if (sfd < 0) | |
168 | return -1; | |
169 | ||
170 | addr.sun_family = PF_LOCAL; | |
171 | strcpy(addr.sun_path, path); | |
172 | if (bind(sfd, &addr, sizeof(addr)) < 0) { | |
173 | close(sfd); | |
174 | return -1; | |
175 | } | |
176 | listen(sfd, 10); | |
177 | fl = fcntl(sfd, F_GETFL, 0); | |
178 | fl |= O_NONBLOCK; | |
179 | fcntl(sfd, F_SETFL, fl); | |
180 | return sfd; | |
181 | } | |
182 | ||
183 | int main(int argc, char *argv[]) | |
184 | { | |
185 | int mdfd; | |
549e9569 NB |
186 | struct mdinfo *mdi, *di; |
187 | struct supertype *container; | |
188 | if (argc != 2) { | |
189 | fprintf(stderr, "Usage: md-manage /device/name/for/container\n"); | |
190 | exit(2); | |
191 | } | |
192 | mdfd = open(argv[1], O_RDWR); | |
193 | if (mdfd < 0) { | |
194 | fprintf(stderr, "md-manage: %s: %s\n", argv[1], | |
195 | strerror(errno)); | |
196 | exit(1); | |
197 | } | |
198 | if (md_get_version(mdfd) < 0) { | |
199 | fprintf(stderr, "md-manage: %s: Not an md device\n", | |
200 | argv[1]); | |
201 | exit(1); | |
202 | } | |
203 | ||
204 | /* hopefully it is a container - we'll check later */ | |
205 | ||
206 | container = malloc(sizeof(*container)); | |
549e9569 NB |
207 | container->devnum = fd2devnum(mdfd); |
208 | container->devname = devnum2devname(container->devnum); | |
e0d6609f | 209 | container->device_name = argv[1]; |
549e9569 NB |
210 | |
211 | /* If this fails, we hope it already exists */ | |
212 | mkdir("/var/run/mdadm", 0600); | |
213 | /* pid file lives in /var/run/mdadm/mdXX.pid */ | |
b109d928 DW |
214 | if (make_pidfile(container->devname, O_EXCL) < 0) { |
215 | if (ping_monitor(container->devname) == 0) { | |
216 | fprintf(stderr, "mdmon: %s already managed\n", | |
217 | container->devname); | |
218 | exit(3); | |
219 | } else { | |
220 | /* cleanup the old monitor, this one is taking over */ | |
221 | try_kill_monitor(container->devname); | |
222 | if (make_pidfile(container->devname, 0) < 0) { | |
223 | fprintf(stderr, "mdmon: %s Cannot create pidfile\n", | |
224 | container->devname); | |
225 | exit(3); | |
226 | } | |
227 | } | |
549e9569 NB |
228 | } |
229 | ||
230 | container->sock = make_control_sock(container->devname); | |
231 | if (container->sock < 0) { | |
232 | fprintf(stderr, "mdmon: Cannot create socket in /var/run/mdadm\n"); | |
233 | exit(3); | |
234 | } | |
235 | container->arrays = NULL; | |
236 | ||
237 | mdi = sysfs_read(mdfd, container->devnum, | |
238 | GET_VERSION|GET_LEVEL|GET_DEVS); | |
239 | ||
240 | if (!mdi) { | |
241 | fprintf(stderr, "mdmon: failed to load sysfs info for %s\n", | |
242 | container->devname); | |
243 | exit(3); | |
244 | } | |
245 | if (mdi->array.level != UnSet) { | |
246 | fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n", | |
247 | argv[1]); | |
248 | exit(3); | |
249 | } | |
250 | if (mdi->array.major_version != -1 || | |
251 | mdi->array.minor_version != -2) { | |
252 | fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n", | |
253 | argv[1]); | |
254 | exit(3); | |
255 | } | |
256 | ||
257 | container->ss = find_metadata_methods(mdi->text_version); | |
258 | if (container->ss == NULL) { | |
259 | fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n", | |
260 | argv[1], mdi->text_version); | |
261 | exit(3); | |
262 | } | |
263 | ||
264 | container->devs = NULL; | |
265 | for (di = mdi->devs; di; di = di->next) { | |
266 | struct mdinfo *cd = malloc(sizeof(*cd)); | |
267 | cd = di; | |
268 | cd->next = container->devs; | |
269 | container->devs = cd; | |
270 | } | |
271 | sysfs_free(mdi); | |
272 | ||
273 | ||
274 | if (container->ss->load_super(container, mdfd, argv[1])) { | |
275 | fprintf(stderr, "mdmon: Cannot load metadata for %s\n", | |
276 | argv[1]); | |
277 | exit(3); | |
278 | } | |
e0d6609f | 279 | close(mdfd); |
5869a76c | 280 | close(mdfd); |
549e9569 NB |
281 | |
282 | mlockall(MCL_FUTURE); | |
283 | ||
3e70c845 | 284 | if (clone_monitor(container) < 0) { |
549e9569 NB |
285 | fprintf(stderr, "md-manage: failed to start monitor process: %s\n", |
286 | strerror(errno)); | |
287 | exit(2); | |
288 | } | |
289 | ||
290 | do_manager(container); | |
291 | ||
292 | exit(0); | |
293 | } |