Make only some commands able to start the daemon

This commit is contained in:
elkowar 2021-08-14 14:44:22 +02:00
parent e72a16a5bd
commit b8d222384b
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
5 changed files with 37 additions and 44 deletions

View file

@ -268,7 +268,7 @@ impl App {
self.eww_config = config;
self.eww_state.clear_all_window_states();
let window_names: Vec<String> = self.open_windows.keys().chain(self.failed_windows.iter()).cloned().collect();
let window_names: Vec<String> = self.open_windows.keys().cloned().chain(self.failed_windows.iter().cloned()).dedup().collect();
for window_name in &window_names {
self.open_window(&window_name, None, None, None, None)?;
}
@ -386,7 +386,7 @@ fn respond_with_result<T>(sender: DaemonResponseSender, result: Result<T>) -> Re
Ok(_) => sender.send_success(String::new()),
Err(e) => {
let formatted = error_handling_ctx::format_error(&e);
println!("{}", formatted);
println!("Action failed with error: {}", formatted);
sender.send_failure(formatted)
},
}

View file

@ -27,6 +27,7 @@ pub mod app;
pub mod application_lifecycle;
pub mod client;
pub mod config;
mod daemon_response;
pub mod display_backend;
pub mod error;
mod error_handling_ctx;
@ -38,7 +39,6 @@ pub mod script_var_handler;
pub mod server;
pub mod util;
pub mod widgets;
mod daemon_response;
fn main() {
let eww_binary_name = std::env::args().next().unwrap();
@ -63,12 +63,9 @@ fn main() {
client::handle_client_only_action(&paths, action)?;
false
}
opts::Action::WithServer(ActionWithServer::KillServer) => {
handle_server_command(&paths, &ActionWithServer::KillServer, 1)?;
false
}
// a running daemon is necessary for this command
opts::Action::WithServer(action) => {
opts::Action::WithServer(action) if action.can_start_daemon() => {
// attempt to just send the command to a running daemon
if let Err(err) = handle_server_command(&paths, &action, 5) {
// connecting to the daemon failed. Thus, start the daemon here!
@ -81,6 +78,7 @@ fn main() {
let (command, response_recv) = action.into_daemon_command();
// start the daemon and give it the command
let fork_result = server::initialize_server(paths.clone(), Some(command))?;
let is_parent = fork_result == ForkResult::Parent;
if let (Some(recv), true) = (response_recv, is_parent) {
@ -91,6 +89,15 @@ fn main() {
true
}
}
opts::Action::WithServer(ActionWithServer::KillServer) => {
handle_server_command(&paths, &ActionWithServer::KillServer, 1)?;
false
}
opts::Action::WithServer(action) => {
handle_server_command(&paths, &action, 5)?;
true
}
// make sure that there isn't already a Eww daemon running.
opts::Action::Daemon if check_server_running(paths.get_ipc_socket_file()) => {

View file

@ -161,6 +161,13 @@ fn parse_var_update_arg(s: &str) -> Result<(VarName, DynVal)> {
}
impl ActionWithServer {
pub fn can_start_daemon(&self) -> bool {
match self {
ActionWithServer::OpenWindow { .. } | ActionWithServer::OpenMany { .. } => true,
_ => false,
}
}
pub fn into_daemon_command(self) -> (app::DaemonCommand, Option<daemon_response::DaemonResponseReceiver>) {
let command = match self {
ActionWithServer::Update { mappings } => app::DaemonCommand::UpdateVars(mappings),

View file

@ -1,7 +1,17 @@
use crate::{EwwPaths, app::{self, DaemonCommand}, config, daemon_response, error_handling_ctx, eww_state::*, ipc_server, script_var_handler, util};
use crate::{
app::{self, DaemonCommand},
config, daemon_response, error_handling_ctx,
eww_state::*,
ipc_server, script_var_handler, util, EwwPaths,
};
use anyhow::*;
use std::{collections::{HashMap, HashSet}, os::unix::io::AsRawFd, path::Path, sync::{atomic::Ordering, Arc}};
use std::{
collections::{HashMap, HashSet},
os::unix::io::AsRawFd,
path::Path,
sync::{atomic::Ordering, Arc},
};
use tokio::sync::mpsc::*;
pub fn initialize_server(paths: EwwPaths, action: Option<DaemonCommand>) -> Result<ForkResult> {
@ -183,11 +193,11 @@ fn do_detach(log_file_path: impl AsRef<Path>) -> Result<ForkResult> {
match unsafe { nix::unistd::fork()? } {
nix::unistd::ForkResult::Child => {
nix::unistd::setsid()?;
match unsafe {nix::unistd::fork()?
}{
match unsafe { nix::unistd::fork()? } {
nix::unistd::ForkResult::Parent { .. } => std::process::exit(0),
nix::unistd::ForkResult::Child => {}
}}
}
}
nix::unistd::ForkResult::Parent { .. } => {
return Ok(ForkResult::Parent);
}

View file

@ -1,31 +0,0 @@
// use eww_config::{
// format_diagnostic::ToDiagnostic,
// parser::{ast::*, from_ast::FromAst},
//};
fn main() {
println!("hi");
// let mut files = codespan_reporting::files::SimpleFiles::new();
// let input = r#"
//(heyho ; :foo { "foo \" } bar " }
//; :baz {(foo == bar ? 12.2 : 12)}
//(foo)
//(defwidget foo [something bla] "foo")
//(baz))"#;
// let file_id = files.add("foo.eww", input);
// let ast = eww_config::parser::parse_string(file_id, input);
// match ast.and_then(eww_config::parser::from_ast::Element::<Ast, Ast>::from_ast) {
// Ok(ast) => {
// println!("{:?}", ast);
//}
// Err(err) => {
// dbg!(&err);
// let diag = err.to_diagnostic();
// use codespan_reporting::term;
// let config = term::Config::default();
// let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
// term::emit(&mut writer, &config, &files, &diag).unwrap();
//}
}