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