summaryrefslogtreecommitdiffstats
path: root/sys-utils/flock.c
diff options
context:
space:
mode:
authorSami Kerola2015-02-08 00:45:24 +0100
committerSami Kerola2015-03-03 20:35:27 +0100
commit59dc9f28b520561de5194d1217a77080a349e133 (patch)
tree1ba21b9516eea6b6354c5c365606544ccb204302 /sys-utils/flock.c
parentwhereis: tell when mandatory option is missing (diff)
downloadkernel-qcow2-util-linux-59dc9f28b520561de5194d1217a77080a349e133.tar.gz
kernel-qcow2-util-linux-59dc9f28b520561de5194d1217a77080a349e133.tar.xz
kernel-qcow2-util-linux-59dc9f28b520561de5194d1217a77080a349e133.zip
flock: add --verbose option
Jenkins script jobs using flock are a great example of a situation in which one may want an automation to be verbose, so that when unexpected events happen there is more hints in logs. Reviewed-by: Benno Schulenberg <bensberg@justemail.net> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/flock.c')
-rw-r--r--sys-utils/flock.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/sys-utils/flock.c b/sys-utils/flock.c
index 38281551b..707c5992b 100644
--- a/sys-utils/flock.c
+++ b/sys-utils/flock.c
@@ -44,6 +44,7 @@
#include "strutils.h"
#include "closestream.h"
#include "timer.h"
+#include "monotonic.h"
static void __attribute__((__noreturn__)) usage(int ex)
{
@@ -66,6 +67,7 @@ static void __attribute__((__noreturn__)) usage(int ex)
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);
+ fputs(_( " --verbose increase verbosity\n"), stderr);
fprintf(stderr, USAGE_SEPARATOR);
fprintf(stderr, USAGE_HELP);
fprintf(stderr, USAGE_VERSION);
@@ -120,6 +122,8 @@ int main(int argc, char *argv[])
int opt, ix;
int do_close = 0;
int status;
+ int verbose = 0;
+ struct timeval time_start, time_done;
/*
* The default exit code for lock conflict or timeout
* is specified in man flock.1
@@ -128,7 +132,9 @@ int main(int argc, char *argv[])
char **cmd_argv = NULL, *sh_c_argv[4];
const char *filename = NULL;
struct sigaction old_sa;
-
+ enum {
+ OPT_VERBOSE = CHAR_MAX + 1
+ };
static const struct option long_options[] = {
{"shared", no_argument, NULL, 's'},
{"exclusive", no_argument, NULL, 'x'},
@@ -139,6 +145,7 @@ int main(int argc, char *argv[])
{"wait", required_argument, NULL, 'w'},
{"conflict-exit-code", required_argument, NULL, 'E'},
{"close", no_argument, NULL, 'o'},
+ {"verbose", no_argument, NULL, OPT_VERBOSE},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
@@ -184,6 +191,9 @@ int main(int argc, char *argv[])
conflict_exit_code = strtos32_or_err(optarg,
_("invalid exit code"));
break;
+ case OPT_VERBOSE:
+ verbose = 1;
+ break;
case 'V':
printf(UTIL_LINUX_VERSION);
exit(EX_OK);
@@ -239,16 +249,23 @@ int main(int argc, char *argv[])
setup_timer(&timeout, &old_timer, &old_sa, timeout_handler);
}
+ if (verbose)
+ gettime_monotonic(&time_start);
while (flock(fd, type | block)) {
switch (errno) {
case EWOULDBLOCK:
/* -n option set and failed to lock. */
+ if (verbose)
+ warnx(_("failed to get lock"));
exit(conflict_exit_code);
case EINTR:
/* Signal received */
- if (timeout_expired)
+ if (timeout_expired) {
/* -w option set and failed to lock. */
+ if (verbose)
+ warnx(_("timeout while waiting to get lock"));
exit(conflict_exit_code);
+ }
/* otherwise try again */
continue;
case EIO:
@@ -282,13 +299,23 @@ int main(int argc, char *argv[])
if (have_timeout)
cancel_timer(&old_timer, &old_sa);
+ if (verbose) {
+ struct timeval delta;
+ gettime_monotonic(&time_done);
+ timersub(&time_done, &time_start, &delta);
+ printf(_("%s: getting lock took %ld.%06ld seconds\n"),
+ program_invocation_short_name, delta.tv_sec,
+ delta.tv_usec);
+ }
status = EX_OK;
if (cmd_argv) {
pid_t w, f;
/* Clear any inherited settings */
signal(SIGCHLD, SIG_DFL);
+ if (verbose)
+ printf(_("%s: executing %s\n"), program_invocation_short_name, cmd_argv[2]);
f = fork();
if (f < 0) {