1 /* Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
3 This file is part of GNU Emacs.
5 GNU Emacs is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Emacs is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Emacs; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 #include <sys/types.h>
42 extern char *egetenv ();
44 #ifdef CLASH_DETECTION
46 /* If system does not have symbolic links, it does not have lstat.
47 In that case, use ordinary stat instead. */
54 /* The name of the directory in which we keep lock files, with a '/'
58 /* The name of the file in the lock directory which is used to
59 arbitrate access to the entire directory. */
60 #define SUPERLOCK_NAME "!!!SuperLock!!!"
62 /* The path to the superlock file. This is SUPERLOCK_NAME appended to
66 /* Set LOCK to the name of the lock file for the filename FILE.
67 char *LOCK; Lisp_Object FILE; */
68 #define MAKE_LOCK_PATH (lock, file) \
69 (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_path) + 1), \
70 fill_in_lock_file_name (lock, (file)))
72 fill_in_lock_file_name (lockfile
, fn
)
73 register char *lockfile
;
74 register Lisp_Object fn
;
78 strcpy (lockfile
, lock_path
);
80 p
= lockfile
+ strlen (lockfile
);
82 strcpy (p
, XSTRING (fn
)->data
);
92 lock_file_owner_name (lfname
)
96 struct passwd
*the_pw
;
97 extern struct passwd
*getpwuid ();
99 if (lstat (lfname
, &s
) == 0)
100 the_pw
= getpwuid (s
.st_uid
);
101 return (the_pw
== 0 ? Qnil
: build_string (the_pw
->pw_name
));
105 /* lock_file locks file fn,
106 meaning it serves notice on the world that you intend to edit that file.
107 This should be done only when about to modify a file-visiting
108 buffer previously unmodified.
109 Do not (normally) call lock_buffer for a buffer already modified,
110 as either the file is already locked, or the user has already
111 decided to go ahead without locking.
113 When lock_buffer returns, either the lock is locked for us,
114 or the user has said to go ahead without locking.
116 If the file is locked by someone else, lock_buffer calls
117 ask-user-about-lock (a Lisp function) with two arguments,
118 the file name and the name of the user who did the locking.
119 This function can signal an error, or return t meaning
120 take away the lock, or return nil meaning ignore the lock. */
122 /* The lock file name is the file name with "/" replaced by "!"
123 and put in the Emacs lock directory. */
124 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
128 register Lisp_Object fn
;
130 register Lisp_Object attack
;
131 register char *lfname
;
133 MAKE_LOCK_PATH (lfname
, fn
);
135 /* See if this file is visited and has changed on disk since it was
138 register Lisp_Object subject_buf
= Fget_file_buffer (fn
);
139 if (!NILP (subject_buf
)
140 && NILP (Fverify_visited_file_modtime (subject_buf
))
141 && !NILP (Ffile_exists_p (fn
)))
142 call1 (intern ("ask-user-about-supersession-threat"), fn
);
145 /* Try to lock the lock. */
146 if (lock_if_free (lfname
) <= 0)
147 /* Return now if we have locked it, or if lock dir does not exist */
150 /* Else consider breaking the lock */
151 attack
= call2 (intern ("ask-user-about-lock"), fn
,
152 lock_file_owner_name (lfname
));
154 /* User says take the lock */
156 lock_superlock (lfname
);
157 lock_file_1 (lfname
, O_WRONLY
) ;
158 unlink (superlock_path
);
161 /* User says ignore the lock */
164 /* Lock the lock file named LFNAME.
165 If MODE is O_WRONLY, we do so even if it is already locked.
166 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
167 Return 1 if successful, 0 if not. */
170 lock_file_1 (lfname
, mode
)
171 int mode
; char *lfname
;
176 if ((fd
= open (lfname
, mode
, 0666)) >= 0)
179 chmod (lfname
, 0666);
183 sprintf (buf
, "%d ", getpid ());
184 write (fd
, buf
, strlen (buf
));
192 /* Lock the lock named LFNAME if possible.
193 Return 0 in that case.
194 Return positive if lock is really locked by someone else.
195 Return -1 if cannot lock for any other reason. */
198 lock_if_free (lfname
)
199 register char *lfname
;
201 register int clasher
;
203 while (lock_file_1 (lfname
, O_WRONLY
| O_EXCL
| O_CREAT
) == 0)
207 clasher
= current_lock_owner (lfname
);
209 if (clasher
!= getpid ())
212 /* Try again to lock it */
217 /* Return the pid of the process that claims to own the lock file LFNAME,
218 or 0 if nobody does or the lock is obsolete,
219 or -1 if something is wrong with the locking mechanism. */
222 current_lock_owner (lfname
)
225 int owner
= current_lock_owner_1 (lfname
);
226 if (owner
== 0 && errno
== ENOENT
)
228 /* Is it locked by a process that exists? */
229 if (owner
!= 0 && (kill (owner
, 0) >= 0 || errno
== EPERM
))
231 if (unlink (lfname
) < 0)
237 current_lock_owner_1 (lfname
)
244 fd
= open (lfname
, O_RDONLY
, 0666);
247 tem
= read (fd
, buf
, sizeof buf
);
249 return (tem
<= 0 ? 0 : atoi (buf
));
255 register Lisp_Object fn
;
257 register char *lfname
;
259 MAKE_LOCK_PATH (lfname
, fn
);
261 lock_superlock (lfname
);
263 if (current_lock_owner_1 (lfname
) == getpid ())
266 unlink (superlock_path
);
269 lock_superlock (lfname
)
274 for (i
= -20; i
< 0 && (fd
= open (superlock_path
,
275 O_WRONLY
| O_EXCL
| O_CREAT
, 0666)) < 0;
285 chmod (superlock_path
, 0666);
289 write (fd
, lfname
, strlen (lfname
));
297 register Lisp_Object tail
;
298 register struct buffer
*b
;
300 for (tail
= Vbuffer_alist
; XGCTYPE (tail
) == Lisp_Cons
;
301 tail
= XCONS (tail
)->cdr
)
303 b
= XBUFFER (XCONS (XCONS (tail
)->car
)->cdr
);
304 if (XTYPE (b
->filename
) == Lisp_String
&&
305 b
->save_modified
< BUF_MODIFF (b
))
306 unlock_file (b
->filename
);
311 DEFUN ("lock-buffer", Flock_buffer
, Slock_buffer
,
313 "Lock FILE, if current buffer is modified.\n\
314 FILE defaults to current buffer's visited file,\n\
315 or else nothing is done if current buffer isn't visiting a file.")
320 fn
= current_buffer
->filename
;
322 CHECK_STRING (fn
, 0);
323 if (current_buffer
->save_modified
< MODIFF
329 DEFUN ("unlock-buffer", Funlock_buffer
, Sunlock_buffer
,
331 "Unlock the file visited in the current buffer,\n\
332 if it should normally be locked.")
335 if (current_buffer
->save_modified
< MODIFF
&&
336 XTYPE (current_buffer
->filename
) == Lisp_String
)
337 unlock_file (current_buffer
->filename
);
342 /* Unlock the file visited in buffer BUFFER. */
344 unlock_buffer (buffer
)
345 struct buffer
*buffer
;
347 if (buffer
->save_modified
< BUF_MODIFF (buffer
) &&
348 XTYPE (buffer
->filename
) == Lisp_String
)
349 unlock_file (buffer
->filename
);
352 DEFUN ("file-locked-p", Ffile_locked_p
, Sfile_locked_p
, 0, 1, 0,
353 "Return nil if the FILENAME is not locked,\n\
354 t if it is locked by you, else a string of the name of the locker.")
358 register char *lfname
;
361 fn
= Fexpand_file_name (fn
, Qnil
);
363 MAKE_LOCK_PATH (lfname
, fn
);
365 owner
= current_lock_owner (lfname
);
368 else if (owner
== getpid ())
371 return (lock_file_owner_name (lfname
));
375 /* Initialization functions. */
379 lock_path
= egetenv ("EMACSLOCKDIR");
381 lock_path
= PATH_LOCK
;
383 /* Make sure it ends with a slash. */
384 if (lock_path
[strlen (lock_path
) - 1] != '/')
386 lock_path
= strcpy ((char *) xmalloc (strlen (lock_path
) + 2),
388 strcat (lock_path
, "/");
391 superlock_path
= (char *) xmalloc ((strlen (lock_path
)
392 + sizeof (SUPERLOCK_NAME
)));
393 strcpy (superlock_path
, lock_path
);
394 strcat (superlock_path
, SUPERLOCK_NAME
);
399 defsubr (&Sunlock_buffer
);
400 defsubr (&Slock_buffer
);
401 defsubr (&Sfile_locked_p
);
404 #endif /* CLASH_DETECTION */