diff options
| author | Elliott Hughes <enh@google.com> | 2015-10-06 07:19:28 -0500 | 
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2015-10-06 07:19:28 -0500 | 
| commit | 661540a99f2c346d7fb2a4b83db2e31a7d204490 (patch) | |
| tree | 5d644a7d2f7a3f7403dda4a1f06ce5ff8392cf72 | |
| parent | d2dbdcab92f4795c1a614f0c0d2c5dee9c78e21a (diff) | |
| download | toybox-661540a99f2c346d7fb2a4b83db2e31a7d204490.tar.gz | |
New command: flock.
The brillo folks wanted this in a shell script they're porting over
(so I've only implemented the fd style they wanted, not the named file
style).
| -rw-r--r-- | toys/other/flock.c | 40 | 
1 files changed, 40 insertions, 0 deletions
diff --git a/toys/other/flock.c b/toys/other/flock.c new file mode 100644 index 00000000..1d56a568 --- /dev/null +++ b/toys/other/flock.c @@ -0,0 +1,40 @@ +/* flock.c - manage advisory file locks + * + * Copyright 2015 The Android Open Source Project + +USE_FLOCK(NEWTOY(flock, "<1>1nsux[-sux]", TOYFLAG_USR|TOYFLAG_BIN)) + +config FLOCK +  bool "flock" +  default y +  help +    usage: flock [-sxun] fd + +    Manage advisory file locks. + +    -s	Shared lock. +    -x	Exclusive lock (default). +    -u	Unlock. +    -n	Non-blocking: fail rather than wait for the lock. +*/ + +#define FOR_flock +#include "toys.h" + +#include <sys/file.h> + +void flock_main(void) +{ +  int fd = xstrtol(*toys.optargs, NULL, 10); +  int op; + +  if ((toys.optflags & FLAG_u)) op = LOCK_UN; +  else op = (toys.optflags & FLAG_s) ? LOCK_SH : LOCK_EX; + +  if ((toys.optflags & FLAG_n)) op |= LOCK_NB; + +  if (flock(fd, op)) { +    if ((op & LOCK_NB) && errno == EAGAIN) toys.exitval = 1; +    else perror_exit("flock"); +  } +}  | 
