]>
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 static void try_kill_monitor(char *devname
)
114 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
115 fd
= open(buf
, O_RDONLY
);
119 if (read(fd
, buf
, sizeof(buf
)) < 0) {
125 pid
= strtoul(buf
, NULL
, 10);
127 /* kill this process if it is mdmon */
128 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
129 fd
= open(buf
, O_RDONLY
);
133 if (read(fd
, buf
, sizeof(buf
)) < 0) {
138 if (strstr(buf
, "mdmon") != NULL
)
142 void remove_pidfile(char *devname
)
149 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
151 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
155 int make_control_sock(char *devname
)
160 struct sockaddr_un addr
;
165 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
167 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
171 addr
.sun_family
= PF_LOCAL
;
172 strcpy(addr
.sun_path
, path
);
173 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
178 fl
= fcntl(sfd
, F_GETFL
, 0);
180 fcntl(sfd
, F_SETFL
, fl
);
184 int socket_hup_requested
;
185 static void hup(int sig
)
187 socket_hup_requested
= 1;
190 static void term(int sig
)
195 static void wake_me(int sig
)
200 /* if we are debugging and starting mdmon by hand then don't fork */
201 static int do_fork(void)
213 int main(int argc
, char *argv
[])
216 struct mdinfo
*mdi
, *di
;
217 struct supertype
*container
;
219 struct sigaction act
;
225 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
228 mdfd
= open(argv
[1], O_RDWR
);
230 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
234 if (md_get_version(mdfd
) < 0) {
235 fprintf(stderr
, "md-manage: %s: Not an md device\n",
240 /* Fork, and have the child tell us when they are ready */
242 if (pipe(pfd
) != 0) {
243 fprintf(stderr
, "mdmon: failed to create pipe\n");
248 fprintf(stderr
, "mdmon: failed to fork: %s\n",
254 default: /* parent */
256 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
258 status
= WEXITSTATUS(status
);
263 pfd
[0] = pfd
[1] = -1;
264 /* hopefully it is a container - we'll check later */
266 container
= malloc(sizeof(*container
));
267 container
->devnum
= fd2devnum(mdfd
);
268 container
->devname
= devnum2devname(container
->devnum
);
269 container
->device_name
= argv
[1];
271 /* If this fails, we hope it already exists */
272 mkdir("/var/run/mdadm", 0600);
273 /* pid file lives in /var/run/mdadm/mdXX.pid */
274 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
275 if (ping_monitor(container
->devname
) == 0) {
276 fprintf(stderr
, "mdmon: %s already managed\n",
282 /* cleanup the old monitor, this one is taking over */
283 try_kill_monitor(container
->devname
);
284 err
= make_pidfile(container
->devname
, 0);
286 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
289 /* FIXME implement a mechanism to
290 * prevent duplicate monitor instances
293 "mdmon: continuing on read-only file system\n");
300 container
->sock
= make_control_sock(container
->devname
);
301 container
->arrays
= NULL
;
303 mdi
= sysfs_read(mdfd
, container
->devnum
,
304 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
307 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
311 if (mdi
->array
.level
!= UnSet
) {
312 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
316 if (mdi
->array
.major_version
!= -1 ||
317 mdi
->array
.minor_version
!= -2) {
318 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
323 container
->ss
= find_metadata_methods(mdi
->text_version
);
324 if (container
->ss
== NULL
) {
325 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
326 argv
[1], mdi
->text_version
);
330 container
->devs
= NULL
;
331 for (di
= mdi
->devs
; di
; di
= di
->next
) {
332 struct mdinfo
*cd
= malloc(sizeof(*cd
));
334 cd
->next
= container
->devs
;
335 container
->devs
= cd
;
340 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
341 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
346 /* Ok, this is close enough. We can say goodbye to our parent now.
349 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
350 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
357 open("/dev/null", O_RDWR
);
365 mlockall(MCL_FUTURE
);
367 /* SIGUSR is sent between parent and child. So both block it
368 * and enable it only with pselect.
371 sigaddset(&set
, SIGUSR1
);
372 sigaddset(&set
, SIGHUP
);
373 sigaddset(&set
, SIGALRM
);
374 sigaddset(&set
, SIGTERM
);
375 sigprocmask(SIG_BLOCK
, &set
, NULL
);
376 act
.sa_handler
= wake_me
;
378 sigaction(SIGUSR1
, &act
, NULL
);
379 sigaction(SIGALRM
, &act
, NULL
);
380 act
.sa_handler
= hup
;
381 sigaction(SIGHUP
, &act
, NULL
);
382 act
.sa_handler
= term
;
383 sigaction(SIGTERM
, &act
, NULL
);
384 act
.sa_handler
= SIG_IGN
;
385 sigaction(SIGPIPE
, &act
, NULL
);
387 if (clone_monitor(container
) < 0) {
388 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
393 do_manager(container
);