[PATCH 1/3] Make xenstored manage its own pidfile

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1/3] Make xenstored manage its own pidfile

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

Signed-off-by: Anthony Liguori <[hidden email]

Regards,

Anthony Liguori

diff -r 1d240086de52 tools/xenstore/utils.c
--- a/tools/xenstore/utils.c Thu Aug  4 15:02:09 2005
+++ b/tools/xenstore/utils.c Thu Aug  4 10:54:13 2005
@@ -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
[hidden email]
http://lists.xensource.com/xen-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 1/3] Make xenstored manage its own pidfile

Muli Ben-Yehuda
On Thu, Aug 04, 2005 at 11:03:11AM -0500, Anthony Liguori wrote:

> The following patch makes xenstored manage its own pidfile which makes
> it impossible for it to be started multiple times.
>
> Signed-off-by: Anthony Liguori <[hidden email]
>
> Regards,
>
> Anthony Liguori

> diff -r 1d240086de52 tools/xenstore/utils.c
> --- a/tools/xenstore/utils.c Thu Aug  4 15:02:09 2005
> +++ b/tools/xenstore/utils.c Thu Aug  4 10:54:13 2005
> @@ -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);

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

> + }
> +
> + if (lockf(fd, F_TLOCK, 0) == -1) {
> + exit(1);

Likewise?

> +
> + len = sprintf(buf, "%d\n", getpid());
> + write(fd, buf, len);

Check returen value from write?

Cheers,
Muli
--
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/


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