1 |
From: "W. Trevor King" <wking@×××××××.us> |
2 |
|
3 |
Starting a "login" version of Bash via `su` is tricky. The naive: |
4 |
|
5 |
su - ${first_user} -c startx |
6 |
|
7 |
fails because `su - ...` clears a number of environment variables (so |
8 |
the prefixed `source /etc/profile` doesn't accomplish anything), but |
9 |
Bash isn't started with the `--login` option, so it doesn't source |
10 |
/etc/profile internally. From bash(1): |
11 |
|
12 |
A login shell is one whose first character of argument zero is a -, |
13 |
or one started with the --login option. |
14 |
... |
15 |
An interactive shell is one started without non-option arguments and |
16 |
without the -c option whose standard input and error are both |
17 |
connected to terminals (as determined by isatty(3)), or one started |
18 |
with the -i option... |
19 |
... |
20 |
When bash is invoked as an interactive login shell, or as a |
21 |
non-interactive shell with the --login option, it first reads and |
22 |
executes commands from the file /etc/profile, if that file exists. |
23 |
After reading that file, it looks for ~/.bash_profile, |
24 |
~/.bash_login, and ~/.profile, in that order, and reads and executes |
25 |
commands from the first one that exists and is readable. The |
26 |
--noprofile option may be used when the shell is started to inhibit |
27 |
this behavior. |
28 |
|
29 |
In order to get the login-style profile loading with a non-interactive |
30 |
`su` invocation, you need to use something like: |
31 |
|
32 |
echo "${command}" | su - "${user}" |
33 |
|
34 |
This starts a login shell and pipes the command in via stdin, which |
35 |
seems to fake Bash into thinking its running from an interactive |
36 |
terminal. Not the most elegant, but the other implementations I can |
37 |
think of are even worse: |
38 |
|
39 |
su - "${user}" -c "bash --login -c ${command}" |
40 |
su - "${user}" -c 'source /etc/profile && |
41 |
(source .bash_profile || ...) && ${command}" |
42 |
|
43 |
The old expression was broken anyway due to unescaped ampersands in |
44 |
the sed expression. From sed(1): |
45 |
|
46 |
s/regexp/replacement/ |
47 |
Attempt to match regexp against the pattern space. If successful, |
48 |
replace that portion matched with replacement. The replacement |
49 |
may contain the special character & to refer to that portion of |
50 |
the pattern space which matched, and the special escapes \1 |
51 |
through \9 to refer to the corresponding matching sub-expressions |
52 |
in the regexp. |
53 |
|
54 |
This means that the old expression (with unescaped ampersands) lead |
55 |
to: |
56 |
|
57 |
source /etc/profile ##STARTX##STARTX su - ${first_user} -c startx |
58 |
|
59 |
with ${first_user} expanded. This commented out startx, so it was |
60 |
never run. |
61 |
|
62 |
Reviewed-by: Matt Turner <mattst88@×××××.com> |
63 |
--- |
64 |
targets/support/livecdfs-update.sh | 4 +--- |
65 |
1 file changed, 1 insertion(+), 3 deletions(-) |
66 |
|
67 |
diff --git a/targets/support/livecdfs-update.sh b/targets/support/livecdfs-update.sh |
68 |
index f8eb425..2b41f9d 100644 |
69 |
--- a/targets/support/livecdfs-update.sh |
70 |
+++ b/targets/support/livecdfs-update.sh |
71 |
@@ -377,9 +377,7 @@ esac |
72 |
# We want the first user to be used when auto-starting X |
73 |
if [ -e /etc/startx ] |
74 |
then |
75 |
- sed -i \ |
76 |
- "s:##STARTX:source /etc/profile && su - ${first_user} -c startx:" \ |
77 |
- /root/.bashrc |
78 |
+ sed -i "s:##STARTX:echo startx | su - '${first_user}':" /root/.bashrc |
79 |
fi |
80 |
|
81 |
if [ -e /lib/rcscripts/addons/udev-start.sh ] |
82 |
-- |
83 |
1.8.3.2 |