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