public inbox for gentoo-catalyst@lists.gentoo.org
 help / color / mirror / Atom feed
From: "W. Trevor King" <wking@tremily.us>
To: Catalyst <gentoo-catalyst@lists.gentoo.org>
Cc: "W. Trevor King" <wking@tremily.us>
Subject: [gentoo-catalyst] [PATCH v2 2/2] livecdfs-update.sh: Use `bash --login` to spawn startx
Date: Wed, 06 Mar 2013 12:02:59 -0500	[thread overview]
Message-ID: <aef26be897370eda98515d666a012ba02b27117b.1362589155.git.wking@tremily.us> (raw)
In-Reply-To: <cover.1362589155.git.wking@tremily.us>
In-Reply-To: <cover.1362589155.git.wking@tremily.us>

From: "W. Trevor King" <wking@tremily.us>

Starting a "login" version of Bash via `su` is tricky.  The naive:

  su - ${first_user} -c startx

fails because `su - ...` clears a number of environment variables (so
the prefixed `source /etc/profile` doesn't accomplish anything), but
Bash isn't started with the `--login` option, so it doesn't source
/etc/profile internally.  From bash(1):

  A login shell is one whose first character of argument zero is a -,
  or one started with the --login option.
  ...
  An interactive shell is one started without non-option arguments and
  without the -c option whose standard input and error are both
  connected to terminals (as determined by isatty(3)), or one started
  with the -i option...
  ...
  When bash is invoked as an interactive login shell, or as a
  non-interactive shell with the --login option, it first reads and
  executes commands from the file /etc/profile, if that file exists.
  After reading that file, it looks for ~/.bash_profile,
  ~/.bash_login, and ~/.profile, in that order, and reads and executes
  commands from the first one that exists and is readable.  The
  --noprofile option may be used when the shell is started to inhibit
  this behavior.

In order to get the login-style profile loading with a non-interactive
`su` invocation, you need to use something like:

  echo "${command}" | su - "${user}"

This starts a login shell and pipes the command in via stdin, which
seems to fake Bash into thinking its running from an interactive
terminal.  Not the most elegant, but the other implementations I can
think of are even worse:

  su - "${user}" -c "bash --login -c ${command}"
  su - "${user}" -c 'source /etc/profile &&
      (source .bash_profile || ...) && ${command}"

The old expression was broken anyway due to unescaped ampersands in
the sed expression.  From sed(1):

  s/regexp/replacement/
    Attempt to match regexp against the pattern space.  If successful,
    replace that portion matched with replacement.  The replacement
    may contain the special character & to refer to that portion of
    the pattern space which matched, and the special escapes \1
    through \9 to refer to the corresponding matching sub-expressions
    in the regexp.

This means that the old expression (with unescaped ampersands) lead
to:

  source /etc/profile ##STARTX##STARTX su - ${first_user} -c startx

with ${first_user} expanded.  This commented out startx, so it was
never run.
---
 targets/support/livecdfs-update.sh | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/targets/support/livecdfs-update.sh b/targets/support/livecdfs-update.sh
index 77d694e..0ac41dd 100644
--- a/targets/support/livecdfs-update.sh
+++ b/targets/support/livecdfs-update.sh
@@ -388,9 +388,7 @@ esac
 # We want the first user to be used when auto-starting X
 if [ -e /etc/startx ]
 then
-	sed -i \
-		"s:##STARTX:source /etc/profile && su - ${first_user} -c startx:" \
-		/root/.bashrc
+	sed -i "s:##STARTX:echo startx | su - '${first_user}':" /root/.bashrc
 fi
 
 if [ -e /lib/rcscripts/addons/udev-start.sh ]
-- 
1.8.2.rc0.16.g20a599e



  parent reply	other threads:[~2013-03-06 17:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-03 16:53 [gentoo-catalyst] [PATCH] livecdfs-update.sh: Escape ampersands in STARTX sed expression W. Trevor King
2013-03-04  3:13 ` [gentoo-catalyst] [PATCH] livecd-bashrc: Avoid a startx race by restricting to tty1 W. Trevor King
2013-03-06 17:02   ` [gentoo-catalyst] [PATCH v2 0/2] Fix livecdfs-update.sh startx handling W. Trevor King
2013-03-06 17:02     ` [gentoo-catalyst] [PATCH v2 1/2] livecd-bashrc: Avoid a startx race by restricting to tty1 W. Trevor King
2013-03-06 17:02     ` W. Trevor King [this message]
2013-03-09  2:55       ` [gentoo-catalyst] [PATCH v2 2/2] livecdfs-update.sh: Use `bash --login` to spawn startx Matt Turner
2013-03-09 11:48         ` [gentoo-catalyst] " W. Trevor King
2013-03-09  2:49   ` [gentoo-catalyst] [PATCH] livecd-bashrc: Avoid a startx race by restricting to tty1 Matt Turner
2013-03-09 11:46     ` [gentoo-catalyst] " W. Trevor King
2013-03-09  2:47 ` [gentoo-catalyst] [PATCH] livecdfs-update.sh: Escape ampersands in STARTX sed expression Matt Turner
2013-03-09 11:48   ` [gentoo-catalyst] " W. Trevor King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aef26be897370eda98515d666a012ba02b27117b.1362589155.git.wking@tremily.us \
    --to=wking@tremily.us \
    --cc=gentoo-catalyst@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox