Skip to content

Commit

Permalink
io/posix: implement wait() and run()
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedede committed Mar 22, 2024
1 parent 7c0be2a commit 4f9221f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
46 changes: 40 additions & 6 deletions io/include/aw/io/posix/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,63 @@ constexpr auto invalid_process_handle = process_handle(-1L );
* Spawn a child process with specified \a path and argument list \a argv.
* Argument list must end with `nullptr`.
*/
AW_IO_EXP process_handle spawn(const char* path, aw::array_ref<char*> argv, std::error_code& ec) noexcept;
AW_IO_EXP process_handle spawn(const char* path, aw::array_view<char*> argv, std::error_code& ec) noexcept;
/*!
* Spawn a child process with specified argument list \a argv. `argv[0]` is used as path.
*/
AW_IO_EXP process_handle spawn(aw::array_ref<char*> argv, std::error_code& ec) noexcept;
AW_IO_EXP int kill(process_handle pid, int signal, std::error_code& ec) noexcept;
AW_IO_EXP process_handle spawn(aw::array_view<char*> argv, std::error_code& ec) noexcept;

inline process_handle spawn(const char* path, aw::array_ref<char*> argv)
inline process_handle spawn(const char* path, aw::array_view<char*> argv)
{
std::error_code ec;
return spawn(path, argv, ec);
}
inline process_handle spawn(aw::array_ref<char*> argv)
inline process_handle spawn(aw::array_view<char*> argv)
{
std::error_code ec;
return spawn(argv, ec);
}
AW_IO_EXP process_handle spawn(std::string path, aw::array_ref<std::string> argv);
AW_IO_EXP process_handle spawn(std::string path, aw::array_ref<std::string> argv, std::error_code& ec);
AW_IO_EXP process_handle spawn(std::string path, aw::array_ref<std::string> argv)
{
std::error_code ec;
return spawn(path, argv, ec);
}


AW_IO_EXP wait_status wait(process_handle pid, std::error_code& ec) noexcept;
inline wait_status wait(process_handle pid)
{
std::error_code ec;
return wait(pid, ec);
}

AW_IO_EXP int kill(process_handle pid, int signal, std::error_code& ec) noexcept;
inline int kill(process_handle pid, int signal)
{
std::error_code ec;
return kill(pid, signal, ec);
}


inline wait_status run(std::string path, aw::array_ref<std::string> argv, std::error_code& ec)
{
auto handle = spawn(path, argv, ec);
if (handle == invalid_process_handle)
return wait_status::failed;

return wait(handle, ec);
}

inline wait_status run(std::string path, aw::array_ref<std::string> argv)
{
std::error_code ec;
return run(path, argv, ec);
}

inline std::string executable_name(std::string path)
{
return path;
}
} // namespace aw::io::posix
#endif // aw_io_posix_process_h
5 changes: 4 additions & 1 deletion io/include/aw/io/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ using posix::process_handle;
using posix::invalid_process_handle;
using posix::spawn;
using posix::kill;
using posix::wait;
using posix::run;
using posix::executable_name;
#elif (AW_PLATFORM == AW_PLATFORM_WIN32)
using win32::invalid_process_handle;
using win32::process_handle;
using win32::spawn;
using win32::run;
using win32::kill;
using win32::wait;
using win32::run;
using win32::executable_name;
#endif
} // namespace aw::platform
Expand Down
2 changes: 1 addition & 1 deletion io/include/aw/io/win32/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ inline wait_status run(
if (handle == invalid_process_handle)
return wait_status::failed;

return wait(handle, ec);
return wait(handle, ec, timeout);
}

inline wait_status run(std::string path, aw::array_view<std::string> argv, timeout_spec_ms timeout = {})
Expand Down
36 changes: 20 additions & 16 deletions io/posix/process.c++
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
/*
* Copyright (C) 2016-2018 hedede <[email protected]>
*
* License LGPLv3 or later:
* GNU Lesser GPL version 3 <http://gnu.org/licenses/lgpl-3.0.html>
* This is free software: you are free to change and redistribute it.
* There is NO WARRANTY, to the extent permitted by law.
*/
#include <aw/platform/process.h>
#include "aw/io/posix/process.h"

#include <aw/types/string_view.h>
#include <aw/algorithm/in.h>

#include <iostream>
#include <vector>

#include <cassert>
Expand All @@ -26,15 +17,18 @@
extern char** environ;
#endif

namespace aw::platform::posix {
namespace aw::io::posix {
AW_IO_EXP
process_handle spawn(const char* path, aw::array_ref<char*> argv, std::error_code& ec) noexcept
process_handle spawn(const char* path, aw::array_view<char*> argv, std::error_code& ec) noexcept
{
/*! enforce `nullptr` at the end of `argv` */
assert( argv.empty() || argv.back() == nullptr );

pid_t pid;
int rc = posix_spawn(&pid, path, nullptr, nullptr, argv.data(), environ);
int rc = posix_spawnp(&pid, path, nullptr, nullptr, argv.data(), environ);
if (rc == 2)
rc = posix_spawn(&pid, path, nullptr, nullptr, argv.data(), environ);

if (rc == 0)
return process_handle( pid );

Expand All @@ -43,15 +37,14 @@ process_handle spawn(const char* path, aw::array_ref<char*> argv, std::error_cod
}

AW_IO_EXP
process_handle spawn(aw::array_ref<char*> argv, std::error_code& ec) noexcept
process_handle spawn(aw::array_view<char*> argv, std::error_code& ec) noexcept
{
return spawn( argv[0], argv, ec );
}

AW_IO_EXP
process_handle spawn(std::string path, aw::array_ref<std::string> argv)
process_handle spawn(std::string path, aw::array_ref<std::string> argv, std::error_code& ec)
{
std::error_code ec;
std::vector<char*> args;
args.push_back(path.data());
for (std::string& arg : argv)
Expand All @@ -75,4 +68,15 @@ int kill(process_handle pid, int signal, std::error_code& ec) noexcept
ec.assign( errno, std::generic_category() );
return ret;
}

AW_IO_EXP wait_status wait(process_handle pid, std::error_code& ec) noexcept
{
int status = 0;
pid_t ret = waitpid( pid_t(pid), &status, 0);
if (ret < 0) {
ec.assign( errno, std::generic_category() );
return wait_status::failed;
}
return wait_status::finished;
}
} // namespace aw::platform::posix
3 changes: 2 additions & 1 deletion io/tests/process.c++
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Test(process_basic_test) {

auto path = io::executable_name("dump_args"s);

auto result = io::run(path, { "a", "b", "c" });
std::vector<std::string> in_args = { "a", "b", "c" };
auto result = io::run(path, in_args);

TestAssert(result == io::wait_status::finished);

Expand Down

0 comments on commit 4f9221f

Please sign in to comment.