File exenv-20251101.46c5da5.obscpio of Package exenv

07070100000000000081A400000000000000000000000169061A6100000005000000000000000000000000000000000000002200000000exenv-20251101.46c5da5/.gitignorebuild07070100000001000081A400000000000000000000000169061A6100000183000000000000000000000000000000000000002000000000exenv-20251101.46c5da5/MakefilePREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1


SRC=exenv.py

build/exenv: build
	cp $(SRC) build/exenv
	chmod +x build/exenv

build:
	mkdir -p build

clean:
	rm -rf build

README.html: exenv.1
	pandoc -f man -t html -o README.html exenv.1

install: build/exenv exenv.1
	install -D -m 755 -t $(BINDIR) build/exenv
	install -D -m 644 -t $(MANDIR) exenv.1

07070100000002000081A400000000000000000000000169061A6100000753000000000000000000000000000000000000002300000000exenv-20251101.46c5da5/README.html<h1>NAME</h1>
<p>exenv - execute a command with the environment of a specified
process</p>
<h1>SYNOPSIS</h1>
<p><strong>exenv</strong> &lt;PID&gt; &lt;command&gt; [args]</p>
<h1>DESCRIPTION</h1>
<p>The <strong>exenv</strong> command allows you to execute a new
process with the same environment variables as a specified running
process.</p>
<dl>
<dt><strong>&lt;PID&gt;</strong></dt>
<dd>
<p>The process ID of the existing process whose environment variables
you want to use.</p>
</dd>
</dl>
<dl>
<dt><strong>&lt;command&gt;</strong></dt>
<dd>
<p>The command to execute. This can include script names or
binaries.</p>
</dd>
</dl>
<dl>
<dt><strong>[args]</strong></dt>
<dd>
<p>Optional arguments to pass to the command.</p>
<p>If the specified <strong>&lt;PID&gt;</strong> is not valid, or if
permission is denied when trying to access the environment of the
specified process, the program will output an error message and
terminate.</p>
</dd>
</dl>
<h1>EXAMPLES</h1>
<p>To execute a command using the environment variables from the process
with PID 1234 obtaining environment variables required to run Xorg
application:</p>
<blockquote>
<p><strong>exenv 1234 /usr/bin/xclip -o</strong></p>
</blockquote>
<p>To use SSH agent of running process you can use:</p>
<blockquote>
<p><strong>exenv 2345 ssh user@server</strong></p>
</blockquote>
<p>To use environment of running process and add something extra:</p>
<blockquote>
<p><strong>exenv 3456 env FOO=bar /usr/bin/some_command --with-args
yes</strong></p>
</blockquote>
<h1>COPYRIGHT</h1>
<p>Copyright © 2025 Tomáš Čech &lt;sleep_walker@gnu.org&gt;</p>
<p>License GPLv3+: GNU GPL version 3 or later
&lt;https://gnu.org/licenses/gpl.html&gt;.</p>
<p><br />
This is free software: you are free to change and redistribute it. There
is NO WARRANTY, to the extent permitted by law.</p>
<h1>SEE ALSO</h1>
<p>bash(1), sh(1)</p>
07070100000003000081A400000000000000000000000169061A61000005DE000000000000000000000000000000000000001F00000000exenv-20251101.46c5da5/exenv.1.\" Man page for exenv
.TH EXENV 1 "October 2023" "1.0" "User Commands"
.SH NAME
exenv \- execute a command in the environment of a specified process
.SH SYNOPSIS
.B exenv
<PID> <command> [args]
.SH DESCRIPTION
The 
.B exenv
command allows you to execute a new process with the same environment variables as a specified running process.

.TP
.B <PID>
The process ID of the existing process whose environment variables you want to use.

.TP
.B <command>
The command to execute. This can include script names or binaries.

.TP
.B [args]
Optional arguments to pass to the command.

If the specified 
.B <PID>
is not valid, or if permission is denied when trying to access the environment of the specified process, the program will output an error message and terminate.
.SH EXAMPLES
To execute a command using the environment variables from the process with PID 1234 obtaining environment variables required to run Xorg application:

.RS
.B exenv 1234 /usr/bin/xclip -o
.RE

To use SSH agent of running process you can use:

.RS
.B exenv 2345 ssh user@server
.RE

To use environment of running process and add something extra:

.RS
.B exenv 3456 env FOO=bar /usr/bin/some_command --with-args yes
.RE

.SH COPYRIGHT
Copyright \(co 2025 Tomáš Čech <sleep_walker@gnu.org>

License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.

.br
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


.SH SEE ALSO
execvpe(2)
07070100000004000081A400000000000000000000000169061A610000069A000000000000000000000000000000000000002000000000exenv-20251101.46c5da5/exenv.py#!/usr/bin/env python3

import os
import sys

class EnvError(Exception):
    pass

class ExecError(Exception):
    pass


def read_env_variables(pid):
    try:
        with open('/proc/{pid}/environ'.format(pid=pid), 'r') as f:
            env_vars = f.read().split('\0')
            return {
                name: value
                for var in env_vars if '=' in var
                for name, _, value in [var.partition('=')]
            }
    except FileNotFoundError as e:
        raise EnvError("Process with PID {pid} not found".format(pid=pid)) from e

    except PermissionError as e:
        raise EnvError("Permission denied: Cannot read the environment variables of the process.") from e


def exec_process_with_env(pid, command):
    env_vars = read_env_variables(pid)
    
    if env_vars is not None:
        env = {key: value for key, value in env_vars.items()}
        try:
            os.execvpe(command[0], command, env)
        except Exception as e:
            print("Failed to execute command: {e}".format(e=e))
            sys.exit(1)

def print_usage():
    print("Usage: exenv <PID> <command> [args]")    
        
def parse_command_line():
    if len(sys.argv) < 3:
        print_usage()
        sys.exit(1)

    try:
        pid = int(sys.argv[1])
    except ValueError:
        print("Invalid PID: {pid}".format(pid=sys.argv[1]))
        print_usage()
        sys.exit(1)

    command = sys.argv[2:]

    return pid, command
    
def main():
    pid, command = parse_command_line()

    try:
        exec_process_with_env(pid, command)
    except (EnvError, ExecError) as e:
        print(e)
        sys.exit(1)
    
    
if __name__ == "__main__":
    main()
07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!13 blocks
openSUSE Build Service is sponsored by