lock.c 8.9 KB

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