Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <system_error>
- #include <cerrno>
- #include <boost/filesystem.hpp>
- #include <boost/optional.hpp>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- namespace fs = boost::filesystem;
- enum class SearchInPath { No, Yes };
- void launch(SearchInPath search_in_path,
- const fs::path& path,
- const std::vector<std::string>& args,
- const boost::optional<fs::path>& log = boost::none) {
- std::vector<char*> exec_args;
- for (const std::string& arg : args) {
- exec_args.push_back(const_cast<char*>(arg.c_str()));
- }
- exec_args.push_back(nullptr);
- pid_t pid = fork();
- if (pid == -1) {
- throw std::system_error(errno, std::system_category());
- } else if (pid == 0) {
- int rc;
- if (log) {
- int fd = open(log.get().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
- if (fd != -1) {
- close(STDERR_FILENO);
- dup2(fd, STDERR_FILENO);
- }
- }
- if (search_in_path == SearchInPath::Yes) {
- rc = execvp(path.c_str(), exec_args.data());
- } else {
- rc = execv(path.c_str(), exec_args.data());
- }
- } else {
- int status;
- int rc = waitpid(pid, &status, 0);
- if (rc == -1) {
- throw std::system_error(errno, std::system_category());
- }
- if (status != 0) {
- throw std::runtime_error("Process existed with return code: " + std::to_string(status));
- }
- }
- }
- int main(int argc, char **argv) {
- fs::path path;
- fs::path daemon;
- fs::path pakpath;
- fs::path log;
- try {
- path = fs::read_symlink("/proc/self/exe").parent_path();
- daemon = path / "linux64" / "daemon";
- pakpath = path / "pkg";
- log = path / "Unvanquished.log";
- launch(SearchInPath::No, daemon.c_str(), {"daemon", "-pakpath", pakpath.string()}, log);
- } catch (const std::exception& e) {
- std::ostringstream msg;
- msg << "Unvanquished has failed to launch.\n";
- msg << "Visit <a href=\"http://forums.unvanquished.net/\">Unvanquished forums</a> for support.\n\n";
- msg << "Please include the following technical information in your post:\n\n";
- msg << "Error message: " << e.what() << ".";
- if (!path.empty()) {
- msg << "\n";
- msg << "Base path: " << path << ".\n";
- msg << "Daemon executable exists: " << (fs::exists(daemon) ? "yes" : "no") << ".\n";
- msg << "Package directory exists: " << (fs::exists(pakpath) ? "yes" : "no") << ".";
- }
- if (fs::exists(log)) {
- msg << "\n\n";
- msg << "Please also incude the contents of the log file.\n\n";
- msg << "Log file location: " << log.string();
- }
- try {
- launch(SearchInPath::Yes, "zenity", {"zenity", "--error", "--text", msg.str()});
- } catch (...) {}
- std::cout << msg.str() << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement