From owner-svn-src-all@FreeBSD.ORG Thu Jun 13 20:46:31 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id A400496D; Thu, 13 Jun 2013 20:46:31 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 8650511EF; Thu, 13 Jun 2013 20:46:31 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r5DKkVHq006565; Thu, 13 Jun 2013 20:46:31 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r5DKkVvF006563; Thu, 13 Jun 2013 20:46:31 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201306132046.r5DKkVvF006563@svn.freebsd.org> From: Eitan Adler Date: Thu, 13 Jun 2013 20:46:31 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r251705 - stable/8/usr.bin/lockf X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Jun 2013 20:46:31 -0000 Author: eadler Date: Thu Jun 13 20:46:30 2013 New Revision: 251705 URL: http://svnweb.freebsd.org/changeset/base/251705 Log: MFC r250462: Add option to lockf to avoid creating a file if it does not exist. PR: bin/170775 Modified: stable/8/usr.bin/lockf/lockf.1 stable/8/usr.bin/lockf/lockf.c Directory Properties: stable/8/usr.bin/lockf/ (props changed) Modified: stable/8/usr.bin/lockf/lockf.1 ============================================================================== --- stable/8/usr.bin/lockf/lockf.1 Thu Jun 13 20:46:29 2013 (r251704) +++ stable/8/usr.bin/lockf/lockf.1 Thu Jun 13 20:46:30 2013 (r251705) @@ -32,7 +32,7 @@ .Nd execute a command while holding a file lock .Sh SYNOPSIS .Nm -.Op Fl ks +.Op Fl kns .Op Fl t Ar seconds .Ar file .Ar command @@ -90,6 +90,18 @@ Causes .Nm to operate silently. Failure to acquire the lock is indicated only in the exit status. +.It Fl n +Causes +.Nm +to fail if the specified lock +.Ar file +does not exist. If +.Fl n +is not specified, +.Nm +will create +.Ar file +if necessary. .It Fl t Ar seconds Specifies a timeout for waiting for the lock. By default, @@ -130,6 +142,10 @@ The utility was unable to create the lock file, e.g., because of insufficient access privileges. +.It Dv EX_UNAVAILABLE +The +.Fl n +option is specified and the specified lock file does not exist. .It Dv EX_USAGE There was an error on the .Nm Modified: stable/8/usr.bin/lockf/lockf.c ============================================================================== --- stable/8/usr.bin/lockf/lockf.c Thu Jun 13 20:46:29 2013 (r251704) +++ stable/8/usr.bin/lockf/lockf.c Thu Jun 13 20:46:30 2013 (r251705) @@ -56,16 +56,20 @@ static volatile sig_atomic_t timed_out; int main(int argc, char **argv) { - int ch, silent, status, waitsec; + int ch, flags, silent, status, waitsec; pid_t child; silent = keep = 0; + flags = O_CREAT; waitsec = -1; /* Infinite. */ - while ((ch = getopt(argc, argv, "skt:")) != -1) { + while ((ch = getopt(argc, argv, "sknt:")) != -1) { switch (ch) { case 'k': keep = 1; break; + case 'n': + flags &= ~O_CREAT; + break; case 's': silent = 1; break; @@ -118,13 +122,13 @@ main(int argc, char **argv) * avoiding the separate step of waiting for the lock. This * yields fairness and improved performance. */ - lockfd = acquire_lock(lockname, O_NONBLOCK); + lockfd = acquire_lock(lockname, flags | O_NONBLOCK); while (lockfd == -1 && !timed_out && waitsec != 0) { if (keep) - lockfd = acquire_lock(lockname, 0); + lockfd = acquire_lock(lockname, flags); else { wait_for_lock(lockname); - lockfd = acquire_lock(lockname, O_NONBLOCK); + lockfd = acquire_lock(lockname, flags | O_NONBLOCK); } } if (waitsec > 0) @@ -165,7 +169,7 @@ acquire_lock(const char *name, int flags { int fd; - if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|flags, 0666)) == -1) { + if ((fd = open(name, flags|O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { if (errno == EAGAIN || errno == EINTR) return (-1); err(EX_CANTCREAT, "cannot open %s", name); @@ -215,7 +219,7 @@ usage(void) { fprintf(stderr, - "usage: lockf [-ks] [-t seconds] file command [arguments]\n"); + "usage: lockf [-kns] [-t seconds] file command [arguments]\n"); exit(EX_USAGE); }