lock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/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. vfs_path_t *vpath;
  162. /* Just to be sure (and don't lock new file) */
  163. if (fname == NULL || *fname == '\0')
  164. return 0;
  165. fname = tilde_expand (fname);
  166. vpath = vfs_path_from_str (fname);
  167. /* Locking on VFS is not supported */
  168. if (!vfs_file_is_local (vpath))
  169. {
  170. g_free ((gpointer) fname);
  171. vfs_path_free (vpath);
  172. return 0;
  173. }
  174. vfs_path_free (vpath);
  175. /* Check if already locked */
  176. lockfname = lock_build_symlink_name (fname);
  177. g_free ((gpointer) fname);
  178. if (lockfname == NULL)
  179. return 0;
  180. if (lstat (lockfname, &statbuf) == 0)
  181. {
  182. lock = lock_get_info (lockfname);
  183. if (lock == NULL)
  184. {
  185. g_free (lockfname);
  186. return 0;
  187. }
  188. lockinfo = lock_extract_info (lock);
  189. /* Check if locking process alive, ask user if required */
  190. if (lockinfo->pid == 0 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH))
  191. {
  192. msg =
  193. g_strdup_printf (_
  194. ("File \"%s\" is already being edited.\n"
  195. "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
  196. lockinfo->who, (int) lockinfo->pid);
  197. /* TODO: Implement "Abort" - needs to rewind undo stack */
  198. switch (query_dialog
  199. (_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"), _("&Ignore lock")))
  200. {
  201. case 0:
  202. break;
  203. case 1:
  204. case -1:
  205. g_free (lockfname);
  206. g_free (msg);
  207. return 0;
  208. }
  209. g_free (msg);
  210. }
  211. unlink (lockfname);
  212. }
  213. /* Create lock symlink */
  214. newlock = lock_build_name ();
  215. symlink_ok = (symlink (newlock, lockfname) != -1);
  216. g_free (newlock);
  217. g_free (lockfname);
  218. return symlink_ok ? 1 : 0;
  219. }
  220. /* --------------------------------------------------------------------------------------------- */
  221. /**
  222. * Lowers file lock if possible
  223. * @returns Always 0
  224. */
  225. int
  226. unlock_file (const char *fname)
  227. {
  228. char *lockfname, *lock;
  229. struct stat statbuf;
  230. /* Just to be sure */
  231. if (fname == NULL || *fname == '\0')
  232. return 0;
  233. fname = tilde_expand (fname);
  234. lockfname = lock_build_symlink_name (fname);
  235. g_free ((gpointer) fname);
  236. if (lockfname == NULL)
  237. return 0;
  238. /* Check if lock exists */
  239. if (lstat (lockfname, &statbuf) == -1)
  240. {
  241. g_free (lockfname);
  242. return 0;
  243. }
  244. lock = lock_get_info (lockfname);
  245. if (lock != NULL)
  246. {
  247. /* Don't touch if lock is not ours */
  248. if (lock_extract_info (lock)->pid != getpid ())
  249. {
  250. g_free (lockfname);
  251. return 0;
  252. }
  253. }
  254. /* Remove lock */
  255. unlink (lockfname);
  256. g_free (lockfname);
  257. return 0;
  258. }
  259. /* --------------------------------------------------------------------------------------------- */