* flock(1) exit code (enhancement request)
@ 2012-05-29 11:45 Jan Kasprzak
2012-06-05 11:46 ` Karel Zak
0 siblings, 1 reply; 6+ messages in thread
From: Jan Kasprzak @ 2012-05-29 11:45 UTC (permalink / raw)
To: util-linux
Hello, util-linux developers!
I use flock(1) from util-linux in my scripts, and with --wait or --nonblock
I would like to be able to distinguish between the "conflicting lock exists"
and "-c <command> exited with code 1" states.
Of course, I can always wrap the -c argument with something like
bash -c "<original_cmd_and_args> || exit 2" with yet another level of
quoting, but I think it is really ugly.
This problem can be solved for example by mapping the exit code 1
of the -c command to something else. I.e. to add something like
-m <number>
--map-exit-code-1-to <number>
where the exit code of flock(1) would be <number>, iff the -c <command> exits
with code 1.
The alternative would be to have user-settable exit code for
the "conflicting lock exists" failure.
What do you think about it? If you agree with the above syntax,
I can of course post a patch to flock(1).
Thanks,
-Yenya
--
| Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> |
| GPG: ID 1024/D3498839 Fingerprint 0D99A7FB206605D7 8B35FCDE05B18A5E |
| http://www.fi.muni.cz/~kas/ Journal: http://www.fi.muni.cz/~kas/blog/ |
Please don't top post and in particular don't attach entire digests to your
mail or we'll all soon be using bittorrent to read the list. --Alan Cox
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: flock(1) exit code (enhancement request) 2012-05-29 11:45 flock(1) exit code (enhancement request) Jan Kasprzak @ 2012-06-05 11:46 ` Karel Zak 2012-06-05 14:08 ` H. Peter Anvin ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Karel Zak @ 2012-06-05 11:46 UTC (permalink / raw) To: Jan Kasprzak; +Cc: util-linux, H. Peter Anvin On Tue, May 29, 2012 at 01:45:18PM +0200, Jan Kasprzak wrote: > This problem can be solved for example by mapping the exit code 1 > of the -c command to something else. I.e. to add something like > > -m <number> > --map-exit-code-1-to <number> > > where the exit code of flock(1) would be <number>, iff the -c <command> exits > with code 1. Not sure if a generic remap for exit(1) is a good solution. > The alternative would be to have user-settable exit code for > the "conflicting lock exists" failure. Yes, It would be nice to have user-settable exit codes, for: --rc-timeout <num> --rc-conflict <num> > What do you think about it? If you agree with the above syntax, > I can of course post a patch to flock(1). Send patch. Karel -- Karel Zak <kzak@redhat.com> http://karelzak.blogspot.com ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: flock(1) exit code (enhancement request) 2012-06-05 11:46 ` Karel Zak @ 2012-06-05 14:08 ` H. Peter Anvin 2012-06-05 14:26 ` Jan Kasprzak 2012-06-05 14:26 ` [PATCH] flock: user-configurable exit code Jan "Yenya" Kasprzak 2 siblings, 0 replies; 6+ messages in thread From: H. Peter Anvin @ 2012-06-05 14:08 UTC (permalink / raw) To: Karel Zak, Jan Kasprzak; +Cc: util-linux Well, if it maps onto sysexits.h in a sensible way that would be my first choice. Karel Zak <kzak@redhat.com> wrote: >On Tue, May 29, 2012 at 01:45:18PM +0200, Jan Kasprzak wrote: >> This problem can be solved for example by mapping the exit >code 1 >> of the -c command to something else. I.e. to add something like >> >> -m <number> >> --map-exit-code-1-to <number> >> >> where the exit code of flock(1) would be <number>, iff the -c ><command> exits >> with code 1. > >Not sure if a generic remap for exit(1) is a good solution. > >> The alternative would be to have user-settable exit code for >> the "conflicting lock exists" failure. > >Yes, It would be nice to have user-settable exit codes, for: > > --rc-timeout <num> > --rc-conflict <num> > >> What do you think about it? If you agree with the above syntax, >> I can of course post a patch to flock(1). > >Send patch. > > Karel > >-- > Karel Zak <kzak@redhat.com> > http://karelzak.blogspot.com -- Sent from my mobile phone. Please excuse brevity and lack of formatting. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: flock(1) exit code (enhancement request) 2012-06-05 11:46 ` Karel Zak 2012-06-05 14:08 ` H. Peter Anvin @ 2012-06-05 14:26 ` Jan Kasprzak 2012-06-05 14:26 ` [PATCH] flock: user-configurable exit code Jan "Yenya" Kasprzak 2 siblings, 0 replies; 6+ messages in thread From: Jan Kasprzak @ 2012-06-05 14:26 UTC (permalink / raw) To: Karel Zak; +Cc: util-linux, H. Peter Anvin Karel Zak wrote: : On Tue, May 29, 2012 at 01:45:18PM +0200, Jan Kasprzak wrote: : > This problem can be solved for example by mapping the exit code 1 : > of the -c command to something else. I.e. to add something like : > : > -m <number> : > --map-exit-code-1-to <number> : > : > where the exit code of flock(1) would be <number>, iff the -c <command> exits : > with code 1. : : Not sure if a generic remap for exit(1) is a good solution. OK, understood. : > The alternative would be to have user-settable exit code for : > the "conflicting lock exists" failure. : : Yes, It would be nice to have user-settable exit codes, for: : : --rc-timeout <num> : --rc-conflict <num> These two states (timeout and conflict) are mutually exclusive, so I think only one option would be sufficient. Also, I don't like the acronym (rc) in the long option. I propose the "--conflict-exit-code <num>" name. : > What do you think about it? If you agree with the above syntax, : > I can of course post a patch to flock(1). : : Send patch. The patch follows in a separate message. -Yenya -- | Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> | | GPG: ID 1024/D3498839 Fingerprint 0D99A7FB206605D7 8B35FCDE05B18A5E | | http://www.fi.muni.cz/~kas/ Journal: http://www.fi.muni.cz/~kas/blog/ | Please don't top post and in particular don't attach entire digests to your mail or we'll all soon be using bittorrent to read the list. --Alan Cox ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] flock: user-configurable exit code 2012-06-05 11:46 ` Karel Zak 2012-06-05 14:08 ` H. Peter Anvin 2012-06-05 14:26 ` Jan Kasprzak @ 2012-06-05 14:26 ` Jan "Yenya" Kasprzak 2012-06-15 9:15 ` Karel Zak 2 siblings, 1 reply; 6+ messages in thread From: Jan "Yenya" Kasprzak @ 2012-06-05 14:26 UTC (permalink / raw) To: Karel Zak; +Cc: util-linux, H. Peter Anvin From: "Jan \"Yenya\" Kasprzak" <kas@fi.muni.cz> When locking the file fails with -n or -w option, caller has no way to distinguish between the exit code 1 of the -c command, and the exit code 1 of flock(1) caused by the conflicting lock. Add a new -E <exitcode> (--conflict-exit-code) option to set the exit code for the case of locking failure to any value. --- sys-utils/flock.1 | 19 ++++++++++++++++--- sys-utils/flock.c | 26 ++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/sys-utils/flock.1 b/sys-utils/flock.1 index 5a6f27f..b50c619 100644 --- a/sys-utils/flock.1 +++ b/sys-utils/flock.1 @@ -71,13 +71,19 @@ cases, for example if the enclosed command group may have forked a background process which should not be holding the lock. .TP \fB\-n\fP, \fB\-\-nb\fP, \fB\-\-nonblock\fP -Fail (with an exit code of 1) rather than wait if the lock cannot be +Fail rather than wait if the lock cannot be immediately acquired. +See the +.I \-E +option for the exit code used. .TP \fB\-w\fP, \fB\-\-wait\fP, \fB\-\-timeout\fP \fIseconds\fP -Fail (with an exit code of 1) if the lock cannot be acquired within +Fail if the lock cannot be acquired within .IR seconds . Decimal fractional values are allowed. +See the +.I \-E +option for the exit code used. .TP \fB\-o\fP, \fB\-\-close\fP Close the file descriptor on which the lock is held before executing @@ -86,6 +92,11 @@ This is useful if .B command spawns a child process which should not be holding the lock. .TP +\fB\-E\fP, \fB\-\-conflict\-exit\-code\fP \fInumber\fP +The exit code used when the \fB\-n\fP option is in use, and the +conflicting lock exists, or the \fB\-w\fP option is in use, +and the timeout is reached. The default value is 1. +.TP \fB\-c\fP, \fB\-\-command\fP \fIcommand\fP Pass a single .IR command , @@ -134,7 +145,9 @@ return values for everything else but an options .I \-n or .I \-w -failures which return 1. +failures which return either the value given by the +.I \-E +option, or 1 by default. .SH AUTHOR .UR hpa@zytor.com H. Peter Anvin diff --git a/sys-utils/flock.c b/sys-utils/flock.c index 1db28a2..5a3422a 100644 --- a/sys-utils/flock.c +++ b/sys-utils/flock.c @@ -58,6 +58,7 @@ static void __attribute__((__noreturn__)) usage(int ex) fputs(_( " -u --unlock remove a lock\n"), stderr); fputs(_( " -n --nonblock fail rather than wait\n"), stderr); fputs(_( " -w --timeout <secs> wait for a limited amount of time\n"), stderr); + fputs(_( " -E --conflict-exit-code <number> exit code after conflict or timeout\n"), stderr); fputs(_( " -o --close close file descriptor before running command\n"), stderr); fputs(_( " -c --command <command> run a single command string through the shell\n"), stderr); fprintf(stderr, USAGE_SEPARATOR); @@ -141,6 +142,11 @@ int main(int argc, char *argv[]) int opt, ix; int do_close = 0; int status; + /* + * The default exit code for lock conflict or timeout + * is specified in man flock.1 + */ + int conflict_exit_code = 1; char **cmd_argv = NULL, *sh_c_argv[4]; const char *filename = NULL; struct sigaction sa, old_sa; @@ -153,6 +159,7 @@ int main(int argc, char *argv[]) {"nb", no_argument, NULL, 'n'}, {"timeout", required_argument, NULL, 'w'}, {"wait", required_argument, NULL, 'w'}, + {"conflict-exit-code", required_argument, NULL, 'E'}, {"close", no_argument, NULL, 'o'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, @@ -171,7 +178,7 @@ int main(int argc, char *argv[]) optopt = 0; while ((opt = - getopt_long(argc, argv, "+sexnouw:hV?", long_options, + getopt_long(argc, argv, "+sexnouw:E:hV?", long_options, &ix)) != EOF) { switch (opt) { case 's': @@ -194,6 +201,10 @@ int main(int argc, char *argv[]) have_timeout = 1; strtotimeval(optarg, &timeout.it_value); break; + case 'E': + conflict_exit_code = strtos32_or_err(optarg, + _("invalid exit code")); + break; case 'V': printf("flock (%s)\n", PACKAGE_STRING); exit(EX_OK); @@ -252,18 +263,13 @@ int main(int argc, char *argv[]) while (flock(fd, type | block)) { switch (errno) { case EWOULDBLOCK: - /* -n option set and failed to lock. The numeric - * exit value is specified in man flock.1 - */ - exit(1); + /* -n option set and failed to lock. */ + exit(conflict_exit_code); case EINTR: /* Signal received */ if (timeout_expired) - /* -w option set and failed to lock. The - * numeric exit value is specified in man - * flock.1 - */ - exit(1); + /* -w option set and failed to lock. */ + exit(conflict_exit_code); /* otherwise try again */ continue; case EIO: -- 1.7.10.2 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] flock: user-configurable exit code 2012-06-05 14:26 ` [PATCH] flock: user-configurable exit code Jan "Yenya" Kasprzak @ 2012-06-15 9:15 ` Karel Zak 0 siblings, 0 replies; 6+ messages in thread From: Karel Zak @ 2012-06-15 9:15 UTC (permalink / raw) To: Jan "Yenya" Kasprzak; +Cc: util-linux, H. Peter Anvin On Tue, Jun 05, 2012 at 04:26:58PM +0200, Jan "Yenya" Kasprzak wrote: > sys-utils/flock.1 | 19 ++++++++++++++++--- > sys-utils/flock.c | 26 ++++++++++++++++---------- > 2 files changed, 32 insertions(+), 13 deletions(-) Applied, thanks. -- Karel Zak <kzak@redhat.com> http://karelzak.blogspot.com ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-06-15 9:15 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-05-29 11:45 flock(1) exit code (enhancement request) Jan Kasprzak 2012-06-05 11:46 ` Karel Zak 2012-06-05 14:08 ` H. Peter Anvin 2012-06-05 14:26 ` Jan Kasprzak 2012-06-05 14:26 ` [PATCH] flock: user-configurable exit code Jan "Yenya" Kasprzak 2012-06-15 9:15 ` Karel Zak
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).