--- nono/host/netdriver.cpp 2026/04/29 17:04:28 1.1.1.1 +++ nono/host/netdriver.cpp 2026/04/29 17:05:11 1.1.1.8 @@ -1,31 +1,21 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" -#include "netdriver.h" -#include "configfile.h" -#include "mystring.h" - -static void *netdriver_run(void *); +// +// ホストネットワークのドライバ (基本クラス) +// -NetDriver *gNetDriver; +#include "netdriver.h" +#include "mainapp.h" +#include // コンストラクタ -NetDriver::NetDriver(const char *name, EthernetDevice *p, - std::mutex *m, std::condition_variable *c) +NetDriver::NetDriver(HostDevice *hostdev_, const char *drivername_) + : inherited(hostdev_, drivername_) { - logname = "netdriver"; - devname = "NetDriver"; - - monitor.Init(30, 7); - - fd = -1; - drivername = name; - parent = p; - mtx = m; - cv = c; } // デストラクタ @@ -33,44 +23,6 @@ NetDriver::~NetDriver() { } -// モニタ -bool -NetDriver::MonitorUpdate() -{ - 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); - return true; -} - -// スレッドが必要な継承クラスが呼ぶ -bool -NetDriver::InitRecvThread() -{ - // 受信スレッド起動 - pthread_t th; - pthread_create(&th, NULL, netdriver_run, NULL); - - return true; -} - -// 受信スレッドのエントリポイント -void * -netdriver_run(void *dummy) -{ - pthread_setname_np("netdriver"); - pthread_detach(pthread_self()); - - gNetDriver->ThreadRun(); - return NULL; -} - // ifup スクリプト実行 bool NetDriver::Run_ifup() @@ -89,29 +41,55 @@ NetDriver::Run_ifdown() bool NetDriver::RunScript(const char *filename) { + std::string cmd; + int r; + // ファイルが存在するか - std::string path = gConfig->SearchFile(filename); + 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; }