From b1c56f9c37c222d58c2aca36bdcb0aff4bd270f9 Mon Sep 17 00:00:00 2001 From: Martin Blapp Date: Tue, 10 Dec 2002 08:47:18 +0000 Subject: Check if rpcbind is already running and print a warning. Fixes segfault if rpcbind is started up a second time. Solution has been taken from mountd(8). Reviewed by: phk Approved by: re (rwatson) --- usr.sbin/rpcbind/rpcbind.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/usr.sbin/rpcbind/rpcbind.c b/usr.sbin/rpcbind/rpcbind.c index af6bc0794679..b2ad5f1de65f 100644 --- a/usr.sbin/rpcbind/rpcbind.c +++ b/usr.sbin/rpcbind/rpcbind.c @@ -61,6 +61,7 @@ static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro"; #include #endif #include +#include #include #include #include @@ -82,6 +83,8 @@ rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */ /* who to suid to if -s is given */ #define RUN_AS "daemon" +#define RPCBINDDLOCK "/var/run/rpcbind.lock" + int runasdaemon = 0; int insecure = 0; int oldstyle_local = 0; @@ -90,6 +93,7 @@ int verboselog = 0; char **hosts = NULL; int nhosts = 0; int on = 1; +int rpcbindlockfd; #ifdef WARMSTART /* Local Variable */ @@ -123,6 +127,14 @@ main(int argc, char *argv[]) parseargs(argc, argv); + /* Check that another rpcbind isn't already running. */ + if ((rpcbindlockfd = (open(RPCBINDDLOCK, + O_RDONLY|O_CREAT, 0444))) == -1) + err(1, "%s", RPCBINDDLOCK); + + if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) + errx(1, "another rpcbind is already running. Aborting"); + getrlimit(RLIMIT_NOFILE, &rl); if (rl.rlim_cur < 128) { if (rl.rlim_max <= 128) @@ -647,6 +659,7 @@ rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf, static void terminate(int dummy __unused) { + close(rpcbindlockfd); #ifdef WARMSTART syslog(LOG_ERR, "rpcbind terminating on signal. Restart with \"rpcbind -w\""); -- cgit v1.3