1 /* Copyright (C) 1985, 1986, 1987, 1993 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 ();
43 extern char *strcpy ();
45 #if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
46 extern struct passwd
*getpwuid (uid_t
);
48 extern struct passwd
*getpwuid ();
51 #ifdef CLASH_DETECTION
53 /* If system does not have symbolic links, it does not have lstat.
54 In that case, use ordinary stat instead. */
61 /* The name of the directory in which we keep lock files, with a '/'
65 /* The name of the file in the lock directory which is used to
66 arbitrate access to the entire directory. */
67 #define SUPERLOCK_NAME "!!!SuperLock!!!"
69 /* The path to the superlock file. This is SUPERLOCK_NAME appended to
73 /* Set LOCK to the name of the lock file for the filename FILE.
74 char *LOCK; Lisp_Object FILE; */
76 #ifndef HAVE_LONG_FILE_NAMES
78 #define MAKE_LOCK_PATH(lock, file) \
79 (lock = (char *) alloca (14 + strlen (lock_path) + 1), \
80 fill_in_lock_short_file_name (lock, (file)))
83 fill_in_lock_short_file_name (lockfile
, fn
)
84 register char *lockfile
;
85 register Lisp_Object fn
;
89 unsigned int word
[2];
90 unsigned char byte
[8];
92 register unsigned char *p
, new;
94 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
95 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
97 crc
.word
[0] = crc
.word
[1] = 0;
99 for (p
= XSTRING (fn
)->data
; new = *p
++; )
102 crc
.byte
[7] = crc
.byte
[6];
103 crc
.byte
[6] = crc
.byte
[5] + new;
104 crc
.byte
[5] = crc
.byte
[4];
105 crc
.byte
[4] = crc
.byte
[3];
106 crc
.byte
[3] = crc
.byte
[2] + new;
107 crc
.byte
[2] = crc
.byte
[1];
108 crc
.byte
[1] = crc
.byte
[0];
111 sprintf (lockfile
, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_path
,
112 crc
.byte
[0], crc
.byte
[1], crc
.byte
[2], crc
.byte
[3],
113 crc
.byte
[4], crc
.byte
[5], crc
.byte
[6]);
116 #else /* defined HAVE_LONG_FILE_NAMES */
118 #define MAKE_LOCK_PATH(lock, file) \
119 (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_path) + 1), \
120 fill_in_lock_file_name (lock, (file)))
123 fill_in_lock_file_name (lockfile
, fn
)
124 register char *lockfile
;
125 register Lisp_Object fn
;
129 strcpy (lockfile
, lock_path
);
131 p
= lockfile
+ strlen (lockfile
);
133 strcpy (p
, XSTRING (fn
)->data
);
141 #endif /* !defined HAVE_LONG_FILE_NAMES */
144 lock_file_owner_name (lfname
)
148 struct passwd
*the_pw
;
150 if (lstat (lfname
, &s
) == 0)
151 the_pw
= getpwuid (s
.st_uid
);
152 return (the_pw
== 0 ? Qnil
: build_string (the_pw
->pw_name
));
156 /* lock_file locks file fn,
157 meaning it serves notice on the world that you intend to edit that file.
158 This should be done only when about to modify a file-visiting
159 buffer previously unmodified.
160 Do not (normally) call lock_buffer for a buffer already modified,
161 as either the file is already locked, or the user has already
162 decided to go ahead without locking.
164 When lock_buffer returns, either the lock is locked for us,
165 or the user has said to go ahead without locking.
167 If the file is locked by someone else, lock_buffer calls
168 ask-user-about-lock (a Lisp function) with two arguments,
169 the file name and the name of the user who did the locking.
170 This function can signal an error, or return t meaning
171 take away the lock, or return nil meaning ignore the lock. */
173 /* The lock file name is the file name with "/" replaced by "!"
174 and put in the Emacs lock directory. */
175 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
177 /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
178 representation of a 14-bytes CRC generated from the file name
179 and put in the Emacs lock directory (not very nice, but it works).
180 (ie., /ka/king/junk.tex -> /!/ec92d3ed24a8f0). */
184 register Lisp_Object fn
;
186 register Lisp_Object attack
;
187 register char *lfname
;
189 MAKE_LOCK_PATH (lfname
, fn
);
191 /* See if this file is visited and has changed on disk since it was
194 register Lisp_Object subject_buf
= Fget_file_buffer (fn
);
195 if (!NILP (subject_buf
)
196 && NILP (Fverify_visited_file_modtime (subject_buf
))
197 && !NILP (Ffile_exists_p (fn
)))
198 call1 (intern ("ask-user-about-supersession-threat"), fn
);
201 /* Try to lock the lock. */
202 if (lock_if_free (lfname
) <= 0)
203 /* Return now if we have locked it, or if lock dir does not exist */
206 /* Else consider breaking the lock */
207 attack
= call2 (intern ("ask-user-about-lock"), fn
,
208 lock_file_owner_name (lfname
));
210 /* User says take the lock */
212 lock_superlock (lfname
);
213 lock_file_1 (lfname
, O_WRONLY
) ;
214 unlink (superlock_path
);
217 /* User says ignore the lock */
220 /* Lock the lock file named LFNAME.
221 If MODE is O_WRONLY, we do so even if it is already locked.
222 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
223 Return 1 if successful, 0 if not. */
226 lock_file_1 (lfname
, mode
)
227 int mode
; char *lfname
;
232 if ((fd
= open (lfname
, mode
, 0666)) >= 0)
235 chmod (lfname
, 0666);
239 sprintf (buf
, "%d ", getpid ());
240 write (fd
, buf
, strlen (buf
));
248 /* Lock the lock named LFNAME if possible.
249 Return 0 in that case.
250 Return positive if lock is really locked by someone else.
251 Return -1 if cannot lock for any other reason. */
254 lock_if_free (lfname
)
255 register char *lfname
;
257 register int clasher
;
259 while (lock_file_1 (lfname
, O_WRONLY
| O_EXCL
| O_CREAT
) == 0)
263 clasher
= current_lock_owner (lfname
);
265 if (clasher
!= getpid ())
268 /* Try again to lock it */
273 /* Return the pid of the process that claims to own the lock file LFNAME,
274 or 0 if nobody does or the lock is obsolete,
275 or -1 if something is wrong with the locking mechanism. */
278 current_lock_owner (lfname
)
281 int owner
= current_lock_owner_1 (lfname
);
282 if (owner
== 0 && errno
== ENOENT
)
284 /* Is it locked by a process that exists? */
285 if (owner
!= 0 && (kill (owner
, 0) >= 0 || errno
== EPERM
))
287 if (unlink (lfname
) < 0)
293 current_lock_owner_1 (lfname
)
300 fd
= open (lfname
, O_RDONLY
, 0666);
303 tem
= read (fd
, buf
, sizeof buf
);
305 return (tem
<= 0 ? 0 : atoi (buf
));
311 register Lisp_Object fn
;
313 register char *lfname
;
315 MAKE_LOCK_PATH (lfname
, fn
);
317 lock_superlock (lfname
);
319 if (current_lock_owner_1 (lfname
) == getpid ())
322 unlink (superlock_path
);
325 lock_superlock (lfname
)
330 for (i
= -20; i
< 0 && (fd
= open (superlock_path
,
331 O_WRONLY
| O_EXCL
| O_CREAT
, 0666)) < 0;
341 chmod (superlock_path
, 0666);
345 write (fd
, lfname
, strlen (lfname
));
353 register Lisp_Object tail
;
354 register struct buffer
*b
;
356 for (tail
= Vbuffer_alist
; XGCTYPE (tail
) == Lisp_Cons
;
357 tail
= XCONS (tail
)->cdr
)
359 b
= XBUFFER (XCONS (XCONS (tail
)->car
)->cdr
);
360 if (XTYPE (b
->filename
) == Lisp_String
&&
361 b
->save_modified
< BUF_MODIFF (b
))
362 unlock_file (b
->filename
);
367 DEFUN ("lock-buffer", Flock_buffer
, Slock_buffer
,
369 "Lock FILE, if current buffer is modified.\n\
370 FILE defaults to current buffer's visited file,\n\
371 or else nothing is done if current buffer isn't visiting a file.")
376 fn
= current_buffer
->filename
;
378 CHECK_STRING (fn
, 0);
379 if (current_buffer
->save_modified
< MODIFF
385 DEFUN ("unlock-buffer", Funlock_buffer
, Sunlock_buffer
,
387 "Unlock the file visited in the current buffer,\n\
388 if it should normally be locked.")
391 if (current_buffer
->save_modified
< MODIFF
&&
392 XTYPE (current_buffer
->filename
) == Lisp_String
)
393 unlock_file (current_buffer
->filename
);
398 /* Unlock the file visited in buffer BUFFER. */
400 unlock_buffer (buffer
)
401 struct buffer
*buffer
;
403 if (buffer
->save_modified
< BUF_MODIFF (buffer
) &&
404 XTYPE (buffer
->filename
) == Lisp_String
)
405 unlock_file (buffer
->filename
);
408 DEFUN ("file-locked-p", Ffile_locked_p
, Sfile_locked_p
, 0, 1, 0,
409 "Return nil if the FILENAME is not locked,\n\
410 t if it is locked by you, else a string of the name of the locker.")
414 register char *lfname
;
417 fn
= Fexpand_file_name (fn
, Qnil
);
419 MAKE_LOCK_PATH (lfname
, fn
);
421 owner
= current_lock_owner (lfname
);
424 else if (owner
== getpid ())
427 return (lock_file_owner_name (lfname
));
431 /* Initialization functions. */
435 lock_path
= egetenv ("EMACSLOCKDIR");
437 lock_path
= PATH_LOCK
;
439 /* Make sure it ends with a slash. */
440 if (lock_path
[strlen (lock_path
) - 1] != '/')
442 char *new_path
= (char *) xmalloc (strlen (lock_path
) + 2);
443 strcpy (new_path
, lock_path
);
444 lock_path
= new_path
;
445 strcat (lock_path
, "/");
448 superlock_path
= (char *) xmalloc ((strlen (lock_path
)
449 + sizeof (SUPERLOCK_NAME
)));
450 strcpy (superlock_path
, lock_path
);
451 strcat (superlock_path
, SUPERLOCK_NAME
);
456 defsubr (&Sunlock_buffer
);
457 defsubr (&Slock_buffer
);
458 defsubr (&Sfile_locked_p
);
461 #endif /* CLASH_DETECTION */