lock.c 8.6 KB

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