LOCK.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <u.h>
  2. #include <libc.h>
  3. /* MAXHOSTNAMELEN is in sys/param.h */
  4. #define MAXHOSTNAMELEN 64
  5. char lockstring[MAXHOSTNAMELEN+8];
  6. void
  7. main(int argc, char *argv[]) {
  8. char *lockfile;
  9. int fd, ppid, ssize;
  10. struct Dir *statbuf;
  11. if (argc != 4) {
  12. fprint(2, "usage: LOCK lockfile hostname ppid\n");
  13. exits("lock failed on usage");
  14. }
  15. lockfile = argv[1];
  16. if ((fd=create(lockfile, ORDWR, DMEXCL|0666)) < 0) {
  17. exits("lock failed on create");
  18. }
  19. ppid = atoi(argv[3]);
  20. ssize = sprint(lockstring, "%s %s\n", argv[2], argv[3]);
  21. if (write(fd, lockstring, ssize) != ssize) {
  22. fprint(2, "LOCK:write(): %r\n");
  23. exits("lock failed on write to lockfile");
  24. }
  25. switch(fork()) {
  26. default:
  27. exits("");
  28. case 0:
  29. break;
  30. case -1:
  31. fprint(2, "LOCK:fork(): %r\n");
  32. exits("lock failed on fork");
  33. }
  34. for(;;) {
  35. statbuf = dirfstat(fd);
  36. if(statbuf == nil)
  37. break;
  38. if (statbuf->length == 0){
  39. free(statbuf);
  40. break;
  41. }
  42. free(statbuf);
  43. if (write(fd, "", 0) < 0)
  44. break;
  45. sleep(3000);
  46. }
  47. close(fd);
  48. postnote(PNGROUP, ppid, "kill");
  49. exits("");
  50. }