lock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * lock.c - packages database locking
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <dpkg-i18n.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <assert.h>
  30. #include <fcntl.h>
  31. #include <sys/file.h>
  32. #include <dpkg.h>
  33. #include <dpkg-db.h>
  34. static void
  35. cu_unlock_file(int argc, void **argv)
  36. {
  37. int lockfd = *(int*)argv[0];
  38. struct flock fl;
  39. assert(lockfd >= 0);
  40. fl.l_type= F_UNLCK;
  41. fl.l_whence= SEEK_SET;
  42. fl.l_start= 0;
  43. fl.l_len= 0;
  44. if (fcntl(lockfd, F_SETLK, &fl) == -1)
  45. ohshite(_("unable to unlock dpkg status database"));
  46. }
  47. void
  48. unlock_file(void)
  49. {
  50. pop_cleanup(ehflag_normaltidy); /* Calls cu_unlock_file. */
  51. }
  52. /* lockfd must be allocated statically as its addresses is passed to
  53. * a cleanup handler. */
  54. void
  55. lock_file(int *lockfd, const char *filename,
  56. const char *emsg, const char *emsg_eagain)
  57. {
  58. struct flock fl;
  59. setcloexec(*lockfd, filename);
  60. fl.l_type = F_WRLCK;
  61. fl.l_whence = SEEK_SET;
  62. fl.l_start = 0;
  63. fl.l_len = 0;
  64. if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
  65. if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
  66. ohshit(emsg_eagain);
  67. ohshite(emsg);
  68. }
  69. push_cleanup(cu_unlock_file, ~0, NULL, 0, 1, lockfd);
  70. }
  71. void
  72. unlockdatabase(void)
  73. {
  74. unlock_file();
  75. }
  76. void lockdatabase(const char *admindir) {
  77. static int dblockfd = -1;
  78. int n;
  79. char *dblockfile= NULL;
  80. n= strlen(admindir);
  81. dblockfile= m_malloc(n+sizeof(LOCKFILE)+2);
  82. strcpy(dblockfile,admindir);
  83. strcpy(dblockfile+n, "/" LOCKFILE);
  84. if (dblockfd == -1) {
  85. dblockfd= open(dblockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  86. if (dblockfd == -1) {
  87. if (errno == EPERM)
  88. ohshit(_("you do not have permission to lock the dpkg status database"));
  89. ohshite(_("unable to open/create status database lockfile"));
  90. }
  91. }
  92. lock_file(&dblockfd, dblockfile,
  93. _("unable to lock dpkg status database"),
  94. _("status database area is locked by another process"));
  95. free(dblockfile);
  96. }