lock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* file locking
  2. Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  3. Authors: 2003 Adam Byrtek
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. /** \file
  18. * \brief Source: file locking
  19. * \author Adam Byrtek
  20. * \date 2003
  21. *
  22. * Locking scheme is based on a documentation found
  23. * in JED editor sources. Abstract from lock.c file (by John E. Davis):
  24. *
  25. * The basic idea here is quite simple. Whenever a buffer is attached to
  26. * a file, and that buffer is modified, then attempt to lock the
  27. * file. Moreover, before writing to a file for any reason, lock the
  28. * file. The lock is really a protocol respected and not a real lock.
  29. * The protocol is this: If in the directory of the file is a
  30. * symbolic link with name ".#FILE", the FILE is considered to be locked
  31. * by the process specified by the link.
  32. */
  33. #include <config.h>
  34. #include <signal.h> /* kill() */
  35. #include <stdio.h>
  36. #include <stdarg.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <errno.h>
  42. #include <sys/stat.h>
  43. #include <pwd.h>
  44. #include <stdlib.h>
  45. #include "lib/global.h"
  46. #include "lib/vfs/mc-vfs/vfs.h"
  47. #include "lib/lock.h"
  48. #include "src/wtools.h" /* query_dialog() */
  49. #define BUF_SIZE 255
  50. #define PID_BUF_SIZE 10
  51. struct lock_s {
  52. char *who;
  53. pid_t pid;
  54. };
  55. /** \fn static char * lock_build_name (void)
  56. * \brief builds user@host.domain.pid string (need to be freed)
  57. * \return a pointer to lock filename
  58. */
  59. static char *
  60. lock_build_name (void)
  61. {
  62. char host[BUF_SIZE];
  63. const char *user = NULL;
  64. struct passwd *pw;
  65. pw = getpwuid (getuid ());
  66. if (pw) user = pw->pw_name;
  67. if (!user) user = getenv ("USER");
  68. if (!user) user = getenv ("USERNAME");
  69. if (!user) user = getenv ("LOGNAME");
  70. if (!user) user = "";
  71. /** \todo Use FQDN, no clean interface, so requires lot of code */
  72. if (gethostname (host, BUF_SIZE - 1) == -1)
  73. *host = '\0';
  74. return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
  75. }
  76. static char *
  77. lock_build_symlink_name (const char *fname)
  78. {
  79. char *fname_copy, *symlink_name;
  80. char absolute_fname[PATH_MAX];
  81. if (mc_realpath (fname, absolute_fname) == NULL)
  82. return NULL;
  83. fname = x_basename (absolute_fname);
  84. fname_copy = g_strdup (fname);
  85. absolute_fname[fname - absolute_fname] = '\0';
  86. symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
  87. g_free (fname_copy);
  88. return symlink_name;
  89. }
  90. /* Extract pid from user@host.domain.pid string */
  91. static struct lock_s *
  92. lock_extract_info (const char *str)
  93. {
  94. size_t i, len;
  95. const char *p, *s;
  96. static char pid[PID_BUF_SIZE], who[BUF_SIZE];
  97. static struct lock_s lock;
  98. len = strlen (str);
  99. for (p = str + len - 1; p >= str; p--)
  100. if (*p == '.')
  101. break;
  102. /* Everything before last '.' is user@host */
  103. i = 0;
  104. for (s = str; s < p && i < BUF_SIZE; s++)
  105. who[i++] = *s;
  106. who[i] = '\0';
  107. /* Treat text between '.' and ':' or '\0' as pid */
  108. i = 0;
  109. for (p = p + 1; (p < str + len) && (*p != ':') && (i < PID_BUF_SIZE); p++)
  110. pid[i++] = *p;
  111. pid[i] = '\0';
  112. lock.pid = (pid_t) atol (pid);
  113. lock.who = who;
  114. return &lock;
  115. }
  116. /* Extract user@host.domain.pid from lock file (static string) */
  117. static char *
  118. lock_get_info (const char *lockfname)
  119. {
  120. int cnt;
  121. static char buf[BUF_SIZE];
  122. cnt = readlink (lockfname, buf, BUF_SIZE - 1);
  123. if (cnt == -1 || *buf == '\0')
  124. return NULL;
  125. buf[cnt] = '\0';
  126. return buf;
  127. }
  128. /* Tries to raise file lock
  129. Returns 1 on success, 0 on failure, -1 if abort
  130. Warning: Might do screen refresh and lose edit->force */
  131. int
  132. lock_file (const char *fname)
  133. {
  134. char *lockfname, *newlock, *msg, *lock;
  135. struct stat statbuf;
  136. struct lock_s *lockinfo;
  137. /* Just to be sure (and don't lock new file) */
  138. if (!fname || !*fname)
  139. return 0;
  140. /* Locking on VFS is not supported */
  141. if (!vfs_file_is_local (fname))
  142. return 0;
  143. /* Check if already locked */
  144. lockfname = lock_build_symlink_name (fname);
  145. if (lockfname == NULL)
  146. return 0;
  147. if (lstat (lockfname, &statbuf) == 0) {
  148. lock = lock_get_info (lockfname);
  149. if (!lock) {
  150. g_free (lockfname);
  151. return 0;
  152. }
  153. lockinfo = lock_extract_info (lock);
  154. /* Check if locking process alive, ask user if required */
  155. if (!lockinfo->pid
  156. || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH)) {
  157. msg =
  158. g_strdup_printf (_
  159. ("File \"%s\" is already being edited.\n"
  160. "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
  161. lockinfo->who, (int) lockinfo->pid);
  162. /* TODO: Implement "Abort" - needs to rewind undo stack */
  163. switch (query_dialog
  164. (_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"),
  165. _("&Ignore lock"))) {
  166. case 0:
  167. break;
  168. case 1:
  169. case -1:
  170. g_free (lockfname);
  171. g_free (msg);
  172. return 0;
  173. }
  174. g_free (msg);
  175. }
  176. unlink (lockfname);
  177. }
  178. /* Create lock symlink */
  179. newlock = lock_build_name ();
  180. if (symlink (newlock, lockfname) == -1) {
  181. g_free (lockfname);
  182. g_free (newlock);
  183. return 0;
  184. }
  185. g_free (lockfname);
  186. g_free (newlock);
  187. return 1;
  188. }
  189. /* Lowers file lock if possible
  190. Always returns 0 */
  191. int
  192. unlock_file (const char *fname)
  193. {
  194. char *lockfname, *lock;
  195. struct stat statbuf;
  196. /* Just to be sure */
  197. if (!fname || !*fname)
  198. return 0;
  199. lockfname = lock_build_symlink_name (fname);
  200. if (lockfname == NULL)
  201. return 0;
  202. /* Check if lock exists */
  203. if (lstat (lockfname, &statbuf) == -1) {
  204. g_free (lockfname);
  205. return 0;
  206. }
  207. lock = lock_get_info (lockfname);
  208. if (lock) {
  209. /* Don't touch if lock is not ours */
  210. if (lock_extract_info (lock)->pid != getpid ()) {
  211. g_free (lockfname);
  212. return 0;
  213. }
  214. }
  215. /* Remove lock */
  216. unlink (lockfname);
  217. g_free (lockfname);
  218. return 0;
  219. }