Improve process handling and environment setup for login shell functionality

This commit is contained in:
kake26 2025-04-24 20:57:38 -05:00
parent 088c61e19b
commit 2f002b9735
Signed by: kake26
GPG key ID: E0A989B571D1F99F
2 changed files with 74 additions and 7 deletions

View file

@ -7,7 +7,65 @@ use Term::ReadLine;
use Cwd;
use File::HomeDir;
use CustomCommands;
$ENV{TERM} = 'xterm-256color' unless exists $ENV{TERM};
use POSIX qw(getlogin getuid setsid tcsetpgrp);
# there is only one
setsid(); # Start a new session
tcsetpgrp(0, getpgrp()); # Set TTY foreground process group
# some env vars
$ENV{HOME} ||= File::HomeDir->my_home;
$ENV{USER} ||= getlogin() || (getpwuid(getuid()))[0] || 'user';
$ENV{LOGNAME} ||= $ENV{USER};
$ENV{SHELL} ||= $0; # Path to pshell.pl or compiled binary
$ENV{PATH} ||= '/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin';
$ENV{TERM} ||= 'xterm-256color';
# we a login shell?
my $is_login_shell = ($0 =~ /^-/ || $ARGV[0] eq '-l');
if ($is_login_shell) {
# Clear inherited environment for security, except essentials
%ENV = (
HOME => $ENV{HOME},
USER => $ENV{USER},
LOGNAME => $ENV{LOGNAME},
SHELL => $ENV{SHELL},
TERM => $ENV{TERM},
);
# Reload system and user configs
}
# system wide env vars
# Parse /etc/environment silently
if (-r '/etc/environment') {
open my $fh, '<', '/etc/environment' or do {
warn "Could not open /etc/environment: $!" if $is_login_shell;
next;
};
while (my $line = <$fh>) {
chomp $line;
next unless $line =~ /^(\w+)=(.*)$/;
$ENV{$1} = $2 unless exists $ENV{$1};
}
close $fh;
}
# Source /etc/profile silently
if (-r '/etc/profile') {
# Run /etc/profile in a subshell, capture env output, and suppress other prints
my $env_output = qx(/bin/sh -c '. /etc/profile >/dev/null 2>&1 && env');
if ($? == 0) {
while ($env_output =~ /^(\w+)=(.*)$/mg) {
$ENV{$1} = $2 unless exists $ENV{$1};
}
} else {
warn "Could not source /etc/profile: $!" if $is_login_shell;
}
}
# Initialize command history and environment
@ -69,7 +127,12 @@ if (-e $rc_file && -r $rc_file) {
while (my $line = <$fh>) {
chomp $line;
next unless length $line;
system($line);
if ($line =~ /^(\w+)=(.*)$/) {
$ENV{$1} = $2;
$env->set($1, $2);
} else {
system($line);
}
}
close $fh;
}