Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/libutil/include/nix/util/processes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/libutil/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 11 additions & 0 deletions src/libutil/processes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "nix/util/processes.hh"

namespace nix {

Pid & Pid::operator=(Pid && other) noexcept
{
swap(*this, other);
return *this;
}

} // namespace nix
13 changes: 13 additions & 0 deletions src/libutil/unix/processes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions src/libutil/windows/processes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Loading