start fleshing out socket code, ping monitor to see if it is alive

From: Dan Williams <dan.j.williams@intel.com>

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
This commit is contained in:
Dan Williams 2008-05-15 16:48:52 +10:00 committed by Neil Brown
parent 0af73f61a2
commit b109d92863
4 changed files with 83 additions and 9 deletions

View File

@ -79,7 +79,7 @@ SRCS = mdadm.c config.c mdstat.c ReadMe.c util.c Manage.c Assemble.c Build.c \
MON_OBJS = mdmon.o monitor.o managemon.o util.o mdstat.o sysfs.o config.o \
Kill.o sg_io.o dlink.o ReadMe.o super0.o super1.o super-intel.o \
super-ddf.o sha1.o crc32.o
super-ddf.o sha1.o crc32.o msg.o
STATICSRC = pwgr.c
@ -125,6 +125,7 @@ mdadm.O2 : $(SRCS) mdadm.h
mdmon : $(MON_OBJS)
$(CC) $(LDFLAGS) -o mdmon $(MON_OBJS) $(LDLIBS)
msg.o: msg.c msg.h
test_stripe : restripe.c mdadm.h
$(CC) $(CXFLAGS) $(LDFLAGS) -o test_stripe -DMAIN restripe.c

View File

@ -74,6 +74,7 @@
#endif
#include "mdadm.h"
#include "mdmon.h"
#include "msg.h"
#include <sys/socket.h>
@ -302,12 +303,35 @@ void manage(struct mdstat_ent *mdstat, struct active_array *aa,
void read_sock(int pfd)
{
int fd;
struct md_message msg;
int terminate = 0;
long fl;
int tmo = 3; /* 3 second timeout before hanging up the socket */
// FIXME set non-blocking
fd = accept(pfd, NULL, NULL);
if (fd < 0)
return;
// FIXME do something useful
fl = fcntl(fd, F_GETFL, 0);
fl |= O_NONBLOCK;
fcntl(fd, F_SETFL, fl);
do {
msg.buf = NULL;
/* read and validate the message */
if (receive_message(fd, &msg, tmo) == 0) {
// FIXME: handle message contents
ack(fd, msg.seq, tmo);
} else {
terminate = 1;
nack(fd, -1, tmo);
}
if (msg.buf)
free(msg.buf);
} while (!terminate);
close(fd);
}
void do_manager(struct supertype *container)

57
mdmon.c
View File

@ -34,6 +34,7 @@
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sched.h>
@ -76,14 +77,14 @@ static struct superswitch *find_metadata_methods(char *vers)
}
static int make_pidfile(char *devname)
static int make_pidfile(char *devname, int o_excl)
{
char path[100];
char pid[10];
int fd;
sprintf(path, "/var/run/mdadm/%s.pid", devname);
fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
if (fd < 0)
return -1;
sprintf(pid, "%d\n", getpid());
@ -92,6 +93,40 @@ static int make_pidfile(char *devname)
return 0;
}
static void try_kill_monitor(char *devname)
{
char buf[100];
int fd;
pid_t pid;
sprintf(buf, "/var/run/mdadm/%s.pid", devname);
fd = open(buf, O_RDONLY);
if (fd < 0)
return;
if (read(fd, buf, sizeof(buf)) < 0) {
close(fd);
return;
}
close(fd);
pid = strtoul(buf, NULL, 10);
/* kill this process if it is mdmon */
sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
fd = open(buf, O_RDONLY);
if (fd < 0)
return;
if (read(fd, buf, sizeof(buf)) < 0) {
close(fd);
return;
}
if (strstr(buf, "mdmon") != NULL)
kill(pid, SIGTERM);
}
static int make_control_sock(char *devname)
{
char path[100];
@ -150,10 +185,20 @@ int main(int argc, char *argv[])
/* If this fails, we hope it already exists */
mkdir("/var/run/mdadm", 0600);
/* pid file lives in /var/run/mdadm/mdXX.pid */
if (make_pidfile(container->devname) < 0) {
fprintf(stderr, "md-manage: %s already managed\n",
container->devname);
exit(3);
if (make_pidfile(container->devname, O_EXCL) < 0) {
if (ping_monitor(container->devname) == 0) {
fprintf(stderr, "mdmon: %s already managed\n",
container->devname);
exit(3);
} else {
/* cleanup the old monitor, this one is taking over */
try_kill_monitor(container->devname);
if (make_pidfile(container->devname, 0) < 0) {
fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
container->devname);
exit(3);
}
}
}
container->sock = make_control_sock(container->devname);

4
msg.h
View File

@ -17,6 +17,9 @@
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _MSG_H
#define _MSG_H
struct mdinfo;
struct md_message {
int seq;
@ -57,3 +60,4 @@ extern int connect_monitor(char *devname);
extern int ping_monitor(char *devname);
extern int send_remove_device(int fd, dev_t rdev, int seq, int tmo);
#endif