From 22078fff3372c317102b4d74fd2f6feb7c2342be Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 19:09:25 +0800 Subject: [PATCH 01/11] README.md: fix spelling. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 22a6c08..b928aa9 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Thanks to: tigrmesh finding a critical issue with the gnome-session handler Profjim several incredibly useful patches lambchops468 consolekit and hibernation patches - CasperVector Massive rearchitecturing and code sanisation + CasperVector Massive rearchitecturing and code sanitation Licensed under GPLv2+ From 202ae9a61ea78296a6e9a9032cec9c5648a2705a Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 19:14:43 +0800 Subject: [PATCH 02/11] README.md: better formatting in markdown. --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b928aa9..62c45ad 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,15 @@ Invocation ---------- To run cdm, use - $ cdm [RCFILE] + + $ cdm [RCFILE] cdm tries to source configuration files in this order, and uses the first existing one: - [RCFILE specified on command line] - $HOME/.cdmrc - /etc/cdmrc + + [RCFILE specified on command line] + $HOME/.cdmrc + /etc/cdmrc To autostart cdm when you log in your account, copy the content of /usr/share/doc/cdm/profile.sh to the tail of your shell profile (~/.profile, From 5dce1c0dbfea2d569a6ca3c6a0e569dda4ccd8e3 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 19:22:19 +0800 Subject: [PATCH 03/11] cdm-xlaunch: use `"$@"' instead of `$*' for better quoting. --- src/cdm-xlaunch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cdm-xlaunch b/src/cdm-xlaunch index e6b4986..79c1829 100755 --- a/src/cdm-xlaunch +++ b/src/cdm-xlaunch @@ -77,7 +77,7 @@ if $consolekit; then fi # Conform to POSIX and do not use `>&' here. -setsid startx $* > /dev/null 2>&1 & +setsid startx "$@" > /dev/null 2>&1 & # If wait(1) returns with a value >128, it was interrupted by kill(1), # so registration was sucessful. From 0b8ca1d3b83dc4849e300f739da6e1645b99481a Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 19:35:48 +0800 Subject: [PATCH 04/11] Polish vim modelines. --- src/cdm | 2 +- src/cdm-xlaunch | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cdm b/src/cdm index 3663685..813ea69 100755 --- a/src/cdm +++ b/src/cdm @@ -193,4 +193,4 @@ case ${flaglist[$binindex]} in ;; esac -# vim:set ts=4 sw=4 et: +# vim:ts=4:sw=4:et diff --git a/src/cdm-xlaunch b/src/cdm-xlaunch index 79c1829..ea92e5a 100755 --- a/src/cdm-xlaunch +++ b/src/cdm-xlaunch @@ -96,4 +96,4 @@ if $consolekit; then fi fi -# vim:set ts=4 sw=4 et: +# vim:ts=4:sw=4:et From b1f9ca34b26ca350b0e154c45672d7eabdcbb3d7 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 19:54:37 +0800 Subject: [PATCH 05/11] Revert commit 2c8cc1a4, and fix the problem in a correct way. --- src/cdm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cdm b/src/cdm index 813ea69..b0fcf84 100755 --- a/src/cdm +++ b/src/cdm @@ -70,22 +70,22 @@ fi # Default options. -binlist=${binlist:-()} -namelist=${namelist:-()} -flaglist=${flaglist:-()} +[[ -z "${binlist[*]}" ]] && binlist=() +[[ -z "${namelist[*]}" ]] && namelist=() +[[ -z "${flaglist[*]}" ]] && flaglist=() dialogrc=${dialogrc:-} countfrom=${countfrom:-0} display=${display:-0} xtty=${xtty:-7} locktty=${locktty:-no} -serverargs=${serverargs:-'-nolisten tcp'} consolekit=${consolekit:-yes} cktimeout=${cktimeout:-30} +serverargs=${serverargs:-'-nolisten tcp'} # Offer all available sessions in /etc/X11/Sessions, # if binlist if not explicitly set in cdmrc. -if [[ "$binlist" == "()" ]]; then +if [[ "${#binlist[@]}" == 0 ]]; then binlist=($(find /etc/X11/Sessions -maxdepth 1 -type f)) for ((i=0; i<${#binlist[@]}; i++)); do declare flaglist[$i]="X" From 2ea165a1bc4a42d0e4ae3536449480f64dfaf849 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 20:13:08 +0800 Subject: [PATCH 06/11] cdm: simplify processing of /etc/X11/Sessions. --- src/cdm | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cdm b/src/cdm index b0fcf84..080d050 100755 --- a/src/cdm +++ b/src/cdm @@ -87,10 +87,8 @@ serverargs=${serverargs:-'-nolisten tcp'} if [[ "${#binlist[@]}" == 0 ]]; then binlist=($(find /etc/X11/Sessions -maxdepth 1 -type f)) - for ((i=0; i<${#binlist[@]}; i++)); do - declare flaglist[$i]="X" - declare namelist[$i]=$(basename ${binlist[$i]}) - done + flaglist=($(sed 's/[[:digit:]]\+/X/g' <<< ${!binlist[@]})) + namelist=(${binlist[@]##*/}) fi # Generate the main menu. From 7aa1654039fea9894ff38ca977f05c2e7d47618e Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 20:38:00 +0800 Subject: [PATCH 07/11] Minor improvements to coding style. --- src/cdm | 32 +++++++++++++++----------------- src/cdm-xlaunch | 6 +++--- src/cdmrc | 12 ++++++------ src/profile.sh | 2 +- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/cdm b/src/cdm index 080d050..239ca4b 100755 --- a/src/cdm +++ b/src/cdm @@ -48,7 +48,7 @@ yesno() case "$value" in [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0;; [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1;; - *) warn "Invalid value for \`$1'; falling back to \`no' for now.";; + *) warn "invalid value for \`$1'; falling back to \`no' for now.";; esac } @@ -83,7 +83,7 @@ cktimeout=${cktimeout:-30} serverargs=${serverargs:-'-nolisten tcp'} # Offer all available sessions in /etc/X11/Sessions, -# if binlist if not explicitly set in cdmrc. +# if $binlist if not explicitly set in cdmrc. if [[ "${#binlist[@]}" == 0 ]]; then binlist=($(find /etc/X11/Sessions -maxdepth 1 -type f)) @@ -121,13 +121,14 @@ case ${flaglist[$binindex]} in # *C*onsole programs. [Cc]) clear + # If $bin is a login shell, it might `exec' cdm again, causing an endless # loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and # only let the shell automatically `exec' cdm when $CDM_SPAWN is not set. # See also the example shell profile file shipped with the cdm package. # Also untrap SIGINT and SIGTSTP before spawning process: If this is not - # done, *ANY* child process of *ANY* child (bash) shell will completely + # done, any child process of any child (bash) shell will completely # ignore SIGINT, which is rather confusing, and cannot be undone. trap - SIGINT SIGTSTP @@ -138,12 +139,10 @@ case ${flaglist[$binindex]} in [Xx]) clear - # If X is already running and locktty=yes, activate it - if $(yesno locktty); then - if xdpyinfo -display ":$display.0" &> /dev/null; then - chvt "$((display+xtty))" - exitnormal - fi + # If X is already running and locktty=yes, activate it. + if $(yesno locktty) && xdpyinfo -display ":$display.0" &> /dev/null; then + chvt "$((display+xtty))" + exitnormal fi # Get the first empty display. @@ -161,17 +160,16 @@ case ${flaglist[$binindex]} in fi done - # run X in current tty - if [[ $xtty == "keep" ]]; then - vt=$(tty) - vt=${vt#/dev/} - if [[ $vt != tty* ]]; then - error "error: invalid TTY" + # Support for running X in current tty. + if [[ $xtty == 'keep' ]]; then + vt="$(tty)" + if [[ "$vt" != '/dev/tty'* ]]; then + error 'invalid TTY.' exiterror fi - vt=${vt#tty} + vt="${vt#/dev/tty}" else - vt=$((xtty+display)) + vt="$((xtty+display))" fi serverargs=":${display} $serverargs vt$vt" diff --git a/src/cdm-xlaunch b/src/cdm-xlaunch index ea92e5a..5535ec7 100755 --- a/src/cdm-xlaunch +++ b/src/cdm-xlaunch @@ -56,7 +56,7 @@ done # Do first to avoid race conditions. if $consolekit; then - info "waiting for ConsoleKit to register X session." + info 'waiting for ConsoleKit to register X session.' sleep "$cktimeout" & clockpid=$! dbuspidfifo=$(mktemp --dry-run --tmpdir $name.XXXXXXXX) if ! mkfifo "$dbuspidfifo"; then @@ -86,11 +86,11 @@ if $consolekit; then if wait "$clockpid" >& /dev/null then kill "$dbuspid" - error "ConsoleKit registration timed out." + error 'ConsoleKit registration timed out.' exit 1 else kill "$dbuspid" - info "ConsoleKit registration succeeded." + info 'ConsoleKit registration succeeded.' exit 0 fi fi diff --git a/src/cdmrc b/src/cdmrc index 1d0525c..daf4449 100644 --- a/src/cdmrc +++ b/src/cdmrc @@ -8,17 +8,17 @@ # # (An example:) # binlist=( -# "~/.xsession" # Launch your X session, -# "/bin/bash --login" # or just execute your shell, -# "/usr/bin/fbterm" # or start a frame buffer console, -# "/usr/bin/cdm ~/.submenu.cdmrc" # or go to a submenu :) +# '~/.xsession' # Launch your X session, +# '/bin/bash --login' # or just execute your shell, +# '/usr/bin/fbterm' # or start a frame buffer console, +# '/usr/bin/cdm ~/.submenu.cdmrc' # or go to a submenu :) # ) # List all program display names, one-by-one matched with $binlist. # Names with whitespaces should be quoted or escaped. # # (Continued example:) -# namelist=("X session" Console FBTerm "Sub menu") +# namelist=('X session' Console FBTerm 'Sub menu') # Type of the programs, one-by-one matched with $binlist. # `C' for *C*onsole programs, which would be `exec'ed. @@ -39,7 +39,7 @@ countfrom=0 display=0 # Where should the first X tty be spawned? -# special value 'keep' causes to run X in current tty +# special value `keep' causes to run X in current tty. xtty=7 # Should cdm(1) stick to the specified display? diff --git a/src/profile.sh b/src/profile.sh index c5eae0a..dd7716a 100644 --- a/src/profile.sh +++ b/src/profile.sh @@ -1,4 +1,4 @@ -if [[ "$(tty)" == /dev/tty* ]]; then +if [[ "$(tty)" == '/dev/tty'* ]]; then [[ -n "$CDM_SPAWN" ]] && return [[ -z "$DISPLAY$SSH_TTY" ]] && exec cdm fi From 6f01247aee68b5f90325a1ce17c94774fa979207 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 21:14:15 +0800 Subject: [PATCH 08/11] Improve quoting for `serverargs' and `launchflags'. --- src/cdm | 18 +++++++++--------- src/cdmrc | 9 +++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cdm b/src/cdm index 239ca4b..39ac56b 100755 --- a/src/cdm +++ b/src/cdm @@ -70,9 +70,6 @@ fi # Default options. -[[ -z "${binlist[*]}" ]] && binlist=() -[[ -z "${namelist[*]}" ]] && namelist=() -[[ -z "${flaglist[*]}" ]] && flaglist=() dialogrc=${dialogrc:-} countfrom=${countfrom:-0} display=${display:-0} @@ -80,7 +77,10 @@ xtty=${xtty:-7} locktty=${locktty:-no} consolekit=${consolekit:-yes} cktimeout=${cktimeout:-30} -serverargs=${serverargs:-'-nolisten tcp'} +[[ -z "${binlist[*]}" ]] && binlist=() +[[ -z "${namelist[*]}" ]] && namelist=() +[[ -z "${flaglist[*]}" ]] && flaglist=() +[[ -z "${serverargs[*]}" ]] && serverargs=(-nolisten tcp) # Offer all available sessions in /etc/X11/Sessions, # if $binlist if not explicitly set in cdmrc. @@ -172,14 +172,14 @@ case ${flaglist[$binindex]} in vt="$((xtty+display))" fi - serverargs=":${display} $serverargs vt$vt" + serverargs=(":${display}" "${serverargs[@]}" "vt$vt") - $(yesno consolekit) && launchflags="-c -t $cktimeout" - if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then + $(yesno consolekit) && launchflags=(-c -t "$cktimeout") + if cdm-xlaunch "${launchflags[@]}" -- "$bin" -- "${serverargs[@]}"; then + exitnormal + else warn "\`cdm-xlaunch' exited unsuccessfully." exiterror - else - exitnormal fi ;; diff --git a/src/cdmrc b/src/cdmrc index daf4449..07ef72f 100644 --- a/src/cdmrc +++ b/src/cdmrc @@ -45,13 +45,14 @@ xtty=7 # Should cdm(1) stick to the specified display? locktty=no -# Additional arguments to pass to X server, which is usually called as: -# /usr/bin/X :$display $serverargs vt$((xtty+display)) -serverargs='-nolisten tcp' - # Use ConsoleKit for X session? consolekit=yes # Timeout for waiting for X session to register with ConsoleKit. cktimeout=30 +# Additional arguments to pass to X server, which is usually called as: +# /usr/bin/X :$display "${serverargs[@]}" vt$((xtty+display)) +# Arguments with whitespaces should be quoted or escaped. +serverargs=(-nolisten tcp) + From 97a16d4c8889ad64fe1bcb867b35dd62b1d00322 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 21:21:07 +0800 Subject: [PATCH 09/11] cdmrc: notify users to explicitly use `$HOME' instead of `~'. --- src/cdmrc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cdmrc b/src/cdmrc index 07ef72f..8db1fd7 100644 --- a/src/cdmrc +++ b/src/cdmrc @@ -8,10 +8,10 @@ # # (An example:) # binlist=( -# '~/.xsession' # Launch your X session, -# '/bin/bash --login' # or just execute your shell, -# '/usr/bin/fbterm' # or start a frame buffer console, -# '/usr/bin/cdm ~/.submenu.cdmrc' # or go to a submenu :) +# "$HOME/.xsession" # Launch your X session, +# '/bin/bash --login' # or just execute your shell, +# '/usr/bin/fbterm' # or start a frame buffer console, +# "/usr/bin/cdm $HOME/.submenu.cdmrc" # or go to a submenu :) # ) # List all program display names, one-by-one matched with $binlist. From 2e61a0520e773c8d74497396ac8ff914f5324763 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 21:31:44 +0800 Subject: [PATCH 10/11] profile.sh: revert 97afbbf0 for reasons given in comments. --- src/profile.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/profile.sh b/src/profile.sh index dd7716a..3dbdeac 100644 --- a/src/profile.sh +++ b/src/profile.sh @@ -1,5 +1,8 @@ -if [[ "$(tty)" == '/dev/tty'* ]]; then +# To avoid potential situation where cdm(1) crashes on every TTY, here we +# default to execute cdm(1) on tty1 only, and leave other TTYs untouched. +if [[ "$(tty)" == '/dev/tty1' ]]; then [[ -n "$CDM_SPAWN" ]] && return - [[ -z "$DISPLAY$SSH_TTY" ]] && exec cdm + # Avoid executing cdm(1) when X11 has already been started. + [[ -z "$DISPLAY$SSH_TTY$(pgrep xinit)" ]] && exec cdm fi From c63e76c6802a101f6a854393cd407982a1c5aeb3 Mon Sep 17 00:00:00 2001 From: "Casper Ti. Vector" Date: Fri, 9 Nov 2012 23:23:00 +0800 Subject: [PATCH 11/11] Reduce nestedness in processing of `binlist'. Now users can use `~' instead of `$HOME' again. --- src/cdm | 9 +++++---- src/cdmrc | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/cdm b/src/cdm index 39ac56b..6c0b529 100755 --- a/src/cdm +++ b/src/cdm @@ -104,7 +104,7 @@ else # Display selection dialog. binindex=$( DIALOGRC="$dialogrc" dialog --colors --stdout \ - --backtitle "${longname} v${ver}" \ + --backtitle "$longname v$ver" \ --ok-label ' Select ' --cancel-label ' Exit ' \ --menu 'Select session' 0 0 0 "${menu[@]}" ) @@ -116,7 +116,7 @@ fi # Run $bin according to its flag. let binindex-=countfrom -bin="${binlist[${binindex}]}" +bin=($(eval echo "${binlist[${binindex}]}")) case ${flaglist[$binindex]} in # *C*onsole programs. [Cc]) @@ -132,7 +132,7 @@ case ${flaglist[$binindex]} in # ignore SIGINT, which is rather confusing, and cannot be undone. trap - SIGINT SIGTSTP - CDM_SPAWN=$$ exec $bin + CDM_SPAWN=$$ exec "${bin[@]}" ;; # *X* programs. @@ -175,7 +175,8 @@ case ${flaglist[$binindex]} in serverargs=(":${display}" "${serverargs[@]}" "vt$vt") $(yesno consolekit) && launchflags=(-c -t "$cktimeout") - if cdm-xlaunch "${launchflags[@]}" -- "$bin" -- "${serverargs[@]}"; then + if cdm-xlaunch "${launchflags[@]}" -- "${bin[@]}" -- "${serverargs[@]}" + then exitnormal else warn "\`cdm-xlaunch' exited unsuccessfully." diff --git a/src/cdmrc b/src/cdmrc index 8db1fd7..07ef72f 100644 --- a/src/cdmrc +++ b/src/cdmrc @@ -8,10 +8,10 @@ # # (An example:) # binlist=( -# "$HOME/.xsession" # Launch your X session, -# '/bin/bash --login' # or just execute your shell, -# '/usr/bin/fbterm' # or start a frame buffer console, -# "/usr/bin/cdm $HOME/.submenu.cdmrc" # or go to a submenu :) +# '~/.xsession' # Launch your X session, +# '/bin/bash --login' # or just execute your shell, +# '/usr/bin/fbterm' # or start a frame buffer console, +# '/usr/bin/cdm ~/.submenu.cdmrc' # or go to a submenu :) # ) # List all program display names, one-by-one matched with $binlist.