Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Jan 17, 2023
1 parent fbd39e4 commit 04a779c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .cargo/config
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"]
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
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 }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
.vscode/
Cargo.lock
14 changes: 14 additions & 0 deletions Cargo.toml
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 = "*"
16 changes: 16 additions & 0 deletions README.md
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)
52 changes: 52 additions & 0 deletions src/main.rs
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);
};
}

0 comments on commit 04a779c

Please sign in to comment.