2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
File locking
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2003-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Written by:
|
|
|
|
Adam Byrtek, 2003
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander 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 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2003-04-02 23:36:10 +04:00
|
|
|
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
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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"
|
2024-02-11 11:50:05 +03:00
|
|
|
#include "lib/util.h"
|
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 ****************************************************************/
|
|
|
|
|
2016-03-03 22:51:40 +03:00
|
|
|
typedef struct
|
2010-10-22 15:17:27 +04:00
|
|
|
{
|
2003-04-05 05:29:15 +04:00
|
|
|
char *who;
|
|
|
|
pid_t pid;
|
2016-03-03 22:51:40 +03:00
|
|
|
} lock_s;
|
2003-04-05 05:29:15 +04:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** 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 ());
|
2023-06-25 09:39:24 +03:00
|
|
|
if (pw != NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
user = pw->pw_name;
|
2023-06-25 09:39:24 +03:00
|
|
|
if (user == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
user = getenv ("USER");
|
2023-06-25 09:39:24 +03:00
|
|
|
if (user == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
user = getenv ("USERNAME");
|
2023-06-25 09:39:24 +03:00
|
|
|
if (user == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
user = getenv ("LOGNAME");
|
2023-06-25 09:39:24 +03:00
|
|
|
if (user == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
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 */
|
2023-06-25 10:49:05 +03:00
|
|
|
if (gethostname (host, sizeof (host) - 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 *
|
2024-06-01 21:12:14 +03:00
|
|
|
lock_build_symlink_name (const vfs_path_t *fname_vpath)
|
2006-03-15 19:08:28 +03:00
|
|
|
{
|
2012-03-21 13:58:17 +04:00
|
|
|
const char *elpath;
|
|
|
|
char *str_filename, *str_dirname, *symlink_name;
|
2006-03-15 19:08:28 +03:00
|
|
|
|
2012-03-05 14:11:26 +04:00
|
|
|
/* get first path piece */
|
|
|
|
elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
|
2006-03-15 19:08:28 +03:00
|
|
|
|
2012-03-21 13:58:17 +04:00
|
|
|
str_filename = g_path_get_basename (elpath);
|
|
|
|
str_dirname = g_path_get_dirname (elpath);
|
2012-03-05 14:11:26 +04:00
|
|
|
symlink_name = g_strconcat (str_dirname, PATH_SEP_STR ".#", str_filename, (char *) NULL);
|
|
|
|
g_free (str_dirname);
|
2012-03-21 13:58:17 +04:00
|
|
|
g_free (str_filename);
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-03-03 22:51:40 +03:00
|
|
|
static 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];
|
2016-03-03 22:51:40 +03:00
|
|
|
static lock_s lock;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2010-01-04 22:06:37 +03:00
|
|
|
len = strlen (str);
|
|
|
|
|
2023-06-25 11:00:47 +03:00
|
|
|
for (p = str + len - 1; p >= str && *p != '.'; p--)
|
|
|
|
;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2003-04-05 05:29:15 +04:00
|
|
|
/* Everything before last '.' is user@host */
|
2023-06-25 11:00:47 +03:00
|
|
|
for (i = 0, s = str; i < sizeof (who) && s < p; i++, s++)
|
|
|
|
who[i] = *s;
|
2023-06-25 10:49:05 +03:00
|
|
|
if (i == sizeof (who))
|
2014-04-20 18:08:54 +04:00
|
|
|
i--;
|
2003-04-05 05:29:15 +04:00
|
|
|
who[i] = '\0';
|
|
|
|
|
|
|
|
/* Treat text between '.' and ':' or '\0' as pid */
|
2023-06-25 11:00:47 +03:00
|
|
|
for (i = 0, p++, s = str + len; i < sizeof (pid) && p < s && *p != ':'; i++, p++)
|
|
|
|
pid[i] = *p;
|
2023-06-25 10:49:05 +03:00
|
|
|
if (i == sizeof (pid))
|
2014-04-20 18:08:54 +04:00
|
|
|
i--;
|
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)
|
|
|
|
*/
|
|
|
|
|
2015-11-15 12:20:30 +03:00
|
|
|
static const char *
|
2004-08-29 22:45:56 +04:00
|
|
|
lock_get_info (const char *lockfname)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
2015-11-15 12:20:30 +03:00
|
|
|
ssize_t cnt;
|
2003-04-02 23:36:10 +04:00
|
|
|
static char buf[BUF_SIZE];
|
|
|
|
|
2023-06-25 10:49:05 +03:00
|
|
|
cnt = readlink (lockfname, buf, sizeof (buf) - 1);
|
2010-03-30 22:59:41 +04:00
|
|
|
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
|
2024-06-01 21:12:14 +03:00
|
|
|
lock_file (const vfs_path_t *fname_vpath)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
2015-11-15 12:20:30 +03:00
|
|
|
char *lockfname = NULL, *newlock, *msg;
|
2003-04-02 23:36:10 +04:00
|
|
|
struct stat statbuf;
|
2016-03-03 22:51:40 +03:00
|
|
|
lock_s *lockinfo;
|
2011-11-03 01:34:35 +04:00
|
|
|
gboolean is_local;
|
|
|
|
gboolean symlink_ok = FALSE;
|
2012-03-05 14:11:26 +04:00
|
|
|
const char *elpath;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2012-03-07 23:44:24 +04:00
|
|
|
if (fname_vpath == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-05 14:11:26 +04:00
|
|
|
elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
|
2003-04-02 23:36:10 +04:00
|
|
|
/* Just to be sure (and don't lock new file) */
|
2012-03-07 23:44:24 +04:00
|
|
|
if (*elpath == '\0')
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2011-05-01 13:16:46 +04:00
|
|
|
|
2003-04-03 02:25:00 +04:00
|
|
|
/* Locking on VFS is not supported */
|
2011-11-03 01:34:35 +04:00
|
|
|
is_local = vfs_file_is_local (fname_vpath);
|
|
|
|
if (is_local)
|
2010-10-22 15:15:32 +04:00
|
|
|
{
|
2011-11-03 01:34:35 +04:00
|
|
|
/* Check if already locked */
|
2012-03-05 14:11:26 +04:00
|
|
|
lockfname = lock_build_symlink_name (fname_vpath);
|
2010-10-22 15:15:32 +04:00
|
|
|
}
|
2003-04-03 02:25:00 +04:00
|
|
|
|
2011-11-03 01:34:35 +04:00
|
|
|
if (!is_local || lockfname == NULL)
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (lstat (lockfname, &statbuf) == 0)
|
|
|
|
{
|
2015-11-15 12:20:30 +03:00
|
|
|
const char *lock;
|
|
|
|
|
2010-10-22 15:17:27 +04:00
|
|
|
lock = lock_get_info (lockfname);
|
|
|
|
if (lock == NULL)
|
2011-11-03 01:34:35 +04:00
|
|
|
goto ret;
|
2010-10-22 15:17:27 +04:00
|
|
|
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:
|
2015-04-19 13:57:38 +03:00
|
|
|
default: /* Esc Esc */
|
2010-10-22 15:17:27 +04:00
|
|
|
g_free (msg);
|
2011-11-03 01:34:35 +04:00
|
|
|
goto ret;
|
2010-10-22 15:17:27 +04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2011-11-03 01:34:35 +04:00
|
|
|
ret:
|
|
|
|
g_free (lockfname);
|
2010-10-22 15:15:32 +04:00
|
|
|
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
|
2012-11-05 17:57:50 +04:00
|
|
|
* @return Always 0
|
2010-11-08 13:21:45 +03:00
|
|
|
*/
|
|
|
|
|
2003-04-02 23:36:10 +04:00
|
|
|
int
|
2024-06-01 21:12:14 +03:00
|
|
|
unlock_file (const vfs_path_t *fname_vpath)
|
2003-04-02 23:36:10 +04:00
|
|
|
{
|
2015-11-15 12:20:30 +03:00
|
|
|
char *lockfname;
|
2023-06-25 11:04:53 +03:00
|
|
|
const char *elpath;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2012-03-07 23:44:24 +04:00
|
|
|
if (fname_vpath == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-05 14:11:26 +04:00
|
|
|
elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
|
|
|
|
/* Just to be sure (and don't lock new file) */
|
2012-03-07 23:44:24 +04:00
|
|
|
if (*elpath == '\0')
|
2010-10-22 15:17:27 +04:00
|
|
|
return 0;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2012-03-05 14:11:26 +04:00
|
|
|
lockfname = lock_build_symlink_name (fname_vpath);
|
2023-06-25 11:04:53 +03:00
|
|
|
if (lockfname != NULL)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2010-10-22 15:15:32 +04:00
|
|
|
|
2023-06-25 11:04:53 +03:00
|
|
|
/* Check if lock exists */
|
|
|
|
if (lstat (lockfname, &statbuf) != -1)
|
|
|
|
{
|
|
|
|
const char *lock;
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2023-06-25 11:04:53 +03:00
|
|
|
lock = lock_get_info (lockfname);
|
|
|
|
/* Don't touch if lock is not ours */
|
|
|
|
if (lock == NULL || lock_extract_info (lock)->pid == getpid ())
|
|
|
|
unlink (lockfname);
|
|
|
|
}
|
2003-04-02 23:36:10 +04:00
|
|
|
|
2023-06-25 11:04:53 +03:00
|
|
|
g_free (lockfname);
|
2003-04-02 23:36:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|