lock.c 850 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * $Id: lock.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
  3. *
  4. * Copyright (C) 1997 Lars Fenneberg
  5. *
  6. * See the file COPYRIGHT for the respective terms and conditions.
  7. * If the file is missing contact me at lf@elemental.net
  8. * and I'll send you a copy.
  9. *
  10. */
  11. #include "includes.h"
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. int do_lock_exclusive(int fd)
  15. {
  16. struct flock fl;
  17. int res;
  18. memset((void *)&fl, 0, sizeof(fl));
  19. fl.l_type = F_WRLCK;
  20. fl.l_whence = fl.l_start = 0;
  21. fl.l_len = 0; /* 0 means "to end of file" */
  22. res = fcntl(fd, F_SETLK, &fl);
  23. if ((res == -1) && (errno == EAGAIN))
  24. errno = EWOULDBLOCK;
  25. return res;
  26. }
  27. int do_unlock(int fd)
  28. {
  29. struct flock fl;
  30. memset((void *)&fl, 0, sizeof(fl));
  31. fl.l_type = F_UNLCK;
  32. fl.l_whence = fl.l_start = 0;
  33. fl.l_len = 0; /* 0 means "to end of file" */
  34. return fcntl(fd, F_SETLK, &fl);
  35. }