editlock.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <config.h>
  18. #include <signal.h> /* kill() */
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <sys/types.h>
  22. #ifdef HAVE_UNISTD_H
  23. # include <unistd.h>
  24. #endif
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include "../src/global.h"
  31. #include "edit.h"
  32. #include "editlock.h"
  33. #include "../src/wtools.h" /* edit_query_dialog () */
  34. #define BUF_SIZE 255
  35. #define PID_BUF_SIZE 10
  36. struct lock_s {
  37. char *who;
  38. pid_t pid;
  39. };
  40. /* Locking scheme used in mcedit is based on a documentation found
  41. in JED editor sources. Abstract from lock.c file (by John E. Davis):
  42. The basic idea here is quite simple. Whenever a buffer is attached to
  43. a file, and that buffer is modified, then attempt to lock the
  44. file. Moreover, before writing to a file for any reason, lock the
  45. file. The lock is really a protocol respected and not a real lock.
  46. The protocol is this: If in the directory of the file is a
  47. symbolic link with name ".#FILE", the FILE is considered to be locked
  48. by the process specified by the link.
  49. */
  50. /* Build user@host.domain.pid string (need to be freed) */
  51. static char *
  52. lock_build_name (void)
  53. {
  54. char host[BUF_SIZE];
  55. const char *user = NULL;
  56. struct passwd *pw;
  57. pw = getpwuid (getuid ());
  58. if (pw) user = pw->pw_name;
  59. if (!user) user = getenv ("USER");
  60. if (!user) user = getenv ("USERNAME");
  61. if (!user) user = getenv ("LOGNAME");
  62. if (!user) user = "";
  63. /* TODO: Use FQDN, no clean interface, so requires lot of code */
  64. if (gethostname (host, BUF_SIZE - 1) == -1)
  65. *host = '\0';
  66. return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
  67. }
  68. static char *
  69. lock_build_symlink_name (const char *fname)
  70. {
  71. char *fname_copy, *symlink_name;
  72. char absolute_fname[PATH_MAX];
  73. if (mc_realpath (fname, absolute_fname) == NULL)
  74. return NULL;
  75. fname = x_basename (absolute_fname);
  76. fname_copy = g_strdup (fname);
  77. absolute_fname[fname - absolute_fname] = '\0';
  78. symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
  79. g_free (fname_copy);
  80. return symlink_name;
  81. }
  82. /* Extract pid from user@host.domain.pid string */
  83. static struct lock_s *
  84. lock_extract_info (const char *str)
  85. {
  86. int i;
  87. const char *p, *s;
  88. static char pid[PID_BUF_SIZE], who[BUF_SIZE];
  89. static struct lock_s lock;
  90. for (p = str + strlen (str) - 1; p >= str; p--)
  91. if (*p == '.')
  92. break;
  93. /* Everything before last '.' is user@host */
  94. i = 0;
  95. for (s = str; s < p && i < BUF_SIZE; s++)
  96. who[i++] = *s;
  97. who[i] = '\0';
  98. /* Treat text between '.' and ':' or '\0' as pid */
  99. i = 0;
  100. for (p = p + 1;
  101. p < str + strlen (str) && *p != ':' && i < PID_BUF_SIZE; p++)
  102. pid[i++] = *p;
  103. pid[i] = '\0';
  104. lock.pid = (pid_t) atol (pid);
  105. lock.who = who;
  106. return &lock;
  107. }
  108. /* Extract user@host.domain.pid from lock file (static string) */
  109. static char *
  110. lock_get_info (const char *lockfname)
  111. {
  112. int cnt;
  113. static char buf[BUF_SIZE];
  114. if ((cnt = readlink (lockfname, buf, BUF_SIZE - 1)) == -1 || !*buf)
  115. return NULL;
  116. buf[cnt] = '\0';
  117. return buf;
  118. }
  119. /* Tries to raise file lock
  120. Returns 1 on success, 0 on failure, -1 if abort
  121. Warning: Might do screen refresh and lose edit->force */
  122. int
  123. edit_lock_file (const char *fname)
  124. {
  125. char *lockfname, *newlock, *msg, *lock;
  126. struct stat statbuf;
  127. struct lock_s *lockinfo;
  128. /* Just to be sure (and don't lock new file) */
  129. if (!fname || !*fname)
  130. return 0;
  131. /* Locking on VFS is not supported */
  132. if (!vfs_file_is_local (fname))
  133. return 0;
  134. /* Check if already locked */
  135. lockfname = lock_build_symlink_name (fname);
  136. if (lockfname == NULL)
  137. return 0;
  138. if (lstat (lockfname, &statbuf) == 0) {
  139. lock = lock_get_info (lockfname);
  140. if (!lock) {
  141. g_free (lockfname);
  142. return 0;
  143. }
  144. lockinfo = lock_extract_info (lock);
  145. /* Check if locking process alive, ask user if required */
  146. if (!lockinfo->pid
  147. || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH)) {
  148. msg =
  149. g_strdup_printf (_
  150. ("File \"%s\" is already being edited\n"
  151. "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
  152. lockinfo->who, (int) lockinfo->pid);
  153. /* TODO: Implement "Abort" - needs to rewind undo stack */
  154. switch (edit_query_dialog2
  155. (_("File locked"), msg, _("&Grab lock"),
  156. _("&Ignore lock"))) {
  157. case 0:
  158. break;
  159. case 1:
  160. case -1:
  161. g_free (lockfname);
  162. g_free (msg);
  163. return 0;
  164. }
  165. g_free (msg);
  166. }
  167. unlink (lockfname);
  168. }
  169. /* Create lock symlink */
  170. newlock = lock_build_name ();
  171. if (symlink (newlock, lockfname) == -1) {
  172. g_free (lockfname);
  173. g_free (newlock);
  174. return 0;
  175. }
  176. g_free (lockfname);
  177. g_free (newlock);
  178. return 1;
  179. }
  180. /* Lowers file lock if possible
  181. Always returns 0 to make 'lock = edit_unlock_file (f)' possible */
  182. int
  183. edit_unlock_file (const char *fname)
  184. {
  185. char *lockfname, *lock;
  186. struct stat statbuf;
  187. /* Just to be sure */
  188. if (!fname || !*fname)
  189. return 0;
  190. lockfname = lock_build_symlink_name (fname);
  191. if (lockfname == NULL)
  192. return 0;
  193. /* Check if lock exists */
  194. if (lstat (lockfname, &statbuf) == -1) {
  195. g_free (lockfname);
  196. return 0;
  197. }
  198. lock = lock_get_info (lockfname);
  199. if (lock) {
  200. /* Don't touch if lock is not ours */
  201. if (lock_extract_info (lock)->pid != getpid ()) {
  202. g_free (lockfname);
  203. return 0;
  204. }
  205. }
  206. /* Remove lock */
  207. unlink (lockfname);
  208. g_free (lockfname);
  209. return 0;
  210. }