2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
10 * You may distribute under the terms of the GNU General Public License as
11 * specified in the README file that comes with the CVS source distribution.
15 * Lock file support for CVS.
18 /* The node Concurrency in doc/cvs.texinfo has a brief introduction to
19 how CVS locks function, and some of the user-visible consequences of
20 their existence. Here is a summary of why they exist (and therefore,
21 the consequences of hacking CVS to read a repository without creating
24 There are two uses. One is the ability to prevent there from being
25 two writers at the same time. This is necessary for any number of
26 reasons (fileattr code, probably others). Commit needs to lock the
27 whole tree so that nothing happens between the up-to-date check and
30 The second use is the ability to ensure that there is not a writer
31 and a reader at the same time (several readers are allowed). Reasons
34 * Readlocks ensure that once CVS has found a collection of rcs
35 files using Find_Names, the files will still exist when it reads
36 them (they may have moved in or out of the attic).
38 * Readlocks provide some modicum of consistency, although this is
39 kind of limited--see the node Concurrency in cvs.texinfo.
41 * Readlocks ensure that the RCS file does not change between
42 RCS_parse and RCS_reparsercsfile time. This one strikes me as
43 important, although I haven't thought up what bad scenarios might
46 * Readlocks ensure that we won't find the file in the state in
47 which it is in between the calls to add_rcs_file and RCS_checkin in
48 commit.c (when a file is being added). This state is a state in
49 which the RCS file parsing routines in rcs.c cannot parse the file.
51 * Readlocks ensure that a reader won't try to look at a
52 half-written fileattr file (fileattr is not updated atomically).
54 (see also the description of anonymous read-only access in
55 "Password authentication security" node in doc/cvs.texinfo).
57 While I'm here, I'll try to summarize a few random suggestions
58 which periodically get made about how locks might be different:
60 1. Check for EROFS. Maybe useful, although in the presence of NFS
61 EROFS does *not* mean that the file system is unchanging.
63 2. Provide an option to disable locks for operations which only
64 read (see above for some of the consequences).
66 3. Have a server internally do the locking. Probably a good
67 long-term solution, and many people have been working hard on code
68 changes which would eventually make it possible to have a server
69 which can handle various connections in one process, but there is
70 much, much work still to be done before this is feasible. */
77 /* This is the directory in which we may have a lock named by the
78 readlock variable, a lock named by the writelock variable, and/or
79 a lock named CVSLCK. The storage is not allocated along with the
80 struct lock; it is allocated by the Reader_Lock caller or in the
81 case of promotablelocks, it is just a pointer to the storage allocated
82 for the ->key field. */
83 const char *repository
;
85 /* The name of the lock files. */
87 #ifdef LOCK_COMPATIBILITY
89 #endif /* LOCK_COMPATIBILITY */
91 /* The name of the master lock dir. Usually CVSLCK. */
92 const char *lockdirname
;
94 /* The full path to the lock dir, if we are currently holding it.
96 * This will be LOCKDIRNAME catted onto REPOSITORY. We waste a little
97 * space by storing it, but save a later malloc/free.
101 /* Note there is no way of knowing whether the readlock and writelock
102 exist. The code which sets the locks doesn't use SIG_beginCrSect
103 to set a flag like we do for CVSLCK. */
104 bool free_repository
;
107 static void remove_locks (void);
108 static int set_lock (struct lock
*lock
, int will_wait
);
109 static void clear_lock (struct lock
*lock
);
110 static void set_lockers_name (struct stat
*statp
);
112 /* Malloc'd array containing the username of the whoever has the lock.
113 Will always be non-NULL in the cases where it is needed. */
114 static char *lockers_name
;
115 /* Malloc'd array specifying name of a readlock within a directory.
117 static char *readlock
;
118 /* Malloc'd array specifying name of a writelock within a directory.
120 static char *writelock
;
121 /* Malloc'd array specifying name of a promotablelock within a directory.
123 static char *promotablelock
;
124 static List
*locklist
;
126 #define L_OK 0 /* success */
127 #define L_ERROR 1 /* error condition */
128 #define L_LOCKED 2 /* lock owned by someone else */
130 /* This is the (single) readlock which is set by Reader_Lock. The
131 repository field is NULL if there is no such lock. */
132 #ifdef LOCK_COMPATIBILITY
133 static struct lock global_readlock
= {NULL
, NULL
, NULL
, CVSLCK
, NULL
, false};
134 static struct lock global_writelock
= {NULL
, NULL
, NULL
, CVSLCK
, NULL
, false};
136 static struct lock global_history_lock
= {NULL
, NULL
, NULL
, CVSHISTORYLCK
,
138 static struct lock global_val_tags_lock
= {NULL
, NULL
, NULL
, CVSVALTAGSLCK
,
141 static struct lock global_readlock
= {NULL
, NULL
, CVSLCK
, NULL
, false};
142 static struct lock global_writelock
= {NULL
, NULL
, CVSLCK
, NULL
, false};
144 static struct lock global_history_lock
= {NULL
, NULL
, CVSHISTORYLCK
, NULL
,
146 static struct lock global_val_tags_lock
= {NULL
, NULL
, CVSVALTAGSLCK
, NULL
,
148 #endif /* LOCK_COMPATIBILITY */
150 /* List of locks set by lock_tree_for_write. This is redundant
151 with locklist, sort of. */
152 static List
*lock_tree_list
;
156 /* Return a newly malloc'd string containing the name of the lock for the
157 repository REPOSITORY and the lock file name within that directory
158 NAME. Also create the directories in which to put the lock file
159 if needed (if we need to, could save system call(s) by doing
160 that only if the actual operation fails. But for now we'll keep
163 lock_name (const char *repository
, const char *name
)
168 const char *short_repos
;
169 mode_t save_umask
= 0000;
172 TRACE (TRACE_FLOW
, "lock_name (%s, %s)",
173 repository
? repository
: "(null)", name
? name
: "(null)");
175 if (!config
->lock_dir
)
177 /* This is the easy case. Because the lock files go directly
178 in the repository, no need to create directories or anything. */
179 assert (name
!= NULL
);
180 assert (repository
!= NULL
);
181 retval
= Xasprintf ("%s/%s", repository
, name
);
188 /* The interesting part of the repository is the part relative
190 assert (current_parsed_root
!= NULL
);
191 assert (current_parsed_root
->directory
!= NULL
);
192 assert (strncmp (repository
, current_parsed_root
->directory
,
193 strlen (current_parsed_root
->directory
)) == 0);
194 short_repos
= repository
+ strlen (current_parsed_root
->directory
) + 1;
196 if (strcmp (repository
, current_parsed_root
->directory
) == 0)
199 assert (short_repos
[-1] == '/');
201 retval
= xmalloc (strlen (config
->lock_dir
)
202 + strlen (short_repos
)
205 strcpy (retval
, config
->lock_dir
);
206 q
= retval
+ strlen (retval
);
209 strcpy (q
, short_repos
);
211 /* In the common case, where the directory already exists, let's
212 keep it to one system call. */
213 if (stat (retval
, &sb
) < 0)
215 /* If we need to be creating more than one directory, we'll
216 get the existence_error here. */
217 if (!existence_error (errno
))
218 error (1, errno
, "cannot stat directory %s", retval
);
222 if (S_ISDIR (sb
.st_mode
))
225 error (1, 0, "%s is not a directory", retval
);
228 /* Now add the directories one at a time, so we can create
231 The idea behind the new_mode stuff is that the directory we
232 end up creating will inherit permissions from its parent
233 directory (we re-set new_mode with each EEXIST). CVSUMASK
234 isn't right, because typically the reason for LockDir is to
235 use a different set of permissions. We probably want to
236 inherit group ownership also (but we don't try to deal with
237 that, some systems do it for us either always or when g+s is on).
239 We don't try to do anything about the permissions on the lock
240 files themselves. The permissions don't really matter so much
241 because the locks will generally be removed by the process
242 which created them. */
244 if (stat (config
->lock_dir
, &sb
) < 0)
245 error (1, errno
, "cannot stat %s", config
->lock_dir
);
246 new_mode
= sb
.st_mode
;
247 save_umask
= umask (0000);
253 while (!ISSLASH (*p
) && *p
!= '\0')
257 strncpy (q
, short_repos
, p
- short_repos
);
258 q
[p
- short_repos
] = '\0';
259 if (!ISSLASH (q
[p
- short_repos
- 1])
260 && CVS_MKDIR (retval
, new_mode
) < 0)
262 int saved_errno
= errno
;
263 if (saved_errno
!= EEXIST
)
264 error (1, errno
, "cannot make directory %s", retval
);
267 if (stat (retval
, &sb
) < 0)
268 error (1, errno
, "cannot stat %s", retval
);
269 new_mode
= sb
.st_mode
;
276 strcpy (q
, short_repos
);
277 if (CVS_MKDIR (retval
, new_mode
) < 0
279 error (1, errno
, "cannot make directory %s", retval
);
285 strcat (retval
, "/");
286 strcat (retval
, name
);
290 assert (umask (save_umask
) == 0000);
299 /* Remove the lock files. For interrupt purposes, it can be assumed that the
300 * first thing this function does is set lock->repository to NULL.
303 * lock The lock to remove.
304 * free True if this lock directory will not be reused (free
305 * lock->repository if necessary).
308 remove_lock_files (struct lock
*lock
, bool free_repository
)
310 TRACE (TRACE_FLOW
, "remove_lock_files (%s)", lock
->repository
);
312 /* If lock->file is set, the lock *might* have been created, but since
313 * Reader_Lock & lock_dir_for_write don't use SIG_beginCrSect the way that
314 * set_lock does, we don't know that. That is why we need to check for
315 * existence_error here.
319 char *tmp
= lock
->file1
;
321 if (CVS_UNLINK (tmp
) < 0 && ! existence_error (errno
))
322 error (0, errno
, "failed to remove lock %s", tmp
);
325 #ifdef LOCK_COMPATIBILITY
328 char *tmp
= lock
->file2
;
330 if (CVS_UNLINK (tmp
) < 0 && ! existence_error (errno
))
331 error (0, errno
, "failed to remove lock %s", tmp
);
334 #endif /* LOCK_COMPATIBILITY */
338 /* And free the repository string. We don't really have to set the
339 * repository string to NULL first since there is no harm in running any of
340 * the above code twice.
342 * Use SIG_beginCrSect since otherwise we might be interrupted between
343 * checking whether free_repository is set and freeing stuff.
348 if (lock
->free_repository
)
350 free ((char *)lock
->repository
);
351 lock
->free_repository
= false;
353 lock
->repository
= NULL
;
361 * Clean up outstanding read and write locks and free their storage.
364 Simple_Lock_Cleanup (void)
366 TRACE (TRACE_FUNCTION
, "Simple_Lock_Cleanup()");
368 /* Avoid interrupts while accessing globals the interrupt handlers might
373 /* clean up simple read locks (if any) */
374 if (global_readlock
.repository
!= NULL
)
375 remove_lock_files (&global_readlock
, true);
376 /* See note in Lock_Cleanup() below. */
381 /* clean up simple write locks (if any) */
382 if (global_writelock
.repository
!= NULL
)
383 remove_lock_files (&global_writelock
, true);
384 /* See note in Lock_Cleanup() below. */
389 /* clean up simple write locks (if any) */
390 if (global_history_lock
.repository
)
391 remove_lock_files (&global_history_lock
, true);
396 if (global_val_tags_lock
.repository
)
397 remove_lock_files (&global_val_tags_lock
, true);
398 /* See note in Lock_Cleanup() below. */
405 * Clean up all outstanding locks and free their storage.
408 * This function needs to be reentrant since a call to exit() can cause a
409 * call to this function, which can then be interrupted by a signal, which
410 * can cause a second call to this function.
418 TRACE (TRACE_FUNCTION
, "Lock_Cleanup()");
420 /* FIXME: Do not perform buffered I/O from an interrupt handler like
421 * this (via error). However, I'm leaving the error-calling code there
422 * in the hope that on the rare occasion the error call is actually made
423 * (e.g., a fluky I/O error or permissions problem prevents the deletion
424 * of a just-created file) reentrancy won't be an issue.
429 /* Avoid being interrupted during calls which set globals to NULL. This
430 * avoids having interrupt handlers attempt to use these global variables
431 * in inconsistent states.
433 * This isn't always necessary, because sometimes we are called via exit()
434 * or the interrupt handler, in which case signals will already be blocked,
435 * but sometimes we might be called from elsewhere.
438 dellist (&lock_tree_list
);
439 /* Unblocking allows any signal to be processed as soon as possible. This
440 * isn't really necessary, but since we know signals can cause us to be
441 * called, why not avoid having blocks of code run twice.
449 * walklist proc for removing a list of locks
452 unlock_proc (Node
*p
, void *closure
)
454 remove_lock_files (p
->data
, false);
461 * Remove locks without discarding the lock information.
466 TRACE (TRACE_FLOW
, "remove_locks()");
468 Simple_Lock_Cleanup ();
470 /* clean up promotable locks (if any) */
472 if (locklist
!= NULL
)
474 /* Use a tmp var since any of these functions could call exit, causing
475 * us to be called a second time.
477 List
*tmp
= locklist
;
479 walklist (tmp
, unlock_proc
, NULL
);
487 * Set the global readlock variable if it isn't already.
490 set_readlock_name (void)
492 if (readlock
== NULL
)
494 readlock
= Xasprintf (
495 #ifdef HAVE_LONG_FILE_NAMES
496 "%s.%s.%ld", CVSRFL
, hostname
,
507 * Create a lock file for readers
510 Reader_Lock (char *xrepository
)
515 TRACE (TRACE_FUNCTION
, "Reader_Lock(%s)", xrepository
);
517 if (noexec
|| readonlyfs
)
520 /* we only do one directory at a time for read locks! */
521 if (global_readlock
.repository
!= NULL
)
523 error (0, 0, "Reader_Lock called while read locks set - Help!");
527 set_readlock_name ();
529 /* remember what we're locking (for Lock_Cleanup) */
530 global_readlock
.repository
= xstrdup (xrepository
);
531 global_readlock
.free_repository
= true;
533 /* get the lock dir for our own */
534 if (set_lock (&global_readlock
, 1) != L_OK
)
536 error (0, 0, "failed to obtain dir lock in repository `%s'",
538 if (readlock
!= NULL
)
541 /* We don't set global_readlock.repository to NULL. I think this
542 only works because recurse.c will give a fatal error if we return
547 /* write a read-lock */
548 global_readlock
.file1
= lock_name (xrepository
, readlock
);
549 if ((fp
= CVS_FOPEN (global_readlock
.file1
, "w+")) == NULL
550 || fclose (fp
) == EOF
)
552 error (0, errno
, "cannot create read lock in repository `%s'",
557 /* free the lock dir */
558 clear_lock (&global_readlock
);
566 * lock_exists() returns 0 if there is no lock file matching FILEPAT in
567 * the repository but not IGNORE; else 1 is returned, to indicate that the
568 * caller should sleep a while and try again.
571 * repository The repository directory to search for locks.
572 * filepat The file name pattern to search for.
573 * ignore The name of a single file which can be ignored.
576 * lockdir The lock dir external to the repository, if any.
579 * 0 No lock matching FILEPAT and not IGNORE exists.
580 * 1 Otherwise and on error.
583 * In the case where errors are encountered reading the directory, a warning
584 * message is printed, 1 is is returned and ERRNO is left set.
587 lock_exists (const char *repository
, const char *filepat
, const char *ignore
)
595 #ifdef CVS_FUDGELOCKS
600 TRACE (TRACE_FLOW
, "lock_exists (%s, %s, %s)",
601 repository
, filepat
, ignore
? ignore
: "(null)");
603 lockdir
= lock_name (repository
, "");
604 lockdir
[strlen (lockdir
) - 1] = '\0'; /* remove trailing slash */
607 if ((dirp
= CVS_OPENDIR (lockdir
)) == NULL
)
608 error (1, 0, "cannot open directory %s", lockdir
);
612 while ((dp
= CVS_READDIR (dirp
)) != NULL
)
614 if (CVS_FNMATCH (filepat
, dp
->d_name
, 0) == 0)
616 /* FIXME: the basename conversion below should be replaced with
617 * a call to the GNULIB basename function once it is imported.
619 /* ignore our plock, if any */
620 if (ignore
&& !fncmp (ignore
, dp
->d_name
))
623 line
= Xasprintf ("%s/%s", lockdir
, dp
->d_name
);
624 if (stat (line
, &sb
) != -1)
626 #ifdef CVS_FUDGELOCKS
628 * If the create time of the file is more than CVSLCKAGE
629 * seconds ago, try to clean-up the lock file, and if
630 * successful, re-open the directory and try again.
632 if (now
>= (sb
.st_ctime
+ CVSLCKAGE
) &&
633 CVS_UNLINK (line
) != -1)
640 set_lockers_name (&sb
);
644 /* If the file doesn't exist, it just means that it
645 * disappeared between the time we did the readdir and the
646 * time we did the stat.
648 if (!existence_error (errno
))
649 error (0, errno
, "cannot stat %s", line
);
659 error (0, errno
, "error reading directory %s", repository
);
672 * readers_exist() returns 0 if there are no reader lock files remaining in
673 * the repository; else 1 is returned, to indicate that the caller should
674 * sleep a while and try again.
676 * See lock_exists() for argument detail.
679 readers_exist (const char *repository
)
681 TRACE (TRACE_FLOW
, "readers_exist (%s)", repository
);
683 /* It is only safe to ignore a readlock set by our process if it was set as
684 * a safety measure to prevent older CVS processes from ignoring our
685 * promotable locks. The code to ignore these readlocks can be removed
686 * once it is deemed unlikely that anyone will be using CVS servers earlier
687 * than version 1.12.4.
689 return lock_exists (repository
, CVSRFLPAT
,
690 #ifdef LOCK_COMPATIBILITY
691 findnode (locklist
, repository
) ? readlock
:
692 #endif /* LOCK_COMPATIBILITY */
699 * promotable_exists() returns 0 if there is no promotable lock file in
700 * the repository; else 1 is returned, to indicate that the caller should
701 * sleep a while and try again.
703 * See lock_exists() for argument detail.
706 promotable_exists (const char *repository
)
708 TRACE (TRACE_FLOW
, "promotable_exists (%s)", repository
);
709 return lock_exists (repository
, CVSPFLPAT
, promotablelock
);
715 * Lock a list of directories for writing
717 static char *lock_error_repos
;
718 static int lock_error
;
723 * Create a lock file for potential writers returns L_OK if lock set ok,
724 * L_LOCKED if lock held by someone else or L_ERROR if an error occurred.
727 set_promotable_lock (struct lock
*lock
)
732 TRACE (TRACE_FUNCTION
, "set_promotable_lock(%s)",
733 lock
->repository
? lock
->repository
: "(null)");
735 if (promotablelock
== NULL
)
737 promotablelock
= Xasprintf (
738 #ifdef HAVE_LONG_FILE_NAMES
739 "%s.%s.%ld", CVSPFL
, hostname
,
746 /* make sure the lock dir is ours (not necessarily unique to us!) */
747 status
= set_lock (lock
, 0);
750 /* we now own a promotable lock - make sure there are no others */
751 if (promotable_exists (lock
->repository
))
753 /* clean up the lock dir */
756 /* indicate we failed due to read locks instead of error */
760 /* write the promotable-lock file */
761 lock
->file1
= lock_name (lock
->repository
, promotablelock
);
762 if ((fp
= CVS_FOPEN (lock
->file1
, "w+")) == NULL
|| fclose (fp
) == EOF
)
766 if (CVS_UNLINK (lock
->file1
) < 0 && ! existence_error (errno
))
767 error (0, errno
, "failed to remove lock %s", lock
->file1
);
769 /* free the lock dir */
772 /* return the error */
774 "cannot create promotable lock in repository `%s'",
779 #ifdef LOCK_COMPATIBILITY
780 /* write the read-lock file. We only do this so that older versions of
781 * CVS will not think it is okay to create a write lock. When it is
782 * decided that versions of CVS earlier than 1.12.4 are not likely to
783 * be used, this code can be removed.
785 set_readlock_name ();
786 lock
->file2
= lock_name (lock
->repository
, readlock
);
787 if ((fp
= CVS_FOPEN (lock
->file2
, "w+")) == NULL
|| fclose (fp
) == EOF
)
791 if ( CVS_UNLINK (lock
->file2
) < 0 && ! existence_error (errno
))
792 error (0, errno
, "failed to remove lock %s", lock
->file2
);
794 /* free the lock dir */
797 /* Remove the promotable lock. */
799 remove_lock_files (lock
, false);
801 /* return the error */
803 "cannot create read lock in repository `%s'",
807 #endif /* LOCK_COMPATIBILITY */
820 * walklist proc for setting write locks. Mostly just a wrapper for the
821 * set_promotable_lock function, which has a prettier API, but no other good
822 * reason for existing separately.
825 * p The current node, as determined by walklist().
829 * lock_error Any previous error encountered while attempting to get
833 * lock_error Set if we encounter an error attempting to get axi
835 * lock_error_repos Set so that if we set lock_error later functions will
836 * be able to report where the other process's lock was
843 set_promotablelock_proc (Node
*p
, void *closure
)
845 /* if some lock was not OK, just skip this one */
846 if (lock_error
!= L_OK
)
849 /* apply the write lock */
850 lock_error_repos
= p
->key
;
851 lock_error
= set_promotable_lock ((struct lock
*)p
->data
);
858 * Print out a message that the lock is still held, then sleep a while.
861 lock_wait (const char *repos
)
868 tm_p
= gmtime (&now
);
869 msg
= Xasprintf ("[%8.8s] waiting for %s's lock in %s",
870 (tm_p
? asctime (tm_p
) : ctime (&now
)) + 11,
871 lockers_name
, repos
);
872 error (0, 0, "%s", msg
);
873 /* Call cvs_flusherr to ensure that the user sees this message as
877 (void)sleep (CVSLCKSLEEP
);
883 * Print out a message when we obtain a lock.
886 lock_obtained (const char *repos
)
893 tm_p
= gmtime (&now
);
894 msg
= Xasprintf ("[%8.8s] obtained lock in %s",
895 (tm_p
? asctime (tm_p
) : ctime (&now
)) + 11, repos
);
896 error (0, 0, "%s", msg
);
897 /* Call cvs_flusherr to ensure that the user sees this message as
906 lock_list_promotably (List
*list
)
910 TRACE (TRACE_FLOW
, "lock_list_promotably ()");
917 "promotable lock failed.\n\
918 WARNING: Read-only repository access mode selected via `cvs -R'.\n\
919 Attempting to write to a read-only filesystem is not allowed.");
923 /* We only know how to do one list at a time */
924 if (locklist
!= NULL
)
927 "lock_list_promotably called while promotable locks set - Help!");
934 /* try to lock everything on the list */
935 lock_error
= L_OK
; /* init for set_promotablelock_proc */
936 lock_error_repos
= NULL
; /* init for set_promotablelock_proc */
937 locklist
= list
; /* init for Lock_Cleanup */
938 if (lockers_name
!= NULL
)
940 lockers_name
= xstrdup ("unknown");
942 (void) walklist (list
, set_promotablelock_proc
, NULL
);
946 case L_ERROR
: /* Real Error */
947 if (wait_repos
!= NULL
)
949 Lock_Cleanup (); /* clean up any locks we set */
950 error (0, 0, "lock failed - giving up");
953 case L_LOCKED
: /* Someone already had a lock */
954 remove_locks (); /* clean up any locks we set */
955 lock_wait (lock_error_repos
); /* sleep a while and try again */
956 wait_repos
= xstrdup (lock_error_repos
);
959 case L_OK
: /* we got the locks set */
960 if (wait_repos
!= NULL
)
962 lock_obtained (wait_repos
);
968 if (wait_repos
!= NULL
)
970 error (0, 0, "unknown lock status %d in lock_list_promotably",
980 * Set the static variable lockers_name appropriately, based on the stat
981 * structure passed in.
984 set_lockers_name (struct stat
*statp
)
988 if (lockers_name
!= NULL
)
990 pw
= (struct passwd
*) getpwuid (statp
->st_uid
);
992 lockers_name
= xstrdup (pw
->pw_name
);
994 lockers_name
= Xasprintf ("uid%lu", (unsigned long) statp
->st_uid
);
1000 * Persistently tries to make the directory "lckdir", which serves as a
1003 * #ifdef CVS_FUDGELOCKS
1004 * If the create time on the directory is greater than CVSLCKAGE
1005 * seconds old, just try to remove the directory.
1010 set_lock (struct lock
*lock
, int will_wait
)
1018 #ifdef CVS_FUDGELOCKS
1022 TRACE (TRACE_FLOW
, "set_lock (%s, %d)",
1023 lock
->repository
? lock
->repository
: "(null)", will_wait
);
1025 masterlock
= lock_name (lock
->repository
, lock
->lockdirname
);
1028 * Note that it is up to the callers of set_lock() to arrange for signal
1029 * handlers that do the appropriate things, like remove the lock
1030 * directory before they exit.
1037 omask
= umask (cvsumask
);
1039 if (CVS_MKDIR (masterlock
, 0777) == 0)
1041 lock
->lockdir
= masterlock
;
1045 lock_obtained (lock
->repository
);
1046 goto after_sig_unblock
;
1050 (void) umask (omask
);
1054 if (errno
!= EEXIST
)
1057 "failed to create lock directory for `%s' (%s)",
1058 lock
->repository
, masterlock
);
1063 /* Find out who owns the lock. If the lock directory is
1064 non-existent, re-try the loop since someone probably just
1065 removed it (thus releasing the lock). */
1066 if (stat (masterlock
, &sb
) < 0)
1068 if (existence_error (errno
))
1071 error (0, errno
, "couldn't stat lock directory `%s'", masterlock
);
1076 #ifdef CVS_FUDGELOCKS
1078 * If the create time of the directory is more than CVSLCKAGE seconds
1079 * ago, try to clean-up the lock directory, and if successful, just
1080 * quietly retry to make it.
1083 if (now
>= (sb
.st_ctime
+ CVSLCKAGE
))
1085 if (CVS_RMDIR (masterlock
) >= 0)
1090 /* set the lockers name */
1091 set_lockers_name (&sb
);
1093 /* if he wasn't willing to wait, return an error */
1100 /* if possible, try a very short sleep without a message */
1101 if (!waited
&& us
< 1000)
1107 ts
.tv_nsec
= us
* 1000;
1108 (void)nanosleep (&ts
, NULL
);
1113 lock_wait (lock
->repository
);
1126 * Clear master lock.
1129 * lock The lock information.
1132 * Sets LOCK->lockdir to NULL after removing the directory it names and
1133 * freeing the storage.
1136 * If we own the master lock directory, its name is stored in LOCK->lockdir.
1137 * We may free LOCK->lockdir.
1140 clear_lock (struct lock
*lock
)
1145 if (CVS_RMDIR (lock
->lockdir
) < 0)
1146 error (0, errno
, "failed to remove lock dir `%s'", lock
->lockdir
);
1147 free (lock
->lockdir
);
1148 lock
->lockdir
= NULL
;
1156 * Create a list of repositories to lock
1160 lock_filesdoneproc (void *callerdat
, int err
, const char *repository
,
1161 const char *update_dir
, List
*entries
)
1167 p
->key
= xstrdup (repository
);
1168 p
->data
= xmalloc (sizeof (struct lock
));
1169 ((struct lock
*)p
->data
)->repository
= p
->key
;
1170 ((struct lock
*)p
->data
)->file1
= NULL
;
1171 #ifdef LOCK_COMPATIBILITY
1172 ((struct lock
*)p
->data
)->file2
= NULL
;
1173 #endif /* LOCK_COMPATIBILITY */
1174 ((struct lock
*)p
->data
)->lockdirname
= CVSLCK
;
1175 ((struct lock
*)p
->data
)->lockdir
= NULL
;
1176 ((struct lock
*)p
->data
)->free_repository
= false;
1178 /* FIXME-KRP: this error condition should not simply be passed by. */
1179 if (p
->key
== NULL
|| addnode (lock_tree_list
, p
) != 0)
1187 lock_tree_promotably (int argc
, char **argv
, int local
, int which
, int aflag
)
1189 TRACE (TRACE_FUNCTION
, "lock_tree_promotably (%d, argv, %d, %d, %d)",
1190 argc
, local
, which
, aflag
);
1193 * Run the recursion processor to find all the dirs to lock and lock all
1196 lock_tree_list
= getlist ();
1198 (NULL
, lock_filesdoneproc
,
1199 NULL
, NULL
, NULL
, argc
,
1200 argv
, local
, which
, aflag
, CVS_LOCK_NONE
,
1202 sortlist (lock_tree_list
, fsortcmp
);
1203 if (lock_list_promotably (lock_tree_list
) != 0)
1204 error (1, 0, "lock failed - giving up");
1209 /* Lock a single directory in REPOSITORY. It is OK to call this if
1210 * a lock has been set with lock_dir_for_write; the new lock will replace
1211 * the old one. If REPOSITORY is NULL, don't do anything.
1213 * We do not clear the dir lock after writing the lock file name since write
1214 * locks are exclusive to all other locks.
1217 lock_dir_for_write (const char *repository
)
1221 TRACE (TRACE_FLOW
, "lock_dir_for_write (%s)", repository
);
1223 if (repository
!= NULL
1224 && (global_writelock
.repository
== NULL
1225 || !strcmp (global_writelock
.repository
, repository
)))
1227 if (writelock
== NULL
)
1229 writelock
= Xasprintf (
1230 #ifdef HAVE_LONG_FILE_NAMES
1231 "%s.%s.%ld", CVSWFL
, hostname
,
1238 if (global_writelock
.repository
!= NULL
)
1239 remove_lock_files (&global_writelock
, true);
1241 global_writelock
.repository
= xstrdup (repository
);
1242 global_writelock
.free_repository
= true;
1248 if (set_lock (&global_writelock
, 1) != L_OK
)
1249 error (1, 0, "failed to obtain write lock in repository `%s'",
1252 /* check if readers exist */
1253 if (readers_exist (repository
)
1254 || promotable_exists (repository
))
1256 clear_lock (&global_writelock
);
1257 lock_wait (repository
); /* sleep a while and try again */
1263 lock_obtained (repository
);
1265 /* write the write-lock file */
1266 global_writelock
.file1
= lock_name (global_writelock
.repository
,
1268 if ((fp
= CVS_FOPEN (global_writelock
.file1
, "w+")) == NULL
1269 || fclose (fp
) == EOF
)
1273 if (CVS_UNLINK (global_writelock
.file1
) < 0
1274 && !existence_error (errno
))
1276 error (0, errno
, "failed to remove write lock %s",
1277 global_writelock
.file1
);
1280 /* free the lock dir */
1281 clear_lock (&global_writelock
);
1283 /* return the error */
1285 "cannot create write lock in repository `%s'",
1286 global_writelock
.repository
);
1289 /* If we upgraded from a promotable lock, remove it. */
1292 Node
*p
= findnode (locklist
, repository
);
1295 remove_lock_files (p
->data
, true);
1307 /* This is the internal implementation behind history_lock & val_tags_lock. It
1308 * gets a write lock for the history or val-tags file.
1315 internal_lock (struct lock
*lock
, const char *xrepository
)
1317 /* remember what we're locking (for Lock_Cleanup) */
1318 assert (!lock
->repository
);
1319 lock
->repository
= Xasprintf ("%s/%s", xrepository
, CVSROOTADM
);
1320 lock
->free_repository
= true;
1322 /* get the lock dir for our own */
1323 if (set_lock (lock
, 1) != L_OK
)
1326 error (0, 0, "failed to obtain history lock in repository `%s'",
1337 /* Lock the CVSROOT/history file for write.
1340 history_lock (const char *xrepository
)
1342 return internal_lock (&global_history_lock
, xrepository
);
1347 /* Remove the CVSROOT/history lock, if it exists.
1350 clear_history_lock ()
1352 remove_lock_files (&global_history_lock
, true);
1357 /* Lock the CVSROOT/val-tags file for write.
1360 val_tags_lock (const char *xrepository
)
1362 return internal_lock (&global_val_tags_lock
, xrepository
);
1367 /* Remove the CVSROOT/val-tags lock, if it exists.
1370 clear_val_tags_lock ()
1372 remove_lock_files (&global_val_tags_lock
, true);