Annotation of nono/host/netdriver.cpp, revision 1.1

1.1     ! root        1: //
        !             2: // nono
        !             3: // Copyright (C) 2019 [email protected]
        !             4: //
        !             5: 
        !             6: #include "header.h"
        !             7: #include "netdriver.h"
        !             8: #include "configfile.h"
        !             9: #include "mystring.h"
        !            10: 
        !            11: static void *netdriver_run(void *);
        !            12: 
        !            13: NetDriver *gNetDriver;
        !            14: 
        !            15: // コンストラクタ
        !            16: NetDriver::NetDriver(const char *name, EthernetDevice *p,
        !            17:        std::mutex *m, std::condition_variable *c)
        !            18: {
        !            19:        logname = "netdriver";
        !            20:        devname = "NetDriver";
        !            21: 
        !            22:        monitor.Init(30, 7);
        !            23: 
        !            24:        fd = -1;
        !            25:        drivername = name;
        !            26:        parent = p;
        !            27:        mtx = m;
        !            28:        cv = c;
        !            29: }
        !            30: 
        !            31: // デストラクタ
        !            32: NetDriver::~NetDriver()
        !            33: {
        !            34: }
        !            35: 
        !            36: // モニタ
        !            37: bool
        !            38: NetDriver::MonitorUpdate()
        !            39: {
        !            40:        monitor.Clear();
        !            41: 
        !            42:        monitor.Print(0, 0, "NetDriver: %s", drivername);
        !            43:        monitor.Print(0, 1, "Device   : %s", devpath.c_str());
        !            44:        monitor.Print(0, 2, "Interface: %s", ifname.c_str());
        !            45: 
        !            46:        monitor.Print(5, 4, "%9s %9s", "Packets", "Bytes");
        !            47:        monitor.Print(0, 5, "Send %9" PRIu64 " %9" PRIu64, tx_pkts, tx_bytes);
        !            48:        monitor.Print(0, 6, "Recv %9" PRIu64 " %9" PRIu64, rx_pkts, rx_bytes);
        !            49:        return true;
        !            50: }
        !            51: 
        !            52: // スレッドが必要な継承クラスが呼ぶ
        !            53: bool
        !            54: NetDriver::InitRecvThread()
        !            55: {
        !            56:        // 受信スレッド起動
        !            57:        pthread_t th;
        !            58:        pthread_create(&th, NULL, netdriver_run, NULL);
        !            59: 
        !            60:        return true;
        !            61: }
        !            62: 
        !            63: // 受信スレッドのエントリポイント
        !            64: void *
        !            65: netdriver_run(void *dummy)
        !            66: {
        !            67:        pthread_setname_np("netdriver");
        !            68:        pthread_detach(pthread_self());
        !            69: 
        !            70:        gNetDriver->ThreadRun();
        !            71:        return NULL;
        !            72: }
        !            73: 
        !            74: // ifup スクリプト実行
        !            75: bool
        !            76: NetDriver::Run_ifup()
        !            77: {
        !            78:        return RunScript("nono-ifup");
        !            79: }
        !            80: 
        !            81: // ifdown スクリプト実行
        !            82: bool
        !            83: NetDriver::Run_ifdown()
        !            84: {
        !            85:        return RunScript("nono-ifdown");
        !            86: }
        !            87: 
        !            88: // スクリプトを実行
        !            89: bool
        !            90: NetDriver::RunScript(const char *filename)
        !            91: {
        !            92:        // ファイルが存在するか
        !            93:        std::string path = gConfig->SearchFile(filename);
        !            94:        if (path.empty()) {
        !            95:                warnx("%s not found", filename);
        !            96:                return false;
        !            97:        }
        !            98: 
        !            99:        // コマンドラインを作成
        !           100:        std::string cmd = path + " " + ifname;
        !           101: 
        !           102:        // 実行
        !           103:        int rv = system(cmd.c_str());
        !           104:        if (rv == -1) {
        !           105:                warn("%s: system() failed", path.c_str());
        !           106:                return false;
        !           107:        }
        !           108:        if (rv == 127) {
        !           109:                warnx("%s: system(): shell failed", path.c_str());
        !           110:                return false;
        !           111:        }
        !           112:        if (rv != 0) {
        !           113:                warnx("%s failed with %d", path.c_str(), rv);
        !           114:                return false;
        !           115:        }
        !           116:        return true;
        !           117: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.