0

I am working on an embedded Linux system (kernel-5.10.24), and it uses ash from busybox as /bin/sh. The system support login from serial console and adb shell from PC.

Now I found the shell started from serial console did read the environments defined in /etc/profile, but the shell started by adb shell does NOT.

For example, the /etc/profile is as follows,

# cat /etc/profile export PATH="/bin:/sbin:/usr/bin:/usr/sbin" if [ "$PS1" ]; then if [ "`id -u`" -eq 0 ]; then export PS1='# ' else export PS1='$ ' fi fi 

in serial console,

# echo $HOME /root # echo $ENV # echo $PATH /bin:/sbin:/usr/bin:/usr/sbin 

But in adb shell,

/sys/kernel/config/usb_gadget # echo $PATH /sbin:/usr/sbin:/bin:/usr/bin /sys/kernel/config/usb_gadget # echo $HOME / 

The shell started by adb shell is launched by adbd in target Linux, so is there a way to make the shell started by adb shell to read /etc/profile or other configuration files for its environments?

3 Answers 3

0

You could try use a wrapper script for achieve what you are asking for:

adb shell 'ENV=/etc/profile sh -i' 

A simple alias for this could be:

alias adbshellenv="adb shell 'ENV=/etc/profile sh -i'" 
1
  • So where should I put the wrapper script, in Linux where the adbd is running? Commented Oct 30, 2023 at 0:55
0

With lots of studies and tests, I think I found one way to solve my request/answer.

Firstly, change the source codes of adbd to call a wrapper script of /etc/init.d/adbshell_wrapper.sh in target Linux.
The wrapper script calls at least 2 things, as follows,

#!/bin/sh /etc/profile /bin/sh 

With this solution, the adb shell can get the same environments as what login gets in serial console.

0

From the beginning of the INVOCATION chapter of man bash:

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

So just change adbd to set the argv[0] of the shell it's starting to e.g. -bash.

3
  • If the system is using ash not bash, shall I use -ash? If so, I will try that and update my result here. Commented Mar 2, 2024 at 12:32
  • Yes. Although you could technically set it to any value that includes - as the first character, using the name of the shell that's being invoked is the expected behavior. Commented Mar 3, 2024 at 1:41
  • I am not sure if I really got your point. I changed the source code of adbd to launch shell as /bin/sh -ash, is it correct or not? I got following result, PS C:\Users\test> adb shell - exec '/bin/sh --login' failed: No such file or directory (2) - Commented Mar 5, 2024 at 6:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.