From a3463829567defda8aefa8425e79ff4d270617ba Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 11 May 2023 09:30:46 +0200 Subject: Use fake lock for fake machine locking --- bbot/agent/agent.cxx | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'bbot') diff --git a/bbot/agent/agent.cxx b/bbot/agent/agent.cxx index 24e3f81..8f54346 100644 --- a/bbot/agent/agent.cxx +++ b/bbot/agent/agent.cxx @@ -505,10 +505,13 @@ public: { fl_ = false; // We have tried. - try_rmfile (fp_, ignore_errors); + if (fd_ != nullfd) + { + try_rmfile (fp_, ignore_errors); - if (flock (fd_.get (), LOCK_UN) != 0 && !ignore_errors) - throw_generic_error (errno); + if (flock (fd_.get (), LOCK_UN) != 0 && !ignore_errors) + throw_generic_error (errno); + } } } @@ -522,23 +525,26 @@ public: { assert (tl.locked () && fl_); - pid_t pid (getpid ()); + if (fd_ != nullfd) + { + pid_t pid (getpid ()); - string l (to_string (pid)); + string l (to_string (pid)); - if (prio) - { - l += ' '; - l += to_string (*prio); - } + if (prio) + { + l += ' '; + l += to_string (*prio); + } - auto n (fdwrite (fd_.get (), l.c_str (), l.size ())); + auto n (fdwrite (fd_.get (), l.c_str (), l.size ())); - if (n == -1) - throw_generic_ios_failure (errno); + if (n == -1) + throw_generic_ios_failure (errno); - if (static_cast (n) != l.size ()) - throw_generic_ios_failure (EFBIG); + if (static_cast (n) != l.size ()) + throw_generic_ios_failure (EFBIG); + } } ~machine_lock () @@ -555,6 +561,8 @@ public: // Implementation details. // public: + // If fd is nullfd, treat it as a fake lock (used for fake machines). + // machine_lock (path&& fp, auto_fd&& fd) : fp_ (move (fp)), fd_ (move (fd)), fl_ (true) {} @@ -719,7 +727,7 @@ try r.push_back ( bootstrapped_machine { dir_path (ops.machines ()) /= mh.name, // For diagnostics. - machine_lock (), + machine_lock (path (), nullfd), // Fake lock. bootstrapped_machine_manifest { machine_manifest { move (mh.id), @@ -1771,7 +1779,7 @@ try { // @@ For now skip machines locked by other processes. // - if (ops.fake_machine_specified () || m.lock.locked ()) + if (m.lock.locked ()) tq.machines.emplace_back (m.manifest.machine.id, m.manifest.machine.name, m.manifest.machine.summary); @@ -1942,9 +1950,7 @@ try { if (mh.name == m.manifest.machine.name) { - if (!ops.fake_machine_specified ()) - m.lock.write (tl, 1234 /* prio */); - + m.lock.write (tl, 1234 /* prio */); pm = &m; } else -- cgit v1.1