lock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. File locking
  3. Copyright (C) 2003-2018
  4. 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. typedef struct
  58. {
  59. char *who;
  60. pid_t pid;
  61. } lock_s;
  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 vfs_path_t * fname_vpath)
  94. {
  95. const char *elpath;
  96. char *str_filename, *str_dirname, *symlink_name;
  97. /* get first path piece */
  98. elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
  99. str_filename = g_path_get_basename (elpath);
  100. str_dirname = g_path_get_dirname (elpath);
  101. symlink_name = g_strconcat (str_dirname, PATH_SEP_STR ".#", str_filename, (char *) NULL);
  102. g_free (str_dirname);
  103. g_free (str_filename);
  104. return symlink_name;
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. /**
  108. * Extract pid from user@host.domain.pid string
  109. */
  110. static 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 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; i < BUF_SIZE && s < p; s++)
  124. who[i++] = *s;
  125. if (i == BUF_SIZE)
  126. i--;
  127. who[i] = '\0';
  128. /* Treat text between '.' and ':' or '\0' as pid */
  129. i = 0;
  130. for (p = p + 1; i < PID_BUF_SIZE && p < str + len && *p != ':'; p++)
  131. pid[i++] = *p;
  132. if (i == PID_BUF_SIZE)
  133. i--;
  134. pid[i] = '\0';
  135. lock.pid = (pid_t) atol (pid);
  136. lock.who = who;
  137. return &lock;
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. /**
  141. * Extract user@host.domain.pid from lock file (static string)
  142. */
  143. static const char *
  144. lock_get_info (const char *lockfname)
  145. {
  146. ssize_t cnt;
  147. static char buf[BUF_SIZE];
  148. cnt = readlink (lockfname, buf, BUF_SIZE - 1);
  149. if (cnt == -1 || *buf == '\0')
  150. return NULL;
  151. buf[cnt] = '\0';
  152. return buf;
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. /*** public functions ****************************************************************************/
  156. /* --------------------------------------------------------------------------------------------- */
  157. /* Tries to raise file lock
  158. Returns 1 on success, 0 on failure, -1 if abort
  159. Warning: Might do screen refresh and lose edit->force */
  160. int
  161. lock_file (const vfs_path_t * fname_vpath)
  162. {
  163. char *lockfname = NULL, *newlock, *msg;
  164. struct stat statbuf;
  165. lock_s *lockinfo;
  166. gboolean is_local;
  167. gboolean symlink_ok = FALSE;
  168. const char *elpath;
  169. if (fname_vpath == NULL)
  170. return 0;
  171. elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
  172. /* Just to be sure (and don't lock new file) */
  173. if (*elpath == '\0')
  174. return 0;
  175. /* Locking on VFS is not supported */
  176. is_local = vfs_file_is_local (fname_vpath);
  177. if (is_local)
  178. {
  179. /* Check if already locked */
  180. lockfname = lock_build_symlink_name (fname_vpath);
  181. }
  182. if (!is_local || lockfname == NULL)
  183. return 0;
  184. if (lstat (lockfname, &statbuf) == 0)
  185. {
  186. const char *lock;
  187. lock = lock_get_info (lockfname);
  188. if (lock == NULL)
  189. goto ret;
  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. default: /* Esc Esc */
  208. g_free (msg);
  209. goto ret;
  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. ret:
  220. g_free (lockfname);
  221. return symlink_ok ? 1 : 0;
  222. }
  223. /* --------------------------------------------------------------------------------------------- */
  224. /**
  225. * Lowers file lock if possible
  226. * @return Always 0
  227. */
  228. int
  229. unlock_file (const vfs_path_t * fname_vpath)
  230. {
  231. char *lockfname;
  232. struct stat statbuf;
  233. const char *elpath, *lock;
  234. if (fname_vpath == NULL)
  235. return 0;
  236. elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
  237. /* Just to be sure (and don't lock new file) */
  238. if (*elpath == '\0')
  239. return 0;
  240. lockfname = lock_build_symlink_name (fname_vpath);
  241. if (lockfname == NULL)
  242. return 0;
  243. /* Check if lock exists */
  244. if (lstat (lockfname, &statbuf) == -1)
  245. goto ret;
  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. goto ret;
  252. }
  253. /* Remove lock */
  254. unlink (lockfname);
  255. ret:
  256. g_free (lockfname);
  257. return 0;
  258. }
  259. /* --------------------------------------------------------------------------------------------- */