0
|
1 /* Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
|
|
2 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.30. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25
|
|
26 #include "buffer.h"
|
|
27 #include "paths.h"
|
|
28
|
|
29 #include "sysfile.h"
|
|
30 #include "sysdir.h"
|
|
31 #include "syspwd.h"
|
|
32 #include "syssignal.h" /* for kill */
|
|
33
|
|
34
|
|
35 #ifdef CLASH_DETECTION
|
|
36
|
|
37 /* FSFmacs uses char *lock_dir and char *superlock_file instead of
|
|
38 the Lisp variables we use. */
|
|
39
|
|
40 /* The name of the directory in which we keep lock files, with a '/'
|
|
41 appended. */
|
|
42 Lisp_Object Vlock_directory;
|
|
43
|
|
44 #if 0 /* FSFmacs */
|
|
45 /* Look in startup.el */
|
|
46 /* The name of the file in the lock directory which is used to
|
|
47 arbitrate access to the entire directory. */
|
|
48 #define SUPERLOCK_NAME "!!!SuperLock!!!"
|
|
49 #endif
|
|
50
|
|
51 /* The name of the superlock file. This is SUPERLOCK_NAME appended to
|
|
52 Vlock_directory. */
|
|
53 Lisp_Object Vsuperlock_file;
|
|
54
|
|
55 Lisp_Object Qask_user_about_supersession_threat;
|
|
56 Lisp_Object Qask_user_about_lock;
|
|
57
|
|
58 static void lock_superlock (CONST char *lfname);
|
|
59 static int lock_file_1 (CONST char *lfname, int mode);
|
|
60 static int lock_if_free (CONST char *lfname);
|
|
61 static int current_lock_owner (CONST char *);
|
|
62 static int current_lock_owner_1 (CONST char *);
|
|
63
|
|
64 /* Set LOCK to the name of the lock file for the filename FILE.
|
|
65 char *LOCK; Lisp_Object FILE;
|
|
66
|
|
67 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory
|
|
68 is a string. */
|
|
69
|
|
70 #ifndef HAVE_LONG_FILE_NAMES
|
|
71
|
14
|
72 #define MAKE_LOCK_NAME(lock, file) \
|
|
73 (lock = (char *) alloca (14 + XSTRING_LENGTH (Vlock_directory) + 1), \
|
0
|
74 fill_in_lock_short_file_name (lock, (file)))
|
|
75
|
|
76 static void
|
|
77 fill_in_lock_short_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
|
|
78 {
|
|
79 REGISTER union
|
|
80 {
|
|
81 unsigned int word [2];
|
|
82 unsigned char byte [8];
|
|
83 } crc;
|
|
84 REGISTER unsigned char *p, new;
|
|
85
|
|
86 CHECK_STRING (Vlock_directory);
|
|
87
|
|
88 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
|
|
89 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
|
|
90
|
|
91 crc.word[0] = crc.word[1] = 0;
|
|
92
|
14
|
93 for (p = XSTRING_DATA (fn); new = *p++; )
|
0
|
94 {
|
|
95 new += crc.byte[6];
|
|
96 crc.byte[6] = crc.byte[5] + new;
|
|
97 crc.byte[5] = crc.byte[4];
|
|
98 crc.byte[4] = crc.byte[3];
|
|
99 crc.byte[3] = crc.byte[2] + new;
|
|
100 crc.byte[2] = crc.byte[1];
|
|
101 crc.byte[1] = crc.byte[0];
|
|
102 crc.byte[0] = new;
|
|
103 }
|
|
104
|
|
105 {
|
|
106 int need_slash = 0;
|
|
107
|
|
108 /* in case lock-directory doesn't end in / */
|
14
|
109 if (XSTRING_BYTE (Vlock_directory,
|
|
110 XSTRING_LENGTH (Vlock_directory) - 1) != '/')
|
0
|
111 need_slash = 1;
|
|
112
|
|
113 sprintf (lockfile, "%s%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
|
14
|
114 (char *) XSTRING_DATA (Vlock_directory),
|
0
|
115 need_slash ? "/" : "",
|
|
116 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
|
|
117 crc.byte[4], crc.byte[5], crc.byte[6]);
|
|
118 }
|
|
119 }
|
|
120
|
|
121 #else /* defined HAVE_LONG_FILE_NAMES */
|
|
122
|
|
123 /* +2 for terminating null and possible extra slash */
|
14
|
124 #define MAKE_LOCK_NAME(lock, file) \
|
|
125 (lock = (char *) alloca (XSTRING_LENGTH (file) + \
|
|
126 XSTRING_LENGTH (Vlock_directory) + 2), \
|
0
|
127 fill_in_lock_file_name (lock, (file)))
|
|
128
|
|
129 static void
|
|
130 fill_in_lock_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
|
|
131 /* fn must be a Lisp_String! */
|
|
132 {
|
|
133 REGISTER char *p;
|
|
134
|
|
135 CHECK_STRING (Vlock_directory);
|
|
136
|
14
|
137 strcpy (lockfile, (char *) XSTRING_DATA (Vlock_directory));
|
0
|
138
|
|
139 p = lockfile + strlen (lockfile);
|
|
140
|
|
141 if (p == lockfile /* lock-directory is empty?? */
|
|
142 || *(p - 1) != '/') /* in case lock-directory doesn't end in / */
|
|
143 {
|
|
144 *p = '/';
|
|
145 p++;
|
|
146 }
|
|
147
|
14
|
148 strcpy (p, (char *) XSTRING_DATA (fn));
|
0
|
149
|
|
150 for (; *p; p++)
|
|
151 {
|
|
152 if (*p == '/')
|
|
153 *p = '!';
|
|
154 }
|
|
155 }
|
|
156 #endif /* !defined HAVE_LONG_FILE_NAMES */
|
|
157
|
|
158 static Lisp_Object
|
|
159 lock_file_owner_name (CONST char *lfname)
|
|
160 {
|
|
161 struct stat s;
|
|
162 struct passwd *the_pw = 0;
|
|
163
|
|
164 if (lstat (lfname, &s) == 0)
|
|
165 the_pw = getpwuid (s.st_uid);
|
|
166 return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
|
|
167 }
|
|
168
|
|
169
|
|
170 /* lock_file locks file fn,
|
|
171 meaning it serves notice on the world that you intend to edit that file.
|
|
172 This should be done only when about to modify a file-visiting
|
|
173 buffer previously unmodified.
|
|
174 Do not (normally) call lock_buffer for a buffer already modified,
|
|
175 as either the file is already locked, or the user has already
|
|
176 decided to go ahead without locking.
|
|
177
|
|
178 When lock_buffer returns, either the lock is locked for us,
|
|
179 or the user has said to go ahead without locking.
|
|
180
|
|
181 If the file is locked by someone else, lock_buffer calls
|
|
182 ask-user-about-lock (a Lisp function) with two arguments,
|
|
183 the file name and the name of the user who did the locking.
|
|
184 This function can signal an error, or return t meaning
|
|
185 take away the lock, or return nil meaning ignore the lock. */
|
|
186
|
|
187 /* The lock file name is the file name with "/" replaced by "!"
|
|
188 and put in the Emacs lock directory. */
|
|
189 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
|
|
190
|
|
191 /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
|
|
192 representation of a 14-bytes CRC generated from the file name
|
|
193 and put in the Emacs lock directory (not very nice, but it works).
|
|
194 (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */
|
|
195
|
|
196 void
|
|
197 lock_file (Lisp_Object fn)
|
|
198 {
|
40
|
199 /* This function can GC. */
|
|
200 /* dmoore - and can destroy current_buffer and all sorts of other
|
|
201 mean nasty things with pointy teeth. If you call this make sure
|
|
202 you protect things right. */
|
|
203
|
0
|
204 REGISTER Lisp_Object attack, orig_fn;
|
|
205 REGISTER char *lfname;
|
|
206 struct gcpro gcpro1, gcpro2;
|
|
207 Lisp_Object subject_buf = Qnil;
|
|
208
|
|
209 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
|
|
210 return;
|
|
211 CHECK_STRING (fn);
|
|
212 CHECK_STRING (Vlock_directory);
|
|
213
|
|
214 GCPRO2 (fn, subject_buf);
|
|
215 orig_fn = fn;
|
|
216 fn = Fexpand_file_name (fn, Qnil);
|
|
217
|
|
218 /* Create the name of the lock-file for file fn */
|
|
219 MAKE_LOCK_NAME (lfname, fn);
|
|
220
|
|
221 /* See if this file is visited and has changed on disk since it was
|
|
222 visited. */
|
|
223 subject_buf = Fget_file_buffer (fn);
|
|
224 if (!NILP (subject_buf)
|
|
225 && NILP (Fverify_visited_file_modtime (subject_buf))
|
|
226 && !NILP (Ffile_exists_p (fn)))
|
|
227 call1_in_buffer (XBUFFER (subject_buf),
|
|
228 Qask_user_about_supersession_threat, fn);
|
|
229
|
|
230 /* Try to lock the lock. */
|
|
231 if (lock_if_free (lfname) <= 0)
|
|
232 /* Return now if we have locked it, or if lock dir does not exist */
|
|
233 goto done;
|
|
234
|
|
235 /* Else consider breaking the lock */
|
|
236 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
|
|
237 current_buffer, Qask_user_about_lock, fn,
|
|
238 lock_file_owner_name (lfname));
|
|
239 if (!NILP (attack))
|
|
240 /* User says take the lock */
|
|
241 {
|
|
242 CHECK_STRING (Vsuperlock_file);
|
|
243 lock_superlock (lfname);
|
|
244 lock_file_1 (lfname, O_WRONLY);
|
14
|
245 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
|
0
|
246 goto done;
|
|
247 }
|
|
248 /* User says ignore the lock */
|
|
249 done:
|
|
250 UNGCPRO;
|
|
251 }
|
|
252
|
|
253
|
|
254 /* Lock the lock file named LFNAME.
|
|
255 If MODE is O_WRONLY, we do so even if it is already locked.
|
|
256 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
|
|
257 Return 1 if successful, 0 if not. */
|
|
258
|
|
259 static int
|
|
260 lock_file_1 (CONST char *lfname, int mode)
|
|
261 {
|
|
262 REGISTER int fd;
|
|
263 char buf[20];
|
|
264
|
|
265 if ((fd = open (lfname, mode, 0666)) >= 0)
|
|
266 {
|
|
267 #ifdef USG
|
|
268 chmod (lfname, 0666);
|
|
269 #else
|
|
270 fchmod (fd, 0666);
|
|
271 #endif
|
|
272 sprintf (buf, "%ld ", (long) getpid ());
|
|
273 write (fd, buf, strlen (buf));
|
|
274 close (fd);
|
|
275 return 1;
|
|
276 }
|
|
277 else
|
|
278 return 0;
|
|
279 }
|
|
280
|
|
281 /* Lock the lock named LFNAME if possible.
|
|
282 Return 0 in that case.
|
|
283 Return positive if lock is really locked by someone else.
|
|
284 Return -1 if cannot lock for any other reason. */
|
|
285
|
|
286 static int
|
|
287 lock_if_free (CONST char *lfname)
|
|
288 {
|
|
289 REGISTER int clasher;
|
|
290
|
|
291 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
|
|
292 {
|
|
293 if (errno != EEXIST)
|
|
294 return -1;
|
|
295 clasher = current_lock_owner (lfname);
|
|
296 if (clasher != 0)
|
|
297 if (clasher != getpid ())
|
|
298 return (clasher);
|
|
299 else return (0);
|
|
300 /* Try again to lock it */
|
|
301 }
|
|
302 return 0;
|
|
303 }
|
|
304
|
|
305 /* Return the pid of the process that claims to own the lock file LFNAME,
|
|
306 or 0 if nobody does or the lock is obsolete,
|
|
307 or -1 if something is wrong with the locking mechanism. */
|
|
308
|
|
309 static int
|
|
310 current_lock_owner (CONST char *lfname)
|
|
311 {
|
|
312 int owner = current_lock_owner_1 (lfname);
|
|
313 if (owner == 0 && errno == ENOENT)
|
|
314 return (0);
|
|
315 /* Is it locked by a process that exists? */
|
|
316 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
|
|
317 return (owner);
|
|
318 if (unlink (lfname) < 0)
|
|
319 return (-1);
|
|
320 return (0);
|
|
321 }
|
|
322
|
|
323 static int
|
|
324 current_lock_owner_1 (CONST char *lfname)
|
|
325 {
|
|
326 REGISTER int fd;
|
|
327 char buf[20];
|
|
328 int tem;
|
|
329
|
|
330 fd = open (lfname, O_RDONLY, 0666);
|
|
331 if (fd < 0)
|
|
332 return 0;
|
|
333 tem = read (fd, buf, sizeof buf);
|
|
334 close (fd);
|
|
335 return (tem <= 0 ? 0 : atoi (buf));
|
|
336 }
|
|
337
|
|
338
|
|
339 void
|
|
340 unlock_file (Lisp_Object fn)
|
|
341 {
|
40
|
342 /* This function can GC. */
|
|
343 /* dmoore - and can destroy current_buffer and all sorts of other
|
|
344 mean nasty things with pointy teeth. If you call this make sure
|
|
345 you protect things right. */
|
|
346
|
0
|
347 REGISTER char *lfname;
|
|
348 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
|
|
349 CHECK_STRING (fn);
|
|
350 CHECK_STRING (Vlock_directory);
|
|
351 CHECK_STRING (Vsuperlock_file);
|
|
352
|
|
353 fn = Fexpand_file_name (fn, Qnil);
|
|
354
|
|
355 MAKE_LOCK_NAME (lfname, fn);
|
|
356
|
|
357 lock_superlock (lfname);
|
|
358
|
|
359 if (current_lock_owner_1 (lfname) == getpid ())
|
|
360 unlink (lfname);
|
|
361
|
14
|
362 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
|
0
|
363 }
|
|
364
|
|
365 static void
|
|
366 lock_superlock (CONST char *lfname)
|
|
367 {
|
|
368 REGISTER int i, fd;
|
|
369 DIR *lockdir;
|
|
370
|
|
371 for (i = -20; i < 0 &&
|
14
|
372 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
|
0
|
373 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
|
|
374 i++)
|
|
375 {
|
|
376 if (errno != EEXIST)
|
|
377 return;
|
|
378
|
|
379 /* This seems to be necessary to prevent Emacs from hanging when the
|
|
380 competing process has already deleted the superlock, but it's still
|
|
381 in the NFS cache. So we force NFS to synchronize the cache. */
|
14
|
382 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
|
0
|
383 if (lockdir)
|
|
384 closedir (lockdir);
|
|
385
|
|
386 emacs_sleep (1);
|
|
387 }
|
|
388 if (fd >= 0)
|
|
389 {
|
|
390 #ifdef USG
|
14
|
391 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
|
0
|
392 #else
|
|
393 fchmod (fd, 0666);
|
|
394 #endif
|
|
395 write (fd, lfname, strlen (lfname));
|
|
396 close (fd);
|
|
397 }
|
|
398 }
|
|
399
|
|
400 void
|
|
401 unlock_all_files (void)
|
|
402 {
|
40
|
403 /* This function can GC. */
|
|
404
|
|
405 Lisp_Object tail;
|
0
|
406 REGISTER struct buffer *b;
|
40
|
407 struct gcpro gcpro1;
|
0
|
408
|
40
|
409 GCPRO1 (tail);
|
0
|
410 for (tail = Vbuffer_alist; GC_CONSP (tail);
|
|
411 tail = XCDR (tail))
|
|
412 {
|
|
413 b = XBUFFER (XCDR (XCAR (tail)));
|
|
414 if (STRINGP (b->file_truename) &&
|
|
415 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
|
|
416 unlock_file (b->file_truename);
|
|
417 }
|
40
|
418 UNGCPRO;
|
0
|
419 }
|
|
420
|
|
421
|
20
|
422 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
|
0
|
423 Lock FILE, if current buffer is modified.
|
|
424 FILE defaults to current buffer's visited file,
|
|
425 or else nothing is done if current buffer isn't visiting a file.
|
20
|
426 */
|
|
427 (fn))
|
0
|
428 {
|
|
429 /* This function can GC */
|
40
|
430 /* dmoore - and can destroy current_buffer and all sorts of other
|
|
431 mean nasty things with pointy teeth. If you call this make sure
|
|
432 you protect things right. */
|
|
433
|
0
|
434 if (NILP (fn))
|
|
435 fn = current_buffer->file_truename;
|
|
436 CHECK_STRING (fn);
|
|
437 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
|
|
438 && !NILP (fn))
|
|
439 lock_file (fn);
|
|
440 return Qnil;
|
|
441 }
|
|
442
|
20
|
443 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
|
0
|
444 Unlock the file visited in the current buffer,
|
|
445 if it should normally be locked.
|
20
|
446 */
|
|
447 ())
|
0
|
448 {
|
40
|
449 /* This function can GC */
|
|
450 /* dmoore - and can destroy current_buffer and all sorts of other
|
|
451 mean nasty things with pointy teeth. If you call this make sure
|
|
452 you protect things right. */
|
|
453
|
0
|
454 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
|
|
455 && STRINGP (current_buffer->file_truename))
|
|
456 unlock_file (current_buffer->file_truename);
|
|
457 return Qnil;
|
|
458 }
|
|
459
|
|
460
|
|
461 /* Unlock the file visited in buffer BUFFER. */
|
|
462
|
|
463 void
|
|
464 unlock_buffer (struct buffer *buffer)
|
|
465 {
|
40
|
466 /* This function can GC */
|
|
467 /* dmoore - and can destroy current_buffer and all sorts of other
|
|
468 mean nasty things with pointy teeth. If you call this make sure
|
|
469 you protect things right. */
|
0
|
470 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
|
|
471 && STRINGP (buffer->file_truename))
|
|
472 unlock_file (buffer->file_truename);
|
|
473 }
|
|
474
|
20
|
475 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
|
0
|
476 Return nil if the FILENAME is not locked,
|
|
477 t if it is locked by you, else a string of the name of the locker.
|
20
|
478 */
|
|
479 (fn))
|
0
|
480 {
|
|
481 /* This function can GC */
|
|
482 REGISTER char *lfname;
|
|
483 int owner;
|
|
484
|
|
485 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
|
|
486 return Qnil;
|
|
487 CHECK_STRING (Vlock_directory);
|
|
488
|
|
489 fn = Fexpand_file_name (fn, Qnil);
|
|
490
|
|
491 MAKE_LOCK_NAME (lfname, fn);
|
|
492
|
|
493 owner = current_lock_owner (lfname);
|
|
494 if (owner <= 0)
|
|
495 return (Qnil);
|
|
496 else if (owner == getpid ())
|
|
497 return (Qt);
|
|
498
|
|
499 return (lock_file_owner_name (lfname));
|
|
500 }
|
|
501
|
|
502 void
|
|
503 syms_of_filelock (void)
|
|
504 {
|
|
505 /* This function can GC */
|
20
|
506 DEFSUBR (Funlock_buffer);
|
|
507 DEFSUBR (Flock_buffer);
|
|
508 DEFSUBR (Ffile_locked_p);
|
0
|
509
|
|
510 defsymbol (&Qask_user_about_supersession_threat,
|
|
511 "ask-user-about-supersession-threat");
|
|
512 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
|
|
513 }
|
|
514
|
|
515 void
|
|
516 vars_of_filelock (void)
|
|
517 {
|
|
518 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
|
|
519 Don't change this
|
|
520 */ );
|
|
521 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
|
|
522 Don't change this
|
|
523 */ );
|
|
524 }
|
|
525
|
|
526 void
|
|
527 complex_vars_of_filelock (void)
|
|
528 {
|
|
529 #ifdef PATH_LOCK
|
|
530 Vlock_directory =
|
|
531 Ffile_name_as_directory (build_string (PATH_LOCK));
|
|
532 #else
|
|
533 Vlock_directory = Qnil;
|
|
534 #endif
|
|
535 #ifdef PATH_SUPERLOCK
|
|
536 Vsuperlock_file =
|
|
537 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
|
|
538 #else
|
|
539 Vsuperlock_file = Qnil;
|
|
540 #endif
|
|
541 /* All the rest done dynamically by startup.el */
|
|
542 }
|
|
543
|
|
544 #endif /* CLASH_DETECTION */
|