aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS4
-rw-r--r--butl/process22
-rw-r--r--butl/process.cxx32
-rw-r--r--butl/process.ixx12
4 files changed, 47 insertions, 23 deletions
diff --git a/NEWS b/NEWS
index d406f23..dfcbd6c 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+Version 0.4.0
+
+ * Add process::current_id().
+
Version 0.3.0
* Add SHA256 hash calculator based on code from the FreeBSD project. That
diff --git a/butl/process b/butl/process
index ef8c057..c485835 100644
--- a/butl/process
+++ b/butl/process
@@ -10,6 +10,7 @@
#endif
#include <cassert>
+#include <cstdint> // uint32_t
#include <system_error>
namespace butl
@@ -26,8 +27,9 @@ namespace butl
bool child_;
};
- struct process
+ class process
{
+ public:
// Start another process using the specified command line. The default
// values to the in, out and err arguments indicate that the child
// process should inherit the parent process stdin, stdout, and stderr,
@@ -81,7 +83,7 @@ namespace butl
bool
try_wait (bool&);
- ~process () {if (id != 0) wait ();}
+ ~process () {if (handle != 0) wait ();}
// Moveable-only type.
//
@@ -91,20 +93,26 @@ namespace butl
process (const process&) = delete;
process& operator= (const process&) = delete;
- // Create an empty or "already terminated" process. That is, id is 0
+ // Create an empty or "already terminated" process. That is, handle is 0
// and exit status is 0.
//
process ();
public:
#ifndef _WIN32
- typedef pid_t id_type;
- typedef int status_type;
+ using handle_type = pid_t;
+ using id_type = pid_t;
+ using status_type = int;
#else
- typedef void* id_type; // Win32 HANDLE.
+ using handle_type = void*; // Win32 HANDLE
+ using id_type = std::uint32_t; // Win32 DWORD
#endif
- id_type id;
+ static id_type
+ current_id ();
+
+ public:
+ handle_type handle;
status_type status;
int out_fd; // Write to this fd to send to the new process' stdin.
diff --git a/butl/process.cxx b/butl/process.cxx
index 4203735..f00f03a 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -5,7 +5,7 @@
#include <butl/process>
#ifndef _WIN32
-# include <unistd.h> // execvp, fork, dup2, pipe, chdir, *_FILENO
+# include <unistd.h> // execvp, fork, dup2, pipe, chdir, *_FILENO, getpid
# include <sys/wait.h> // waitpid
#else
# ifndef WIN32_LEAN_AND_MEAN
@@ -36,12 +36,12 @@ namespace butl
(err == -1 && pipe (in_efd) == -1))
throw process_error (errno, false);
- id = fork ();
+ handle = fork ();
- if (id == -1)
+ if (handle == -1)
throw process_error (errno, false);
- if (id == 0)
+ if (handle == 0)
{
// Child. If requested, close the write end of the pipe and duplicate
// the read end to stdin. Then close the original read end descriptor.
@@ -109,10 +109,10 @@ namespace butl
bool process::
wait ()
{
- if (id != 0)
+ if (handle != 0)
{
- int r (waitpid (id, &status, 0));
- id = 0; // We have tried.
+ int r (waitpid (handle, &status, 0));
+ handle = 0; // We have tried.
if (r == -1)
throw process_error (errno, false);
@@ -124,14 +124,14 @@ namespace butl
bool process::
try_wait (bool& s)
{
- if (id != 0)
+ if (handle != 0)
{
- int r (waitpid (id, &status, WNOHANG));
+ int r (waitpid (handle, &status, WNOHANG));
if (r == 0) // Not exited yet.
return false;
- id = 0; // We have tried.
+ handle = 0; // We have tried.
if (r == -1)
throw process_error (errno, false);
@@ -141,8 +141,20 @@ namespace butl
return true;
}
+ process::id_type process::
+ current_id ()
+ {
+ return getpid ();
+ }
+
#else // _WIN32
+ process::id_type process::
+ current_id ()
+ {
+ return GetCurrentProcessId ();
+ }
+
static void
print_error (char const* name)
{
diff --git a/butl/process.ixx b/butl/process.ixx
index e840372..3b7ed7d 100644
--- a/butl/process.ixx
+++ b/butl/process.ixx
@@ -6,7 +6,7 @@ namespace butl
{
inline process::
process ()
- : id (0),
+ : handle (0),
status (0), // This is a bit of an assumption.
out_fd (-1),
in_ofd (-1),
@@ -24,13 +24,13 @@ namespace butl
inline process::
process (process&& p)
- : id (p.id),
+ : handle (p.handle),
status (p.status),
out_fd (p.out_fd),
in_ofd (p.in_ofd),
in_efd (p.in_efd)
{
- p.id = 0;
+ p.handle = 0;
}
inline process& process::
@@ -38,16 +38,16 @@ namespace butl
{
if (this != &p)
{
- if (id != 0)
+ if (handle != 0)
wait ();
- id = p.id;
+ handle = p.handle;
status = p.status;
out_fd = p.out_fd;
in_ofd = p.in_ofd;
in_efd = p.in_efd;
- p.id = 0;
+ p.handle = 0;
}
return *this;