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 {
|
|
199 /* This function can GC */
|
|
200 REGISTER Lisp_Object attack, orig_fn;
|
|
201 REGISTER char *lfname;
|
|
202 struct gcpro gcpro1, gcpro2;
|
|
203 Lisp_Object subject_buf = Qnil;
|
|
204
|
|
205 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
|
|
206 return;
|
|
207 CHECK_STRING (fn);
|
|
208 CHECK_STRING (Vlock_directory);
|
|
209
|
|
210 GCPRO2 (fn, subject_buf);
|
|
211 orig_fn = fn;
|
|
212 fn = Fexpand_file_name (fn, Qnil);
|
|
213
|
|
214 /* Create the name of the lock-file for file fn */
|
|
215 MAKE_LOCK_NAME (lfname, fn);
|
|
216
|
|
217 /* See if this file is visited and has changed on disk since it was
|
|
218 visited. */
|
|
219 subject_buf = Fget_file_buffer (fn);
|
|
220 if (!NILP (subject_buf)
|
|
221 && NILP (Fverify_visited_file_modtime (subject_buf))
|
|
222 && !NILP (Ffile_exists_p (fn)))
|
|
223 call1_in_buffer (XBUFFER (subject_buf),
|
|
224 Qask_user_about_supersession_threat, fn);
|
|
225
|
|
226 /* Try to lock the lock. */
|
|
227 if (lock_if_free (lfname) <= 0)
|
|
228 /* Return now if we have locked it, or if lock dir does not exist */
|
|
229 goto done;
|
|
230
|
|
231 /* Else consider breaking the lock */
|
|
232 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
|
|
233 current_buffer, Qask_user_about_lock, fn,
|
|
234 lock_file_owner_name (lfname));
|
|
235 if (!NILP (attack))
|
|
236 /* User says take the lock */
|
|
237 {
|
|
238 CHECK_STRING (Vsuperlock_file);
|
|
239 lock_superlock (lfname);
|
|
240 lock_file_1 (lfname, O_WRONLY);
|
14
|
241 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
|
0
|
242 goto done;
|
|
243 }
|
|
244 /* User says ignore the lock */
|
|
245 done:
|
|
246 UNGCPRO;
|
|
247 }
|
|
248
|
|
249
|
|
250 /* Lock the lock file named LFNAME.
|
|
251 If MODE is O_WRONLY, we do so even if it is already locked.
|
|
252 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
|
|
253 Return 1 if successful, 0 if not. */
|
|
254
|
|
255 static int
|
|
256 lock_file_1 (CONST char *lfname, int mode)
|
|
257 {
|
|
258 REGISTER int fd;
|
|
259 char buf[20];
|
|
260
|
|
261 if ((fd = open (lfname, mode, 0666)) >= 0)
|
|
262 {
|
|
263 #ifdef USG
|
|
264 chmod (lfname, 0666);
|
|
265 #else
|
|
266 fchmod (fd, 0666);
|
|
267 #endif
|
|
268 sprintf (buf, "%ld ", (long) getpid ());
|
|
269 write (fd, buf, strlen (buf));
|
|
270 close (fd);
|
|
271 return 1;
|
|
272 }
|
|
273 else
|
|
274 return 0;
|
|
275 }
|
|
276
|
|
277 /* Lock the lock named LFNAME if possible.
|
|
278 Return 0 in that case.
|
|
279 Return positive if lock is really locked by someone else.
|
|
280 Return -1 if cannot lock for any other reason. */
|
|
281
|
|
282 static int
|
|
283 lock_if_free (CONST char *lfname)
|
|
284 {
|
|
285 REGISTER int clasher;
|
|
286
|
|
287 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
|
|
288 {
|
|
289 if (errno != EEXIST)
|
|
290 return -1;
|
|
291 clasher = current_lock_owner (lfname);
|
|
292 if (clasher != 0)
|
|
293 if (clasher != getpid ())
|
|
294 return (clasher);
|
|
295 else return (0);
|
|
296 /* Try again to lock it */
|
|
297 }
|
|
298 return 0;
|
|
299 }
|
|
300
|
|
301 /* Return the pid of the process that claims to own the lock file LFNAME,
|
|
302 or 0 if nobody does or the lock is obsolete,
|
|
303 or -1 if something is wrong with the locking mechanism. */
|
|
304
|
|
305 static int
|
|
306 current_lock_owner (CONST char *lfname)
|
|
307 {
|
|
308 int owner = current_lock_owner_1 (lfname);
|
|
309 if (owner == 0 && errno == ENOENT)
|
|
310 return (0);
|
|
311 /* Is it locked by a process that exists? */
|
|
312 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
|
|
313 return (owner);
|
|
314 if (unlink (lfname) < 0)
|
|
315 return (-1);
|
|
316 return (0);
|
|
317 }
|
|
318
|
|
319 static int
|
|
320 current_lock_owner_1 (CONST char *lfname)
|
|
321 {
|
|
322 REGISTER int fd;
|
|
323 char buf[20];
|
|
324 int tem;
|
|
325
|
|
326 fd = open (lfname, O_RDONLY, 0666);
|
|
327 if (fd < 0)
|
|
328 return 0;
|
|
329 tem = read (fd, buf, sizeof buf);
|
|
330 close (fd);
|
|
331 return (tem <= 0 ? 0 : atoi (buf));
|
|
332 }
|
|
333
|
|
334
|
|
335 void
|
|
336 unlock_file (Lisp_Object fn)
|
|
337 {
|
|
338 REGISTER char *lfname;
|
|
339 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
|
|
340 CHECK_STRING (fn);
|
|
341 CHECK_STRING (Vlock_directory);
|
|
342 CHECK_STRING (Vsuperlock_file);
|
|
343
|
|
344 fn = Fexpand_file_name (fn, Qnil);
|
|
345
|
|
346 MAKE_LOCK_NAME (lfname, fn);
|
|
347
|
|
348 lock_superlock (lfname);
|
|
349
|
|
350 if (current_lock_owner_1 (lfname) == getpid ())
|
|
351 unlink (lfname);
|
|
352
|
14
|
353 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
|
0
|
354 }
|
|
355
|
|
356 static void
|
|
357 lock_superlock (CONST char *lfname)
|
|
358 {
|
|
359 REGISTER int i, fd;
|
|
360 DIR *lockdir;
|
|
361
|
|
362 for (i = -20; i < 0 &&
|
14
|
363 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
|
0
|
364 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
|
|
365 i++)
|
|
366 {
|
|
367 if (errno != EEXIST)
|
|
368 return;
|
|
369
|
|
370 /* This seems to be necessary to prevent Emacs from hanging when the
|
|
371 competing process has already deleted the superlock, but it's still
|
|
372 in the NFS cache. So we force NFS to synchronize the cache. */
|
14
|
373 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
|
0
|
374 if (lockdir)
|
|
375 closedir (lockdir);
|
|
376
|
|
377 emacs_sleep (1);
|
|
378 }
|
|
379 if (fd >= 0)
|
|
380 {
|
|
381 #ifdef USG
|
14
|
382 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
|
0
|
383 #else
|
|
384 fchmod (fd, 0666);
|
|
385 #endif
|
|
386 write (fd, lfname, strlen (lfname));
|
|
387 close (fd);
|
|
388 }
|
|
389 }
|
|
390
|
|
391 void
|
|
392 unlock_all_files (void)
|
|
393 {
|
|
394 REGISTER Lisp_Object tail;
|
|
395 REGISTER struct buffer *b;
|
|
396
|
|
397 for (tail = Vbuffer_alist; GC_CONSP (tail);
|
|
398 tail = XCDR (tail))
|
|
399 {
|
|
400 b = XBUFFER (XCDR (XCAR (tail)));
|
|
401 if (STRINGP (b->file_truename) &&
|
|
402 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
|
|
403 unlock_file (b->file_truename);
|
|
404 }
|
|
405 }
|
|
406
|
|
407
|
2
|
408 DEFUN ("lock-buffer", Flock_buffer, Slock_buffer, 0, 1, 0 /*
|
0
|
409 Lock FILE, if current buffer is modified.
|
|
410 FILE defaults to current buffer's visited file,
|
|
411 or else nothing is done if current buffer isn't visiting a file.
|
|
412 */ )
|
|
413 (fn)
|
|
414 Lisp_Object fn;
|
|
415 {
|
|
416 /* This function can GC */
|
|
417 if (NILP (fn))
|
|
418 fn = current_buffer->file_truename;
|
|
419 CHECK_STRING (fn);
|
|
420 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
|
|
421 && !NILP (fn))
|
|
422 lock_file (fn);
|
|
423 return Qnil;
|
|
424 }
|
|
425
|
2
|
426 DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer, 0, 0, 0 /*
|
0
|
427 Unlock the file visited in the current buffer,
|
|
428 if it should normally be locked.
|
|
429 */ )
|
|
430 ()
|
|
431 {
|
|
432 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
|
|
433 && STRINGP (current_buffer->file_truename))
|
|
434 unlock_file (current_buffer->file_truename);
|
|
435 return Qnil;
|
|
436 }
|
|
437
|
|
438
|
|
439 /* Unlock the file visited in buffer BUFFER. */
|
|
440
|
|
441 void
|
|
442 unlock_buffer (struct buffer *buffer)
|
|
443 {
|
|
444 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
|
|
445 && STRINGP (buffer->file_truename))
|
|
446 unlock_file (buffer->file_truename);
|
|
447 }
|
|
448
|
|
449 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0 /*
|
|
450 Return nil if the FILENAME is not locked,
|
|
451 t if it is locked by you, else a string of the name of the locker.
|
|
452 */ )
|
|
453 (fn)
|
|
454 Lisp_Object fn;
|
|
455 {
|
|
456 /* This function can GC */
|
|
457 REGISTER char *lfname;
|
|
458 int owner;
|
|
459
|
|
460 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
|
|
461 return Qnil;
|
|
462 CHECK_STRING (Vlock_directory);
|
|
463
|
|
464 fn = Fexpand_file_name (fn, Qnil);
|
|
465
|
|
466 MAKE_LOCK_NAME (lfname, fn);
|
|
467
|
|
468 owner = current_lock_owner (lfname);
|
|
469 if (owner <= 0)
|
|
470 return (Qnil);
|
|
471 else if (owner == getpid ())
|
|
472 return (Qt);
|
|
473
|
|
474 return (lock_file_owner_name (lfname));
|
|
475 }
|
|
476
|
|
477 void
|
|
478 syms_of_filelock (void)
|
|
479 {
|
|
480 /* This function can GC */
|
|
481 defsubr (&Sunlock_buffer);
|
|
482 defsubr (&Slock_buffer);
|
|
483 defsubr (&Sfile_locked_p);
|
|
484
|
|
485 defsymbol (&Qask_user_about_supersession_threat,
|
|
486 "ask-user-about-supersession-threat");
|
|
487 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
|
|
488 }
|
|
489
|
|
490 void
|
|
491 vars_of_filelock (void)
|
|
492 {
|
|
493 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
|
|
494 Don't change this
|
|
495 */ );
|
|
496 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
|
|
497 Don't change this
|
|
498 */ );
|
|
499 }
|
|
500
|
|
501 void
|
|
502 complex_vars_of_filelock (void)
|
|
503 {
|
|
504 #ifdef PATH_LOCK
|
|
505 Vlock_directory =
|
|
506 Ffile_name_as_directory (build_string (PATH_LOCK));
|
|
507 #else
|
|
508 Vlock_directory = Qnil;
|
|
509 #endif
|
|
510 #ifdef PATH_SUPERLOCK
|
|
511 Vsuperlock_file =
|
|
512 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
|
|
513 #else
|
|
514 Vsuperlock_file = Qnil;
|
|
515 #endif
|
|
516 /* All the rest done dynamically by startup.el */
|
|
517 }
|
|
518
|
|
519 #endif /* CLASH_DETECTION */
|