]>
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
*array_list
;
45 struct active_array
*discard_this
;
46 struct active_array
*pending_discard
;
48 int run_child(void *v
)
50 struct supertype
*c
= v
;
55 int clone_monitor(struct supertype
*container
)
58 static char stack
[4096];
61 pipe(container
->pipe
);
63 rv
= clone(run_child
, stack
+4096-64,
64 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
72 static struct superswitch
*find_metadata_methods(char *vers
)
74 if (strcmp(vers
, "ddf") == 0)
80 static int make_pidfile(char *devname
, int o_excl
)
85 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
87 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
90 sprintf(pid
, "%d\n", getpid());
91 write(fd
, pid
, strlen(pid
));
96 static void try_kill_monitor(char *devname
)
102 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
103 fd
= open(buf
, O_RDONLY
);
107 if (read(fd
, buf
, sizeof(buf
)) < 0) {
113 pid
= strtoul(buf
, NULL
, 10);
115 /* kill this process if it is mdmon */
116 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
117 fd
= open(buf
, O_RDONLY
);
121 if (read(fd
, buf
, sizeof(buf
)) < 0) {
126 if (strstr(buf
, "mdmon") != NULL
)
130 static int make_control_sock(char *devname
)
135 struct sockaddr_un addr
;
137 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
139 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
143 addr
.sun_family
= PF_LOCAL
;
144 strcpy(addr
.sun_path
, path
);
145 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
150 fl
= fcntl(sfd
, F_GETFL
, 0);
152 fcntl(sfd
, F_SETFL
, fl
);
156 int main(int argc
, char *argv
[])
160 struct mdinfo
*mdi
, *di
;
161 struct supertype
*container
;
163 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
166 mdfd
= open(argv
[1], O_RDWR
);
168 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
172 if (md_get_version(mdfd
) < 0) {
173 fprintf(stderr
, "md-manage: %s: Not an md device\n",
178 /* hopefully it is a container - we'll check later */
180 container
= malloc(sizeof(*container
));
181 container
->devfd
= mdfd
;
182 container
->devnum
= fd2devnum(mdfd
);
183 container
->devname
= devnum2devname(container
->devnum
);
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 */
188 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
189 if (ping_monitor(container
->devname
) == 0) {
190 fprintf(stderr
, "mdmon: %s already managed\n",
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",
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");
209 container
->arrays
= NULL
;
211 mdi
= sysfs_read(mdfd
, container
->devnum
,
212 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
215 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
219 if (mdi
->array
.level
!= UnSet
) {
220 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
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",
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
);
238 container
->devs
= NULL
;
239 for (di
= mdi
->devs
; di
; di
= di
->next
) {
240 struct mdinfo
*cd
= malloc(sizeof(*cd
));
242 cd
->next
= container
->devs
;
243 container
->devs
= cd
;
248 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
249 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
255 mlockall(MCL_FUTURE
);
257 pipefd
= clone_monitor(container
);
259 fprintf(stderr
, "md-manage: failed to start monitor process: %s\n",
264 do_manager(container
);