]>
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
;
52 int run_child(void *v
)
54 struct supertype
*c
= v
;
60 int clone_monitor(struct supertype
*container
)
62 static char stack
[4096];
64 mon_tid
= clone(run_child
, stack
+4096-64,
65 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
68 mgr_tid
= syscall(SYS_gettid
);
73 static struct superswitch
*find_metadata_methods(char *vers
)
75 if (strcmp(vers
, "ddf") == 0)
77 if (strcmp(vers
, "imsm") == 0)
83 static int make_pidfile(char *devname
, int o_excl
)
88 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
90 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
93 sprintf(pid
, "%d\n", getpid());
94 write(fd
, pid
, strlen(pid
));
99 static void try_kill_monitor(char *devname
)
105 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
106 fd
= open(buf
, O_RDONLY
);
110 if (read(fd
, buf
, sizeof(buf
)) < 0) {
116 pid
= strtoul(buf
, NULL
, 10);
118 /* kill this process if it is mdmon */
119 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
120 fd
= open(buf
, O_RDONLY
);
124 if (read(fd
, buf
, sizeof(buf
)) < 0) {
129 if (strstr(buf
, "mdmon") != NULL
)
133 void remove_pidfile(char *devname
)
137 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
141 static int make_control_sock(char *devname
)
146 struct sockaddr_un addr
;
148 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
150 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
154 addr
.sun_family
= PF_LOCAL
;
155 strcpy(addr
.sun_path
, path
);
156 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
161 fl
= fcntl(sfd
, F_GETFL
, 0);
163 fcntl(sfd
, F_SETFL
, fl
);
167 static void wake_me(int sig
)
172 /* if we are debugging and starting mdmon by hand then don't fork */
173 static int do_fork(void)
185 int main(int argc
, char *argv
[])
188 struct mdinfo
*mdi
, *di
;
189 struct supertype
*container
;
191 struct sigaction act
;
196 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
199 mdfd
= open(argv
[1], O_RDWR
);
201 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
205 if (md_get_version(mdfd
) < 0) {
206 fprintf(stderr
, "md-manage: %s: Not an md device\n",
211 /* Fork, and have the child tell us when they are ready */
216 fprintf(stderr
, "mdmon: failed to fork: %s\n",
222 default: /* parent */
224 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
226 status
= WEXITSTATUS(status
);
231 pfd
[0] = pfd
[1] = -1;
232 /* hopefully it is a container - we'll check later */
234 container
= malloc(sizeof(*container
));
235 container
->devnum
= fd2devnum(mdfd
);
236 container
->devname
= devnum2devname(container
->devnum
);
237 container
->device_name
= argv
[1];
239 /* If this fails, we hope it already exists */
240 mkdir("/var/run/mdadm", 0600);
241 /* pid file lives in /var/run/mdadm/mdXX.pid */
242 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
243 if (ping_monitor(container
->devname
) == 0) {
244 fprintf(stderr
, "mdmon: %s already managed\n",
248 /* cleanup the old monitor, this one is taking over */
249 try_kill_monitor(container
->devname
);
250 if (make_pidfile(container
->devname
, 0) < 0) {
251 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
258 container
->sock
= make_control_sock(container
->devname
);
259 if (container
->sock
< 0) {
260 fprintf(stderr
, "mdmon: Cannot create socket in /var/run/mdadm\n");
263 container
->arrays
= NULL
;
265 mdi
= sysfs_read(mdfd
, container
->devnum
,
266 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
269 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
273 if (mdi
->array
.level
!= UnSet
) {
274 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
278 if (mdi
->array
.major_version
!= -1 ||
279 mdi
->array
.minor_version
!= -2) {
280 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
285 container
->ss
= find_metadata_methods(mdi
->text_version
);
286 if (container
->ss
== NULL
) {
287 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
288 argv
[1], mdi
->text_version
);
292 container
->devs
= NULL
;
293 for (di
= mdi
->devs
; di
; di
= di
->next
) {
294 struct mdinfo
*cd
= malloc(sizeof(*cd
));
296 cd
->next
= container
->devs
;
297 container
->devs
= cd
;
302 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
303 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
308 /* Ok, this is close enough. We can say goodbye to our parent now.
311 write(pfd
[1], &status
, sizeof(status
));
317 open("/dev/null", O_RDWR
);
325 mlockall(MCL_FUTURE
);
327 /* SIGUSR is sent between parent and child. So both block it
328 * and enable it only with pselect.
331 sigaddset(&set
, SIGUSR1
);
332 sigprocmask(SIG_BLOCK
, &set
, NULL
);
333 act
.sa_handler
= wake_me
;
335 sigaction(SIGUSR1
, &act
, NULL
);
336 act
.sa_handler
= SIG_IGN
;
337 sigaction(SIGPIPE
, &act
, NULL
);
339 if (clone_monitor(container
) < 0) {
340 fprintf(stderr
, "md-manage: failed to start monitor process: %s\n",
345 do_manager(container
);