]>
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>
46 struct active_array
*discard_this
;
47 struct active_array
*pending_discard
;
51 int run_child(void *v
)
53 struct supertype
*c
= v
;
55 mon_tid
= syscall(SYS_gettid
);
60 int clone_monitor(struct supertype
*container
)
62 static char stack
[4096];
66 rv
= 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 static int make_pidfile(char *devname
, int o_excl
)
90 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
92 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
95 sprintf(pid
, "%d\n", getpid());
96 write(fd
, pid
, strlen(pid
));
101 static void try_kill_monitor(char *devname
)
107 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
108 fd
= open(buf
, O_RDONLY
);
112 if (read(fd
, buf
, sizeof(buf
)) < 0) {
118 pid
= strtoul(buf
, NULL
, 10);
120 /* kill this process if it is mdmon */
121 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
122 fd
= open(buf
, O_RDONLY
);
126 if (read(fd
, buf
, sizeof(buf
)) < 0) {
131 if (strstr(buf
, "mdmon") != NULL
)
135 void remove_pidfile(char *devname
)
139 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
143 static int make_control_sock(char *devname
)
148 struct sockaddr_un addr
;
150 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
152 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
156 addr
.sun_family
= PF_LOCAL
;
157 strcpy(addr
.sun_path
, path
);
158 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
163 fl
= fcntl(sfd
, F_GETFL
, 0);
165 fcntl(sfd
, F_SETFL
, fl
);
169 static void wake_me(int sig
)
174 int main(int argc
, char *argv
[])
177 struct mdinfo
*mdi
, *di
;
178 struct supertype
*container
;
180 struct sigaction act
;
183 fprintf(stderr
, "Usage: md-manage /device/name/for/container\n");
186 mdfd
= open(argv
[1], O_RDWR
);
188 fprintf(stderr
, "md-manage: %s: %s\n", argv
[1],
192 if (md_get_version(mdfd
) < 0) {
193 fprintf(stderr
, "md-manage: %s: Not an md device\n",
198 /* hopefully it is a container - we'll check later */
200 container
= malloc(sizeof(*container
));
201 container
->devnum
= fd2devnum(mdfd
);
202 container
->devname
= devnum2devname(container
->devnum
);
203 container
->device_name
= argv
[1];
205 /* If this fails, we hope it already exists */
206 mkdir("/var/run/mdadm", 0600);
207 /* pid file lives in /var/run/mdadm/mdXX.pid */
208 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
209 if (ping_monitor(container
->devname
) == 0) {
210 fprintf(stderr
, "mdmon: %s already managed\n",
214 /* cleanup the old monitor, this one is taking over */
215 try_kill_monitor(container
->devname
);
216 if (make_pidfile(container
->devname
, 0) < 0) {
217 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
224 container
->sock
= make_control_sock(container
->devname
);
225 if (container
->sock
< 0) {
226 fprintf(stderr
, "mdmon: Cannot create socket in /var/run/mdadm\n");
229 container
->arrays
= NULL
;
231 mdi
= sysfs_read(mdfd
, container
->devnum
,
232 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
235 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
239 if (mdi
->array
.level
!= UnSet
) {
240 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
244 if (mdi
->array
.major_version
!= -1 ||
245 mdi
->array
.minor_version
!= -2) {
246 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
251 container
->ss
= find_metadata_methods(mdi
->text_version
);
252 if (container
->ss
== NULL
) {
253 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
254 argv
[1], mdi
->text_version
);
258 container
->devs
= NULL
;
259 for (di
= mdi
->devs
; di
; di
= di
->next
) {
260 struct mdinfo
*cd
= malloc(sizeof(*cd
));
262 cd
->next
= container
->devs
;
263 container
->devs
= cd
;
268 if (container
->ss
->load_super(container
, mdfd
, argv
[1])) {
269 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
274 mlockall(MCL_FUTURE
);
276 /* SIGUSR is sent between parent and child. So both block it
277 * and enable it only with pselect.
280 sigaddset(&set
, SIGUSR1
);
281 sigprocmask(SIG_BLOCK
, &set
, NULL
);
282 act
.sa_handler
= wake_me
;
284 sigaction(SIGUSR1
, &act
, NULL
);
285 act
.sa_handler
= SIG_IGN
;
286 sigaction(SIGPIPE
, &act
, NULL
);
288 if (clone_monitor(container
) < 0) {
289 fprintf(stderr
, "md-manage: failed to start monitor process: %s\n",
294 do_manager(container
);