Merge pull request #15110 from obsidiansystems/pid-cleanup
libutil: add useful functions to Pid
This commit is contained in:
@@ -35,6 +35,10 @@ class Pid
|
||||
#endif
|
||||
public:
|
||||
Pid();
|
||||
Pid(const Pid &) = delete;
|
||||
Pid(Pid && other) noexcept;
|
||||
Pid & operator=(const Pid &) = delete;
|
||||
Pid & operator=(Pid && other) noexcept;
|
||||
#ifndef _WIN32
|
||||
Pid(pid_t pid);
|
||||
void operator=(pid_t pid);
|
||||
@@ -53,6 +57,18 @@ public:
|
||||
void setKillSignal(int signal);
|
||||
pid_t release();
|
||||
#endif
|
||||
|
||||
friend void swap(Pid & lhs, Pid & rhs) noexcept
|
||||
{
|
||||
using std::swap;
|
||||
#ifndef _WIN32
|
||||
swap(lhs.pid, rhs.pid);
|
||||
swap(lhs.separatePG, rhs.separatePG);
|
||||
swap(lhs.killSignal, rhs.killSignal);
|
||||
#else
|
||||
swap(lhs.pid, rhs.pid);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
@@ -156,6 +156,7 @@ sources = [ config_priv_h ] + files(
|
||||
'pos-table.cc',
|
||||
'position.cc',
|
||||
'posix-source-accessor.cc',
|
||||
'processes.cc',
|
||||
'serialise.cc',
|
||||
'signature/local-keys.cc',
|
||||
'signature/signer.cc',
|
||||
|
||||
11
src/libutil/processes.cc
Normal file
11
src/libutil/processes.cc
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "nix/util/processes.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
Pid & Pid::operator=(Pid && other) noexcept
|
||||
{
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
@@ -35,6 +35,14 @@ namespace nix {
|
||||
|
||||
Pid::Pid() {}
|
||||
|
||||
Pid::Pid(Pid && other) noexcept
|
||||
: pid(other.pid)
|
||||
, separatePG(other.separatePG)
|
||||
, killSignal(other.killSignal)
|
||||
{
|
||||
other.release();
|
||||
}
|
||||
|
||||
Pid::Pid(pid_t pid)
|
||||
: pid(pid)
|
||||
{
|
||||
@@ -113,7 +121,12 @@ void Pid::setKillSignal(int signal)
|
||||
pid_t Pid::release()
|
||||
{
|
||||
pid_t p = pid;
|
||||
/* We use the move assignment operator rather than setting the individual fields so we aren't duplicating the
|
||||
default values from the header, which would be hard to keep in sync. If we just used the assignment operator
|
||||
without manually resetting pid first it would kill that process, however, so we do manually reset that one field.
|
||||
*/
|
||||
pid = -1;
|
||||
*this = Pid();
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,11 @@ using namespace nix::windows;
|
||||
|
||||
Pid::Pid() {}
|
||||
|
||||
Pid::Pid(Pid && other) noexcept
|
||||
: pid(std::move(other.pid))
|
||||
{
|
||||
}
|
||||
|
||||
Pid::Pid(AutoCloseFD pid)
|
||||
: pid(std::move(pid))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user