#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <config.h>
#ifdef VMS
#include "vms-pwd.h"
#else
#include <pwd.h>
#endif
#include <sys/file.h>
#ifdef USG
#include <fcntl.h>
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef __FreeBSD__
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#include "lisp.h"
#include "buffer.h"
#include "charset.h"
#include "coding.h"
#include "systime.h"
#include <time.h>
#include <errno.h>
#ifndef errno
extern int errno;
#endif
Lisp_Object Vtemporary_file_directory;
#ifdef CLASH_DETECTION
#include <utmp.h>
#ifndef BOOT_TIME_FILE
#define BOOT_TIME_FILE "/var/run/random-seed"
#endif
#ifndef WTMP_FILE
#define WTMP_FILE "/var/log/wtmp"
#endif
static time_t boot_time;
static int boot_time_initialized;
extern Lisp_Object Vshell_file_name;
static time_t
get_boot_time ()
{
int counter;
if (boot_time_initialized)
return boot_time;
boot_time_initialized = 1;
#if defined (CTL_KERN) && defined (KERN_BOOTTIME)
{
int mib[2];
size_t size;
struct timeval boottime_val;
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof (boottime_val);
if (sysctl (mib, 2, &boottime_val, &size, NULL, 0) >= 0)
{
boot_time = boottime_val.tv_sec;
return boot_time;
}
}
#endif
if (BOOT_TIME_FILE)
{
struct stat st;
if (stat (BOOT_TIME_FILE, &st) == 0)
{
boot_time = st.st_mtime;
return boot_time;
}
}
#if defined (BOOT_TIME) && ! defined (NO_WTMP_FILE)
#ifndef CANNOT_DUMP
if (! initialized)
return boot_time;
#endif
get_boot_time_1 ((char *) 0, 0);
if (boot_time)
return boot_time;
get_boot_time_1 (WTMP_FILE, 1);
for (counter = 0; counter < 20 && ! boot_time; counter++)
{
char cmd_string[100];
Lisp_Object tempname, filename;
int delete_flag = 0;
filename = Qnil;
sprintf (cmd_string, "%s.%d", WTMP_FILE, counter);
tempname = build_string (cmd_string);
if (! NILP (Ffile_exists_p (tempname)))
filename = tempname;
else
{
sprintf (cmd_string, "%s.%d.gz", WTMP_FILE, counter);
tempname = build_string (cmd_string);
if (! NILP (Ffile_exists_p (tempname)))
{
Lisp_Object args[6];
tempname = Fexpand_file_name (build_string ("wt"),
Vtemporary_file_directory);
tempname = make_temp_name (tempname, 1);
args[0] = Vshell_file_name;
args[1] = Qnil;
args[2] = Qnil;
args[3] = Qnil;
args[4] = build_string ("-c");
sprintf (cmd_string, "gunzip < %s.%d.gz > %s",
WTMP_FILE, counter, XSTRING (tempname)->data);
args[5] = build_string (cmd_string);
Fcall_process (6, args);
filename = tempname;
delete_flag = 1;
}
}
if (! NILP (filename))
{
get_boot_time_1 (XSTRING (filename)->data, 1);
if (delete_flag)
unlink (XSTRING (filename)->data);
}
}
return boot_time;
#else
return 0;
#endif
}
#ifdef BOOT_TIME
get_boot_time_1 (filename, newest)
char *filename;
int newest;
{
struct utmp ut, *utp;
int desc;
if (filename)
{
desc = open (filename, O_RDONLY);
if (desc < 0)
return;
close (desc);
utmpname (filename);
}
setutent ();
while (1)
{
ut.ut_type = BOOT_TIME;
utp = getutid (&ut);
if (! utp)
break;
if (utp->ut_time > boot_time)
{
boot_time = utp->ut_time;
if (! newest)
break;
}
utp = getutent ();
if (! utp)
break;
}
endutent ();
}
#endif
typedef struct
{
char *user;
char *host;
unsigned long pid;
time_t boot_time;
} lock_info_type;
#define LOCK_PID_MAX (4 * sizeof (unsigned long))
#define FREE_LOCK_INFO(i) do { xfree ((i).user); xfree ((i).host); } while (0)
#define MAKE_LOCK_NAME(lock, file) \
(lock = (char *) alloca (STRING_BYTES (XSTRING (file)) + 2 + 1), \
fill_in_lock_file_name (lock, (file)))
static void
fill_in_lock_file_name (lockfile, fn)
register char *lockfile;
register Lisp_Object fn;
{
register char *p;
strcpy (lockfile, XSTRING (fn)->data);
for (p = lockfile + strlen (lockfile); p != lockfile && *p != '/'; p--)
p[2] = *p;
p[1] = '.';
p[2] = '#';
}
static int
lock_file_1 (lfname, force)
char *lfname;
int force;
{
register int err;
time_t boot_time;
char *user_name;
char *host_name;
char *lock_info_str;
if (STRINGP (Fuser_login_name (Qnil)))
user_name = (char *)XSTRING (Fuser_login_name (Qnil))->data;
else
user_name = "";
if (STRINGP (Fsystem_name ()))
host_name = (char *)XSTRING (Fsystem_name ())->data;
else
host_name = "";
lock_info_str = (char *)alloca (strlen (user_name) + strlen (host_name)
+ LOCK_PID_MAX + 5);
boot_time = get_boot_time ();
if (boot_time)
sprintf (lock_info_str, "%s@%s.%lu:%lu", user_name, host_name,
(unsigned long) getpid (), (unsigned long) boot_time);
else
sprintf (lock_info_str, "%s@%s.%lu", user_name, host_name,
(unsigned long) getpid ());
err = symlink (lock_info_str, lfname);
if (errno == EEXIST && force)
{
unlink (lfname);
err = symlink (lock_info_str, lfname);
}
return err == 0;
}
int
within_one_second (a, b)
time_t a, b;
{
return (a - b >= -1 && a - b <= 1);
}
static int
current_lock_owner (owner, lfname)
lock_info_type *owner;
char *lfname;
{
#ifndef index
extern char *rindex (), *index ();
#endif
int o, p, len, ret;
int local_owner = 0;
char *at, *dot, *colon;
char *lfinfo = 0;
int bufsize = 50;
do
{
bufsize *= 2;
lfinfo = (char *) xrealloc (lfinfo, bufsize);
len = readlink (lfname, lfinfo, bufsize);
}
while (len >= bufsize);
if (len == -1)
{
xfree (lfinfo);
return errno == ENOENT ? 0 : -1;
}
lfinfo[len] = 0;
if (!owner)
{
owner = (lock_info_type *) alloca (sizeof (lock_info_type));
local_owner = 1;
}
at = index (lfinfo, '@');
dot = rindex (lfinfo, '.');
if (!at || !dot)
{
xfree (lfinfo);
return -1;
}
len = at - lfinfo;
owner->user = (char *) xmalloc (len + 1);
strncpy (owner->user, lfinfo, len);
owner->user[len] = 0;
owner->pid = atoi (dot + 1);
colon = dot;
while (*colon && *colon != ':')
colon++;
if (*colon == ':')
owner->boot_time = atoi (colon + 1);
else
owner->boot_time = 0;
len = dot - at - 1;
owner->host = (char *) xmalloc (len + 1);
strncpy (owner->host, at + 1, len);
owner->host[len] = 0;
xfree (lfinfo);
if (STRINGP (Fsystem_name ())
&& strcmp (owner->host, XSTRING (Fsystem_name ())->data) == 0)
{
if (owner->pid == getpid ())
ret = 2;
else if (owner->pid > 0
&& (kill (owner->pid, 0) >= 0 || errno == EPERM)
&& (owner->boot_time == 0
|| within_one_second (owner->boot_time, get_boot_time ())))
ret = 1;
else if (unlink (lfname) < 0)
ret = -1;
else
ret = 0;
}
else
{
ret = 1;
}
if (local_owner || ret <= 0)
{
FREE_LOCK_INFO (*owner);
}
return ret;
}
static int
lock_if_free (clasher, lfname)
lock_info_type *clasher;
register char *lfname;
{
while (lock_file_1 (lfname, 0) == 0)
{
int locker;
if (errno != EEXIST)
return -1;
locker = current_lock_owner (clasher, lfname);
if (locker == 2)
{
FREE_LOCK_INFO (*clasher);
return 0;
}
else if (locker == 1)
return 1;
else if (locker == -1)
return -1;
}
return 0;
}
void
lock_file (fn)
Lisp_Object fn;
{
register Lisp_Object attack, orig_fn, encoded_fn;
register char *lfname, *locker;
lock_info_type lock_info;
struct gcpro gcpro1;
if (! NILP (Vpurify_flag))
return;
orig_fn = fn;
GCPRO1 (fn);
fn = Fexpand_file_name (fn, Qnil);
encoded_fn = ENCODE_FILE (fn);
MAKE_LOCK_NAME (lfname, encoded_fn);
{
register Lisp_Object subject_buf;
subject_buf = get_truename_buffer (orig_fn);
if (!NILP (subject_buf)
&& NILP (Fverify_visited_file_modtime (subject_buf))
&& !NILP (Ffile_exists_p (fn)))
call1 (intern ("ask-user-about-supersession-threat"), fn);
}
UNGCPRO;
if (lock_if_free (&lock_info, lfname) <= 0)
return;
locker = (char *) alloca (strlen (lock_info.user) + strlen (lock_info.host)
+ LOCK_PID_MAX + 9);
sprintf (locker, "%s@%s (pid %lu)", lock_info.user, lock_info.host,
lock_info.pid);
FREE_LOCK_INFO (lock_info);
attack = call2 (intern ("ask-user-about-lock"), fn, build_string (locker));
if (!NILP (attack))
{
lock_file_1 (lfname, 1);
return;
}
}
void
unlock_file (fn)
register Lisp_Object fn;
{
register char *lfname;
fn = Fexpand_file_name (fn, Qnil);
fn = ENCODE_FILE (fn);
MAKE_LOCK_NAME (lfname, fn);
if (current_lock_owner (0, lfname) == 2)
unlink (lfname);
}
void
unlock_all_files ()
{
register Lisp_Object tail;
register struct buffer *b;
for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr)
{
b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
if (STRINGP (b->file_truename) && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
{
register char *lfname;
MAKE_LOCK_NAME (lfname, b->file_truename);
if (current_lock_owner (0, lfname) == 2)
unlink (lfname);
}
}
}
DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
0, 1, 0,
"Lock FILE, if current buffer is modified.\n\
FILE defaults to current buffer's visited file,\n\
or else nothing is done if current buffer isn't visiting a file.")
(file)
Lisp_Object file;
{
if (NILP (file))
file = current_buffer->file_truename;
else
CHECK_STRING (file, 0);
if (SAVE_MODIFF < MODIFF
&& !NILP (file))
lock_file (file);
return Qnil;
}
DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
0, 0, 0,
"Unlock the file visited in the current buffer,\n\
if it should normally be locked.")
()
{
if (SAVE_MODIFF < MODIFF
&& STRINGP (current_buffer->file_truename))
unlock_file (current_buffer->file_truename);
return Qnil;
}
void
unlock_buffer (buffer)
struct buffer *buffer;
{
if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
&& STRINGP (buffer->file_truename))
unlock_file (buffer->file_truename);
}
DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
"Return nil if the FILENAME is not locked,\n\
t if it is locked by you, else a string of the name of the locker.")
(filename)
Lisp_Object filename;
{
Lisp_Object ret;
register char *lfname;
int owner;
lock_info_type locker;
filename = Fexpand_file_name (filename, Qnil);
MAKE_LOCK_NAME (lfname, filename);
owner = current_lock_owner (&locker, lfname);
if (owner <= 0)
ret = Qnil;
else if (owner == 2)
ret = Qt;
else
ret = build_string (locker.user);
if (owner > 0)
FREE_LOCK_INFO (locker);
return ret;
}
void
init_filelock ()
{
boot_time = 0;
boot_time_initialized = 0;
}
void
syms_of_filelock ()
{
DEFVAR_LISP ("temporary-file-directory", &Vtemporary_file_directory,
"The directory for writing temporary files.");
Vtemporary_file_directory = Qnil;
defsubr (&Sunlock_buffer);
defsubr (&Slock_buffer);
defsubr (&Sfile_locked_p);
}
#endif