Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility for spawning subprocesses #98

Merged
merged 5 commits into from
Jul 17, 2023
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
165 changes: 165 additions & 0 deletions include/gz/utils/Subprocess.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef GZ_UTILS__SUBPROCESS_HH_
#define GZ_UTILS__SUBPROCESS_HH_

#include "detail/subprocess.h"
#include <string>
#include <vector>

#include <gz/utils/detail/subprocess.h>

// This header contains a wrapper to the cross-platform subprocess library.
// It can be used to spawn processes to interact with, primarily for testing.

namespace gz
{
namespace utils
{

class Subprocess
{
public: Subprocess(const std::vector<std::string> &_commandLine,
const std::vector<std::string> &_environment = {}):
commandLine(_commandLine),
environment(_environment)
{
this->Create();
}

private: void Create()
{
if (this->process)
return;

Check warning on line 48 in include/gz/utils/Subprocess.hh

View check run for this annotation

Codecov / codecov/patch

include/gz/utils/Subprocess.hh#L48

Added line #L48 was not covered by tests

this->process = new subprocess_s;

std::vector<const char*> commandLineCstr;
for (const auto &val : this->commandLine)
{
commandLineCstr.push_back(val.c_str());
}
commandLineCstr.push_back(nullptr);

std::vector<const char*> environmentCstr;
for (const auto &val : this->environment)
{
environmentCstr.push_back(val.c_str());
}
environmentCstr.push_back(nullptr);

int ret = -1;
if (this->environment.size())
{
ret = subprocess_create_ex(commandLineCstr.data(),
0, environmentCstr.data(), this->process);
}
else
{
ret = subprocess_create(commandLineCstr.data(), 0, this->process);
}

if (0 != ret)
{
std::cerr << "failed to create subprocess" << std::endl;
delete this->process;
this->process = nullptr;
}
}

public: ~Subprocess()
{
if (this->process)
subprocess_destroy(this->process);
delete this->process;
}

public: std::string Stdout()
{
std::string result{""};
if (this->process)
{
auto p_stdout = subprocess_stdout(this->process);
char buffer[128];
while (!feof(p_stdout))
{
if (fgets(buffer, 128, p_stdout) != NULL)
result += buffer;
}
}
return result;
}

public: std::string Stderr()
{
std::string result{""};
if (this->process)
{
auto p_stdout = subprocess_stderr(this->process);
char buffer[128];
while (!feof(p_stdout))
{
if (fgets(buffer, 128, p_stdout) != NULL)
result += buffer;
}
}
return result;

mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
}

public: bool Alive()
{
if (this->process)
return subprocess_alive(this->process);
else
return false;
}

public: bool Terminate()
{
if (this->process)
return subprocess_terminate(this->process) != 0;
else
return false;
}

public: int Join()
{
int return_code = -1;
if (this->process)
{
auto ret = subprocess_join(this->process, &return_code);
if (ret != 0)
{
std::cerr << "Failed to join subprocess" << std::endl;

Check warning on line 149 in include/gz/utils/Subprocess.hh

View check run for this annotation

Codecov / codecov/patch

include/gz/utils/Subprocess.hh#L149

Added line #L149 was not covered by tests
}
}

return return_code;
}

protected: std::vector<std::string> commandLine;

protected: std::vector<std::string> environment;

protected: subprocess_s * process {nullptr};
};
} // namespace utils
} // namespace gz

#endif // GZ_UTILS__SUBPROCESS_HH_
Loading