summaryrefslogtreecommitdiff
path: root/sys/src/cmd/lp/LOCK.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/lp/LOCK.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/lp/LOCK.c')
-rwxr-xr-xsys/src/cmd/lp/LOCK.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/sys/src/cmd/lp/LOCK.c b/sys/src/cmd/lp/LOCK.c
new file mode 100755
index 000000000..c8892d4f5
--- /dev/null
+++ b/sys/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, ORDWR, DMEXCL|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("");
+}