/etc/profile.d/chruby.sh graphical login issue with Ubuntu 12.04 #56

Closed
mkremer opened this Issue Dec 22, 2012 · 30 comments

Projects

None yet

4 participants

@mkremer
Contributor
mkremer commented Dec 22, 2012

When using scripts/setup.sh to install chruby on Ubuntu 12.04 it adds /etc/profile.d/chruby.sh after which I can no longer login on Unity (regardless of my default shell, I tried both with bash and zsh). On my system I deleted /etc/profile.d/chruby.sh and added the source command to my ~/.zshrc and am now able to login without issue and use chruby.

@postmodern
Owner

I'm curious what /bin/sh points to.

@postmodern
Owner

Also, did you set zsh as your shell via chsh or did you configure your terminal to run zsh instead of /bin/sh.

@mkremer
Contributor
mkremer commented Dec 23, 2012

Thanks for the rapid response!

I'll take a look when I get to my laptop to see where /bin/sh points to.

I used chsh to set my shell to zsh.

@mkremer
Contributor
mkremer commented Dec 23, 2012

/bin/sh points to /bin/dash

Also, here are the contents of /etc/profile on my system (which to my memory is the default version of it):

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
@postmodern
Owner

I suspect Ubuntu is spawning dash (with login-mode) in order to execute X11/Unity commands. This then loads /etc/profile.d/*.sh and errors on chruby.sh. I can wrap the chruby.sh config in a bash/zsh check.

@postmodern
Owner

After much discussion, this appears to be the best way to cause dash to stop parsing/evaling chruby.sh, before it gets to the Array syntax:

[ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] || return
@postmodern postmodern added a commit that referenced this issue Dec 24, 2012
@postmodern Add a bash/zsh check in /etc/profile.d/chruby.sh (issue #56).
* This will ensure that dash will not error on loading chruby.sh.
b894098
@mkremer
Contributor
mkremer commented Dec 24, 2012

With this change I can login without issue, however chruby doesn't get loaded.

@postmodern
Owner

Could you post your /etc/zshrc and /etc/zprofile?

@mkremer
Contributor
mkremer commented Dec 25, 2012

When I set my user up to use bash the new /etc/profile.d/chruby.sh file works! :)

Ubuntu puts its global zsh config in /etc/zsh, all the default files contain only comments except for /etc/zsh/zshrc and /etc/zsh/zshenv. I'll add the contents of both those files, although /etc/zsh/zshenv is quite boring.

# /etc/zsh/zshrc: system-wide .zshrc file for zsh(1).
#
# This file is sourced only for interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#
# Global Order: zshenv, zprofile, zshrc, zlogin

READNULLCMD=${PAGER:-/usr/bin/pager}

if [[ "$TERM" != emacs ]]; then
[[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M emacs "$terminfo[kich1]" overwrite-mode
[[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M vicmd "$terminfo[kich1]" overwrite-mode

[[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]" vi-up-line-or-history
[[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]" vi-forward-char
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history
[[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char

# ncurses fogyatekos
[[ "$terminfo[kcuu1]" == ""* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history
[[ "$terminfo[kcud1]" == ""* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history
[[ "$terminfo[kcuf1]" == ""* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char
[[ "$terminfo[kcub1]" == ""* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char
[[ "$terminfo[khome]" == ""* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M viins "${terminfo[kend]/O/[}" end-of-line
[[ "$terminfo[khome]" == ""* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M emacs "${terminfo[kend]/O/[}" end-of-line
fi

zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin \
                 /usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin

unalias run-help
autoload run-help

# If you don't want compinit called here, place the line
# skip_global_compinit=1
# in your $ZDOTDIR/.zshenv or $ZDOTDIR/.zprofice
if [[ -z "$skip_global_compinit" ]]; then
  autoload -U compinit
  compinit
fi
# /etc/zsh/zshenv: system-wide .zshenv file for zsh(1).
#
# This file is sourced on all invocations of the shell.
# If the -f flag is present or if the NO_RCS option is
# set within this file, all other initialization files
# are skipped.
#
# This file should contain commands to set the command
# search path, plus other important environment variables.
# This file should not contain commands that produce
# output or assume the shell is attached to a tty.
#
# Global Order: zshenv, zprofile, zshrc, zlogin

if [[ -z "$PATH" || "$PATH" == "/bin:/usr/bin" ]]
then
    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
fi

export PATH=$PATH:/opt/vagrant/bin
@postmodern
Owner

Double check if any of the global zsh config files load /etc/profile.d/*.sh. For example, Fedora 17's zshrc.

@mkremer
Contributor
mkremer commented Dec 27, 2012

It looks like that isn't the case, I'll investigate in more detail as soon as I can.

@mkremer
Contributor
mkremer commented Jan 1, 2013

I've taken another hard look, and they aren't being loaded :/

@mkremer
Contributor
mkremer commented Jan 20, 2013

To make this work on Ubuntu with zsh by default I guess the setup script would have to add something to /etc/zsh/zprofile or maybe even /etc/zsh/zshenv. Maybe its best to add a note to the README to explain this exception for Ubuntu 12.04? Or would you rather update the setup script to cater for Ubuntu's exception? In any case I'd be willing to help out...

@postmodern
Owner

Did the updated profile.d script work for you? It should only load under bash/zsh.

@mkremer
Contributor
mkremer commented Jan 20, 2013

When I log in graphically then the profile.d script doesn't get loaded, even if I use bash. When I log in on a console (by switching with CTRL+ALT+F1) it works only for bash, because Ubuntu (at least) 12.04 doesn't load the /etc/profile.d/*.sh files for zsh.

If I modify my /etc/zsh/zprofile to execute the script it does work, but again only when I login using a console:

# /etc/zsh/zprofile: system-wide .zprofile file for zsh(1).
#
# This file is sourced only for login shells (i.e. shells
# invoked with "-" as the first character of argv[0], and
# shells invoked with the -l flag.)
#
# Global Order: zshenv, zprofile, zshrc, zlogin
. /etc/profile.d/chruby.sh
@mkremer
Contributor
mkremer commented Jan 20, 2013

Once I've logged in graphically, and I execute ". /etc/profile.d/chruby.sh" in a terminal window then chruby gets loaded for that session as you'd expect.

@mkremer
Contributor
mkremer commented Jan 20, 2013

I'm doing some digging into this... I've modified /etc/profile.d/chruby.sh to output the env to a file, and it shows there is no $BASH_VERSION or $ZSH_VERSION (explaining why a graphical login doesn't load chruby).

$SHELL is set to /usr/bin/zsh

@mkremer
Contributor
mkremer commented Jan 20, 2013

I think I am doing something wrong with outputting the env, because when I login through the console I also do not get $BASH_VERSION or $ZSH_VERSION in the output. I output the env using "env > /home/mkremer/loginenv"

@mkremer
Contributor
mkremer commented Jan 20, 2013

I got all the shell variables by using the set command instead of env, when logging in graphically there really is no $BASH_VERSION or $ZSH_VERSION.

@postmodern
Owner

I think you should report this to Ubuntu. They are apparently have customized how the shell is loaded for Unity/terminal. I wish zsh had an equivalent to /etc/profile.d/ so I could just drop in a chruby.sh file.

@mkremer
Contributor
mkremer commented Jan 20, 2013

I think you're right, when I next have the time I'll figure out how to report this to Ubuntu.

Looking at the default files present in /etc/profile.d it wouldn't do any harm if Ubuntu would simply load those up for zsh like Fedora does...

To make chruby work for me in both cases I've added the below snippet to my .zshrc:

if [ -s /usr/local/share/chruby/chruby.sh ] && ! type chruby > /dev/null; then
  source /usr/local/share/chruby/chruby.sh && chruby ruby-1.9 # Load chruby if it is installed
fi

Perhaps its worthwhile updating the README to indicate the state of this issue, so other Ubuntu users know how to make this work for them?

I'll be sure to update this issue when I report this to Ubuntu.

Thanks for all your help so far on this issue, I really appreciate it a lot!

@mkremer
Contributor
mkremer commented Jan 20, 2013

I've posted a question on Launchpad: https://answers.launchpad.net/unity-greeter/+question/219654

Hopefully this will lead to a solution :)

@envygeeks

How about $PS1 ;)

@mkremer
Contributor
mkremer commented Jan 25, 2013

The bigger problem is that Ubuntu 12.04 doesn't handle the login process with the user's login shell, it seems to be processing it with dash instead which seems to fail on chruby.sh

@mkremer
Contributor
mkremer commented Feb 5, 2013

Unfortunately my launchpad question got no answer and then got cleaned up by the Launchpad janitor :(

I've opened a question on askubuntu in the hopes of getting an answer there: http://askubuntu.com/questions/251618/how-does-the-shell-environment-load-when-logging-in-graphically

@mkremer
Contributor
mkremer commented Feb 7, 2013

Robert Ancell re-opened my question on Launchpad and posted an answer on https://answers.launchpad.net/lightdm/+question/219654 that clarifies some of this, I have asked him for more information.

@DavidEGrayson
Contributor

Hello. I see that the original issue discussed in this thread has not been fixed. I use Ubuntu 12.04, my /bin/sh points to dash, and after I installed chruby.sh into my /etc/profile.d (using a symbolic link) I was unable to log in graphically. I thought for many minutes that I was just typing my password incorrectly because Ubuntu showed me no error messages.

I have incorporated postmodern's suggestion from earlier in this thread into my fork of chruby. Hopefully it can be pulled or cherry-picked into the real chruby. Here is the commit where I fixed it:

DavidEGrayson@3cf52ed

@DavidEGrayson
Contributor

Actually, never mind; chruby.sh doesn't need to be changed. It seems risky to program chruby.sh to silently fail like I was suggesting. The approach that I now see on the README for chruby is probably better. It says to make a script in /etc/profile.d/ that sources chruby.sh if and only if the user is running bash or zsh.

@postmodern
Owner

I think we can close this. [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] || return should work.

@postmodern postmodern closed this May 23, 2013
@postmodern
Owner

@DavidEGrayson another problem is that every file would need the bash/zsh check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment