lock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. File locking
  3. Copyright (C) 2003, 2004, 2005, 2006, 2007, 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Adam Byrtek, 2003
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file
  20. * \brief Source: file locking
  21. * \author Adam Byrtek
  22. * \date 2003
  23. *
  24. * Locking scheme is based on a documentation found
  25. * in JED editor sources. Abstract from lock.c file (by John E. Davis):
  26. *
  27. * The basic idea here is quite simple. Whenever a buffer is attached to
  28. * a file, and that buffer is modified, then attempt to lock the
  29. * file. Moreover, before writing to a file for any reason, lock the
  30. * file. The lock is really a protocol respected and not a real lock.
  31. * The protocol is this: If in the directory of the file is a
  32. * symbolic link with name ".#FILE", the FILE is considered to be locked
  33. * by the process specified by the link.
  34. */
  35. #include <config.h>
  36. #include <signal.h> /* kill() */
  37. #include <stdio.h>
  38. #include <stdarg.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <errno.h>
  44. #include <sys/stat.h>
  45. #include <pwd.h>
  46. #include <stdlib.h>
  47. #include "lib/global.h"
  48. #include "lib/vfs/vfs.h"
  49. #include "lib/util.h" /* tilde_expand() */
  50. #include "lib/lock.h"
  51. #include "lib/widget.h" /* query_dialog() */
  52. /*** global variables ****************************************************************************/
  53. /*** file scope macro definitions ****************************************************************/
  54. #define BUF_SIZE 255
  55. #define PID_BUF_SIZE 10
  56. /*** file scope type declarations ****************************************************************/
  57. struct lock_s
  58. {
  59. char *who;
  60. pid_t pid;
  61. };
  62. /*** file scope variables ************************************************************************/
  63. /*** file scope functions ************************************************************************/
  64. /* --------------------------------------------------------------------------------------------- */
  65. /** \fn static char * lock_build_name (void)
  66. * \brief builds user@host.domain.pid string (need to be freed)
  67. * \return a pointer to lock filename
  68. */
  69. static char *
  70. lock_build_name (void)
  71. {
  72. char host[BUF_SIZE];
  73. const char *user = NULL;
  74. struct passwd *pw;
  75. pw = getpwuid (getuid ());
  76. if (pw)
  77. user = pw->pw_name;
  78. if (!user)
  79. user = getenv ("USER");
  80. if (!user)
  81. user = getenv ("USERNAME");
  82. if (!user)
  83. user = getenv ("LOGNAME");
  84. if (!user)
  85. user = "";
  86. /** \todo Use FQDN, no clean interface, so requires lot of code */
  87. if (gethostname (host, BUF_SIZE - 1) == -1)
  88. *host = '\0';
  89. return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. static char *
  93. lock_build_symlink_name (const char *fname)
  94. {
  95. char *fname_copy, *symlink_name;
  96. char absolute_fname[PATH_MAX];
  97. if (mc_realpath (fname, absolute_fname) == NULL)
  98. return NULL;
  99. fname = x_basename (absolute_fname);
  100. fname_copy = g_strdup (fname);
  101. absolute_fname[fname - absolute_fname] = '\0';
  102. symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
  103. g_free (fname_copy);
  104. return symlink_name;
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. /**
  108. * Extract pid from user@host.domain.pid string
  109. */
  110. static struct lock_s *
  111. lock_extract_info (const char *str)
  112. {
  113. size_t i, len;
  114. const char *p, *s;
  115. static char pid[PID_BUF_SIZE], who[BUF_SIZE];
  116. static struct lock_s lock;
  117. len = strlen (str);
  118. for (p = str + len - 1; p >= str; p--)
  119. if (*p == '.')
  120. break;
  121. /* Everything before last '.' is user@host */
  122. i = 0;
  123. for (s = str; s < p && i < BUF_SIZE; s++)
  124. who[i++] = *s;
  125. who[i] = '\0';
  126. /* Treat text between '.' and ':' or '\0' as pid */
  127. i = 0;
  128. for (p = p + 1; (p < str + len) && (*p != ':') && (i < PID_BUF_SIZE); p++)
  129. pid[i++] = *p;
  130. pid[i] = '\0';
  131. lock.pid = (pid_t) atol (pid);
  132. lock.who = who;
  133. return &lock;
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. /**
  137. * Extract user@host.domain.pid from lock file (static string)
  138. */
  139. static char *
  140. lock_get_info (const char *lockfname)
  141. {
  142. int cnt;
  143. static char buf[BUF_SIZE];
  144. cnt = readlink (lockfname, buf, BUF_SIZE - 1);
  145. if (cnt == -1 || *buf == '\0')
  146. return NULL;
  147. buf[cnt] = '\0';
  148. return buf;
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. /*** public functions ****************************************************************************/
  152. /* --------------------------------------------------------------------------------------------- */
  153. /* Tries to raise file lock
  154. Returns 1 on success, 0 on failure, -1 if abort
  155. Warning: Might do screen refresh and lose edit->force */
  156. int
  157. lock_file (const char *fname)
  158. {
  159. char *lockfname, *newlock, *msg, *lock;
  160. struct stat statbuf;
  161. struct lock_s *lockinfo;
  162. gboolean symlink_ok;
  163. vfs_path_t *vpath;
  164. /* Just to be sure (and don't lock new file) */
  165. if (fname == NULL || *fname == '\0')
  166. return 0;
  167. fname = tilde_expand (fname);
  168. vpath = vfs_path_from_str (fname);
  169. /* Locking on VFS is not supported */
  170. if (!vfs_file_is_local (vpath))
  171. {
  172. g_free ((gpointer) fname);
  173. vfs_path_free (vpath);
  174. return 0;
  175. }
  176. vfs_path_free (vpath);
  177. /* Check if already locked */
  178. lockfname = lock_build_symlink_name (fname);
  179. g_free ((gpointer) fname);
  180. if (lockfname == NULL)
  181. return 0;
  182. if (lstat (lockfname, &statbuf) == 0)
  183. {
  184. lock = lock_get_info (lockfname);
  185. if (lock == NULL)
  186. {
  187. g_free (lockfname);
  188. return 0;
  189. }
  190. lockinfo = lock_extract_info (lock);
  191. /* Check if locking process alive, ask user if required */
  192. if (lockinfo->pid == 0 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH))
  193. {
  194. msg =
  195. g_strdup_printf (_
  196. ("File \"%s\" is already being edited.\n"
  197. "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
  198. lockinfo->who, (int) lockinfo->pid);
  199. /* TODO: Implement "Abort" - needs to rewind undo stack */
  200. switch (query_dialog
  201. (_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"), _("&Ignore lock")))
  202. {
  203. case 0:
  204. break;
  205. case 1:
  206. case -1:
  207. g_free (lockfname);
  208. g_free (msg);
  209. return 0;
  210. }
  211. g_free (msg);
  212. }
  213. unlink (lockfname);
  214. }
  215. /* Create lock symlink */
  216. newlock = lock_build_name ();
  217. symlink_ok = (symlink (newlock, lockfname) != -1);
  218. g_free (newlock);
  219. g_free (lockfname);
  220. return symlink_ok ? 1 : 0;
  221. }
  222. /* --------------------------------------------------------------------------------------------- */
  223. /**
  224. * Lowers file lock if possible
  225. * @returns Always 0
  226. */
  227. int
  228. unlock_file (const char *fname)
  229. {
  230. char *lockfname, *lock;
  231. struct stat statbuf;
  232. /* Just to be sure */
  233. if (fname == NULL || *fname == '\0')
  234. return 0;
  235. fname = tilde_expand (fname);
  236. lockfname = lock_build_symlink_name (fname);
  237. g_free ((gpointer) fname);
  238. if (lockfname == NULL)
  239. return 0;
  240. /* Check if lock exists */
  241. if (lstat (lockfname, &statbuf) == -1)
  242. {
  243. g_free (lockfname);
  244. return 0;
  245. }
  246. lock = lock_get_info (lockfname);
  247. if (lock != NULL)
  248. {
  249. /* Don't touch if lock is not ours */
  250. if (lock_extract_info (lock)->pid != getpid ())
  251. {
  252. g_free (lockfname);
  253. return 0;
  254. }
  255. }
  256. /* Remove lock */
  257. unlink (lockfname);
  258. g_free (lockfname);
  259. return 0;
  260. }
  261. /* --------------------------------------------------------------------------------------------- */