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

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

unix.superglobalmegacorp.com

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