]>
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.
30 #include <sys/socket.h>
44 struct active_array
*discard_this
;
45 struct active_array
*pending_discard
;
46 struct md_generic_cmd
*active_cmd
;
48 int run_child(void *v
)
50 struct supertype
*c
= v
;
52 /* SIGUSR is sent from child to parent, So child must block it */
54 sigaddset(&set
, SIGUSR1
);
55 sigprocmask(SIG_BLOCK
, &set
, NULL
);
61 int clone_monitor(struct supertype
*container
)
63 static char stack
[4096];
66 rv
= pipe(container
->mgr_pipe
);
69 rv
= pipe(container
->mon_pipe
);
73 rv
= clone(run_child
, stack
+4096-64,
74 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
82 close(container
->mon_pipe
[0]);
83 close(container
->mon_pipe
[1]);
85 close(container
->mgr_pipe
[0]);
86 close(container
->mgr_pipe
[1]);
91 static struct superswitch
*find_metadata_methods(char *vers
)
93 if (strcmp(vers
, "ddf") == 0)
95 if (strcmp(vers
, "imsm") == 0)
101 static int make_pidfile(char *devname
, int o_excl
)
106 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
108 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
111 sprintf(pid
, "%d\n", getpid());
112 write(fd
, pid
, strlen(pid
));
117 static void try_kill_monitor(char *devname
)
123 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
124 fd
= open(buf
, O_RDONLY
);
128 if (read(fd
, buf
, sizeof(buf
)) < 0) {
134 pid
= strtoul(buf
, NULL
, 10);
136 /* kill this process if it is mdmon */
137 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
138 fd
= open(buf
, O_RDONLY
);
142 if (read(fd
, buf
, sizeof(buf
)) < 0) {
147 if (strstr(buf
, "mdmon") != NULL
)
151 void remove_pidfile(char *devname
)
155 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
159 static int make_control_sock(char *devname
)
164 struct sockaddr_un addr
;
166 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
168 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
172 addr
.sun_family
= PF_LOCAL
;
173 strcpy(addr
.sun_path
, path
);
174 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
179 fl
= fcntl(sfd
, F_GETFL
, 0);
181 fcntl(sfd
, F_SETFL
, fl
);
185 int main(int argc
, char *argv
[])
188 struct mdinfo
*mdi
, *di
;
189 struct supertype
*container
;
191 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
194 mdfd
= open(argv
[1], O_RDWR
);
196 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
200 if (md_get_version(mdfd
) < 0) {
201 fprintf(stderr
, "md-manage: %s: Not an md device\n",
206 /* hopefully it is a container - we'll check later */
208 container
= malloc(sizeof(*container
));
209 container
->devnum
= fd2devnum(mdfd
);
210 container
->devname
= devnum2devname(container
->devnum
);
211 container
->device_name
= argv
[1];
213 /* If this fails, we hope it already exists */
214 mkdir("/var/run/mdadm", 0600);
215 /* pid file lives in /var/run/mdadm/mdXX.pid */
216 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
217 if (ping_monitor(container
->devname
) == 0) {
218 fprintf(stderr
, "mdmon: %s already managed\n",
222 /* cleanup the old monitor, this one is taking over */
223 try_kill_monitor(container
->devname
);
224 if (make_pidfile(container
->devname
, 0) < 0) {
225 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
232 container
->sock
= make_control_sock(container
->devname
);
233 if (container
->sock
< 0) {
234 fprintf(stderr
, "mdmon: Cannot create socket in /var/run/mdadm\n");
237 container
->arrays
= NULL
;
239 mdi
= sysfs_read(mdfd
, container
->devnum
,
240 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
243 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
247 if (mdi
->array
.level
!= UnSet
) {
248 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
252 if (mdi
->array
.major_version
!= -1 ||
253 mdi
->array
.minor_version
!= -2) {
254 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
259 container
->ss
= find_metadata_methods(mdi
->text_version
);
260 if (container
->ss
== NULL
) {
261 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
262 argv
[1], mdi
->text_version
);
266 container
->devs
= NULL
;
267 for (di
= mdi
->devs
; di
; di
= di
->next
) {
268 struct mdinfo
*cd
= malloc(sizeof(*cd
));
270 cd
->next
= container
->devs
;
271 container
->devs
= cd
;
276 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
277 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
284 mlockall(MCL_FUTURE
);
286 if (clone_monitor(container
) < 0) {
287 fprintf(stderr
, "md-manage: failed to start monitor process: %s\n",
292 do_manager(container
);