Skip to contentSkip to conversation

Make xenstored manage its own pidfile

PATCH In review Anthony Liguori 2 participants 2 messages Aug 4, 2005 – Aug 5, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Make xenstored manage its own pidfile

The following patch makes xenstored manage its own pidfile which makes it impossible for it to be started multiple times.

Regards,

Anthony Liguori

tools/xenstore/utils.c +15 -0
diff -r 1d240086de52 tools/xenstore/utils.c
@@ -84,6 +84,9 @@
 void daemonize(void)
 {
 	pid_t pid;
+	int fd;
+	size_t len;
+	char buf[100];
 
 	/* Separate from our parent via fork, so init inherits us. */
 	if ((pid = fork()) < 0)
@@ -101,6 +104,18 @@
 	chdir("/");
 	/* Discard our parent's old-fashioned umask prejudices. */
 	umask(0);
+
+	fd = open("/var/run/xenstored.pid", O_RDWR | O_CREAT);
+	if (fd == -1) {
+		exit(1);
+	}
+
+	if (lockf(fd, F_TLOCK, 0) == -1) {
+		exit(1);
+	}
+
+	len = sprintf(buf, "%d\n", getpid());
+	write(fd, buf, len);
 }
 
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 23 lines · Anthony Liguori

The following patch makes xenstored manage its own pidfile which makes it impossible for it to be started multiple times.

Regards,

Anthony Liguori

diff -r 1d240086de52 tools/xenstore/utils.c

tools/xenstore/utils.c +3 -0
@@ -84,6 +84,9 @@
 void daemonize(void)
 {
 	pid_t pid;
+	int fd;
+	size_t len;
+	char buf[100];
 
 	/* Separate from our parent via fork, so init inherits us. */
 	if ((pid = fork()) < 0)

tools/xenstore/utils.c line 110

Quoted 7 lines
tools/xenstore/utils.c +4 -0
@@ -101,6 +104,18 @@
 	chdir("/");
 	/* Discard our parent's old-fashioned umask prejudices. */
 	umask(0);
+
+	fd = open("/var/run/xenstored.pid", O_RDWR | O_CREAT);
+	if (fd == -1) {
+		exit(1);

How about exit(errno), with or without a descriptive message?

tools/xenstore/utils.c line 114

Quoted 4 lines
tools/xenstore/utils.c +4 -0
+	}
+
+	if (lockf(fd, F_TLOCK, 0) == -1) {
+		exit(1);

Likewise?

tools/xenstore/utils.c line 118

Quoted 3 lines
tools/xenstore/utils.c +3 -0
+
+	len = sprintf(buf, "%d\n", getpid());
+	write(fd, buf, len);

Check returen value from write?

Cheers,
Muli

Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/