SHLVL=2 in outermost shell in unity terminals (REGRESSION)

Bug #1707977 reported by jimav
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
unity (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

The bash variable SHLVL is 2, rather than 1, in the outermost shell process in terminal emulators (gnome-terminal, xterm, etc. -- it doesn't matter). This makes SHLVL useless, and breaks many scripts (run from .bashrc etc.) which look for SHLVL=1 to save/restore state of some kind.

This is a painful REGRESSION since 16.04.

SHLVL=1 in pty terminals (Ctrl-Alt-F1 etc.) so the problem is not in bash.

The parent of the "outer most" shell (which has SHLVL=2) is not another bash instance,
but the terminal emulator (gnome-terminal-server, xterm, etc.). This is expected.
Since the problem exists with various terminal emulators, the bug must be in generic gui initialization or gui-app startup code (not in gnome-terminal or xterm).

=> Speculation: Somewhere in the initial GUI startup code there is a script executed by bash which starts long-running processes (systemd?) and then exits. SHLVL=1 is inherited in the environment by the child, and so any shell the child later spawns set SHLVL=2. If that's the case, then it might suffice to "unset SHLVL" before starting the child(ren). Again, this is just speculation.

ProblemType: Bug
DistroRelease: Ubuntu 17.04
Package: unity 7.5.0+17.04.20170407.1-0ubuntu1
ProcVersionSignature: Ubuntu 4.10.0-28.32-generic 4.10.17
Uname: Linux 4.10.0-28-generic x86_64
NonfreeKernelModules: nvidia_uvm nvidia_drm nvidia_modeset nvidia
.proc.driver.nvidia.gpus.0000.03.00.0: Error: [Errno 21] Is a directory: '/proc/driver/nvidia/gpus/0000:03:00.0'
.proc.driver.nvidia.registry: Binary: ""
.proc.driver.nvidia.version:
 NVRM version: NVIDIA UNIX x86_64 Kernel Module 375.66 Mon May 1 15:29:16 PDT 2017
 GCC version: gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2)
.proc.driver.nvidia.warnings.fbdev:
 Your system is not currently configured to drive a VGA console
 on the primary VGA device. The NVIDIA Linux graphics driver
 requires the use of a text-mode VGA console. Use of other console
 drivers including, but not limited to, vesafb, may result in
 corruption and stability problems, and is not supported.
.tmp.unity_support_test.0:

ApportVersion: 2.20.4-0ubuntu4.5
Architecture: amd64
BootLog: WDStockRoot: clean, 408304/4890624 files, 6257387/19531008 blocks
CompizPlugins: No value set for `/apps/compiz-1/general/screen0/options/active_plugins'
CompositorRunning: compiz
CompositorUnredirectDriverBlacklist: '(nouveau|Intel).*Mesa 8.0'
CompositorUnredirectFSW: true
CurrentDesktop: Unity:Unity7
Date: Tue Aug 1 10:07:13 2017
DistUpgraded: 2017-07-26 09:01:41,817 DEBUG Running PostInstallScript: './xorg_fix_proprietary.py'
DistroCodename: zesty
DistroVariant: ubuntu
DkmsStatus:
 bbswitch, 0.8, 4.10.0-28-generic, x86_64: installed
 bbswitch, 0.8, 4.4.0-87-generic, x86_64: installed
 nvidia-375, 375.66, 4.10.0-28-generic, x86_64: installed
DpkgLog:

GconfCompiz:
 /apps/compiz-1/plugins:
   /apps/compiz-1/plugins/grid:
    /apps/compiz-1/plugins/grid/screen0:
     /apps/compiz-1/plugins/grid/screen0/options:
      top_edge_action = 0
GraphicsCard:
 NVIDIA Corporation GK208 [GeForce GT 630 Rev. 2] [10de:1284] (rev a1) (prog-if 00 [VGA controller])
   Subsystem: ZOTAC International (MCO) Ltd. GK208 [GeForce GT 630 Rev. 2] [19da:1308]
InstallationDate: Installed on 2017-01-19 (194 days ago)
InstallationMedia: Ubuntu 16.04.1 LTS "Xenial Xerus" - Release amd64 (20160719)
MachineType: System manufacturer System Product Name
ProcKernelCmdLine: BOOT_IMAGE=/boot/vmlinuz-4.10.0-28-generic root=UUID=cc35a925-d0d6-4331-b791-4d52605d5a8d ro quiet splash vt.handoff=7
SourcePackage: unity
UpgradeStatus: Upgraded to zesty on 2017-07-26 (6 days ago)
dmi.bios.date: 09/21/2010
dmi.bios.vendor: American Megatrends Inc.
dmi.bios.version: 1408
dmi.board.asset.tag: To Be Filled By O.E.M.
dmi.board.name: P6T
dmi.board.vendor: ASUSTeK Computer INC.
dmi.board.version: Rev 1.xx
dmi.chassis.asset.tag: Asset-1234567890
dmi.chassis.type: 3
dmi.chassis.vendor: Chassis Manufacture
dmi.chassis.version: Chassis Version
dmi.modalias: dmi:bvnAmericanMegatrendsInc.:bvr1408:bd09/21/2010:svnSystemmanufacturer:pnSystemProductName:pvrSystemVersion:rvnASUSTeKComputerINC.:rnP6T:rvrRev1.xx:cvnChassisManufacture:ct3:cvrChassisVersion:
dmi.product.name: System Product Name
dmi.product.version: System Version
dmi.sys.vendor: System manufacturer
version.compiz: compiz 1:0.9.13.1+17.04.20170109-0ubuntu1
version.ia32-libs: ia32-libs N/A
version.libdrm2: libdrm2 2.4.76-1
version.libgl1-mesa-dri: libgl1-mesa-dri 17.0.3-1ubuntu1
version.libgl1-mesa-dri-experimental: libgl1-mesa-dri-experimental N/A
version.libgl1-mesa-glx: libgl1-mesa-glx 17.0.3-1ubuntu1
version.nvidia-graphics-drivers: nvidia-graphics-drivers-* N/A
version.xserver-xorg-core: xserver-xorg-core 2:1.19.3-1ubuntu1.1
version.xserver-xorg-input-evdev: xserver-xorg-input-evdev 1:2.10.5-1ubuntu1
version.xserver-xorg-video-ati: xserver-xorg-video-ati 1:7.9.0-0ubuntu1
version.xserver-xorg-video-intel: xserver-xorg-video-intel 2:2.99.917+git20170309-0ubuntu1
version.xserver-xorg-video-nouveau: xserver-xorg-video-nouveau 1:1.0.14-0ubuntu1
xserver.bootTime: Tue Aug 1 09:56:34 2017
xserver.configfile: default
xserver.devices:
 input Power Button KEYBOARD, id 6
 input Power Button KEYBOARD, id 7
 input Heng Yu Technology Deck 87 Francium Pro KEYBOARD, id 8
 input Heng Yu Technology Deck 87 Francium Pro KEYBOARD, id 9
 input ImPS/2 Generic Wheel Mouse MOUSE, id 10
xserver.errors:

xserver.logfile: /var/log/Xorg.0.log
xserver.outputs:

xserver.version: 2:1.19.3-1ubuntu1.1

Revision history for this message
jimav (james-avera) wrote :
Revision history for this message
jimav (james-avera) wrote :

I hope this bug will be fixed before the next release. But meanwhile, here is a nasty workaround which can be used in bash startup scripts:

i_am_toplevel() {
  case "$SHLVL" in
    1) return 0 ;;
    3) return 1 ;; # (optimize this case)
    2) # HACK FOR Ubuntu 16.10, 17.04 ... where SHLVL=2 at top
       if egrep -s '(terminal-server|^xterm)' /proc/$PPID/cmdline ; then
         return 0
       fi
       ;;
  esac
  return 1
}

Then say "if i_am_toplevel ; then ... "
instead of "if [ $SHLVL -eq 1 ] ..."

Revision history for this message
jimav (james-avera) wrote :

Oh joy, a bug in the work-around (and no way to edit the post).
Please add "-a" to the egrep command line to avoid a warning due to the nul terminator in cmdline.

CORRECTED work-around for .bashrc :

i_am_toplevel() {
  case "$SHLVL" in
    1) return 0 ;;
    3) return 1 ;;
    2) # HACK FOR Ubuntu 16.10, 17.04 ... where SHLVL=2 at top
       if egrep -as '(terminal-server|^xterm)' /proc/$PPID/cmdline ; then
         return 0
       fi
       ;;
  esac
  return 1
}

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in unity (Ubuntu):
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.