editlock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* editor 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: editor file locking
  19. * \author Adam Byrtek
  20. * \date 2003
  21. *
  22. * Locking scheme used in mcedit 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 "../src/global.h"
  46. #include "edit-impl.h"
  47. #include "editlock.h"
  48. #include "../src/wtools.h" /* edit_query_dialog () */
  49. #include "../src/strutil.h" /* utf string functions */
  50. #include "../vfs/vfs.h"
  51. #define BUF_SIZE 255
  52. #define PID_BUF_SIZE 10
  53. struct lock_s {
  54. char *who;
  55. pid_t pid;
  56. };
  57. /** \fn static char * lock_build_name (void)
  58. * \brief builds user@host.domain.pid string (need to be freed)
  59. * \return a pointer to lock filename
  60. */
  61. static char *
  62. lock_build_name (void)
  63. {
  64. char host[BUF_SIZE];
  65. const char *user = NULL;
  66. struct passwd *pw;
  67. pw = getpwuid (getuid ());
  68. if (pw) user = pw->pw_name;
  69. if (!user) user = getenv ("USER");
  70. if (!user) user = getenv ("USERNAME");
  71. if (!user) user = getenv ("LOGNAME");
  72. if (!user) user = "";
  73. /** \todo Use FQDN, no clean interface, so requires lot of code */
  74. if (gethostname (host, BUF_SIZE - 1) == -1)
  75. *host = '\0';
  76. return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
  77. }
  78. static char *
  79. lock_build_symlink_name (const char *fname)
  80. {
  81. char *fname_copy, *symlink_name;
  82. char absolute_fname[PATH_MAX];
  83. if (mc_realpath (fname, absolute_fname) == NULL)
  84. return NULL;
  85. fname = x_basename (absolute_fname);
  86. fname_copy = g_strdup (fname);
  87. absolute_fname[fname - absolute_fname] = '\0';
  88. symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
  89. g_free (fname_copy);
  90. return symlink_name;
  91. }
  92. /* Extract pid from user@host.domain.pid string */
  93. static struct lock_s *
  94. lock_extract_info (const char *str)
  95. {
  96. int i;
  97. const char *p, *s;
  98. static char pid[PID_BUF_SIZE], who[BUF_SIZE];
  99. static struct lock_s lock;
  100. for (p = str + str_term_width1 (str) - 1; p >= str; p--)
  101. if (*p == '.')
  102. break;
  103. /* Everything before last '.' is user@host */
  104. i = 0;
  105. for (s = str; s < p && i < BUF_SIZE; s++)
  106. who[i++] = *s;
  107. who[i] = '\0';
  108. /* Treat text between '.' and ':' or '\0' as pid */
  109. i = 0;
  110. for (p = p + 1;
  111. p < str + str_term_width1 (str) && *p != ':' && i < PID_BUF_SIZE; p++)
  112. pid[i++] = *p;
  113. pid[i] = '\0';
  114. lock.pid = (pid_t) atol (pid);
  115. lock.who = who;
  116. return &lock;
  117. }
  118. /* Extract user@host.domain.pid from lock file (static string) */
  119. static char *
  120. lock_get_info (const char *lockfname)
  121. {
  122. int cnt;
  123. static char buf[BUF_SIZE];
  124. if ((cnt = readlink (lockfname, buf, BUF_SIZE - 1)) == -1 || !*buf)
  125. return NULL;
  126. buf[cnt] = '\0';
  127. return buf;
  128. }
  129. /* Tries to raise file lock
  130. Returns 1 on success, 0 on failure, -1 if abort
  131. Warning: Might do screen refresh and lose edit->force */
  132. int
  133. edit_lock_file (const char *fname)
  134. {
  135. char *lockfname, *newlock, *msg, *lock;
  136. struct stat statbuf;
  137. struct lock_s *lockinfo;
  138. /* Just to be sure (and don't lock new file) */
  139. if (!fname || !*fname)
  140. return 0;
  141. /* Locking on VFS is not supported */
  142. if (!vfs_file_is_local (fname))
  143. return 0;
  144. /* Check if already locked */
  145. lockfname = lock_build_symlink_name (fname);
  146. if (lockfname == NULL)
  147. return 0;
  148. if (lstat (lockfname, &statbuf) == 0) {
  149. lock = lock_get_info (lockfname);
  150. if (!lock) {
  151. g_free (lockfname);
  152. return 0;
  153. }
  154. lockinfo = lock_extract_info (lock);
  155. /* Check if locking process alive, ask user if required */
  156. if (!lockinfo->pid
  157. || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH)) {
  158. msg =
  159. g_strdup_printf (_
  160. ("File \"%s\" is already being edited\n"
  161. "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
  162. lockinfo->who, (int) lockinfo->pid);
  163. /* TODO: Implement "Abort" - needs to rewind undo stack */
  164. switch (edit_query_dialog2
  165. (_("File locked"), msg, _("&Grab lock"),
  166. _("&Ignore lock"))) {
  167. case 0:
  168. break;
  169. case 1:
  170. case -1:
  171. g_free (lockfname);
  172. g_free (msg);
  173. return 0;
  174. }
  175. g_free (msg);
  176. }
  177. unlink (lockfname);
  178. }
  179. /* Create lock symlink */
  180. newlock = lock_build_name ();
  181. if (symlink (newlock, lockfname) == -1) {
  182. g_free (lockfname);
  183. g_free (newlock);
  184. return 0;
  185. }
  186. g_free (lockfname);
  187. g_free (newlock);
  188. return 1;
  189. }
  190. /* Lowers file lock if possible
  191. Always returns 0 to make 'lock = edit_unlock_file (f)' possible */
  192. int
  193. edit_unlock_file (const char *fname)
  194. {
  195. char *lockfname, *lock;
  196. struct stat statbuf;
  197. /* Just to be sure */
  198. if (!fname || !*fname)
  199. return 0;
  200. lockfname = lock_build_symlink_name (fname);
  201. if (lockfname == NULL)
  202. return 0;
  203. /* Check if lock exists */
  204. if (lstat (lockfname, &statbuf) == -1) {
  205. g_free (lockfname);
  206. return 0;
  207. }
  208. lock = lock_get_info (lockfname);
  209. if (lock) {
  210. /* Don't touch if lock is not ours */
  211. if (lock_extract_info (lock)->pid != getpid ()) {
  212. g_free (lockfname);
  213. return 0;
  214. }
  215. }
  216. /* Remove lock */
  217. unlink (lockfname);
  218. g_free (lockfname);
  219. return 0;
  220. }