diff options
| author | Richard M. Stallman | 1996-12-31 22:11:05 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1996-12-31 22:11:05 +0000 |
| commit | 8dbbc384daa395eb77880590d9356b92477f5c81 (patch) | |
| tree | 072b5b9d09d5e5abf3255b6e3b0f1716f34cef43 /src | |
| parent | 570591721b7eadd42e1119bbb2ad4b1d628d7d9b (diff) | |
| download | emacs-8dbbc384daa395eb77880590d9356b92477f5c81.tar.gz emacs-8dbbc384daa395eb77880590d9356b92477f5c81.zip | |
Total rewrite.
Diffstat (limited to 'src')
| -rw-r--r-- | src/filelock.c | 544 |
1 files changed, 252 insertions, 292 deletions
diff --git a/src/filelock.c b/src/filelock.c index 6c1b6b2a1c4..d30571a3598 100644 --- a/src/filelock.c +++ b/src/filelock.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc. | 1 | /* Copyright (C) 1985, 86, 87, 93, 94, 96 Free Software Foundation, Inc. |
| 2 | 2 | ||
| 3 | This file is part of GNU Emacs. | 3 | This file is part of GNU Emacs. |
| 4 | 4 | ||
| @@ -26,189 +26,297 @@ Boston, MA 02111-1307, USA. */ | |||
| 26 | #include "vms-pwd.h" | 26 | #include "vms-pwd.h" |
| 27 | #else | 27 | #else |
| 28 | #include <pwd.h> | 28 | #include <pwd.h> |
| 29 | #endif | 29 | #endif /* not VMS */ |
| 30 | 30 | ||
| 31 | #include <errno.h> | ||
| 32 | #include <sys/file.h> | 31 | #include <sys/file.h> |
| 33 | #ifdef USG | 32 | #ifdef USG |
| 34 | #include <fcntl.h> | 33 | #include <fcntl.h> |
| 34 | #include <string.h> | ||
| 35 | #endif /* USG */ | 35 | #endif /* USG */ |
| 36 | 36 | ||
| 37 | #include "lisp.h" | 37 | #include "lisp.h" |
| 38 | #include <paths.h> | ||
| 39 | #include "buffer.h" | 38 | #include "buffer.h" |
| 40 | 39 | ||
| 41 | #ifdef SYSV_SYSTEM_DIR | 40 | #include <errno.h> |
| 42 | #include <dirent.h> | 41 | #ifndef errno |
| 43 | #else /* not SYSV_SYSTEM_DIR */ | ||
| 44 | #ifdef NONSYSTEM_DIR_LIBRARY | ||
| 45 | #include "ndir.h" | ||
| 46 | #else /* not NONSYSTEM_DIR_LIBRARY */ | ||
| 47 | #ifdef MSDOS | ||
| 48 | #include <dirent.h> | ||
| 49 | #else | ||
| 50 | #include <sys/dir.h> | ||
| 51 | #endif | ||
| 52 | #endif /* not NONSYSTEM_DIR_LIBRARY */ | ||
| 53 | #ifndef MSDOS | ||
| 54 | extern DIR *opendir (); | ||
| 55 | #endif /* not MSDOS */ | ||
| 56 | #endif /* not SYSV_SYSTEM_DIR */ | ||
| 57 | |||
| 58 | extern int errno; | 42 | extern int errno; |
| 59 | |||
| 60 | extern char *egetenv (); | ||
| 61 | extern char *strcpy (); | ||
| 62 | |||
| 63 | #ifdef DECLARE_GETPWUID_WITH_UID_T | ||
| 64 | extern struct passwd *getpwuid (uid_t); | ||
| 65 | #else | ||
| 66 | extern struct passwd *getpwuid (); | ||
| 67 | #endif | 43 | #endif |
| 68 | 44 | ||
| 69 | #ifdef CLASH_DETECTION | 45 | #ifdef CLASH_DETECTION |
| 70 | 46 | ||
| 71 | /* If system does not have symbolic links, it does not have lstat. | 47 | /* The strategy: to lock a file FN, create a symlink .#FN in FN's |
| 72 | In that case, use ordinary stat instead. */ | 48 | directory, with link data `user@host.pid'. This avoids a single |
| 49 | mount (== failure) point for lock files. | ||
| 50 | |||
| 51 | When the host in the lock data is the current host, we can check if | ||
| 52 | the pid is valid with kill. | ||
| 53 | |||
| 54 | Otherwise, we could look at a separate file that maps hostnames to | ||
| 55 | reboot times to see if the remote pid can possibly be valid, since we | ||
| 56 | don't want Emacs to have to communicate via pipes or sockets or | ||
| 57 | whatever to other processes, either locally or remotely; rms says | ||
| 58 | that's too unreliable. Hence the separate file, which could | ||
| 59 | theoretically be updated by daemons running separately -- but this | ||
| 60 | whole idea is unimplemented; in practice, at least in our | ||
| 61 | environment, it seems such stale locks arise fiarly infrequently, and | ||
| 62 | Emacs' standard methods of dealing with clashes suffice. | ||
| 63 | |||
| 64 | We use symlinks instead of normal files because (1) they can be | ||
| 65 | stored more efficiently on the filesystem, since the kernel knows | ||
| 66 | they will be small, and (2) all the info about the lock can be read | ||
| 67 | in a single system call (readlink). Although we could use regular | ||
| 68 | files to be useful on old systems lacking symlinks, noawdays | ||
| 69 | virtually all such systems are probably single-user anyway, so it | ||
| 70 | didn't seem worth the complication. | ||
| 71 | |||
| 72 | Similarly, we don't worry about a possible 14-character limit on | ||
| 73 | file names, because those are all the same systems that don't have | ||
| 74 | symlinks. | ||
| 75 | |||
| 76 | This is compatible with the locking scheme used by Interleaf (which | ||
| 77 | has contributed this implementation for Emacs), and was designed by | ||
| 78 | Ethan Jacobson, Kimbo Mundy, and others. | ||
| 79 | |||
| 80 | --karl@cs.umb.edu/karl@hq.ileaf.com. */ | ||
| 73 | 81 | ||
| 74 | #ifndef S_IFLNK | 82 | |
| 75 | #define lstat stat | 83 | /* Here is the structure that stores information about a lock. */ |
| 76 | #endif | ||
| 77 | |||
| 78 | |||
| 79 | /* The name of the directory in which we keep lock files, with a '/' | ||
| 80 | appended. */ | ||
| 81 | char *lock_dir; | ||
| 82 | 84 | ||
| 83 | /* The name of the file in the lock directory which is used to | 85 | typedef struct |
| 84 | arbitrate access to the entire directory. */ | 86 | { |
| 85 | #define SUPERLOCK_NAME "!!!SuperLock!!!" | 87 | char *user; |
| 88 | char *host; | ||
| 89 | int pid; | ||
| 90 | } lock_info_type; | ||
| 86 | 91 | ||
| 87 | /* The name of the superlock file. This is SUPERLOCK_NAME appended to | 92 | /* When we read the info back, we might need this much more. */ |
| 88 | lock_dir. */ | 93 | #define LOCK_PID_MAX 21 /* enough for signed 64 bits plus null */ |
| 89 | char *superlock_file; | ||
| 90 | 94 | ||
| 91 | /* Set LOCK to the name of the lock file for the filename FILE. | 95 | /* Free the two dynamically-allocated pieces in PTR. */ |
| 92 | char *LOCK; Lisp_Object FILE; */ | 96 | #define FREE_LOCK_INFO(i) do { xfree ((i).user); xfree ((i).host); } while (0) |
| 93 | 97 | ||
| 94 | #ifndef HAVE_LONG_FILE_NAMES | ||
| 95 | 98 | ||
| 99 | /* Write the name of the lock file for FN into LFNAME. Length will be | ||
| 100 | that of FN plus two more for the leading `.#' plus one for the null. */ | ||
| 96 | #define MAKE_LOCK_NAME(lock, file) \ | 101 | #define MAKE_LOCK_NAME(lock, file) \ |
| 97 | (lock = (char *) alloca (14 + strlen (lock_dir) + 1), \ | 102 | (lock = (char *) alloca (XSTRING (file)->size + 2 + 1), \ |
| 98 | fill_in_lock_short_file_name (lock, (file))) | 103 | fill_in_lock_file_name (lock, (file))) |
| 99 | |||
| 100 | 104 | ||
| 101 | fill_in_lock_short_file_name (lockfile, fn) | 105 | static void |
| 106 | fill_in_lock_file_name (lockfile, fn) | ||
| 102 | register char *lockfile; | 107 | register char *lockfile; |
| 103 | register Lisp_Object fn; | 108 | register Lisp_Object fn; |
| 104 | { | 109 | { |
| 105 | register union | 110 | register char *p; |
| 106 | { | 111 | |
| 107 | unsigned int word [2]; | 112 | strcpy (lockfile, XSTRING (fn)->data); |
| 108 | unsigned char byte [8]; | 113 | |
| 109 | } crc; | 114 | /* Shift the nondirectory part of the file name (including the null) |
| 110 | register unsigned char *p, new; | 115 | right two characters. Here is one of the places where we'd have to |
| 116 | do something to support 14-character-max file names. */ | ||
| 117 | for (p = lockfile + strlen (lockfile); p != lockfile && *p != '/'; p--) | ||
| 118 | p[2] = *p; | ||
| 111 | 119 | ||
| 112 | /* 7-bytes cyclic code for burst correction on byte-by-byte basis. | 120 | /* Insert the `.#'. */ |
| 113 | the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */ | 121 | p[1] = '.'; |
| 122 | p[2] = '#'; | ||
| 123 | } | ||
| 114 | 124 | ||
| 115 | crc.word[0] = crc.word[1] = 0; | 125 | /* Lock the lock file named LFNAME. |
| 126 | If FORCE is nonzero, we do so even if it is already locked. | ||
| 127 | Return 1 if successful, 0 if not. */ | ||
| 116 | 128 | ||
| 117 | for (p = XSTRING (fn)->data; new = *p++; ) | 129 | static int |
| 130 | lock_file_1 (lfname, force) | ||
| 131 | char *lfname; | ||
| 132 | int force; | ||
| 133 | { | ||
| 134 | register int err; | ||
| 135 | char *user_name = XSTRING (Fuser_login_name (Qnil))->data; | ||
| 136 | char *host_name = XSTRING (Fsystem_name ())->data; | ||
| 137 | char *lock_info_str = alloca (strlen (user_name) + strlen (host_name) + 21); | ||
| 138 | |||
| 139 | sprintf (lock_info_str, "%s@%s.%d", user_name, host_name, getpid ()); | ||
| 140 | |||
| 141 | err = symlink (lock_info_str, lfname); | ||
| 142 | if (errno == EEXIST && force) | ||
| 118 | { | 143 | { |
| 119 | new += crc.byte[6]; | 144 | unlink (lfname); |
| 120 | crc.byte[6] = crc.byte[5] + new; | 145 | err = symlink (lock_info_str, lfname); |
| 121 | crc.byte[5] = crc.byte[4]; | ||
| 122 | crc.byte[4] = crc.byte[3]; | ||
| 123 | crc.byte[3] = crc.byte[2] + new; | ||
| 124 | crc.byte[2] = crc.byte[1]; | ||
| 125 | crc.byte[1] = crc.byte[0]; | ||
| 126 | crc.byte[0] = new; | ||
| 127 | } | 146 | } |
| 128 | sprintf (lockfile, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_dir, | ||
| 129 | crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3], | ||
| 130 | crc.byte[4], crc.byte[5], crc.byte[6]); | ||
| 131 | } | ||
| 132 | 147 | ||
| 133 | #else /* defined HAVE_LONG_FILE_NAMES */ | 148 | return err == 0; |
| 149 | } | ||
| 134 | 150 | ||
| 135 | #define MAKE_LOCK_NAME(lock, file) \ | ||
| 136 | (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_dir) + 1), \ | ||
| 137 | fill_in_lock_file_name (lock, (file))) | ||
| 138 | 151 | ||
| 152 | |||
| 153 | /* Return 0 if nobody owns the lock file LFNAME or the lock is obsolete, | ||
| 154 | 1 if another process owns it (and set OWNER (if non-null) to info), | ||
| 155 | 2 if the current process owns it, | ||
| 156 | or -1 if something is wrong with the locking mechanism. */ | ||
| 139 | 157 | ||
| 140 | fill_in_lock_file_name (lockfile, fn) | 158 | static int |
| 141 | register char *lockfile; | 159 | current_lock_owner (owner, lfname) |
| 142 | register Lisp_Object fn; | 160 | lock_info_type *owner; |
| 161 | char *lfname; | ||
| 143 | { | 162 | { |
| 144 | register char *p; | 163 | #ifndef index |
| 145 | 164 | extern char *rindex (), *index (); | |
| 146 | strcpy (lockfile, lock_dir); | 165 | #endif |
| 166 | int o, p, len, ret; | ||
| 167 | int local_owner = 0; | ||
| 168 | char *at, *dot; | ||
| 169 | char *lfinfo = 0; | ||
| 170 | int bufsize = 50; | ||
| 171 | /* Read arbitrarily-long contents of symlink. Similar code in | ||
| 172 | file-symlink-p in fileio.c. */ | ||
| 173 | do | ||
| 174 | { | ||
| 175 | bufsize *= 2; | ||
| 176 | lfinfo = (char *) xrealloc (lfinfo, bufsize); | ||
| 177 | len = readlink (lfname, lfinfo, bufsize); | ||
| 178 | } | ||
| 179 | while (len >= bufsize); | ||
| 180 | |||
| 181 | /* If nonexistent lock file, all is well; otherwise, got strange error. */ | ||
| 182 | if (len == -1) | ||
| 183 | { | ||
| 184 | xfree (lfinfo); | ||
| 185 | return errno == ENOENT ? 0 : -1; | ||
| 186 | } | ||
| 147 | 187 | ||
| 148 | p = lockfile + strlen (lockfile); | 188 | /* Link info exists, so `len' is its length. Null terminate. */ |
| 189 | lfinfo[len] = 0; | ||
| 190 | |||
| 191 | /* Even if the caller doesn't want the owner info, we still have to | ||
| 192 | read it to determine return value, so allocate it. */ | ||
| 193 | if (!owner) | ||
| 194 | { | ||
| 195 | owner = alloca (sizeof (lock_info_type)); | ||
| 196 | local_owner = 1; | ||
| 197 | } | ||
| 198 | |||
| 199 | /* Parse USER@HOST.PID. If can't parse, return -1. */ | ||
| 200 | /* The USER is everything before the first @. */ | ||
| 201 | at = index (lfinfo, '@'); | ||
| 202 | dot = rindex (lfinfo, '.'); | ||
| 203 | if (!at || !dot) { | ||
| 204 | xfree (lfinfo); | ||
| 205 | return -1; | ||
| 206 | } | ||
| 207 | len = at - lfinfo; | ||
| 208 | owner->user = (char *) xmalloc (len + 1); | ||
| 209 | strncpy (owner->user, lfinfo, len); | ||
| 210 | owner->user[len] = 0; | ||
| 211 | |||
| 212 | /* The PID is everything after the last `.'. */ | ||
| 213 | owner->pid = atoi (dot + 1); | ||
| 149 | 214 | ||
| 150 | strcpy (p, XSTRING (fn)->data); | 215 | /* The host is everything in between. */ |
| 216 | len = dot - at - 1; | ||
| 217 | owner->host = (char *) xmalloc (len + 1); | ||
| 218 | strncpy (owner->host, at + 1, len); | ||
| 219 | owner->host[len] = 0; | ||
| 151 | 220 | ||
| 152 | for (; *p; p++) | 221 | /* We're done looking at the link info. */ |
| 222 | xfree (lfinfo); | ||
| 223 | |||
| 224 | /* On current host? */ | ||
| 225 | if (strcmp (owner->host, XSTRING (Fsystem_name ())->data) == 0) | ||
| 153 | { | 226 | { |
| 154 | if (*p == '/') | 227 | if (owner->pid == getpid ()) |
| 155 | *p = '!'; | 228 | ret = 2; /* We own it. */ |
| 229 | |||
| 230 | if (owner->pid > 0 | ||
| 231 | && (kill (owner->pid, 0) >= 0 || errno == EPERM)) | ||
| 232 | ret = 1; /* An existing process on this machine owns it. */ | ||
| 233 | |||
| 234 | /* The owner process is dead or has a strange pid (<=0), so try to | ||
| 235 | zap the lockfile. */ | ||
| 236 | if (unlink (lfname) < 0) | ||
| 237 | ret = -1; | ||
| 238 | |||
| 239 | ret = 0; | ||
| 156 | } | 240 | } |
| 241 | else | ||
| 242 | { /* If we wanted to support the check for stale locks on remote machines, | ||
| 243 | here's where we'd do it. */ | ||
| 244 | ret = 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | /* Avoid garbage. */ | ||
| 248 | if (local_owner || ret <= 0) | ||
| 249 | { | ||
| 250 | FREE_LOCK_INFO (*owner); | ||
| 251 | } | ||
| 252 | return ret; | ||
| 157 | } | 253 | } |
| 158 | #endif /* !defined HAVE_LONG_FILE_NAMES */ | ||
| 159 | 254 | ||
| 160 | static Lisp_Object | 255 | |
| 161 | lock_file_owner_name (lfname) | 256 | /* Lock the lock named LFNAME if possible. |
| 162 | char *lfname; | 257 | Return 0 in that case. |
| 163 | { | 258 | Return positive if some other process owns the lock, and info about |
| 164 | struct stat s; | 259 | that process in CLASHER. |
| 165 | struct passwd *the_pw; | 260 | Return -1 if cannot lock for any other reason. */ |
| 166 | 261 | ||
| 167 | if (lstat (lfname, &s) == 0) | 262 | static int |
| 168 | the_pw = getpwuid (s.st_uid); | 263 | lock_if_free (clasher, lfname) |
| 169 | else | 264 | lock_info_type *clasher; |
| 170 | the_pw = 0; | 265 | register char *lfname; |
| 266 | { | ||
| 267 | while (lock_file_1 (lfname, 0) == 0) | ||
| 268 | { | ||
| 269 | int locker; | ||
| 171 | 270 | ||
| 172 | return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name)); | 271 | if (errno != EEXIST) |
| 272 | return -1; | ||
| 273 | |||
| 274 | locker = current_lock_owner (clasher, lfname); | ||
| 275 | if (locker == 2) | ||
| 276 | { | ||
| 277 | FREE_LOCK_INFO (*clasher); | ||
| 278 | return 0; /* We ourselves locked it. */ | ||
| 279 | } | ||
| 280 | else if (locker == 1) | ||
| 281 | return 1; /* Someone else has it. */ | ||
| 282 | else if (locker == -1) | ||
| 283 | return -1; /* Something's wrong. */ | ||
| 284 | |||
| 285 | /* If some other error, or no such lock, try to lock again. */ | ||
| 286 | /* Is there a case where we loop forever? */ | ||
| 287 | } | ||
| 288 | return 0; | ||
| 173 | } | 289 | } |
| 174 | 290 | ||
| 175 | 291 | /* lock_file locks file FN, | |
| 176 | /* lock_file locks file fn, | ||
| 177 | meaning it serves notice on the world that you intend to edit that file. | 292 | meaning it serves notice on the world that you intend to edit that file. |
| 178 | This should be done only when about to modify a file-visiting | 293 | This should be done only when about to modify a file-visiting |
| 179 | buffer previously unmodified. | 294 | buffer previously unmodified. |
| 180 | Do not (normally) call lock_buffer for a buffer already modified, | 295 | Do not (normally) call this for a buffer already modified, |
| 181 | as either the file is already locked, or the user has already | 296 | as either the file is already locked, or the user has already |
| 182 | decided to go ahead without locking. | 297 | decided to go ahead without locking. |
| 183 | 298 | ||
| 184 | When lock_buffer returns, either the lock is locked for us, | 299 | When this returns, either the lock is locked for us, |
| 185 | or the user has said to go ahead without locking. | 300 | or the user has said to go ahead without locking. |
| 186 | 301 | ||
| 187 | If the file is locked by someone else, lock_buffer calls | 302 | If the file is locked by someone else, this calls |
| 188 | ask-user-about-lock (a Lisp function) with two arguments, | 303 | ask-user-about-lock (a Lisp function) with two arguments, |
| 189 | the file name and the name of the user who did the locking. | 304 | the file name and info about the user who did the locking. |
| 190 | This function can signal an error, or return t meaning | 305 | This function can signal an error, or return t meaning |
| 191 | take away the lock, or return nil meaning ignore the lock. */ | 306 | take away the lock, or return nil meaning ignore the lock. */ |
| 192 | 307 | ||
| 193 | /* The lock file name is the file name with "/" replaced by "!" | ||
| 194 | and put in the Emacs lock directory. */ | ||
| 195 | /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */ | ||
| 196 | |||
| 197 | /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex | ||
| 198 | representation of a 14-bytes CRC generated from the file name | ||
| 199 | and put in the Emacs lock directory (not very nice, but it works). | ||
| 200 | (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */ | ||
| 201 | |||
| 202 | void | 308 | void |
| 203 | lock_file (fn) | 309 | lock_file (fn) |
| 204 | register Lisp_Object fn; | 310 | register Lisp_Object fn; |
| 205 | { | 311 | { |
| 206 | register Lisp_Object attack, orig_fn; | 312 | register Lisp_Object attack, orig_fn; |
| 207 | register char *lfname; | 313 | register char *lfname, *locker; |
| 314 | lock_info_type lock_info; | ||
| 208 | 315 | ||
| 209 | orig_fn = fn; | 316 | orig_fn = fn; |
| 210 | fn = Fexpand_file_name (fn, Qnil); | 317 | fn = Fexpand_file_name (fn, Qnil); |
| 211 | 318 | ||
| 319 | /* Create the name of the lock-file for file fn */ | ||
| 212 | MAKE_LOCK_NAME (lfname, fn); | 320 | MAKE_LOCK_NAME (lfname, fn); |
| 213 | 321 | ||
| 214 | /* See if this file is visited and has changed on disk since it was | 322 | /* See if this file is visited and has changed on disk since it was |
| @@ -223,113 +331,27 @@ lock_file (fn) | |||
| 223 | } | 331 | } |
| 224 | 332 | ||
| 225 | /* Try to lock the lock. */ | 333 | /* Try to lock the lock. */ |
| 226 | if (lock_if_free (lfname) <= 0) | 334 | if (lock_if_free (&lock_info, lfname) <= 0) |
| 227 | /* Return now if we have locked it, or if lock dir does not exist */ | 335 | /* Return now if we have locked it, or if lock creation failed */ |
| 228 | return; | 336 | return; |
| 229 | 337 | ||
| 230 | /* Else consider breaking the lock */ | 338 | /* Else consider breaking the lock */ |
| 231 | attack = call2 (intern ("ask-user-about-lock"), fn, | 339 | locker = alloca (strlen (lock_info.user) + strlen (lock_info.host) |
| 232 | lock_file_owner_name (lfname)); | 340 | + LOCK_PID_MAX + 9); |
| 341 | sprintf (locker, "%s@%s (pid %d)", lock_info.user, lock_info.host, | ||
| 342 | lock_info.pid); | ||
| 343 | FREE_LOCK_INFO (lock_info); | ||
| 344 | |||
| 345 | attack = call2 (intern ("ask-user-about-lock"), fn, build_string (locker)); | ||
| 233 | if (!NILP (attack)) | 346 | if (!NILP (attack)) |
| 234 | /* User says take the lock */ | 347 | /* User says take the lock */ |
| 235 | { | 348 | { |
| 236 | lock_superlock (lfname); | 349 | lock_file_1 (lfname, 1); |
| 237 | lock_file_1 (lfname, O_WRONLY) ; | ||
| 238 | unlink (superlock_file); | ||
| 239 | return; | 350 | return; |
| 240 | } | 351 | } |
| 241 | /* User says ignore the lock */ | 352 | /* User says ignore the lock */ |
| 242 | } | 353 | } |
| 243 | 354 | ||
| 244 | /* Lock the lock file named LFNAME. | ||
| 245 | If MODE is O_WRONLY, we do so even if it is already locked. | ||
| 246 | If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free. | ||
| 247 | Return 1 if successful, 0 if not. */ | ||
| 248 | |||
| 249 | int | ||
| 250 | lock_file_1 (lfname, mode) | ||
| 251 | int mode; char *lfname; | ||
| 252 | { | ||
| 253 | register int fd; | ||
| 254 | char buf[20]; | ||
| 255 | |||
| 256 | if ((fd = open (lfname, mode, 0666)) >= 0) | ||
| 257 | { | ||
| 258 | #ifdef USG | ||
| 259 | chmod (lfname, 0666); | ||
| 260 | #else | ||
| 261 | fchmod (fd, 0666); | ||
| 262 | #endif | ||
| 263 | sprintf (buf, "%d ", getpid ()); | ||
| 264 | write (fd, buf, strlen (buf)); | ||
| 265 | close (fd); | ||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | else | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | /* Lock the lock named LFNAME if possible. | ||
| 273 | Return 0 in that case. | ||
| 274 | Return positive if lock is really locked by someone else. | ||
| 275 | Return -1 if cannot lock for any other reason. */ | ||
| 276 | |||
| 277 | int | ||
| 278 | lock_if_free (lfname) | ||
| 279 | register char *lfname; | ||
| 280 | { | ||
| 281 | register int clasher; | ||
| 282 | |||
| 283 | while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0) | ||
| 284 | { | ||
| 285 | if (errno != EEXIST) | ||
| 286 | return -1; | ||
| 287 | clasher = current_lock_owner (lfname); | ||
| 288 | if (clasher != 0) | ||
| 289 | if (clasher != getpid ()) | ||
| 290 | return (clasher); | ||
| 291 | else return (0); | ||
| 292 | /* Try again to lock it */ | ||
| 293 | } | ||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | /* Return the pid of the process that claims to own the lock file LFNAME, | ||
| 298 | or 0 if nobody does or the lock is obsolete, | ||
| 299 | or -1 if something is wrong with the locking mechanism. */ | ||
| 300 | |||
| 301 | int | ||
| 302 | current_lock_owner (lfname) | ||
| 303 | char *lfname; | ||
| 304 | { | ||
| 305 | int owner = current_lock_owner_1 (lfname); | ||
| 306 | if (owner == 0 && errno == ENOENT) | ||
| 307 | return (0); | ||
| 308 | /* Is it locked by a process that exists? */ | ||
| 309 | if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM)) | ||
| 310 | return (owner); | ||
| 311 | if (unlink (lfname) < 0) | ||
| 312 | return (-1); | ||
| 313 | return (0); | ||
| 314 | } | ||
| 315 | |||
| 316 | int | ||
| 317 | current_lock_owner_1 (lfname) | ||
| 318 | char *lfname; | ||
| 319 | { | ||
| 320 | register int fd; | ||
| 321 | char buf[20]; | ||
| 322 | int tem; | ||
| 323 | |||
| 324 | fd = open (lfname, O_RDONLY, 0666); | ||
| 325 | if (fd < 0) | ||
| 326 | return 0; | ||
| 327 | tem = read (fd, buf, sizeof buf); | ||
| 328 | close (fd); | ||
| 329 | return (tem <= 0 ? 0 : atoi (buf)); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | void | 355 | void |
| 334 | unlock_file (fn) | 356 | unlock_file (fn) |
| 335 | register Lisp_Object fn; | 357 | register Lisp_Object fn; |
| @@ -340,77 +362,8 @@ unlock_file (fn) | |||
| 340 | 362 | ||
| 341 | MAKE_LOCK_NAME (lfname, fn); | 363 | MAKE_LOCK_NAME (lfname, fn); |
| 342 | 364 | ||
| 343 | lock_superlock (lfname); | 365 | if (current_lock_owner (0, lfname) == 2) |
| 344 | |||
| 345 | if (current_lock_owner_1 (lfname) == getpid ()) | ||
| 346 | unlink (lfname); | 366 | unlink (lfname); |
| 347 | |||
| 348 | unlink (superlock_file); | ||
| 349 | } | ||
| 350 | |||
| 351 | lock_superlock (lfname) | ||
| 352 | char *lfname; | ||
| 353 | { | ||
| 354 | register int i, fd; | ||
| 355 | DIR *lockdir; | ||
| 356 | struct stat first_stat, last_stat; | ||
| 357 | |||
| 358 | for (i = -20; i < 0; | ||
| 359 | i++) | ||
| 360 | { | ||
| 361 | fd = open (superlock_file, | ||
| 362 | O_WRONLY | O_EXCL | O_CREAT, 0666); | ||
| 363 | |||
| 364 | /* If we succeeded in creating the superlock, we win. | ||
| 365 | Fill in our info and return. */ | ||
| 366 | if (fd >= 0) | ||
| 367 | { | ||
| 368 | #ifdef USG | ||
| 369 | chmod (superlock_file, 0666); | ||
| 370 | #else | ||
| 371 | fchmod (fd, 0666); | ||
| 372 | #endif | ||
| 373 | write (fd, lfname, strlen (lfname)); | ||
| 374 | close (fd); | ||
| 375 | return; | ||
| 376 | } | ||
| 377 | |||
| 378 | /* If the problem is not just that it is already locked, | ||
| 379 | give up. */ | ||
| 380 | if (errno != EEXIST) | ||
| 381 | return; | ||
| 382 | |||
| 383 | message ("Superlock file exists, retrying..."); | ||
| 384 | |||
| 385 | if (i == -20) | ||
| 386 | stat (superlock_file, &first_stat); | ||
| 387 | |||
| 388 | if (i == -1) | ||
| 389 | stat (superlock_file, &last_stat); | ||
| 390 | |||
| 391 | /* This seems to be necessary to prevent Emacs from hanging when the | ||
| 392 | competing process has already deleted the superlock, but it's still | ||
| 393 | in the NFS cache. So we force NFS to synchronize the cache. */ | ||
| 394 | lockdir = opendir (lock_dir); | ||
| 395 | |||
| 396 | if (lockdir) | ||
| 397 | closedir (lockdir); | ||
| 398 | |||
| 399 | sleep (1); | ||
| 400 | } | ||
| 401 | |||
| 402 | if (first_stat.st_ctime == last_stat.st_ctime) | ||
| 403 | { | ||
| 404 | int value; | ||
| 405 | value = unlink (superlock_file); | ||
| 406 | |||
| 407 | if (value != -1) | ||
| 408 | message ("Superlock file deleted"); | ||
| 409 | else | ||
| 410 | message ("Failed to delete superlock file"); | ||
| 411 | } | ||
| 412 | else | ||
| 413 | message ("Giving up on the superlock file"); | ||
| 414 | } | 367 | } |
| 415 | 368 | ||
| 416 | void | 369 | void |
| @@ -426,7 +379,6 @@ unlock_all_files () | |||
| 426 | unlock_file (b->file_truename); | 379 | unlock_file (b->file_truename); |
| 427 | } | 380 | } |
| 428 | } | 381 | } |
| 429 | |||
| 430 | 382 | ||
| 431 | DEFUN ("lock-buffer", Flock_buffer, Slock_buffer, | 383 | DEFUN ("lock-buffer", Flock_buffer, Slock_buffer, |
| 432 | 0, 1, 0, | 384 | 0, 1, 0, |
| @@ -458,7 +410,6 @@ if it should normally be locked.") | |||
| 458 | return Qnil; | 410 | return Qnil; |
| 459 | } | 411 | } |
| 460 | 412 | ||
| 461 | |||
| 462 | /* Unlock the file visited in buffer BUFFER. */ | 413 | /* Unlock the file visited in buffer BUFFER. */ |
| 463 | 414 | ||
| 464 | unlock_buffer (buffer) | 415 | unlock_buffer (buffer) |
| @@ -475,20 +426,27 @@ t if it is locked by you, else a string of the name of the locker.") | |||
| 475 | (filename) | 426 | (filename) |
| 476 | Lisp_Object filename; | 427 | Lisp_Object filename; |
| 477 | { | 428 | { |
| 429 | Lisp_Object ret; | ||
| 478 | register char *lfname; | 430 | register char *lfname; |
| 479 | int owner; | 431 | int owner; |
| 432 | lock_info_type locker; | ||
| 480 | 433 | ||
| 481 | filename = Fexpand_file_name (filename, Qnil); | 434 | filename = Fexpand_file_name (filename, Qnil); |
| 482 | 435 | ||
| 483 | MAKE_LOCK_NAME (lfname, filename); | 436 | MAKE_LOCK_NAME (lfname, filename); |
| 484 | 437 | ||
| 485 | owner = current_lock_owner (lfname); | 438 | owner = current_lock_owner (&locker, lfname); |
| 486 | if (owner <= 0) | 439 | if (owner <= 0) |
| 487 | return (Qnil); | 440 | ret = Qnil; |
| 488 | else if (owner == getpid ()) | 441 | else if (owner == 2) |
| 489 | return (Qt); | 442 | ret = Qt; |
| 490 | 443 | else | |
| 491 | return (lock_file_owner_name (lfname)); | 444 | ret = build_string (locker.user); |
| 445 | |||
| 446 | if (owner > 0) | ||
| 447 | FREE_LOCK_INFO (locker); | ||
| 448 | |||
| 449 | return ret; | ||
| 492 | } | 450 | } |
| 493 | 451 | ||
| 494 | 452 | ||
| @@ -496,6 +454,7 @@ t if it is locked by you, else a string of the name of the locker.") | |||
| 496 | 454 | ||
| 497 | init_filelock () | 455 | init_filelock () |
| 498 | { | 456 | { |
| 457 | #if 0 | ||
| 499 | char *new_name; | 458 | char *new_name; |
| 500 | 459 | ||
| 501 | lock_dir = egetenv ("EMACSLOCKDIR"); | 460 | lock_dir = egetenv ("EMACSLOCKDIR"); |
| @@ -515,6 +474,7 @@ init_filelock () | |||
| 515 | + sizeof (SUPERLOCK_NAME))); | 474 | + sizeof (SUPERLOCK_NAME))); |
| 516 | strcpy (superlock_file, lock_dir); | 475 | strcpy (superlock_file, lock_dir); |
| 517 | strcat (superlock_file, SUPERLOCK_NAME); | 476 | strcat (superlock_file, SUPERLOCK_NAME); |
| 477 | #endif | ||
| 518 | } | 478 | } |
| 519 | 479 | ||
| 520 | syms_of_filelock () | 480 | syms_of_filelock () |