/contrib/cvs/src/lock.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1166 lines · 721 code · 155 blank · 290 comment · 152 complexity · 4e7f7379d3ba7b898cd1a98e5093dcba MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
  3. *
  4. * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
  5. * and others.
  6. *
  7. * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
  8. * Portions Copyright (C) 1989-1992, Brian Berliner
  9. *
  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.
  12. *
  13. * Set Lock
  14. *
  15. * Lock file support for CVS.
  16. *
  17. * $FreeBSD$
  18. */
  19. /* The node Concurrency in doc/cvs.texinfo has a brief introduction to
  20. how CVS locks function, and some of the user-visible consequences of
  21. their existence. Here is a summary of why they exist (and therefore,
  22. the consequences of hacking CVS to read a repository without creating
  23. locks):
  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
  28. the actual checkin.
  29. The second use is the ability to ensure that there is not a writer
  30. and a reader at the same time (several readers are allowed). Reasons
  31. for this are:
  32. * Readlocks ensure that once CVS has found a collection of rcs
  33. files using Find_Names, the files will still exist when it reads
  34. them (they may have moved in or out of the attic).
  35. * Readlocks provide some modicum of consistency, although this is
  36. kind of limited--see the node Concurrency in cvs.texinfo.
  37. * Readlocks ensure that the RCS file does not change between
  38. RCS_parse and RCS_reparsercsfile time. This one strikes me as
  39. important, although I haven't thought up what bad scenarios might
  40. be.
  41. * Readlocks ensure that we won't find the file in the state in
  42. which it is in between the calls to add_rcs_file and RCS_checkin in
  43. commit.c (when a file is being added). This state is a state in
  44. which the RCS file parsing routines in rcs.c cannot parse the file.
  45. * Readlocks ensure that a reader won't try to look at a
  46. half-written fileattr file (fileattr is not updated atomically).
  47. (see also the description of anonymous read-only access in
  48. "Password authentication security" node in doc/cvs.texinfo).
  49. While I'm here, I'll try to summarize a few random suggestions
  50. which periodically get made about how locks might be different:
  51. 1. Check for EROFS. Maybe useful, although in the presence of NFS
  52. EROFS does *not* mean that the file system is unchanging.
  53. 2. Provide an option to disable locks for operations which only
  54. read (see above for some of the consequences).
  55. 3. Have a server internally do the locking. Probably a good
  56. long-term solution, and many people have been working hard on code
  57. changes which would eventually make it possible to have a server
  58. which can handle various connections in one process, but there is
  59. much, much work still to be done before this is feasible. */
  60. #include "cvs.h"
  61. #include <assert.h>
  62. #ifdef HAVE_NANOSLEEP
  63. # include "xtime.h"
  64. #else /* HAVE_NANOSLEEP */
  65. # if !defined HAVE_USLEEP && defined HAVE_SELECT
  66. /* use select as a workaround */
  67. # include "xselect.h"
  68. # endif /* !defined HAVE_USLEEP && defined HAVE_SELECT */
  69. #endif /* !HAVE_NANOSLEEP */
  70. struct lock {
  71. /* This is the directory in which we may have a lock named by the
  72. readlock variable, a lock named by the writelock variable, and/or
  73. a lock named CVSLCK. The storage is not allocated along with the
  74. struct lock; it is allocated by the Reader_Lock caller or in the
  75. case of writelocks, it is just a pointer to the storage allocated
  76. for the ->key field. */
  77. char *repository;
  78. /* The name of the master lock dir. Usually CVSLCK. */
  79. const char *lockdirname;
  80. /* The full path to the lock dir, if we are currently holding it.
  81. *
  82. * This will be LOCKDIRNAME catted onto REPOSITORY. We waste a little
  83. * space by storing it, but save a later malloc/free.
  84. */
  85. char *lockdir;
  86. /* Note there is no way of knowing whether the readlock and writelock
  87. exist. The code which sets the locks doesn't use SIG_beginCrSect
  88. to set a flag like we do for CVSLCK. */
  89. };
  90. static void remove_locks PROTO((void));
  91. static int readers_exist PROTO((char *repository));
  92. static int set_lock PROTO ((struct lock *lock, int will_wait));
  93. static void clear_lock PROTO ((struct lock *lock));
  94. static void set_lockers_name PROTO((struct stat *statp));
  95. static int set_writelock_proc PROTO((Node * p, void *closure));
  96. static int unlock_proc PROTO((Node * p, void *closure));
  97. static int write_lock PROTO ((struct lock *lock));
  98. static void lock_simple_remove PROTO ((struct lock *lock));
  99. static void lock_wait PROTO((char *repository));
  100. static void lock_obtained PROTO((char *repository));
  101. /* Malloc'd array containing the username of the whoever has the lock.
  102. Will always be non-NULL in the cases where it is needed. */
  103. static char *lockers_name;
  104. /* Malloc'd array specifying name of a readlock within a directory.
  105. Or NULL if none. */
  106. static char *readlock;
  107. /* Malloc'd array specifying name of a writelock within a directory.
  108. Or NULL if none. */
  109. static char *writelock;
  110. /* Malloc'd array specifying the name of a CVSLCK file (absolute pathname).
  111. Will always be non-NULL in the cases where it is used. */
  112. static List *locklist;
  113. #define L_OK 0 /* success */
  114. #define L_ERROR 1 /* error condition */
  115. #define L_LOCKED 2 /* lock owned by someone else */
  116. /* This is the (single) readlock which is set by Reader_Lock. The
  117. repository field is NULL if there is no such lock. */
  118. static struct lock global_readlock = {NULL, CVSLCK, NULL};
  119. static struct lock global_history_lock = {NULL, CVSHISTORYLCK, NULL};
  120. static struct lock global_val_tags_lock = {NULL, CVSVALTAGSLCK, NULL};
  121. /* List of locks set by lock_tree_for_write. This is redundant
  122. with locklist, sort of. */
  123. static List *lock_tree_list;
  124. /* If we set locks with lock_dir_for_write, then locked_dir contains
  125. the malloc'd name of the repository directory which we have locked.
  126. locked_list is the same thing packaged into a list and is redundant
  127. with locklist the same way that lock_tree_list is. */
  128. static char *locked_dir;
  129. static List *locked_list;
  130. /* LockDir from CVSROOT/config. */
  131. char *lock_dir;
  132. static char *lock_name PROTO ((const char *repository, const char *name));
  133. /* Return a newly malloc'd string containing the name of the lock for the
  134. repository REPOSITORY and the lock file name within that directory
  135. NAME. Also create the directories in which to put the lock file
  136. if needed (if we need to, could save system call(s) by doing
  137. that only if the actual operation fails. But for now we'll keep
  138. things simple). */
  139. static char *
  140. lock_name (repository, name)
  141. const char *repository;
  142. const char *name;
  143. {
  144. char *retval;
  145. const char *p;
  146. char *q;
  147. const char *short_repos;
  148. mode_t save_umask = 0;
  149. int saved_umask = 0;
  150. if (lock_dir == NULL)
  151. {
  152. /* This is the easy case. Because the lock files go directly
  153. in the repository, no need to create directories or anything. */
  154. retval = xmalloc (strlen (repository) + strlen (name) + 10);
  155. (void) sprintf (retval, "%s/%s", repository, name);
  156. }
  157. else
  158. {
  159. struct stat sb;
  160. mode_t new_mode = 0;
  161. /* The interesting part of the repository is the part relative
  162. to CVSROOT. */
  163. assert (current_parsed_root != NULL);
  164. assert (current_parsed_root->directory != NULL);
  165. assert (strncmp (repository, current_parsed_root->directory,
  166. strlen (current_parsed_root->directory)) == 0);
  167. short_repos = repository + strlen (current_parsed_root->directory) + 1;
  168. if (strcmp (repository, current_parsed_root->directory) == 0)
  169. short_repos = ".";
  170. else
  171. assert (short_repos[-1] == '/');
  172. retval = xmalloc (strlen (lock_dir)
  173. + strlen (short_repos)
  174. + strlen (name)
  175. + 10);
  176. strcpy (retval, lock_dir);
  177. q = retval + strlen (retval);
  178. *q++ = '/';
  179. strcpy (q, short_repos);
  180. /* In the common case, where the directory already exists, let's
  181. keep it to one system call. */
  182. if (CVS_STAT (retval, &sb) < 0)
  183. {
  184. /* If we need to be creating more than one directory, we'll
  185. get the existence_error here. */
  186. if (!existence_error (errno))
  187. error (1, errno, "cannot stat directory %s", retval);
  188. }
  189. else
  190. {
  191. if (S_ISDIR (sb.st_mode))
  192. goto created;
  193. else
  194. error (1, 0, "%s is not a directory", retval);
  195. }
  196. /* Now add the directories one at a time, so we can create
  197. them if needed.
  198. The idea behind the new_mode stuff is that the directory we
  199. end up creating will inherit permissions from its parent
  200. directory (we re-set new_mode with each EEXIST). CVSUMASK
  201. isn't right, because typically the reason for LockDir is to
  202. use a different set of permissions. We probably want to
  203. inherit group ownership also (but we don't try to deal with
  204. that, some systems do it for us either always or when g+s is on).
  205. We don't try to do anything about the permissions on the lock
  206. files themselves. The permissions don't really matter so much
  207. because the locks will generally be removed by the process
  208. which created them. */
  209. if (CVS_STAT (lock_dir, &sb) < 0)
  210. error (1, errno, "cannot stat %s", lock_dir);
  211. new_mode = sb.st_mode;
  212. save_umask = umask (0000);
  213. saved_umask = 1;
  214. p = short_repos;
  215. while (1)
  216. {
  217. while (!ISDIRSEP (*p) && *p != '\0')
  218. ++p;
  219. if (ISDIRSEP (*p))
  220. {
  221. strncpy (q, short_repos, p - short_repos);
  222. q[p - short_repos] = '\0';
  223. if (!ISDIRSEP (q[p - short_repos - 1])
  224. && CVS_MKDIR (retval, new_mode) < 0)
  225. {
  226. int saved_errno = errno;
  227. if (saved_errno != EEXIST)
  228. error (1, errno, "cannot make directory %s", retval);
  229. else
  230. {
  231. if (CVS_STAT (retval, &sb) < 0)
  232. error (1, errno, "cannot stat %s", retval);
  233. new_mode = sb.st_mode;
  234. }
  235. }
  236. ++p;
  237. }
  238. else
  239. {
  240. strcpy (q, short_repos);
  241. if (CVS_MKDIR (retval, new_mode) < 0
  242. && errno != EEXIST)
  243. error (1, errno, "cannot make directory %s", retval);
  244. goto created;
  245. }
  246. }
  247. created:;
  248. strcat (retval, "/");
  249. strcat (retval, name);
  250. if (saved_umask)
  251. {
  252. assert (umask (save_umask) == 0000);
  253. saved_umask = 0;
  254. }
  255. }
  256. return retval;
  257. }
  258. /*
  259. * Clean up all outstanding locks
  260. */
  261. void
  262. Lock_Cleanup ()
  263. {
  264. /* FIXME: error handling here is kind of bogus; we sometimes will call
  265. error, which in turn can call us again. For the moment work around
  266. this by refusing to reenter this function (this is a kludge). */
  267. /* FIXME-reentrancy: the workaround isn't reentrant. */
  268. static int in_lock_cleanup = 0;
  269. if (trace)
  270. (void) fprintf (stderr, "%s-> Lock_Cleanup()\n", CLIENT_SERVER_STR);
  271. if (in_lock_cleanup)
  272. return;
  273. in_lock_cleanup = 1;
  274. remove_locks ();
  275. dellist (&lock_tree_list);
  276. if (locked_dir != NULL)
  277. {
  278. dellist (&locked_list);
  279. free (locked_dir);
  280. locked_dir = NULL;
  281. locked_list = NULL;
  282. }
  283. if (global_history_lock.repository) clear_history_lock ();
  284. if (global_val_tags_lock.repository) clear_val_tags_lock ();
  285. in_lock_cleanup = 0;
  286. }
  287. /*
  288. * Remove locks without discarding the lock information
  289. */
  290. static void
  291. remove_locks ()
  292. {
  293. /* clean up simple locks (if any) */
  294. if (global_readlock.repository != NULL)
  295. {
  296. lock_simple_remove (&global_readlock);
  297. global_readlock.repository = NULL;
  298. }
  299. /* clean up multiple locks (if any) */
  300. if (locklist != (List *) NULL)
  301. {
  302. (void) walklist (locklist, unlock_proc, NULL);
  303. locklist = (List *) NULL;
  304. }
  305. }
  306. /*
  307. * walklist proc for removing a list of locks
  308. */
  309. static int
  310. unlock_proc (p, closure)
  311. Node *p;
  312. void *closure;
  313. {
  314. lock_simple_remove (p->data);
  315. return (0);
  316. }
  317. /* Remove the lock files. */
  318. static void
  319. lock_simple_remove (lock)
  320. struct lock *lock;
  321. {
  322. char *tmp;
  323. /* If readlock is set, the lock directory *might* have been created, but
  324. since Reader_Lock doesn't use SIG_beginCrSect the way that set_lock
  325. does, we don't know that. That is why we need to check for
  326. existence_error here. */
  327. if (readlock != NULL)
  328. {
  329. tmp = lock_name (lock->repository, readlock);
  330. if (CVS_UNLINK (tmp) < 0 && ! existence_error (errno))
  331. error (0, errno, "failed to remove lock %s", tmp);
  332. free (tmp);
  333. }
  334. /* If writelock is set, the lock directory *might* have been created, but
  335. since write_lock doesn't use SIG_beginCrSect the way that set_lock
  336. does, we don't know that. That is why we need to check for
  337. existence_error here. */
  338. if (writelock != NULL)
  339. {
  340. tmp = lock_name (lock->repository, writelock);
  341. if (CVS_UNLINK (tmp) < 0 && ! existence_error (errno))
  342. error (0, errno, "failed to remove lock %s", tmp);
  343. free (tmp);
  344. }
  345. clear_lock (lock);
  346. }
  347. /*
  348. * Create a lock file for readers
  349. */
  350. int
  351. Reader_Lock (xrepository)
  352. char *xrepository;
  353. {
  354. int err = 0;
  355. FILE *fp;
  356. char *tmp;
  357. if (trace)
  358. (void) fprintf (stderr, "%s-> Reader_Lock(%s)\n", CLIENT_SERVER_STR,
  359. xrepository);
  360. if (noexec || readonlyfs)
  361. return 0;
  362. /* we only do one directory at a time for read locks! */
  363. if (global_readlock.repository != NULL)
  364. {
  365. error (0, 0, "Reader_Lock called while read locks set - Help!");
  366. return 1;
  367. }
  368. if (readlock == NULL)
  369. {
  370. readlock = xmalloc (strlen (hostname) + sizeof (CVSRFL) + 40);
  371. (void) sprintf (readlock,
  372. #ifdef HAVE_LONG_FILE_NAMES
  373. "%s.%s.%ld", CVSRFL, hostname,
  374. #else
  375. "%s.%ld", CVSRFL,
  376. #endif
  377. (long) getpid ());
  378. }
  379. /* remember what we're locking (for Lock_Cleanup) */
  380. global_readlock.repository = xrepository;
  381. /* get the lock dir for our own */
  382. if (set_lock (&global_readlock, 1) != L_OK)
  383. {
  384. error (0, 0, "failed to obtain dir lock in repository `%s'",
  385. xrepository);
  386. if (readlock != NULL)
  387. free (readlock);
  388. readlock = NULL;
  389. /* We don't set global_readlock.repository to NULL. I think this
  390. only works because recurse.c will give a fatal error if we return
  391. a nonzero value. */
  392. return 1;
  393. }
  394. /* write a read-lock */
  395. tmp = lock_name (xrepository, readlock);
  396. if ((fp = CVS_FOPEN (tmp, "w+")) == NULL || fclose (fp) == EOF)
  397. {
  398. error (0, errno, "cannot create read lock in repository `%s'",
  399. xrepository);
  400. if (readlock != NULL)
  401. free (readlock);
  402. readlock = NULL;
  403. err = 1;
  404. }
  405. free (tmp);
  406. /* free the lock dir */
  407. clear_lock (&global_readlock);
  408. return err;
  409. }
  410. /*
  411. * Lock a list of directories for writing
  412. */
  413. static char *lock_error_repos;
  414. static int lock_error;
  415. static int Writer_Lock PROTO ((List * list));
  416. static int
  417. Writer_Lock (list)
  418. List *list;
  419. {
  420. char *wait_repos;
  421. if (noexec)
  422. return 0;
  423. if (readonlyfs) {
  424. error (0, 0, "write lock failed - read-only repository");
  425. return (1);
  426. }
  427. /* We only know how to do one list at a time */
  428. if (locklist != (List *) NULL)
  429. {
  430. error (0, 0, "Writer_Lock called while write locks set - Help!");
  431. return 1;
  432. }
  433. wait_repos = NULL;
  434. for (;;)
  435. {
  436. /* try to lock everything on the list */
  437. lock_error = L_OK; /* init for set_writelock_proc */
  438. lock_error_repos = (char *) NULL; /* init for set_writelock_proc */
  439. locklist = list; /* init for Lock_Cleanup */
  440. if (lockers_name != NULL)
  441. free (lockers_name);
  442. lockers_name = xstrdup ("unknown");
  443. (void) walklist (list, set_writelock_proc, NULL);
  444. switch (lock_error)
  445. {
  446. case L_ERROR: /* Real Error */
  447. if (wait_repos != NULL)
  448. free (wait_repos);
  449. Lock_Cleanup (); /* clean up any locks we set */
  450. error (0, 0, "lock failed - giving up");
  451. return 1;
  452. case L_LOCKED: /* Someone already had a lock */
  453. remove_locks (); /* clean up any locks we set */
  454. lock_wait (lock_error_repos); /* sleep a while and try again */
  455. wait_repos = xstrdup (lock_error_repos);
  456. continue;
  457. case L_OK: /* we got the locks set */
  458. if (wait_repos != NULL)
  459. {
  460. lock_obtained (wait_repos);
  461. free (wait_repos);
  462. }
  463. return 0;
  464. default:
  465. if (wait_repos != NULL)
  466. free (wait_repos);
  467. error (0, 0, "unknown lock status %d in Writer_Lock",
  468. lock_error);
  469. return 1;
  470. }
  471. }
  472. }
  473. /*
  474. * walklist proc for setting write locks
  475. */
  476. static int
  477. set_writelock_proc (p, closure)
  478. Node *p;
  479. void *closure;
  480. {
  481. /* if some lock was not OK, just skip this one */
  482. if (lock_error != L_OK)
  483. return 0;
  484. /* apply the write lock */
  485. lock_error_repos = p->key;
  486. lock_error = write_lock (p->data);
  487. return 0;
  488. }
  489. /*
  490. * Create a lock file for writers returns L_OK if lock set ok, L_LOCKED if
  491. * lock held by someone else or L_ERROR if an error occurred
  492. */
  493. static int
  494. write_lock (lock)
  495. struct lock *lock;
  496. {
  497. int status;
  498. FILE *fp;
  499. char *tmp;
  500. if (trace)
  501. (void) fprintf (stderr, "%s-> write_lock(%s)\n",
  502. CLIENT_SERVER_STR, lock->repository);
  503. if (writelock == NULL)
  504. {
  505. writelock = xmalloc (strlen (hostname) + sizeof (CVSWFL) + 40);
  506. (void) sprintf (writelock,
  507. #ifdef HAVE_LONG_FILE_NAMES
  508. "%s.%s.%ld", CVSWFL, hostname,
  509. #else
  510. "%s.%ld", CVSWFL,
  511. #endif
  512. (long) getpid());
  513. }
  514. /* make sure the lock dir is ours (not necessarily unique to us!) */
  515. status = set_lock (lock, 0);
  516. if (status == L_OK)
  517. {
  518. /* we now own a writer - make sure there are no readers */
  519. if (readers_exist (lock->repository))
  520. {
  521. /* clean up the lock dir if we created it */
  522. if (status == L_OK)
  523. {
  524. clear_lock (lock);
  525. }
  526. /* indicate we failed due to read locks instead of error */
  527. return L_LOCKED;
  528. }
  529. /* write the write-lock file */
  530. tmp = lock_name (lock->repository, writelock);
  531. if ((fp = CVS_FOPEN (tmp, "w+")) == NULL || fclose (fp) == EOF)
  532. {
  533. int xerrno = errno;
  534. if ( CVS_UNLINK (tmp) < 0 && ! existence_error (errno))
  535. error (0, errno, "failed to remove lock %s", tmp);
  536. /* free the lock dir if we created it */
  537. if (status == L_OK)
  538. {
  539. clear_lock (lock);
  540. }
  541. /* return the error */
  542. error (0, xerrno, "cannot create write lock in repository `%s'",
  543. lock->repository);
  544. free (tmp);
  545. return L_ERROR;
  546. }
  547. free (tmp);
  548. return L_OK;
  549. }
  550. else
  551. return status;
  552. }
  553. /*
  554. * readers_exist() returns 0 if there are no reader lock files remaining in
  555. * the repository; else 1 is returned, to indicate that the caller should
  556. * sleep a while and try again.
  557. */
  558. static int
  559. readers_exist (repository)
  560. char *repository;
  561. {
  562. char *lockdir;
  563. char *line;
  564. DIR *dirp;
  565. struct dirent *dp;
  566. struct stat sb;
  567. int ret;
  568. #ifdef CVS_FUDGELOCKS
  569. time_t now;
  570. (void)time (&now);
  571. #endif
  572. lockdir = lock_name (repository, "");
  573. assert (lockdir != NULL);
  574. lockdir[strlen (lockdir) - 1] = '\0'; /* remove trailing slash */
  575. do {
  576. if ((dirp = CVS_OPENDIR (lockdir)) == NULL)
  577. error (1, 0, "cannot open directory %s", lockdir);
  578. ret = 0;
  579. errno = 0;
  580. while ((dp = CVS_READDIR (dirp)) != NULL)
  581. {
  582. if (CVS_FNMATCH (CVSRFLPAT, dp->d_name, 0) == 0)
  583. {
  584. line = xmalloc (strlen (lockdir) + 1 + strlen (dp->d_name) + 1);
  585. (void)sprintf (line, "%s/%s", lockdir, dp->d_name);
  586. if (CVS_STAT (line, &sb) != -1)
  587. {
  588. #ifdef CVS_FUDGELOCKS
  589. /*
  590. * If the create time of the file is more than CVSLCKAGE
  591. * seconds ago, try to clean-up the lock file, and if
  592. * successful, re-open the directory and try again.
  593. */
  594. if (now >= (sb.st_ctime + CVSLCKAGE) &&
  595. CVS_UNLINK (line) != -1)
  596. {
  597. free (line);
  598. ret = -1;
  599. break;
  600. }
  601. #endif
  602. set_lockers_name (&sb);
  603. }
  604. else
  605. {
  606. /* If the file doesn't exist, it just means that it
  607. * disappeared between the time we did the readdir and the
  608. * time we did the stat.
  609. */
  610. if (!existence_error (errno))
  611. error (0, errno, "cannot stat %s", line);
  612. }
  613. errno = 0;
  614. free (line);
  615. ret = 1;
  616. break;
  617. }
  618. errno = 0;
  619. }
  620. if (errno != 0)
  621. error (0, errno, "error reading directory %s", repository);
  622. CVS_CLOSEDIR (dirp);
  623. } while (ret < 0);
  624. if (lockdir != NULL)
  625. free (lockdir);
  626. return ret;
  627. }
  628. /*
  629. * Set the static variable lockers_name appropriately, based on the stat
  630. * structure passed in.
  631. */
  632. static void
  633. set_lockers_name (statp)
  634. struct stat *statp;
  635. {
  636. struct passwd *pw;
  637. if (lockers_name != NULL)
  638. free (lockers_name);
  639. if ((pw = (struct passwd *)getpwuid (statp->st_uid)) !=
  640. (struct passwd *)NULL)
  641. {
  642. lockers_name = xstrdup (pw->pw_name);
  643. }
  644. else
  645. {
  646. lockers_name = xmalloc (20);
  647. (void)sprintf (lockers_name, "uid%lu", (unsigned long) statp->st_uid);
  648. }
  649. }
  650. /*
  651. * Persistently tries to make the directory "lckdir", which serves as a
  652. * lock.
  653. *
  654. * #ifdef CVS_FUDGELOCKS
  655. * If the create time on the directory is greater than CVSLCKAGE
  656. * seconds old, just try to remove the directory.
  657. * #endif
  658. *
  659. */
  660. static int
  661. set_lock (lock, will_wait)
  662. struct lock *lock;
  663. int will_wait;
  664. {
  665. int waited;
  666. long us;
  667. struct stat sb;
  668. mode_t omask;
  669. char *masterlock;
  670. int status;
  671. #ifdef CVS_FUDGELOCKS
  672. time_t now;
  673. #endif
  674. masterlock = lock_name (lock->repository, lock->lockdirname);
  675. /*
  676. * Note that it is up to the callers of set_lock() to arrange for signal
  677. * handlers that do the appropriate things, like remove the lock
  678. * directory before they exit.
  679. */
  680. waited = 0;
  681. us = 1;
  682. for (;;)
  683. {
  684. status = -1;
  685. omask = umask (cvsumask);
  686. SIG_beginCrSect ();
  687. if (CVS_MKDIR (masterlock, 0777) == 0)
  688. {
  689. lock->lockdir = masterlock;
  690. SIG_endCrSect ();
  691. status = L_OK;
  692. if (waited)
  693. lock_obtained (lock->repository);
  694. goto after_sig_unblock;
  695. }
  696. SIG_endCrSect ();
  697. after_sig_unblock:
  698. (void) umask (omask);
  699. if (status != -1)
  700. goto done;
  701. if (errno != EEXIST)
  702. {
  703. error (0, errno,
  704. "failed to create lock directory for `%s' (%s)",
  705. lock->repository, masterlock);
  706. status = L_ERROR;
  707. goto done;
  708. }
  709. /* Find out who owns the lock. If the lock directory is
  710. non-existent, re-try the loop since someone probably just
  711. removed it (thus releasing the lock). */
  712. if (CVS_STAT (masterlock, &sb) < 0)
  713. {
  714. if (existence_error (errno))
  715. continue;
  716. error (0, errno, "couldn't stat lock directory `%s'", masterlock);
  717. status = L_ERROR;
  718. goto done;
  719. }
  720. #ifdef CVS_FUDGELOCKS
  721. /*
  722. * If the create time of the directory is more than CVSLCKAGE seconds
  723. * ago, try to clean-up the lock directory, and if successful, just
  724. * quietly retry to make it.
  725. */
  726. (void) time (&now);
  727. if (now >= (sb.st_ctime + CVSLCKAGE))
  728. {
  729. if (CVS_RMDIR (masterlock) >= 0)
  730. continue;
  731. }
  732. #endif
  733. /* set the lockers name */
  734. set_lockers_name (&sb);
  735. /* if he wasn't willing to wait, return an error */
  736. if (!will_wait)
  737. {
  738. status = L_LOCKED;
  739. goto done;
  740. }
  741. /* if possible, try a very short sleep without a message */
  742. if (!waited && us < 1000)
  743. {
  744. us += us;
  745. #if defined HAVE_NANOSLEEP
  746. {
  747. struct timespec ts;
  748. ts.tv_sec = 0;
  749. ts.tv_nsec = us * 1000;
  750. (void)nanosleep (&ts, NULL);
  751. continue;
  752. }
  753. #elif defined HAVE_USLEEP
  754. (void)usleep (us);
  755. continue;
  756. #elif defined HAVE_SELECT
  757. {
  758. struct timeval tv;
  759. tv.tv_sec = 0;
  760. tv.tv_usec = us;
  761. (void)select (0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL, &tv);
  762. continue;
  763. }
  764. #endif
  765. }
  766. lock_wait (lock->repository);
  767. waited = 1;
  768. }
  769. done:
  770. if (!lock->lockdir) free (masterlock);
  771. return status;
  772. }
  773. /*
  774. * Clear master lock.
  775. *
  776. * INPUTS
  777. * lock The lock information.
  778. *
  779. * OUTPUTS
  780. * Sets LOCK->lockdir to NULL after removing the directory it names and
  781. * freeing the storage.
  782. *
  783. * ASSUMPTIONS
  784. * If we own the master lock directory, its name is stored in LOCK->lockdir.
  785. * We may free LOCK->lockdir.
  786. *
  787. */
  788. static void
  789. clear_lock (lock)
  790. struct lock *lock;
  791. {
  792. SIG_beginCrSect ();
  793. if (lock->lockdir)
  794. {
  795. if (CVS_RMDIR (lock->lockdir) < 0)
  796. error (0, errno, "failed to remove lock dir `%s'", lock->lockdir);
  797. free (lock->lockdir);
  798. lock->lockdir = NULL;
  799. }
  800. SIG_endCrSect ();
  801. }
  802. /*
  803. * Print out a message that the lock is still held, then sleep a while.
  804. */
  805. static void
  806. lock_wait (repos)
  807. char *repos;
  808. {
  809. time_t now;
  810. char *msg;
  811. struct tm *tm_p;
  812. (void) time (&now);
  813. tm_p = gmtime (&now);
  814. msg = xmalloc (100 + strlen (lockers_name) + strlen (repos));
  815. sprintf (msg, "[%8.8s] waiting for %s's lock in %s",
  816. (tm_p ? asctime (tm_p) : ctime (&now)) + 11,
  817. lockers_name, repos);
  818. error (0, 0, "%s", msg);
  819. /* Call cvs_flusherr to ensure that the user sees this message as
  820. soon as possible. */
  821. cvs_flusherr ();
  822. free (msg);
  823. (void) sleep (CVSLCKSLEEP);
  824. }
  825. /*
  826. * Print out a message when we obtain a lock.
  827. */
  828. static void
  829. lock_obtained (repos)
  830. char *repos;
  831. {
  832. time_t now;
  833. char *msg;
  834. struct tm *tm_p;
  835. (void) time (&now);
  836. tm_p = gmtime (&now);
  837. msg = xmalloc (100 + strlen (repos));
  838. sprintf (msg, "[%8.8s] obtained lock in %s",
  839. (tm_p ? asctime (tm_p) : ctime (&now)) + 11, repos);
  840. error (0, 0, "%s", msg);
  841. /* Call cvs_flusherr to ensure that the user sees this message as
  842. soon as possible. */
  843. cvs_flusherr ();
  844. free (msg);
  845. }
  846. static int lock_filesdoneproc PROTO ((void *callerdat, int err,
  847. const char *repository,
  848. const char *update_dir,
  849. List *entries));
  850. /*
  851. * Create a list of repositories to lock
  852. */
  853. /* ARGSUSED */
  854. static int
  855. lock_filesdoneproc (callerdat, err, repository, update_dir, entries)
  856. void *callerdat;
  857. int err;
  858. const char *repository;
  859. const char *update_dir;
  860. List *entries;
  861. {
  862. Node *p;
  863. p = getnode ();
  864. p->type = LOCK;
  865. p->key = xstrdup (repository);
  866. p->data = xmalloc (sizeof (struct lock));
  867. ((struct lock *)p->data)->repository = p->key;
  868. ((struct lock *)p->data)->lockdirname = CVSLCK;
  869. ((struct lock *)p->data)->lockdir = NULL;
  870. /* FIXME-KRP: this error condition should not simply be passed by. */
  871. if (p->key == NULL || addnode (lock_tree_list, p) != 0)
  872. freenode (p);
  873. return (err);
  874. }
  875. void
  876. lock_tree_for_write (argc, argv, local, which, aflag)
  877. int argc;
  878. char **argv;
  879. int local;
  880. int which;
  881. int aflag;
  882. {
  883. /*
  884. * Run the recursion processor to find all the dirs to lock and lock all
  885. * the dirs
  886. */
  887. lock_tree_list = getlist ();
  888. start_recursion ((FILEPROC) NULL, lock_filesdoneproc,
  889. (DIRENTPROC) NULL, (DIRLEAVEPROC) NULL, NULL, argc,
  890. argv, local, which, aflag, CVS_LOCK_NONE,
  891. (char *) NULL, 0, (char *) NULL);
  892. sortlist (lock_tree_list, fsortcmp);
  893. if (Writer_Lock (lock_tree_list) != 0)
  894. error (1, 0, "lock failed - giving up");
  895. }
  896. /* Lock a single directory in REPOSITORY. It is OK to call this if
  897. a lock has been set with lock_dir_for_write; the new lock will replace
  898. the old one. If REPOSITORY is NULL, don't do anything. */
  899. void
  900. lock_dir_for_write (repository)
  901. char *repository;
  902. {
  903. if (repository != NULL
  904. && (locked_dir == NULL
  905. || strcmp (locked_dir, repository) != 0))
  906. {
  907. Node *node;
  908. if (locked_dir != NULL)
  909. Lock_Cleanup ();
  910. locked_dir = xstrdup (repository);
  911. locked_list = getlist ();
  912. node = getnode ();
  913. node->type = LOCK;
  914. node->key = xstrdup (repository);
  915. node->data = xmalloc (sizeof (struct lock));
  916. ((struct lock *)node->data)->repository = node->key;
  917. ((struct lock *)node->data)->lockdirname = CVSLCK;
  918. ((struct lock *)node->data)->lockdir = NULL;
  919. (void) addnode (locked_list, node);
  920. Writer_Lock (locked_list);
  921. }
  922. }
  923. /* This is the internal implementation behind history_lock & val_tags_lock. It
  924. * gets a write lock for the history or val-tags file.
  925. *
  926. * RETURNS
  927. * true, on success
  928. * false, on error
  929. */
  930. static int internal_lock PROTO ((struct lock *lock, const char *xrepository));
  931. static int
  932. internal_lock (lock, xrepository)
  933. struct lock *lock;
  934. const char *xrepository;
  935. {
  936. /* remember what we're locking (for Lock_Cleanup) */
  937. assert (!lock->repository);
  938. lock->repository = xmalloc (strlen (xrepository) + sizeof (CVSROOTADM) + 2);
  939. sprintf (lock->repository, "%s/%s", xrepository, CVSROOTADM);
  940. /* get the lock dir for our own */
  941. if (set_lock (lock, 1) != L_OK)
  942. {
  943. if (!really_quiet)
  944. error (0, 0, "failed to obtain history lock in repository `%s'",
  945. xrepository);
  946. return 0;
  947. }
  948. return 1;
  949. }
  950. /* This is the internal implementation behind history_lock & val_tags_lock. It
  951. * removes the write lock for the history or val-tags file, when it exists.
  952. */
  953. static void internal_clear_lock PROTO((struct lock *lock));
  954. static void
  955. internal_clear_lock (lock)
  956. struct lock *lock;
  957. {
  958. SIG_beginCrSect ();
  959. if (lock->repository)
  960. {
  961. free (lock->repository);
  962. lock->repository = NULL;
  963. }
  964. SIG_endCrSect ();
  965. clear_lock (lock);
  966. }
  967. /* Lock the CVSROOT/history file for write.
  968. */
  969. int
  970. history_lock (xrepository)
  971. const char *xrepository;
  972. {
  973. return internal_lock (&global_history_lock, xrepository);
  974. }
  975. /* Remove the CVSROOT/history lock, if it exists.
  976. */
  977. void
  978. clear_history_lock ()
  979. {
  980. internal_clear_lock (&global_history_lock);
  981. }
  982. /* Lock the CVSROOT/val-tags file for write.
  983. */
  984. int
  985. val_tags_lock (xrepository)
  986. const char *xrepository;
  987. {
  988. return internal_lock (&global_val_tags_lock, xrepository);
  989. }
  990. /* Remove the CVSROOT/val-tags lock, if it exists.
  991. */
  992. void
  993. clear_val_tags_lock ()
  994. {
  995. internal_clear_lock (&global_val_tags_lock);
  996. }