int fd;
int n;
+ if (mkdir(pid_dir, 0600) < 0 &&
+ errno != EEXIST)
+ return -errno;
sprintf(path, "%s/%s.pid", pid_dir, devname);
fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
- if (takeover) {
- pid_dir = VAR_RUN;
+ pid_dir = VAR_RUN;
+ victim = mdmon_pid(container->devnum);
+ if (victim < 0) {
+ pid_dir = ALT_RUN;
victim = mdmon_pid(container->devnum);
- if (victim < 0) {
- pid_dir = ALT_RUN;
- victim = mdmon_pid(container->devnum);
- }
- if (victim >= 0)
- victim_sock = connect_monitor(container->devname);
}
+ if (victim >= 0)
+ victim_sock = connect_monitor(container->devname);
ignore = chdir("/");
- if (victim < 0) {
- if (ping_monitor(container->devname) == 0) {
+ if (!takeover && victim > 0 && victim_sock >= 0) {
+ if (fping_monitor(victim_sock) == 0) {
fprintf(stderr, "mdmon: %s already managed\n",
container->devname);
exit(3);
}
- /* if there is a pid file, kill whoever is there just in case */
- victim = mdmon_pid(container->devnum);
+ close(victim_sock);
}
if (container->ss->load_super(container, mdfd, devname)) {
fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
*/
if (victim > 0)
remove_pidfile(devname);
- if (mkdir(VAR_RUN, 0600) >= 0 || errno == EEXIST)
- pid_dir = VAR_RUN;
- else if (mkdir(ALT_RUN, 0600) >= 0 || errno == EEXIST)
- pid_dir = ALT_RUN;
- else {
- fprintf(stderr, "mdmon: Neither %s nor %s are writable\n"
- " cannot create .pid or .sock files. Aborting\n",
- VAR_RUN, ALT_RUN);
- exit(3);
- }
+ pid_dir = VAR_RUN;
if (make_pidfile(devname) < 0) {
- fprintf(stderr, "mdmon: Cannot create pid file in %s - aborting.\n",
- pid_dir);
- exit(3);
+ /* Try the alternate */
+ pid_dir = ALT_RUN;
+ if (make_pidfile(devname) < 0) {
+ fprintf(stderr, "mdmon: Neither %s nor %s are writable\n"
+ " cannot create .pid or .sock files. Aborting\n",
+ VAR_RUN, ALT_RUN);
+ exit(3);
+ }
}
container->sock = make_control_sock(devname);