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

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: 
1.1.1.4 ! root       23:        monitor_size = nnSize(30, 7);
1.1       root       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: // モニタ
1.1.1.4 ! root       38: void
        !            39: NetDriver::MonitorUpdate(TextScreen& monitor)
1.1       root       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: }
                     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: {
1.1.1.3   root       67:        PTHREAD_SETNAME("netdriver");
1.1       root       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:        // ファイルが存在するか
1.1.1.2   root       93:        std::string path = gMainApp.SearchFile(filename);
1.1       root       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.