lock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/file.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. static void
  33. cu_unlock_file(int argc, void **argv)
  34. {
  35. int lockfd = *(int*)argv[0];
  36. struct flock fl;
  37. assert(lockfd >= 0);
  38. fl.l_type= F_UNLCK;
  39. fl.l_whence= SEEK_SET;
  40. fl.l_start= 0;
  41. fl.l_len= 0;
  42. if (fcntl(lockfd, F_SETLK, &fl) == -1)
  43. ohshite(_("unable to unlock dpkg status database"));
  44. }
  45. void
  46. unlock_file(void)
  47. {
  48. pop_cleanup(ehflag_normaltidy); /* Calls cu_unlock_file. */
  49. }
  50. /* lockfd must be allocated statically as its addresses is passed to
  51. * a cleanup handler. */
  52. void
  53. lock_file(int *lockfd, const char *filename,
  54. const char *emsg, const char *emsg_eagain)
  55. {
  56. struct flock fl;
  57. setcloexec(*lockfd, filename);
  58. fl.l_type = F_WRLCK;
  59. fl.l_whence = SEEK_SET;
  60. fl.l_start = 0;
  61. fl.l_len = 0;
  62. if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
  63. if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
  64. ohshit(emsg_eagain);
  65. ohshite(emsg);
  66. }
  67. push_cleanup(cu_unlock_file, ~0, NULL, 0, 1, lockfd);
  68. }
  69. void
  70. unlockdatabase(void)
  71. {
  72. unlock_file();
  73. }
  74. void lockdatabase(const char *admindir) {
  75. static int dblockfd = -1;
  76. int n;
  77. char *dblockfile= NULL;
  78. n= strlen(admindir);
  79. dblockfile= m_malloc(n+sizeof(LOCKFILE)+2);
  80. strcpy(dblockfile,admindir);
  81. strcpy(dblockfile+n, "/" LOCKFILE);
  82. if (dblockfd == -1) {
  83. dblockfd= open(dblockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  84. if (dblockfd == -1) {
  85. if (errno == EPERM)
  86. ohshit(_("you do not have permission to lock the dpkg status database"));
  87. ohshite(_("unable to open/create status database lockfile"));
  88. }
  89. }
  90. lock_file(&dblockfd, dblockfile,
  91. _("unable to lock dpkg status database"),
  92. _("status database area is locked by another process"));
  93. free(dblockfile);
  94. }