aboutsummaryrefslogtreecommitdiff
path: root/src/or/main.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-09-01 20:06:26 +0000
committerNick Mathewson <nickm@torproject.org>2008-09-01 20:06:26 +0000
commitf80ac31d742c2d86f1812b895ef61a5885e2447e (patch)
tree9b223723aced9e7a8acc55044120cc406c77bd9b /src/or/main.c
parent6a29ad853b630a1970fd95e17ecd79306e7c39e6 (diff)
downloadtor-f80ac31d742c2d86f1812b895ef61a5885e2447e.tar
tor-f80ac31d742c2d86f1812b895ef61a5885e2447e.tar.gz
Add a lockfile to the Tor data directory to avoid situations where two Tors start with the same datadir, or where a --list-fingerprints races with a server to create keys, or such.
svn:r16722
Diffstat (limited to 'src/or/main.c')
-rw-r--r--src/or/main.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/or/main.c b/src/or/main.c
index c3f902e77..1d57cff5e 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -1850,6 +1850,54 @@ tor_init(int argc, char *argv[])
return 0;
}
+static tor_lockfile_t *lockfile = NULL;
+
+int
+try_locking(or_options_t *options, int err_if_locked)
+{
+ if (lockfile)
+ return 0;
+ else {
+ char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
+ int already_locked = 0;
+ tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
+ tor_free(fname);
+ if (!lf) {
+ if (err_if_locked && already_locked) {
+ int r;
+ log_warn(LD_GENERAL, "It looks like another Tor process is running "
+ "with the same data directory. Waiting 5 seconds to see "
+ "if it goes away.");
+ sleep(5);
+ r = try_locking(options, 0);
+ if (r<0) {
+ log_err(LD_GENERAL, "No, it's still there. Exiting.");
+ exit(0);
+ }
+ return r;
+ }
+ return -1;
+ }
+ lockfile = lf;
+ return 0;
+ }
+}
+
+int
+have_lockfile(void)
+{
+ return lockfile != NULL;
+}
+
+void
+release_lockfile(void)
+{
+ if (lockfile) {
+ tor_lockfile_unlock(lockfile);
+ lockfile = NULL;
+ }
+}
+
/** Free all memory that we might have allocated somewhere.
* If <b>postfork</b>, we are a worker process and we want to free
* only the parts of memory that we won't touch. If !<b>postfork</b>,