Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
server: load services & add a command to list them
cmd/beterrabad/daemon.ha | 46 ++++++++++++++++++++++++++++++++++++++++ cmd/beterrabad/executor.ha | 12 ++++++++++ cmd/beterrabad/main.ha | 2 cmd/beterrabad/socket.ha | 9 ++++++-
diff --git a/cmd/beterrabad/daemon.ha b/cmd/beterrabad/daemon.ha new file mode 100644 index 0000000000000000000000000000000000000000..ec62df82883162c14d088c3ac0935b1147f43a15 --- /dev/null +++ b/cmd/beterrabad/daemon.ha @@ -0,0 +1,46 @@ +use log; +use fmt; +use fs; +use os; +use path; +use strings; +use os::exec; +use beterraba; + +fn setup(serv: *server) void = { + // XXX: Load configuration as well? + load(serv); +}; + +// TODO: Load according to the configured place +fn load(serv: *server) void = { + const servdir = "/home/eletrotupi/.config/beterraba"; + let servnames: []str = []; + defer free(servnames); + + const it = os::iter(servdir)!; + defer os::finish(it); + + for (true) { + match (fs::next(it)) { + case let ent: fs::dirent => + if (ent.name == "." || ent.name == "..") { + continue; + }; + + if (strings::hassuffix(ent.name, ".service")) { + append(servnames, strings::dup(ent.name)); + }; + + case void => + break; + }; + }; + + for (let i = 0z; i < len(servnames); i += 1) { + const servpath = path::join(servdir, servnames[i]); + const servfile = os::open(servpath)!; + + append(serv.services, beterraba::parse(servfile)); + }; +}; diff --git a/cmd/beterrabad/executor.ha b/cmd/beterrabad/executor.ha index ecad7703296c18d1f53dd3e3b682bb99f0a49f29..a3360c40722028eb6f4907fba5bfb30cada85b59 100644 --- a/cmd/beterrabad/executor.ha +++ b/cmd/beterrabad/executor.ha @@ -2,6 +2,7 @@ use log; use fmt; use os; use os::exec; +use strings; use beterraba; type booterror = !(exec::error | exec::nocmd); @@ -29,6 +30,17 @@ return bootstrerror(err); case void => return "Started successfully"; }; +}; + +fn list_services(cmdargs: str, serv: *server) str = { + let names: []str = []; + defer free(names); + + for (let i = 0z; i < len(serv.services); i += 1) { + append(names, serv.services[i].name); + }; + + return strings::join("|", names...); }; // Check if the process is still alive and bail out if it's ok diff --git a/cmd/beterrabad/main.ha b/cmd/beterrabad/main.ha index dc8c90d65a1e83710adad744661e23a2fd64ad62..d3f17184392bb4b5bf2588ddb7c6b05850b9fd87 100644 --- a/cmd/beterrabad/main.ha +++ b/cmd/beterrabad/main.ha @@ -19,9 +19,9 @@ const serv = bind(sigfd); defer shutdown(&serv); - //const flags = rt::fcntl(sock.sock, rt::F_GETFL, 0)!; //rt::fcntl(sock.sock, rt::F_SETFL, flags | rt::O_CLOEXEC)!; + setup(&serv); log::println("beterrabad running"); for (dispatch(&serv)) void; diff --git a/cmd/beterrabad/socket.ha b/cmd/beterrabad/socket.ha index 8b72c28ae9cb3d5172e56eca6321b38f8eae98cf..ce581a2799499a08cd24ef379bf5597bbe19c12d 100644 --- a/cmd/beterrabad/socket.ha +++ b/cmd/beterrabad/socket.ha @@ -1,3 +1,4 @@ +use beterraba; use bufio; use dirs; use encoding::utf8; @@ -22,6 +23,7 @@ sock: net::socket, signalfd: io::file, pollfd: []poll::pollfd, clients: []client, + services: []beterraba::service, disconnected: bool }; @@ -172,7 +174,7 @@ case utf8::invalid => log::fatal("invalid utf-8"); }; - match (exec(line, client)) { + match (exec(line, s, client)) { case void => void; case servererror => @@ -180,7 +182,7 @@ log::fatal("Fudeu"); }; }; -fn exec(line: str, client: *client) (servererror | void) = { +fn exec(line: str, server: *server, client: *client) (servererror | void) = { let buf = bufio::dynamic(io::mode::WRITE); let sline = strings::cut(line, " "); let cmd = sline.0; @@ -190,6 +192,9 @@ // TODO: Flesh out command execution here switch (cmd) { case "start" => let status = start_service(args); + fmt::fprintf(&buf, status)?; + case "list" => + let status = list_services(args, server); fmt::fprintf(&buf, status)?; case "ping" => fmt::fprintf(&buf, "pong")?;