summaryrefslogtreecommitdiff
path: root/source3/lib/pidfile.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>1998-12-05 08:09:59 +0000
committerAndrew Tridgell <tridge@samba.org>1998-12-05 08:09:59 +0000
commit84ba7b422dd65330a990e9d75b6225978c671180 (patch)
treed194c54b13638ab871a1194ebd8929b56c019dcb /source3/lib/pidfile.c
parent99a84ce2ddf784245fac271172582991a750710a (diff)
downloadsamba-84ba7b422dd65330a990e9d75b6225978c671180.tar.gz
samba-84ba7b422dd65330a990e9d75b6225978c671180.tar.bz2
samba-84ba7b422dd65330a990e9d75b6225978c671180.zip
fixed pidfile handling to check for a lock on the file, so we can be
sure it is a samba process that is running. (This used to be commit f7ad78e369ebf2f4d31e8259e3e1fdd4c087b037)
Diffstat (limited to 'source3/lib/pidfile.c')
-rw-r--r--source3/lib/pidfile.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/source3/lib/pidfile.c b/source3/lib/pidfile.c
index 52a3be875f..3e943577d8 100644
--- a/source3/lib/pidfile.c
+++ b/source3/lib/pidfile.c
@@ -32,26 +32,42 @@ extern int DEBUGLEVEL;
does not exist */
pid_t pidfile_pid(char *name)
{
- FILE *f;
+ int fd;
+ char pidstr[20];
unsigned ret;
pstring pidFile;
slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name);
- f = sys_fopen(pidFile, "r");
- if (!f) {
+ fd = open(pidFile, O_NONBLOCK | O_RDWR);
+ if (fd == -1) {
return 0;
}
- if (fscanf(f,"%u", &ret) != 1) {
- fclose(f);
- return 0;
+ ZERO_ARRAY(pidstr);
+
+ if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
+ goto ok;
}
- fclose(f);
+
+ ret = atoi(pidstr);
- if (!process_exists(ret)) return 0;
+ if (!process_exists(ret)) {
+ goto ok;
+ }
+
+ if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
+ /* we could get the lock - it can't be a Samba process */
+ goto ok;
+ }
+ close(fd);
return (pid_t)ret;
+
+ ok:
+ close(fd);
+ unlink(pidFile);
+ return 0;
}
/* create a pid file in the lock directory. open it and leave it locked */
@@ -65,14 +81,14 @@ void pidfile_create(char *name)
slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name);
pid = pidfile_pid(name);
- if (pid > 0 && process_exists(pid)) {
- DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
- name, pidFile, pid));
- exit(1);
- }
-
- fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY, 0644);
- if (fd < 0) {
+ if (pid != 0) {
+ DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
+ name, pidFile, pid));
+ exit(1);
+ }
+
+ fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
+ if (fd == -1) {
DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile,
strerror(errno)));
exit(1);
@@ -93,3 +109,4 @@ void pidfile_create(char *name)
}
/* Leave pid file open & locked for the duration... */
}
+