4 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2011
5 The Free Software Foundation, Inc.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * \brief Source: file locking
31 * Locking scheme is based on a documentation found
32 * in JED editor sources. Abstract from lock.c file (by John E. Davis):
34 * The basic idea here is quite simple. Whenever a buffer is attached to
35 * a file, and that buffer is modified, then attempt to lock the
36 * file. Moreover, before writing to a file for any reason, lock the
37 * file. The lock is really a protocol respected and not a real lock.
38 * The protocol is this: If in the directory of the file is a
39 * symbolic link with name ".#FILE", the FILE is considered to be locked
40 * by the process specified by the link.
45 #include <signal.h> /* kill() */
48 #include <sys/types.h>
57 #include "lib/global.h"
58 #include "lib/vfs/vfs.h"
59 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/widget.h" /* query_dialog() */
63 /*** global variables ****************************************************************************/
65 /*** file scope macro definitions ****************************************************************/
68 #define PID_BUF_SIZE 10
70 /*** file scope type declarations ****************************************************************/
78 /*** file scope variables ************************************************************************/
80 /*** file scope functions ************************************************************************/
81 /* --------------------------------------------------------------------------------------------- */
82 /** \fn static char * lock_build_name (void)
83 * \brief builds user@host.domain.pid string (need to be freed)
84 * \return a pointer to lock filename
88 lock_build_name (void)
91 const char *user
= NULL
;
94 pw
= getpwuid (getuid ());
98 user
= getenv ("USER");
100 user
= getenv ("USERNAME");
102 user
= getenv ("LOGNAME");
106 /** \todo Use FQDN, no clean interface, so requires lot of code */
107 if (gethostname (host
, BUF_SIZE
- 1) == -1)
110 return g_strdup_printf ("%s@%s.%d", user
, host
, (int) getpid ());
113 /* --------------------------------------------------------------------------------------------- */
116 lock_build_symlink_name (const char *fname
)
118 char *fname_copy
, *symlink_name
;
119 char absolute_fname
[PATH_MAX
];
121 if (mc_realpath (fname
, absolute_fname
) == NULL
)
124 fname
= x_basename (absolute_fname
);
125 fname_copy
= g_strdup (fname
);
126 absolute_fname
[fname
- absolute_fname
] = '\0';
127 symlink_name
= g_strconcat (absolute_fname
, ".#", fname_copy
, (char *) NULL
);
133 /* --------------------------------------------------------------------------------------------- */
135 * Extract pid from user@host.domain.pid string
138 static struct lock_s
*
139 lock_extract_info (const char *str
)
143 static char pid
[PID_BUF_SIZE
], who
[BUF_SIZE
];
144 static struct lock_s lock
;
148 for (p
= str
+ len
- 1; p
>= str
; p
--)
152 /* Everything before last '.' is user@host */
154 for (s
= str
; s
< p
&& i
< BUF_SIZE
; s
++)
158 /* Treat text between '.' and ':' or '\0' as pid */
160 for (p
= p
+ 1; (p
< str
+ len
) && (*p
!= ':') && (i
< PID_BUF_SIZE
); p
++)
164 lock
.pid
= (pid_t
) atol (pid
);
169 /* --------------------------------------------------------------------------------------------- */
171 * Extract user@host.domain.pid from lock file (static string)
175 lock_get_info (const char *lockfname
)
178 static char buf
[BUF_SIZE
];
180 cnt
= readlink (lockfname
, buf
, BUF_SIZE
- 1);
181 if (cnt
== -1 || *buf
== '\0')
187 /* --------------------------------------------------------------------------------------------- */
188 /*** public functions ****************************************************************************/
189 /* --------------------------------------------------------------------------------------------- */
191 /* Tries to raise file lock
192 Returns 1 on success, 0 on failure, -1 if abort
193 Warning: Might do screen refresh and lose edit->force */
196 lock_file (const vfs_path_t
* fname_vpath
)
198 char *lockfname
= NULL
, *newlock
, *msg
, *lock
;
200 struct lock_s
*lockinfo
;
202 gboolean symlink_ok
= FALSE
;
203 char *fname_tmp
, *fname
;
205 /* Just to be sure (and don't lock new file) */
206 fname_tmp
= vfs_path_to_str (fname_vpath
);
207 if (fname_tmp
== NULL
|| *fname_tmp
== '\0')
213 fname
= tilde_expand (fname_tmp
);
216 /* Locking on VFS is not supported */
217 is_local
= vfs_file_is_local (fname_vpath
);
220 /* Check if already locked */
221 lockfname
= lock_build_symlink_name (fname
);
226 if (!is_local
|| lockfname
== NULL
)
229 if (lstat (lockfname
, &statbuf
) == 0)
231 lock
= lock_get_info (lockfname
);
234 lockinfo
= lock_extract_info (lock
);
236 /* Check if locking process alive, ask user if required */
237 if (lockinfo
->pid
== 0 || !(kill (lockinfo
->pid
, 0) == -1 && errno
== ESRCH
))
241 ("File \"%s\" is already being edited.\n"
242 "User: %s\nProcess ID: %d"), x_basename (lockfname
) + 2,
243 lockinfo
->who
, (int) lockinfo
->pid
);
244 /* TODO: Implement "Abort" - needs to rewind undo stack */
246 (_("File locked"), msg
, D_NORMAL
, 2, _("&Grab lock"), _("&Ignore lock")))
254 break; /* FIXME: unneeded? */
261 /* Create lock symlink */
262 newlock
= lock_build_name ();
263 symlink_ok
= (symlink (newlock
, lockfname
) != -1);
268 return symlink_ok
? 1 : 0;
271 /* --------------------------------------------------------------------------------------------- */
273 * Lowers file lock if possible
278 unlock_file (const vfs_path_t
* fname_vpath
)
280 char *lockfname
, *lock
;
282 char *fname_tmp
, *fname
;
284 /* Just to be sure */
285 fname_tmp
= vfs_path_to_str (fname_vpath
);
286 if (fname_tmp
== NULL
|| *fname_tmp
== '\0')
292 fname
= tilde_expand (fname_tmp
);
294 lockfname
= lock_build_symlink_name (fname
);
297 if (lockfname
== NULL
)
300 /* Check if lock exists */
301 if (lstat (lockfname
, &statbuf
) == -1)
304 lock
= lock_get_info (lockfname
);
307 /* Don't touch if lock is not ours */
308 if (lock_extract_info (lock
)->pid
!= getpid ())
320 /* --------------------------------------------------------------------------------------------- */