-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build] | ||
target = "x86_64-unknown-linux-musl" | ||
rustflags = ["-C", "target-feature=+crt-static", "-C", "strip=symbols"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- action | ||
tags: | ||
- '*' | ||
|
||
jobs: | ||
build_and_release: | ||
name: importenv | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Setup toolchain | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
target: x86_64-unknown-linux-musl | ||
- name: Build | ||
run: cargo build --release | ||
- name: Release | ||
uses: softprops/action-gh-release@v1 | ||
if: startsWith(github.ref, 'refs/tags/') | ||
with: | ||
files: target/x86_64-unknown-linux-musl/release/importenv | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
.vscode/ | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "importenv" | ||
version = "0.0.1" | ||
readme = "README.md" | ||
license-file = "LICENSE" | ||
repository = "https://github.com/VHSgunzo/importenv" | ||
description = "Launching an executable file with environment variables from a specific process id" | ||
|
||
[profile.release] | ||
strip = "symbols" | ||
|
||
[dependencies] | ||
chrono = "*" | ||
nix = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
# importenv | ||
Launching an executable file with environment variables from a specific process id | ||
|
||
## To get started: | ||
* **Download the latest revision** | ||
``` | ||
git clone https://github.com/VHSgunzo/importenv.git && cd importenv | ||
``` | ||
* **Usage** | ||
``` | ||
./importenv $PID {command} {command args} | ||
``` | ||
|
||
* **Сompile a binary** | ||
``` | ||
cargo build --release | ||
``` | ||
* Or take an already precompiled binary file from the [releases](https://github.com/VHSgunzo/importenv/releases) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
extern crate nix; | ||
extern crate chrono; | ||
use chrono::Local; | ||
use std::str::from_utf8; | ||
use nix::unistd::execvpe; | ||
use std::{process::exit, env, fs::{OpenOptions}, path::Path, io::Read, ffi::{CString, CStr}}; | ||
|
||
static RED: &str = "\x1b[91m"; | ||
static RESETCOLOR: &str = "\x1b[0m"; | ||
|
||
pub fn error_msg(msg: &str) { | ||
let date = Local::now().format("%Y.%m.%d %H:%M:%S"); | ||
eprintln!("{}[ ERROR ][{}]: {}{}", RED, date, msg, RESETCOLOR); | ||
} | ||
|
||
fn main() { | ||
let mut exec_args: Vec<String> = env::args().collect(); | ||
if exec_args.len() < 3 { | ||
error_msg(&format!("Syntax: {} <PID> <command> <command args>", exec_args[0])); | ||
exit(1); | ||
}; | ||
let pid = exec_args.remove(1); | ||
exec_args.drain(0..1); | ||
let exec_prog = CString::new(exec_args[0].clone()).unwrap(); | ||
let exec_prog = exec_prog.as_c_str(); | ||
let exec_args: Vec<_> = exec_args.iter() | ||
.map(|s| CString::new(s.as_bytes()).unwrap()).collect(); | ||
|
||
let environ = format!("/proc/{}/environ", pid); | ||
let environ_file_path = Path::new(&environ); | ||
let mut environ_file = OpenOptions::new() | ||
.read(true) | ||
.write(false) | ||
.create(false) | ||
.open(environ_file_path) | ||
.unwrap_or_else(|err| { | ||
error_msg(&format!("{}: \"{}\"", &err.to_string(), environ)); | ||
exit(1); | ||
}); | ||
let mut env_buf = Vec::new(); | ||
environ_file.read_to_end(&mut env_buf).unwrap(); | ||
let mut exec_env = Vec::new(); | ||
for var in env_buf.split(|b| *b == 0) { | ||
exec_env.push(CString::new(from_utf8(&var).unwrap()).unwrap()); | ||
}; | ||
exec_env.pop(); | ||
let exec_env: Vec<&CStr> = exec_env.iter().map(|c| c.as_c_str()).collect(); | ||
if let Err(err) = execvpe(exec_prog, &exec_args, &exec_env) { | ||
error_msg(&format!("{}: {:?}", &err.to_string(), exec_prog)); | ||
exit(1); | ||
}; | ||
} |