ia64/xen-unstable
changeset 13007:87f220709073
Steal the write_pidfile function from xenstored_core, and use it to ensure
that blktapctrl only starts once.
Signed-off-by: Ewan Mellor <ewan@xensource.com>
that blktapctrl only starts once.
Signed-off-by: Ewan Mellor <ewan@xensource.com>
author | Ewan Mellor <ewan@xensource.com> |
---|---|
date | Wed Dec 13 11:23:01 2006 +0000 (2006-12-13) |
parents | e2792d1612b3 |
children | 10f51535cc84 |
files | tools/blktap/drivers/blktapctrl.c |
line diff
1.1 --- a/tools/blktap/drivers/blktapctrl.c Wed Dec 13 11:13:08 2006 +0000 1.2 +++ b/tools/blktap/drivers/blktapctrl.c Wed Dec 13 11:23:01 2006 +0000 1.3 @@ -57,6 +57,8 @@ 1.4 #include "blktapctrl.h" 1.5 #include "tapdisk.h" 1.6 1.7 +#define PIDFILE "/var/run/blktapctrl.pid" 1.8 + 1.9 #define NUM_POLL_FDS 2 1.10 #define MSG_SIZE 4096 1.11 #define MAX_TIMEOUT 10 1.12 @@ -622,6 +624,29 @@ static void print_drivers(void) 1.13 DPRINTF("Found driver: [%s]\n",dtypes[i]->name); 1.14 } 1.15 1.16 +static void write_pidfile(long pid) 1.17 +{ 1.18 + char buf[100]; 1.19 + int len; 1.20 + int fd; 1.21 + 1.22 + fd = open(PIDFILE, O_RDWR | O_CREAT, 0600); 1.23 + if (fd == -1) { 1.24 + DPRINTF("Opening pid file failed (%d)\n", errno); 1.25 + exit(1); 1.26 + } 1.27 + 1.28 + /* We exit silently if daemon already running. */ 1.29 + if (lockf(fd, F_TLOCK, 0) == -1) 1.30 + exit(0); 1.31 + 1.32 + len = sprintf(buf, "%ld\n", pid); 1.33 + if (write(fd, buf, len) != len) { 1.34 + DPRINTF("Writing pid file failed (%d)\n", errno); 1.35 + exit(1); 1.36 + } 1.37 +} 1.38 + 1.39 int main(int argc, char *argv[]) 1.40 { 1.41 char *devname; 1.42 @@ -681,6 +706,7 @@ int main(int argc, char *argv[]) 1.43 ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE ); 1.44 1.45 process = getpid(); 1.46 + write_pidfile(process); 1.47 ret = ioctl(ctlfd, BLKTAP_IOCTL_SENDPID, process ); 1.48 1.49 /*Static pollhooks*/ 1.50 @@ -716,3 +742,13 @@ int main(int argc, char *argv[]) 1.51 closelog(); 1.52 return -1; 1.53 } 1.54 + 1.55 +/* 1.56 + * Local variables: 1.57 + * c-file-style: "linux" 1.58 + * indent-tabs-mode: t 1.59 + * c-indent-level: 8 1.60 + * c-basic-offset: 8 1.61 + * tab-width: 8 1.62 + * End: 1.63 + */