3

Based on this thread I modified my /etc/acpi/powerbtn.sh script to account for LXDE sessions:

# getXuser gets the X user belonging to the display in $displaynum.
# If you want the foreground X user, use getXconsole!
getXuser() {
        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
        if [ x"$user" = x"" ]; then
                startx=`pgrep -n startx`
                if [ x"$startx" != x"" ]; then
                        user=`ps -o user --no-headers $startx`
                fi
        fi
        if [ x"$user" != x"" ]; then
                userhome=`getent passwd $user | cut -d: -f6`
                export XAUTHORITY=$userhome/.Xauthority
        else
                export XAUTHORITY=""
        fi
        export XUSER=$user
}

 if [ -n $(pidof lxsession) ]; then
    for x in /tmp/.X11-unix/*; do
       displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
       getXuser;
       if [ x"$XAUTHORITY" != x"" ]; then
           export DISPLAY=":$displaynum"
           export _LXSESSION_PID=`pidof lxsession`
           lxsession-logout
           exit
       fi
    done
 fi

# If all else failed, just initiate a plain shutdown.
/sbin/shutdown -h now "Power button pressed"

The script works fine when run in a terminal - both as a user and root - but won't work if run by ACPID.

The only case the script is triggered by ACPI is when I have a root session open in gnome-terminal.

Do you have any idea what could be wrong? Is there any other info I can provide that could help you figure out what's going on?

I tried setting the environment variables manually but if I do so the script only works until the first time I launch a command with root.


System info:

  • Ubuntu 12.04.2 LTS

  • single-user

  • running LXDE/Openbox


Edit:

I ran some diagnostics and found out that both XUSER and XAUTHORITY remain empty when run by ACPID. No idea why, though.

Glutanimate
  • 2,168
  • 4
  • 22
  • 38
  • 2
    Check `$PATH`. As `acpid` is started during init, it's going to have limited environment variables. one of the utilities you're using could be somewhere not included in `$PATH`. For additional debug, I'd add `exec &>> /tmp/powerbtn.log` (if bash or compatible) to the beginning of the script. This will send all the script's output to `/tmp/powerbtn.log`. – phemmer Aug 18 '13 at 19:12
  • @Patrick thanks for the advice! `exec &>> /tmp/powerbtn.log` didn't work for some reason but I was able to to redirect the content of the key variables into a similar log file and debug it that way. Care to look at my workaround? I am not quite sure if it might create more problems. Thanks again! – Glutanimate Aug 23 '13 at 11:51

1 Answers1

0

So after some debugging work I was finally able to trace the problem back to the user detection by pinky. For some odd reason pinky -fw won't list the user display under normal circumstances. Only after starting a root session it's able to detect the correct display:

# DEBUG OUTPUT WITHOUT ROOT SESSION

##################
displaynum: 0  # correct
##################
pinkyfw: bob       tty7     04:09  Aug 18 17:59
pinky: Login    Name                 TTY      Idle   When         Where
bob      Bob                  tty7     04:09  Aug 18 17:59              # notice the missing
##################                                                      # information on display used
pinkytest: bob # testing a workaround
user: # empty because awk didin't find a match for ":0" in pinky -fw
##################

# DEBUG OUTPUT WITH ROOT SESSION

##################
displaynum: 0 # correct
##################
pinkyfw: bob       tty7     04:04  Aug 18 17:59
bob       pts/3           Aug 18 21:59 :0
pinky: Login    Name                 TTY      Idle   When         Where
bob      Bob                  tty7     04:04  Aug 18 17:59
bob      Bob                  pts/3           Aug 18 21:59 :0          # after starting a root session
##################
pinkytest: bob
user: bob # awk found a match for ":0"
##################

# DEBUG OUTPUT WITHOUT ROOT SESSION, WORKAROUND APPLIED

##################
displaynum: 0  # correct
##################
pinkyfw: bob       tty7     04:09  Aug 18 17:59
pinky: Login    Name                 TTY      Idle   When         Where
bob      Bob                  tty7     04:09  Aug 18 17:59              
##################                                                      
pinkytest: bob 
user: bob
##################

This is the workaround I applied:

getXuser() {
        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
        if [ x"$user" = x"" ]; then
                startx=`pgrep -n startx`
                if [ x"$startx" != x"" ]; then
                        user=`ps -o user --no-headers $startx`
                fi
        fi
        if [ x"$user" = x"" ]; then                           # lines added
               user=$(pinky -fw | awk '{ print $1; exit; }')  # lines added
        fi                                                    # lines added
        if [ x"$user" != x"" ]; then
                userhome=`getent passwd $user | cut -d: -f6`
                export XAUTHORITY=$userhome/.Xauthority
        else
                export XAUTHORITY=""
        fi
        export XUSER=$user
}

Unfortunately I don't now enough about pinky and user management under Linux to be able to tell if this workaround might create even more problems. I guess it's better than just hard-coding the user name and display into the file (which didn't even work when I tried).

Glutanimate
  • 2,168
  • 4
  • 22
  • 38