|
|
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:
1.1.1.8 ! root 7: //
! 8: // ホストネットワークのドライバ (基本クラス)
! 9: //
! 10:
1.1 root 11: #include "netdriver.h"
1.1.1.2 root 12: #include "mainapp.h"
1.1.1.6 root 13: #include <sys/wait.h>
1.1 root 14:
15: // コンストラクタ
1.1.1.8 ! root 16: NetDriver::NetDriver(HostDevice *hostdev_, const char *drivername_)
! 17: : inherited(hostdev_, drivername_)
1.1 root 18: {
19: }
20:
21: // デストラクタ
22: NetDriver::~NetDriver()
23: {
24: }
25:
26: // ifup スクリプト実行
27: bool
28: NetDriver::Run_ifup()
29: {
30: return RunScript("nono-ifup");
31: }
32:
33: // ifdown スクリプト実行
34: bool
35: NetDriver::Run_ifdown()
36: {
37: return RunScript("nono-ifdown");
38: }
39:
40: // スクリプトを実行
41: bool
42: NetDriver::RunScript(const char *filename)
43: {
1.1.1.6 root 44: std::string cmd;
45: int r;
46:
1.1 root 47: // ファイルが存在するか
1.1.1.2 root 48: std::string path = gMainApp.SearchFile(filename);
1.1 root 49: if (path.empty()) {
1.1.1.6 root 50: errmsg = string_format("%s not found", filename);
51: goto abort;
1.1 root 52: }
53:
54: // コマンドラインを作成
1.1.1.6 root 55: cmd = path + " " + ifname;
56: putmsg(1, "%s: command: %s", __func__, cmd.c_str());
1.1 root 57:
58: // 実行
1.1.1.6 root 59: r = system(cmd.c_str());
60: if (r == -1) {
61: errmsg = string_format("system(\"%s\"): %s",
62: cmd.c_str(), strerror(errno));
63: goto abort;
64: }
65:
66: if (WIFEXITED(r)) {
67: int exitcode = WEXITSTATUS(r);
68: if (exitcode == 0) {
69: // ここが正常終了
70: putmsg(1, "%s: \"%s\" exited successfully", __func__, cmd.c_str());
71: return true;
72: }
73: if (exitcode == 127) {
74: errmsg = string_format("\"%s\": shell execution failed",
75: cmd.c_str());
76: } else {
77: errmsg = string_format("\"%s\" exited with code %d",
78: cmd.c_str(), exitcode);
79: }
80: } else if (WIFSIGNALED(r)) {
81: int signo = WTERMSIG(r);
82: errmsg = string_format("\"%s\" terminated with signal %d",
83: cmd.c_str(), signo);
84: } else {
85: // どうしたらいいかよく分からんのでとりあえず表示だけ。
86: errmsg = string_format("\"%s\" terminated:"
87: "IFSTOPPED=%d STOPSIG=%d IFCONTINUED=%d COREDUMP=%d",
88: cmd.c_str(),
89: WIFSTOPPED(r), WSTOPSIG(r), WIFCONTINUED(r), WCOREDUMP(r));
90: }
91:
92: abort:
93: putmsg(1, "%s: %s", __func__, errmsg.c_str());
94: return false;
95: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.