2010-06-09 16:24:30 +04:00
|
|
|
/* file locking
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2007-09-25 19:33:35 +04:00
|
|
|
Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
|
2003-04-02 23:36:10 +04:00
|
|
|
|
|
|
|
Authors: 2003 Adam Byrtek
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301, USA.
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2010-10-22 15:17:27 +04:00
|
|
|
*/
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2009-02-07 00:09:50 +03:00
|
|
|
/** \file
|
2010-06-09 16:24:30 +04:00
|
|
|
* \brief Source: file locking
|
2009-02-07 00:09:50 +03:00
|
|
|
* \author Adam Byrtek
|
|
|
|
* \date 2003
|
|
|
|
*
|
2010-06-09 16:24:30 +04:00
|
|
|
* Locking scheme is based on a documentation found
|
2009-02-07 00:09:50 +03:00
|
|
|
* in JED editor sources. Abstract from lock.c file (by John E. Davis):
|
|
|
|
*
|
|
|
|
* The basic idea here is quite simple. Whenever a buffer is attached to
|
|
|
|
* a file, and that buffer is modified, then attempt to lock the
|
|
|
|
* file. Moreover, before writing to a file for any reason, lock the
|
|
|
|
* file. The lock is really a protocol respected and not a real lock.
|
|
|
|
* The protocol is this: If in the directory of the file is a
|
|
|
|
* symbolic link with name ".#FILE", the FILE is considered to be locked
|
|
|
|
* by the process specified by the link.
|
|
|
|
*/
|
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
#include <config.h>
|
2009-02-06 01:27:37 +03:00
|
|
|
|
2010-10-22 15:17:27 +04:00
|
|
|
#include <signal.h> /* kill() */
|
2005-02-22 20:00:36 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <unistd.h>
|
2005-02-22 20:00:36 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <pwd.h>
|
2005-02-22 20:00:36 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2011-02-15 16:44:17 +03:00
|
|
|
#include "lib/vfs/vfs.h"
|
2010-10-22 15:15:32 +04:00
|
|
|
#include "lib/util.h" /* tilde_expand() */
|
2010-06-09 16:24:30 +04:00
|
|
|
#include "lib/lock.h"
|
2010-11-12 11:03:57 +03:00
|
|
|
#include "lib/widget.h" /* query_dialog() */
|
2009-12-29 07:35:55 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
#define BUF_SIZE 255
|
|
|
|
#define PID_BUF_SIZE 10
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
2010-10-22 15:17:27 +04:00
|
|
|
struct lock_s
|
|
|
|
{
|
2003-04-05 05:29:15 +04:00
|
|
|
char *who;
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-02-07 00:09:50 +03:00
|
|
|
/** \fn static char * lock_build_name (void)
|
|
|
|
* \brief builds user@host.domain.pid string (need to be freed)
|
|
|
|
* \return a pointer to lock filename
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
static char *
|
2005-02-07 23:08:01 +03:00
|
|
|
lock_build_name (void)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
2004-08-29 22:45:56 +04:00
|
|
|
char host[BUF_SIZE];
|
2007-11-02 17:02:23 +03:00
|
|
|
const char *user = NULL;
|
|
|
|
struct passwd *pw;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2007-11-02 17:02:23 +03:00
|
|
|
pw = getpwuid (getuid ());
|
2010-10-22 15:17:27 +04:00
|
|
|
if (pw)
|
|
|
|
user = pw->pw_name;
|
|
|
|
if (!user)
|
|
|
|
user = getenv ("USER");
|
|
|
|
if (!user)
|
|
|
|
user = getenv ("USERNAME");
|
|
|
|
if (!user)
|
|
|
|
user = getenv ("LOGNAME");
|
|
|
|
if (!user)
|
|
|
|
user = "";
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2009-02-07 00:09:50 +03:00
|
|
|
/** \todo Use FQDN, no clean interface, so requires lot of code */
|
2003-04-02 23:36:10 +04:00
|
|
|
if (gethostname (host, BUF_SIZE - 1) == -1)
|
2010-10-22 15:17:27 +04:00
|
|
|
*host = '\0';
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2004-08-16 08:49:08 +04:00
|
|
|
return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2006-03-15 19:08:28 +03:00
|
|
|
static char *
|
|
|
|
lock_build_symlink_name (const char *fname)
|
|
|
|
{
|
|
|
|
char *fname_copy, *symlink_name;
|
|
|
|
char absolute_fname[PATH_MAX];
|
|
|
|
|
|
|
|
if (mc_realpath (fname, absolute_fname) == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
return NULL;
|
2006-03-15 19:08:28 +03:00
|
|
|
|
|
|
|
fname = x_basename (absolute_fname);
|
2009-02-06 01:40:32 +03:00
|
|
|
fname_copy = g_strdup (fname);
|
2006-03-15 19:08:28 +03:00
|
|
|
absolute_fname[fname - absolute_fname] = '\0';
|
|
|
|
symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (fname_copy);
|
2006-03-15 19:08:28 +03:00
|
|
|
|
|
|
|
return symlink_name;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Extract pid from user@host.domain.pid string
|
|
|
|
*/
|
|
|
|
|
2003-04-05 05:29:15 +04:00
|
|
|
static struct lock_s *
|
2004-08-29 22:45:56 +04:00
|
|
|
lock_extract_info (const char *str)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
2010-01-04 22:06:37 +03:00
|
|
|
size_t i, len;
|
2004-08-29 22:45:56 +04:00
|
|
|
const char *p, *s;
|
2003-04-05 05:29:15 +04:00
|
|
|
static char pid[PID_BUF_SIZE], who[BUF_SIZE];
|
|
|
|
static struct lock_s lock;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2010-01-04 22:06:37 +03:00
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
for (p = str + len - 1; p >= str; p--)
|
2010-10-22 15:17:27 +04:00
|
|
|
if (*p == '.')
|
|
|
|
break;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2003-04-05 05:29:15 +04:00
|
|
|
/* Everything before last '.' is user@host */
|
|
|
|
i = 0;
|
|
|
|
for (s = str; s < p && i < BUF_SIZE; s++)
|
2010-10-22 15:17:27 +04:00
|
|
|
who[i++] = *s;
|
2003-04-05 05:29:15 +04:00
|
|
|
who[i] = '\0';
|
|
|
|
|
|
|
|
/* Treat text between '.' and ':' or '\0' as pid */
|
2003-04-02 23:36:10 +04:00
|
|
|
i = 0;
|
2010-01-04 22:06:37 +03:00
|
|
|
for (p = p + 1; (p < str + len) && (*p != ':') && (i < PID_BUF_SIZE); p++)
|
2010-10-22 15:17:27 +04:00
|
|
|
pid[i++] = *p;
|
2003-04-02 23:36:10 +04:00
|
|
|
pid[i] = '\0';
|
|
|
|
|
2003-04-05 05:29:15 +04:00
|
|
|
lock.pid = (pid_t) atol (pid);
|
|
|
|
lock.who = who;
|
|
|
|
return &lock;
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Extract user@host.domain.pid from lock file (static string)
|
|
|
|
*/
|
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
static char *
|
2004-08-29 22:45:56 +04:00
|
|
|
lock_get_info (const char *lockfname)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
|
|
|
int cnt;
|
|
|
|
static char buf[BUF_SIZE];
|
|
|
|
|
2010-03-30 22:59:41 +04:00
|
|
|
cnt = readlink (lockfname, buf, BUF_SIZE - 1);
|
|
|
|
if (cnt == -1 || *buf == '\0')
|
2010-10-22 15:17:27 +04:00
|
|
|
return NULL;
|
2003-04-02 23:36:10 +04:00
|
|
|
buf[cnt] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-04-02 23:36:10 +04:00
|
|
|
|
|
|
|
/* Tries to raise file lock
|
2005-02-07 10:31:19 +03:00
|
|
|
Returns 1 on success, 0 on failure, -1 if abort
|
2003-04-05 05:43:58 +04:00
|
|
|
Warning: Might do screen refresh and lose edit->force */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
int
|
2010-06-09 16:24:30 +04:00
|
|
|
lock_file (const char *fname)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
|
|
|
char *lockfname, *newlock, *msg, *lock;
|
|
|
|
struct stat statbuf;
|
2003-04-05 05:29:15 +04:00
|
|
|
struct lock_s *lockinfo;
|
2010-10-22 15:15:32 +04:00
|
|
|
gboolean symlink_ok;
|
2011-05-01 13:16:46 +04:00
|
|
|
vfs_path_t *vpath;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
|
|
|
/* Just to be sure (and don't lock new file) */
|
2010-10-22 15:15:32 +04:00
|
|
|
if (fname == NULL || *fname == '\0')
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2010-10-22 15:15:32 +04:00
|
|
|
fname = tilde_expand (fname);
|
|
|
|
|
2011-05-01 13:16:46 +04:00
|
|
|
vpath = vfs_path_from_str (fname);
|
|
|
|
|
2003-04-03 02:25:00 +04:00
|
|
|
/* Locking on VFS is not supported */
|
2011-05-01 13:16:46 +04:00
|
|
|
if (!vfs_file_is_local (vpath))
|
2010-10-22 15:15:32 +04:00
|
|
|
{
|
2010-11-10 12:20:09 +03:00
|
|
|
g_free ((gpointer) fname);
|
2011-05-01 13:16:46 +04:00
|
|
|
vfs_path_free (vpath);
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2010-10-22 15:15:32 +04:00
|
|
|
}
|
2011-05-01 13:16:46 +04:00
|
|
|
vfs_path_free (vpath);
|
2003-04-03 02:25:00 +04:00
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
/* Check if already locked */
|
2006-03-15 19:08:28 +03:00
|
|
|
lockfname = lock_build_symlink_name (fname);
|
2010-11-10 12:20:09 +03:00
|
|
|
g_free ((gpointer) fname);
|
2006-03-15 19:08:28 +03:00
|
|
|
if (lockfname == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (lstat (lockfname, &statbuf) == 0)
|
|
|
|
{
|
|
|
|
lock = lock_get_info (lockfname);
|
|
|
|
if (lock == NULL)
|
|
|
|
{
|
|
|
|
g_free (lockfname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lockinfo = lock_extract_info (lock);
|
|
|
|
|
|
|
|
/* Check if locking process alive, ask user if required */
|
|
|
|
if (lockinfo->pid == 0 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH))
|
|
|
|
{
|
|
|
|
msg =
|
|
|
|
g_strdup_printf (_
|
|
|
|
("File \"%s\" is already being edited.\n"
|
|
|
|
"User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
|
|
|
|
lockinfo->who, (int) lockinfo->pid);
|
|
|
|
/* TODO: Implement "Abort" - needs to rewind undo stack */
|
|
|
|
switch (query_dialog
|
|
|
|
(_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"), _("&Ignore lock")))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case -1:
|
|
|
|
g_free (lockfname);
|
|
|
|
g_free (msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
g_free (msg);
|
|
|
|
}
|
|
|
|
unlink (lockfname);
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create lock symlink */
|
2005-02-07 23:08:01 +03:00
|
|
|
newlock = lock_build_name ();
|
2010-10-22 15:15:32 +04:00
|
|
|
symlink_ok = (symlink (newlock, lockfname) != -1);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (newlock);
|
2010-10-22 15:15:32 +04:00
|
|
|
g_free (lockfname);
|
|
|
|
|
|
|
|
return symlink_ok ? 1 : 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Lowers file lock if possible
|
|
|
|
* @returns Always 0
|
|
|
|
*/
|
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
int
|
2010-06-09 16:24:30 +04:00
|
|
|
unlock_file (const char *fname)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
|
|
|
char *lockfname, *lock;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
/* Just to be sure */
|
2010-10-22 15:15:32 +04:00
|
|
|
if (fname == NULL || *fname == '\0')
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2010-10-22 15:15:32 +04:00
|
|
|
fname = tilde_expand (fname);
|
2006-03-15 19:08:28 +03:00
|
|
|
lockfname = lock_build_symlink_name (fname);
|
2010-11-10 12:20:09 +03:00
|
|
|
g_free ((gpointer) fname);
|
2010-10-22 15:15:32 +04:00
|
|
|
|
2006-03-15 19:08:28 +03:00
|
|
|
if (lockfname == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
|
|
|
/* Check if lock exists */
|
2010-10-22 15:17:27 +04:00
|
|
|
if (lstat (lockfname, &statbuf) == -1)
|
|
|
|
{
|
|
|
|
g_free (lockfname);
|
|
|
|
return 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
lock = lock_get_info (lockfname);
|
2010-10-22 15:17:27 +04:00
|
|
|
if (lock != NULL)
|
|
|
|
{
|
|
|
|
/* Don't touch if lock is not ours */
|
|
|
|
if (lock_extract_info (lock)->pid != getpid ())
|
|
|
|
{
|
|
|
|
g_free (lockfname);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove lock */
|
|
|
|
unlink (lockfname);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (lockfname);
|
2003-04-02 23:36:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|