diff options
| author | rsc <devnull@localhost> | 2005-08-31 02:18:29 +0000 |
|---|---|---|
| committer | rsc <devnull@localhost> | 2005-08-31 02:18:29 +0000 |
| commit | 262ecfed9f7e39811f34517d82e848b8ec20f863 (patch) | |
| tree | b9947441be3e7b6a3d7c5e944854591e1b027bfb /src/cmd/lp/LOCK.c | |
| parent | 2863ba101f0c9fec34756948e263cd534a3634ee (diff) | |
| download | plan9port-262ecfed9f7e39811f34517d82e848b8ec20f863.tar.gz plan9port-262ecfed9f7e39811f34517d82e848b8ec20f863.zip | |
Initial lp.
Diffstat (limited to 'src/cmd/lp/LOCK.c')
| -rw-r--r-- | src/cmd/lp/LOCK.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/cmd/lp/LOCK.c b/src/cmd/lp/LOCK.c new file mode 100644 index 00000000..4ca1ec3a --- /dev/null +++ b/src/cmd/lp/LOCK.c @@ -0,0 +1,57 @@ +#include <u.h> +#include <libc.h> + +/* MAXHOSTNAMELEN is in sys/param.h */ +#define MAXHOSTNAMELEN 64 + +char lockstring[MAXHOSTNAMELEN+8]; + +void +main(int argc, char *argv[]) { + char *lockfile; + int fd, ppid, ssize; + struct Dir *statbuf; + + if (argc != 4) { + fprint(2, "usage: LOCK lockfile hostname ppid\n"); + exits("lock failed on usage"); + } + lockfile = argv[1]; + if ((fd=create(lockfile, OLOCK|ORDWR, 0666)) < 0) { + exits("lock failed on create"); + } + ppid = atoi(argv[3]); + ssize = sprint(lockstring, "%s %s\n", argv[2], argv[3]); + if (write(fd, lockstring, ssize) != ssize) { + fprint(2, "LOCK:write(): %r\n"); + exits("lock failed on write to lockfile"); + } + + switch(fork()) { + default: + exits(""); + case 0: + break; + case -1: + fprint(2, "LOCK:fork(): %r\n"); + exits("lock failed on fork"); + } + + for(;;) { + statbuf = dirfstat(fd); + if(statbuf == nil) + break; + if (statbuf->length == 0){ + free(statbuf); + break; + } + free(statbuf); + if (write(fd, "", 0) < 0) + break; + sleep(3000); + } + + close(fd); + postnote(PNGROUP, ppid, "kill"); + exits(""); +} |
