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