From efc2732f55c909a2d6228be2185a69c3569f7c97 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 2 Jul 1998 23:57:56 +0000 Subject: Fix for pidfile startup message. Jeremy. (This used to be commit 108284cc28d44ffea028209cf28b746008bdf455) --- source3/lib/pidfile.c | 69 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/pidfile.c b/source3/lib/pidfile.c index 9cb3f5afef..fecc759da9 100644 --- a/source3/lib/pidfile.c +++ b/source3/lib/pidfile.c @@ -28,6 +28,31 @@ extern int DEBUGLEVEL; #define O_NONBLOCK #endif +/* return the pid in a pidfile. return 0 if the process (or pidfile) + does not exist */ +int pidfile_pid(char *name) +{ + FILE *f; + unsigned ret; + pstring pidFile; + + slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name); + + f = fopen(pidFile, "r"); + if (!f) { + return 0; + } + + if (fscanf(f,"%u", &ret) != 1) { + fclose(f); + return 0; + } + fclose(f); + + if (!process_exists(ret)) return 0; + + return ret; +} /* create a pid file in the lock directory. open it and leave it locked */ void pidfile_create(char *name) @@ -40,57 +65,31 @@ 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\n", name)); - exit(1); - } + 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 = open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY, 0644); if (fd < 0) { - DEBUG(0,("ERROR: can't open %s: %s\n", pidFile, + DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, strerror(errno))); exit(1); } if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) { - DEBUG(0,("ERROR: %s is already running\n", name)); + DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n", + name, pidFile, strerror(errno))); exit(1); } memset(buf, 0, sizeof(buf)); slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) getpid()); if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { - DEBUG(0,("ERROR: can't write to %s: %s\n", + DEBUG(0,("ERROR: can't write to file %s: %s\n", pidFile, strerror(errno))); exit(1); } /* Leave pid file open & locked for the duration... */ } - - -/* return the pid in a pidfile. return 0 if the process (or pidfile) - does not exist */ -int pidfile_pid(char *name) -{ - FILE *f; - pstring pidFile; - unsigned ret; - - slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name); - - f = fopen(pidFile, "r"); - if (!f) { - return 0; - } - - if (fscanf(f,"%u", &ret) != 1) { - fclose(f); - return 0; - } - fclose(f); - - if (!process_exists(ret)) return 0; - - return ret; -} - -- cgit