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