--- nono/host/netdriver.cpp 2026/04/29 17:04:48 1.1.1.5 +++ nono/host/netdriver.cpp 2026/04/29 17:05:11 1.1.1.8 @@ -4,28 +4,18 @@ // Licensed under nono-license.txt // +// +// ホストネットワークのドライバ (基本クラス) +// + #include "netdriver.h" #include "mainapp.h" -#include "mythread.h" - -static void *netdriver_run(void *); - -std::unique_ptr gNetDriver; +#include // コンストラクタ -NetDriver::NetDriver(const char *drivername_, EthernetDevice *parent_, - std::mutex *mtx_, std::condition_variable *cv_) +NetDriver::NetDriver(HostDevice *hostdev_, const char *drivername_) + : inherited(hostdev_, drivername_) { - logname = "netdriver"; - devname = "NetDriver"; - - monitor_size = nnSize(30, 7); - - fd = -1; - drivername = drivername_; - parent = parent_; - mtx = mtx_; - cv = cv_; } // デストラクタ @@ -33,43 +23,6 @@ NetDriver::~NetDriver() { } -// モニタ -void -NetDriver::MonitorUpdate(TextScreen& monitor) -{ - monitor.Clear(); - - monitor.Print(0, 0, "NetDriver: %s", drivername); - monitor.Print(0, 1, "Device : %s", devpath.c_str()); - monitor.Print(0, 2, "Interface: %s", ifname.c_str()); - - monitor.Print(5, 4, "%9s %9s", "Packets", "Bytes"); - monitor.Print(0, 5, "Send %9" PRIu64 " %9" PRIu64, tx_pkts, tx_bytes); - monitor.Print(0, 6, "Recv %9" PRIu64 " %9" PRIu64, rx_pkts, rx_bytes); -} - -// スレッドが必要な継承クラスが呼ぶ -bool -NetDriver::InitRecvThread() -{ - // 受信スレッド起動 - pthread_t th; - pthread_create(&th, NULL, netdriver_run, NULL); - - return true; -} - -// 受信スレッドのエントリポイント -void * -netdriver_run(void *dummy) -{ - PTHREAD_SETNAME("netdriver"); - pthread_detach(pthread_self()); - - gNetDriver->ThreadRun(); - return NULL; -} - // ifup スクリプト実行 bool NetDriver::Run_ifup() @@ -88,29 +41,55 @@ NetDriver::Run_ifdown() bool NetDriver::RunScript(const char *filename) { + std::string cmd; + int r; + // ファイルが存在するか std::string path = gMainApp.SearchFile(filename); if (path.empty()) { - warnx("%s not found", filename); - return false; + errmsg = string_format("%s not found", filename); + goto abort; } // コマンドラインを作成 - std::string cmd = path + " " + ifname; + cmd = path + " " + ifname; + putmsg(1, "%s: command: %s", __func__, cmd.c_str()); // 実行 - int rv = system(cmd.c_str()); - if (rv == -1) { - warn("%s: system() failed", path.c_str()); - return false; + r = system(cmd.c_str()); + if (r == -1) { + errmsg = string_format("system(\"%s\"): %s", + cmd.c_str(), strerror(errno)); + goto abort; } - if (rv == 127) { - warnx("%s: system(): shell failed", path.c_str()); - return false; - } - if (rv != 0) { - warnx("%s failed with %d", path.c_str(), rv); - return false; + + if (WIFEXITED(r)) { + int exitcode = WEXITSTATUS(r); + if (exitcode == 0) { + // ここが正常終了 + putmsg(1, "%s: \"%s\" exited successfully", __func__, cmd.c_str()); + return true; + } + if (exitcode == 127) { + errmsg = string_format("\"%s\": shell execution failed", + cmd.c_str()); + } else { + errmsg = string_format("\"%s\" exited with code %d", + cmd.c_str(), exitcode); + } + } else if (WIFSIGNALED(r)) { + int signo = WTERMSIG(r); + errmsg = string_format("\"%s\" terminated with signal %d", + cmd.c_str(), signo); + } else { + // どうしたらいいかよく分からんのでとりあえず表示だけ。 + errmsg = string_format("\"%s\" terminated:" + "IFSTOPPED=%d STOPSIG=%d IFCONTINUED=%d COREDUMP=%d", + cmd.c_str(), + WIFSTOPPED(r), WSTOPSIG(r), WIFCONTINUED(r), WCOREDUMP(r)); } - return true; + + abort: + putmsg(1, "%s: %s", __func__, errmsg.c_str()); + return false; }