editlock.c 6.4 KB

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