From 1b4b2a291d9a8e2ad2fa0771e7386a3f6d89512d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 3 Nov 2006 01:49:19 +0000 Subject: r19539: Move pidfile to smbd/ (This used to be commit eefa64cbe392c4c4dcbf71b8bcf5128cce0339ba) --- source4/smbd/pidfile.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 source4/smbd/pidfile.c (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c new file mode 100644 index 0000000000..493de66b44 --- /dev/null +++ b/source4/smbd/pidfile.c @@ -0,0 +1,121 @@ +/* this code is broken - there is a race condition with the unlink (tridge) */ + +/* + Unix SMB/CIFS implementation. + pidfile handling + Copyright (C) Andrew Tridgell 1998 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "system/filesys.h" + +/** + * @file + * @brief Pid file handling + */ + +/** + * return the pid in a pidfile. return 0 if the process (or pidfile) + * does not exist + */ +pid_t pidfile_pid(const char *name) +{ + int fd; + char pidstr[20]; + pid_t ret; + char *pidFile; + + asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name); + + fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644); + + if (fd == -1) { + SAFE_FREE(pidFile); + return 0; + } + + ZERO_STRUCT(pidstr); + + if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) { + goto noproc; + } + + ret = (pid_t)atoi(pidstr); + + if (!process_exists(ret)) { + goto noproc; + } + + if (fcntl_lock(fd,F_SETLK,0,1,F_RDLCK)) { + /* we could get the lock - it can't be a Samba process */ + goto noproc; + } + + close(fd); + SAFE_FREE(pidFile); + return ret; + + noproc: + close(fd); + unlink(pidFile); + SAFE_FREE(pidFile); + return 0; +} + +/** + * create a pid file in the pid directory. open it and leave it locked + */ +void pidfile_create(const char *name) +{ + int fd; + char buf[20]; + char *pidFile; + pid_t pid; + + asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name); + + pid = pidfile_pid(name); + if (pid != 0) { + DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", + name, pidFile, (int)pid)); + exit(1); + } + + fd = 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); + } + + if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) { + 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, strlen(buf)) != (ssize_t)strlen(buf)) { + DEBUG(0,("ERROR: can't write to file %s: %s\n", + pidFile, strerror(errno))); + exit(1); + } + + /* Leave pid file open & locked for the duration... */ + SAFE_FREE(pidFile); +} -- cgit From 0479a2f1cbae51fcd8dbdc3c148c808421fb4d25 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 02:07:03 +0000 Subject: r23792: convert Samba4 to GPLv3 There are still a few tidyups of old FSF addresses to come (in both s3 and s4). More commits soon. (This used to be commit fcf38a38ac691abd0fa51b89dc951a08e89fdafa) --- source4/smbd/pidfile.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index 493de66b44..fdb502d41d 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -7,7 +7,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -16,8 +16,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From ffeee68e4b72dd94fee57366bd8d38b8c284c3d4 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 8 Sep 2007 12:42:09 +0000 Subject: r25026: Move param/param.h out of includes.h (This used to be commit abe8349f9b4387961ff3665d8c589d61cd2edf31) --- source4/smbd/pidfile.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index fdb502d41d..167555fd28 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -21,6 +21,7 @@ #include "includes.h" #include "system/filesys.h" +#include "param/param.h" /** * @file -- cgit From 7a287e07043cf937e22f8051c1a324d8a30c53e1 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 8 Sep 2007 13:34:42 +0000 Subject: r25028: Fix more warnings. (This used to be commit 3aa7ee4a0d8837471deeaa1c5a1f4a0d2a14aa6e) --- source4/smbd/pidfile.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index 167555fd28..57ff68709f 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -22,6 +22,7 @@ #include "includes.h" #include "system/filesys.h" #include "param/param.h" +#include "smbd/pidfile.h" /** * @file -- cgit From 37d53832a4623653f706e77985a79d84bd7c6694 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 28 Sep 2007 01:17:46 +0000 Subject: r25398: Parse loadparm context to all lp_*() functions. (This used to be commit 3fcc960839c6e5ca4de2c3c042f12f369ac5f238) --- source4/smbd/pidfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index 57ff68709f..9a8b095f15 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -40,7 +40,7 @@ pid_t pidfile_pid(const char *name) pid_t ret; char *pidFile; - asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name); + asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name); fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644); @@ -87,7 +87,7 @@ void pidfile_create(const char *name) char *pidFile; pid_t pid; - asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name); + asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name); pid = pidfile_pid(name); if (pid != 0) { -- cgit From efa384375f61049d7e7c43a77dc8abe0e034e04d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 1 Oct 2007 22:13:02 +0000 Subject: r25454: Use standard bool types in a couple more places. (This used to be commit 9243b551f30c7aa2763115516a6adcfe5bbddc58) --- source4/smbd/pidfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index 9a8b095f15..4847ddd7b5 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -103,7 +103,7 @@ void pidfile_create(const char *name) exit(1); } - if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) { + if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==false) { DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n", name, pidFile, strerror(errno))); exit(1); -- cgit From 6c999cd12344f2bb8b1d2941210b4c205b3e0aad Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 2 Dec 2007 22:32:11 +0100 Subject: r26236: Remove more uses of global_loadparm or specify loadparm_context explicitly. (This used to be commit 5b29ef7c03d9ae76b0ca909e9f03a58e1bad3521) --- source4/smbd/pidfile.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source4/smbd/pidfile.c') diff --git a/source4/smbd/pidfile.c b/source4/smbd/pidfile.c index 4847ddd7b5..3567846070 100644 --- a/source4/smbd/pidfile.c +++ b/source4/smbd/pidfile.c @@ -33,14 +33,14 @@ * return the pid in a pidfile. return 0 if the process (or pidfile) * does not exist */ -pid_t pidfile_pid(const char *name) +pid_t pidfile_pid(const char *piddir, const char *name) { int fd; char pidstr[20]; pid_t ret; char *pidFile; - asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name); + asprintf(&pidFile, "%s/%s.pid", piddir, name); fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644); @@ -80,16 +80,16 @@ pid_t pidfile_pid(const char *name) /** * create a pid file in the pid directory. open it and leave it locked */ -void pidfile_create(const char *name) +void pidfile_create(const char *piddir, const char *name) { int fd; char buf[20]; char *pidFile; pid_t pid; - asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name); + asprintf(&pidFile, "%s/%s.pid", piddir, name); - pid = pidfile_pid(name); + pid = pidfile_pid(piddir, name); if (pid != 0) { DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", name, pidFile, (int)pid)); -- cgit