Skip to content

Commit

Permalink
Merge pull request #143 from mahkoh/jorth/config-run-privileged
Browse files Browse the repository at this point in the history
config: allow running commands privileged
  • Loading branch information
mahkoh authored Apr 1, 2024
2 parents 4558bdb + affea49 commit 12681f4
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 5 deletions.
6 changes: 6 additions & 0 deletions jay-config/src/_private/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ impl Client {
})
}

pub fn get_socket_path(&self) -> Option<String> {
let res = self.send_with_response(&ClientMessage::GetSocketPath);
get_response!(res, None, GetSocketPath { path });
Some(path)
}

pub fn create_pollable(&self, fd: i32) -> Result<PollableId, String> {
let res = self.send_with_response(&ClientMessage::AddPollable { fd });
get_response!(
Expand Down
4 changes: 4 additions & 0 deletions jay-config/src/_private/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ pub enum ClientMessage<'a> {
SetExplicitSyncEnabled {
enabled: bool,
},
GetSocketPath,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -576,6 +577,9 @@ pub enum Response {
GetInputDeviceDevnode {
devnode: String,
},
GetSocketPath {
path: String,
},
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
15 changes: 15 additions & 0 deletions jay-config/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ impl Command {
self.fd(2, fd)
}

/// Runs the application with access to privileged wayland protocols.
///
/// The default is `false`.
pub fn privileged(&mut self) -> &mut Self {
match get!(self).get_socket_path() {
Some(path) => {
self.env("WAYLAND_DISPLAY", &format!("{path}.jay"));
}
_ => {
log::error!("Compositor did not send the socket path");
}
}
self
}

/// Executes the command.
///
/// This consumes all attached file descriptors.
Expand Down
14 changes: 14 additions & 0 deletions src/config/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,19 @@ impl ConfigProxyHandler {
self.state.explicit_sync_enabled.set(enabled);
}

fn handle_get_socket_path(&self) {
match self.state.acceptor.get() {
Some(a) => {
self.respond(Response::GetSocketPath {
path: a.socket_name().to_string(),
});
}
_ => {
log::warn!("There is no acceptor");
}
}
}

fn handle_connector_connected(&self, connector: Connector) -> Result<(), CphError> {
let connector = self.get_connector(connector)?;
self.respond(Response::ConnectorConnected {
Expand Down Expand Up @@ -1732,6 +1745,7 @@ impl ConfigProxyHandler {
ClientMessage::SetExplicitSyncEnabled { enabled } => {
self.handle_set_explicit_sync_enabled(enabled)
}
ClientMessage::GetSocketPath => self.handle_get_socket_path(),
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions toml-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub struct Exec {
pub prog: String,
pub args: Vec<String>,
pub envs: Vec<(String, String)>,
pub privileged: bool,
}

#[derive(Debug, Clone)]
Expand Down
15 changes: 11 additions & 4 deletions toml-config/src/config/parsers/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{
config::{
context::Context,
extractor::{arr, opt, str, val, Extractor, ExtractorError},
extractor::{arr, bol, opt, recover, str, val, Extractor, ExtractorError},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
parsers::{
env::{EnvParser, EnvParserError},
Expand All @@ -11,7 +11,7 @@ use {
Exec,
},
toml::{
toml_span::{Span, Spanned, SpannedExt},
toml_span::{DespanExt, Span, Spanned, SpannedExt},
toml_value::Value,
},
},
Expand Down Expand Up @@ -45,6 +45,7 @@ impl Parser for ExecParser<'_> {
prog: string.to_string(),
args: vec![],
envs: vec![],
privileged: false,
})
}

Expand All @@ -61,6 +62,7 @@ impl Parser for ExecParser<'_> {
prog,
args,
envs: vec![],
privileged: false,
})
}

Expand All @@ -70,8 +72,12 @@ impl Parser for ExecParser<'_> {
table: &IndexMap<Spanned<String>, Spanned<Value>>,
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (prog, args_val, envs_val) =
ext.extract((str("prog"), opt(arr("args")), opt(val("env"))))?;
let (prog, args_val, envs_val, privileged) = ext.extract((
str("prog"),
opt(arr("args")),
opt(val("env")),
recover(opt(bol("privileged"))),
))?;
let mut args = vec![];
if let Some(args_val) = args_val {
for arg in args_val.value {
Expand All @@ -86,6 +92,7 @@ impl Parser for ExecParser<'_> {
prog: prog.value.to_string(),
args,
envs,
privileged: privileged.despan().unwrap_or(false),
})
}
}
2 changes: 1 addition & 1 deletion toml-config/src/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keymap = """
};
"""

on-graphics-initialized = { type = "exec", exec = "mako" }
on-graphics-initialized = { type = "exec", exec = { prog = "mako", privileged = true } }

[shortcuts]
alt-h = "focus-left"
Expand Down
3 changes: 3 additions & 0 deletions toml-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ fn create_command(exec: &Exec) -> Command {
for (k, v) in &exec.envs {
command.env(k, v);
}
if exec.privileged {
command.privileged();
}
command
}

Expand Down
4 changes: 4 additions & 0 deletions toml-spec/spec/spec.generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@
"type": "string",
"description": ""
}
},
"privileged": {
"type": "boolean",
"description": "If `true`, the executable gets access to privileged wayland protocols.\n\nThe default is `false`.\n"
}
},
"required": [
Expand Down
8 changes: 8 additions & 0 deletions toml-spec/spec/spec.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,14 @@ The table has the following fields:

The value of this field should be a table whose values are strings.

- `privileged` (optional):

If `true`, the executable gets access to privileged wayland protocols.

The default is `false`.

The value of this field should be a boolean.


<a name="types-GfxApi"></a>
### `GfxApi`
Expand Down
7 changes: 7 additions & 0 deletions toml-spec/spec/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,13 @@ Exec:
values:
kind: string
description: The environment variables to pass to the executable.
privileged:
kind: boolean
required: false
description: |
If `true`, the executable gets access to privileged wayland protocols.
The default is `false`.
SimpleActionName:
Expand Down

0 comments on commit 12681f4

Please sign in to comment.