]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
4 * When md arrays have user-space managed metadata, this is the program
5 * that does the managing.
7 * Given one argument: the name of the array (e.g. /dev/md0) that is
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.
15 * The main thread communicates with the priority thread by writing over
17 * Separate programs can communicate with the main thread via Unix-domain
19 * The two threads share address space and open file table.
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <sys/syscall.h>
47 struct active_array
*discard_this
;
48 struct active_array
*pending_discard
;
54 int run_child(void *v
)
56 struct supertype
*c
= v
;
62 int clone_monitor(struct supertype
*container
)
64 static char stack
[4096];
66 mon_tid
= clone(run_child
, stack
+4096-64,
67 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
70 mgr_tid
= syscall(SYS_gettid
);
75 static struct superswitch
*find_metadata_methods(char *vers
)
77 if (strcmp(vers
, "ddf") == 0)
79 if (strcmp(vers
, "imsm") == 0)
85 int make_pidfile(char *devname
, int o_excl
)
95 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
97 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
100 sprintf(pid
, "%d\n", getpid());
101 n
= write(fd
, pid
, strlen(pid
));
108 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
110 if (mdstat
->metadata_version
== NULL
||
111 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
112 !is_subarray(mdstat
->metadata_version
+9) ||
113 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
114 mdstat
->metadata_version
[10+strlen(container
)] != '/')
120 void remove_pidfile(char *devname
);
121 static void try_kill_monitor(char *devname
)
126 struct mdstat_ent
*mdstat
;
128 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
129 fd
= open(buf
, O_RDONLY
);
133 if (read(fd
, buf
, sizeof(buf
)) < 0) {
139 pid
= strtoul(buf
, NULL
, 10);
141 /* kill this process if it is mdmon */
142 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
143 fd
= open(buf
, O_RDONLY
);
147 if (read(fd
, buf
, sizeof(buf
)) < 0) {
152 if (!strstr(buf
, "mdmon"))
157 mdstat
= mdstat_read(0, 0);
158 for ( ; mdstat
; mdstat
= mdstat
->next
)
159 if (is_container_member(mdstat
, devname
)) {
160 sprintf(buf
, "/dev/%s", mdstat
->dev
);
164 remove_pidfile(devname
);
167 void remove_pidfile(char *devname
)
174 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
176 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
180 int make_control_sock(char *devname
)
185 struct sockaddr_un addr
;
190 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
192 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
196 addr
.sun_family
= PF_LOCAL
;
197 strcpy(addr
.sun_path
, path
);
198 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
203 fl
= fcntl(sfd
, F_GETFL
, 0);
205 fcntl(sfd
, F_SETFL
, fl
);
209 int socket_hup_requested
;
210 static void hup(int sig
)
212 socket_hup_requested
= 1;
215 static void term(int sig
)
220 static void wake_me(int sig
)
225 /* if we are debugging and starting mdmon by hand then don't fork */
226 static int do_fork(void)
238 int main(int argc
, char *argv
[])
241 struct mdinfo
*mdi
, *di
;
242 struct supertype
*container
;
244 struct sigaction act
;
250 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
253 mdfd
= open(argv
[1], O_RDWR
);
255 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
259 if (md_get_version(mdfd
) < 0) {
260 fprintf(stderr
, "md-manage: %s: Not an md device\n",
265 /* Fork, and have the child tell us when they are ready */
267 if (pipe(pfd
) != 0) {
268 fprintf(stderr
, "mdmon: failed to create pipe\n");
273 fprintf(stderr
, "mdmon: failed to fork: %s\n",
279 default: /* parent */
281 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
283 status
= WEXITSTATUS(status
);
288 pfd
[0] = pfd
[1] = -1;
289 /* hopefully it is a container - we'll check later */
291 container
= malloc(sizeof(*container
));
292 container
->devnum
= fd2devnum(mdfd
);
293 container
->devname
= devnum2devname(container
->devnum
);
294 container
->device_name
= argv
[1];
296 /* SIGUSR is sent between parent and child. So both block it
297 * and enable it only with pselect.
300 sigaddset(&set
, SIGUSR1
);
301 sigaddset(&set
, SIGHUP
);
302 sigaddset(&set
, SIGALRM
);
303 sigaddset(&set
, SIGTERM
);
304 sigprocmask(SIG_BLOCK
, &set
, NULL
);
305 act
.sa_handler
= wake_me
;
307 sigaction(SIGUSR1
, &act
, NULL
);
308 sigaction(SIGALRM
, &act
, NULL
);
309 act
.sa_handler
= hup
;
310 sigaction(SIGHUP
, &act
, NULL
);
311 act
.sa_handler
= term
;
312 sigaction(SIGTERM
, &act
, NULL
);
313 act
.sa_handler
= SIG_IGN
;
314 sigaction(SIGPIPE
, &act
, NULL
);
316 /* If this fails, we hope it already exists */
317 mkdir("/var/run/mdadm", 0600);
318 /* pid file lives in /var/run/mdadm/mdXX.pid */
319 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
320 if (ping_monitor(container
->devname
) == 0) {
321 fprintf(stderr
, "mdmon: %s already managed\n",
327 /* cleanup the old monitor, this one is taking over */
328 try_kill_monitor(container
->devname
);
329 err
= make_pidfile(container
->devname
, 0);
331 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
334 /* FIXME implement a mechanism to
335 * prevent duplicate monitor instances
338 "mdmon: continuing on read-only file system\n");
345 container
->sock
= make_control_sock(container
->devname
);
346 container
->arrays
= NULL
;
348 mdi
= sysfs_read(mdfd
, container
->devnum
,
349 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
352 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
356 if (mdi
->array
.level
!= UnSet
) {
357 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
361 if (mdi
->array
.major_version
!= -1 ||
362 mdi
->array
.minor_version
!= -2) {
363 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
368 container
->ss
= find_metadata_methods(mdi
->text_version
);
369 if (container
->ss
== NULL
) {
370 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
371 argv
[1], mdi
->text_version
);
375 container
->devs
= NULL
;
376 for (di
= mdi
->devs
; di
; di
= di
->next
) {
377 struct mdinfo
*cd
= malloc(sizeof(*cd
));
379 cd
->next
= container
->devs
;
380 container
->devs
= cd
;
385 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
386 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
391 /* Ok, this is close enough. We can say goodbye to our parent now.
394 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
395 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
402 open("/dev/null", O_RDWR
);
410 mlockall(MCL_FUTURE
);
412 if (clone_monitor(container
) < 0) {
413 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
418 do_manager(container
);