lock.c 7.1 KB

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