diff options
author | login <login@tilde.team> | 2022-05-18 17:21:32 +0000 |
---|---|---|
committer | login <login@tilde.team> | 2022-05-18 17:21:32 +0000 |
commit | e50751da3901c32aaef1f940ce628fc93e36d95e (patch) | |
tree | 72935eda3d26e69ec1728c53463c44d869728d89 | |
parent | cad83e8167ce6dd68f2ac47750de5248b568177e (diff) | |
download | tcoin-e50751da3901c32aaef1f940ce628fc93e36d95e.tar.gz |
Try-catch blocks in those cases where fork() fails
I've tried to add try-catch blocks in those cases where fork()s fail. It doesn't quite work well though.
-rwxr-xr-x | ntcoin | 4 | ||||
-rwxr-xr-x | ntcoin_freebsd_tildeguru | 4 | ||||
-rwxr-xr-x | ntcoin_openbsd_tildeinstitute | 4 | ||||
-rw-r--r-- | pcoin.cpp | 39 | ||||
-rw-r--r-- | tcoin.cpp | 51 |
5 files changed, 79 insertions, 23 deletions
diff --git a/ntcoin b/ntcoin index 7d91bfd..b781358 100755 --- a/ntcoin +++ b/ntcoin @@ -259,5 +259,9 @@ else /bin/ln -s "`/usr/bin/realpath -s $1`/tcoin/bin/compile_pcoin" ~/"bin/compile_pcoin" /bin/ln -s "`/usr/bin/realpath -s $1`/tcoin/bin/edit_pcoin" ~/"bin/edit_pcoin" fi + /bin/echo '#!/bin/bash +'`which nano`' "'`/bin/pwd`'/pcoin.cpp" "'`/bin/pwd`'/tcoin.cpp"' > "`/usr/bin/realpath -s $1`/tcoin/bin/edit_ptcoin" + /bin/chmod 540 "`/usr/bin/realpath -s $1`/tcoin/bin/edit_ptcoin" + /bin/ln -s "`/usr/bin/realpath -s $1`/tcoin/bin/edit_ptcoin" ~/"bin/edit_ptcoin" fi fi diff --git a/ntcoin_freebsd_tildeguru b/ntcoin_freebsd_tildeguru index 61e680b..be4aa1d 100755 --- a/ntcoin_freebsd_tildeguru +++ b/ntcoin_freebsd_tildeguru @@ -264,5 +264,9 @@ else /bin/ln -s "`/bin/realpath -q $1`/tcoin/bin/compile_pcoin" ~/"bin/compile_pcoin" /bin/ln -s "`/bin/realpath -q $1`/tcoin/bin/edit_pcoin" ~/"bin/edit_pcoin" fi + /bin/echo '#!/usr/local/bin/bash +'`which nano`' "'`/bin/pwd`'/pcoin.cpp" "'`/bin/pwd`'/tcoin.cpp"' > "`/bin/realpath -q $1`/tcoin/bin/edit_ptcoin" + /bin/chmod 540 "`/bin/realpath -q $1`/tcoin/bin/edit_ptcoin" + /bin/ln -s "`/bin/realpath -q $1`/tcoin/bin/edit_ptcoin" ~/"bin/edit_ptcoin" fi fi diff --git a/ntcoin_openbsd_tildeinstitute b/ntcoin_openbsd_tildeinstitute index 24dd89b..6488059 100755 --- a/ntcoin_openbsd_tildeinstitute +++ b/ntcoin_openbsd_tildeinstitute @@ -262,5 +262,9 @@ else /bin/ln -s "`/usr/local/bin/grealpath -s $1`/tcoin/bin/compile_pcoin" ~/"bin/compile_pcoin" /bin/ln -s "`/usr/local/bin/grealpath -s $1`/tcoin/bin/edit_pcoin" ~/"bin/edit_pcoin" fi + /bin/echo '#!/usr/local/bin/bash +'`which nano`' "'`/bin/pwd`'/pcoin.cpp" "'`/bin/pwd`'/tcoin.cpp"' > "`/usr/local/bin/grealpath -s $1`/tcoin/bin/edit_ptcoin" + /bin/chmod 540 "`/usr/local/bin/grealpath -s $1`/tcoin/bin/edit_ptcoin" + /bin/ln -s "`/usr/local/bin/grealpath -s $1`/tcoin/bin/edit_ptcoin" ~/"bin/edit_ptcoin" fi fi diff --git a/pcoin.cpp b/pcoin.cpp index 2c644ad..fa28af6 100644 --- a/pcoin.cpp +++ b/pcoin.cpp @@ -221,19 +221,43 @@ int strctcmp(const char*a, const char*b) } std::string exec(const char* cmd) { + int i=0; + do + { std::array<char, 128> buffer; std::string result; - std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); - if (!pipe) throw std::runtime_error("popen() failed!"); - while (!feof(pipe.get())) { - if (fgets(buffer.data(), 128, pipe.get()) != nullptr) - result += buffer.data(); + try + { + std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); + if (!pipe) + { + ++i; + std::cout << "popen() failed - " << i << std::endl; + continue; + } + + while (!feof(pipe.get())) { + if (fgets(buffer.data(), 128, pipe.get()) != nullptr) + result += buffer.data(); + } + return result; } - return result; + catch (const std::exception& e) + { + ++i; + std::cout << "popen() failed - " << i << " (exception " << e.what() << ")" << std::endl; + continue; + } + } while(i < 100); + + throw std::runtime_error("popen() failed!"); + return std::string(""); //dummy line, never executes } std::string exec2(const char* cmd, std::string input) { - std::string data_length_str = exec((std::string(cmd) + std::string(PIPED_WORD_COUNT_CMD)).c_str()); + std::string data_length_cmd_str = std::string(cmd) + std::string(PIPED_WORD_COUNT_CMD); + const char* data_length_cmd_cstr = data_length_cmd_str.c_str(); + std::string data_length_str = exec(data_length_cmd_cstr); long long int data_length = strtol_fast(data_length_str.c_str())+1; std::vector <char> buffer; buffer.reserve(data_length); @@ -248,6 +272,7 @@ std::string exec2(const char* cmd, std::string input) { if (fgets(buffer.data(), data_length, fp->out) != nullptr) result += buffer.data(); } + pclose2(fp); return result; } diff --git a/tcoin.cpp b/tcoin.cpp index f8fe665..461f443 100644 --- a/tcoin.cpp +++ b/tcoin.cpp @@ -209,31 +209,50 @@ int strctcmp(const char*a, const char*b) } std::string exec(const char* cmd) { - std::array<char, 128> buffer; - std::string result; - std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); - if (!pipe) throw std::runtime_error("popen() failed!"); - while (!feof(pipe.get())) { - if (fgets(buffer.data(), 128, pipe.get()) != nullptr) - result += buffer.data(); - } - return result; + int i=0; + do + { + std::array<char, 128> buffer; + std::string result; + try + { + std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); + if (!pipe) + { + ++i; + continue; + } + + while (!feof(pipe.get())) { + if (fgets(buffer.data(), 128, pipe.get()) != nullptr) + result += buffer.data(); + } + return result; + } + catch (const std::exception& e) + { + ++i; + std::cout << "popen() failed - " << i << " (exception " << e.what() << ")" << std::endl; + continue; + } + } while(i < 100); + + throw std::runtime_error("popen() failed!"); + return std::string(""); //dummy line, never executes } std::string exec2(const char* cmd, std::string input) { - std::string data_length_str = exec((std::string(cmd) + std::string(PIPED_WORD_COUNT_CMD)).c_str()); + std::string data_length_cmd_str = std::string(cmd) + std::string(PIPED_WORD_COUNT_CMD); + const char* data_length_cmd_cstr = data_length_cmd_str.c_str(); + std::string data_length_str = exec(data_length_cmd_cstr); long long int data_length = strtol_fast(data_length_str.c_str())+1; std::vector <char> buffer; buffer.reserve(data_length); std::string result; files_t *fp = popen2(cmd); - if (!fp) - { - pclose2(fp); - throw std::runtime_error("popen2() failed!"); - } + if (!fp) throw std::runtime_error("popen2() failed!"); - fputs((input + std::string("\n")).c_str(), fp->in); + fputs((input+std::string("\n")).c_str(), fp->in); std::fflush(fp->in); while (!feof(fp->out)) { |