flock(1) exit code (enhancement request)
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
Signature · 5 lines
| 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 CoxKarel Zak
maintainerQuoted 8 lines · Jan Kasprzak
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.
Quoted 2 lines
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>Quoted 2 lines
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
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Well, if it maps onto sysexits.h in a sensible way that would be my first choice.
Quoted 32 lines · Karel Zak
Quoted 1 line · Jan Kasprzak
This problem can be solved for example by mapping the exit
code 1
Quoted 6 lines
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
Quoted 1 line
with code 1.
Not sure if a generic remap for exit(1) is a good solution.
Quoted 2 lines
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>Quoted 2 lines
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
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.
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
Signature · 5 lines
| 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 CoxPATCH 1/1 flock: user-configurable exit code
In reply to Karel ZakFrom: "Jan \"Yenya\" Kasprzak" <[email protected]>
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(-)
sys-utils/flock.1 +16 -3
@@ -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 [email protected] H. Peter Anvin
sys-utils/flock.c +16 -10
@@ -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 :
Signature · 1 line
1.7.10.2
Re: [PATCH] flock: user-configurable exit code
Quoted 3 lines · Jan "Yenya" Kasprzak
sys-utils/flock.1 | 19 ++++++++++++++++--- sys-utils/flock.c | 26 ++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-)
Applied, thanks.
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com