LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 01-12-2009, 02:44 PM   #1
Romanus81
Member
 
Registered: Feb 2008
Posts: 201

Rep: Reputation: 31
/etc/acpi/acpi_handler.sh and running a confirmation script.


I wrote a script that uses $DISPLAY to detect if the user is currently in an X session or a terminal (tty1-6), so that I can press my power button and get either a message in a terminal or a popup in X that asks weather I want to shutdown, suspend, reboot, or hibernate. I told the /etc/acpi/acpi_handler.sh to run this script so that when I press my power button it will ask me what I want to do, but it doesn't seem to work. I commented out all the stuff that was in the acpi script already (the case $1 in button, etc.) and just put /usr/bin/powerscript, thinking that would work for all cases.
Just typing powerscript in a terminal runs the program as expected in X and at the tty's. What could I do to make this work?
 
Old 01-16-2009, 02:28 AM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
I think I know the problem is, but I don't know the solution.

When the acpi handler runs the script, it is not run in the context of a logged in user. In other words, it knows the button (or key) has been pressed but it doesn't know "who" pressed it. I would think there would be a way to determine which VT or GUI is currently displayed, but I don't know what it is. Once you have that information, presumably you could send your response to the correct location in the correct format.
 
Old 01-16-2009, 09:55 AM   #3
malo_umoran
Member
 
Registered: Dec 2003
Distribution: Desktop: Slackware 13.1 &13.37 | Server: Debian 6.0
Posts: 270

Rep: Reputation: 32
try this:

export DISPLAY=:0.0
export XAUTHORITY=/home/$USER/.Xauthority
your_command_here


or this as an another example, although first one should work:

export USER=xxx
su - $USER -c "env DISPLAY=:0.0 HOME=/home/$USER your_command_here"


some applications might need exported DBUS_SESSION_BUS_ADDRESS. you can find it in /proc/$pid/eviron ($pid is pid of gnome-session or startkde: pgrep -u $USER gnome-session").
 
Old 01-16-2009, 05:04 PM   #4
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
@malo_umoran

The problem with your suggestions is the OP doesn't whether the user is using a virtual terminal or a GUI (and there is, at least in principle, the possibility that more than one X session is running), and for the second suggestion, does not know who the user is. Do you have any idea how to obtain that info? If the first question can be answered I think the second would be fairly easy. I scrounged around for a little bit and didn't find anything.
 
Old 01-16-2009, 11:27 PM   #5
guanx
Senior Member
 
Registered: Dec 2008
Posts: 1,014

Rep: Reputation: 146Reputation: 146
If you really want such things, a simple way is to create a pipe (mkfifo) and read from it. In acpi_handler you write something to the pipe, then the program reading the pipe knows and could display a dialog.
 
Old 01-19-2009, 07:50 AM   #6
malo_umoran
Member
 
Registered: Dec 2003
Distribution: Desktop: Slackware 13.1 &13.37 | Server: Debian 6.0
Posts: 270

Rep: Reputation: 32
I assumed that he knows who is the user. I am using this approach on my notebook and I am the only user.


Quote:
Originally Posted by blackhole54 View Post
The problem with your suggestions is the OP doesn't whether the user is using a virtual terminal or a GUI
I am doing it with "fgconsole" without any problems for all my keys (start wireless, start ethernet, suspend to ram/disk, power off, turn on fan/usb...)

Code:
if [[ `fgconsole` -lt 7 ]]; then
  echo -n 0
else
  echo -n `zenity --question --title="Question" --text="$question"`
fi
If the VT is less than 7, script is called from the console, not from the X server. Usually you could use "$TERM" (if it is "linux", it "has to be" on the console, not from X), but $TERM is here from acpid, not the user.


If you start X with startx on the console, you tty7 = vt7 and you should be able to find it with "ps". But if you use GDM/KDM, it is always owned by the root. hmmmm ... still thinking about it

there should be some way to find out who is the owner of VT7 or tty7...
 
Old 01-20-2009, 04:32 AM   #7
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by malo_umoran View Post
I am doing it with "fgconsole" ...
fgconsole was the missing ingredient that I didn't know about. Thanks.

Once you've identified the fg console as being a GUI, you can (I think) search the /tmp/.X${DISPLAY}-lock file(s) for process numbers and use the arguments for these processes to map between console numbers and $DISPLAY numbers. You can find the logged in user (if any) of $DISPLAY using who. (Hmmm. Is there a more straight forward way?)

But I fear we have lost the OP. I probably won't be posting back until somebody who actually wants this problem solved shows up.

Last edited by blackhole54; 01-20-2009 at 04:34 AM.
 
Old 01-20-2009, 05:28 AM   #8
malo_umoran
Member
 
Registered: Dec 2003
Distribution: Desktop: Slackware 13.1 &13.37 | Server: Debian 6.0
Posts: 270

Rep: Reputation: 32
Quote:
You can find the logged in user (if any) of $DISPLAY using who. (Hmmm. Is there a more straight forward way?)
perfect ... I forgot about who. I was playing with "ps" and parsing process params like this to find the right DISPLAY.
Code:
vtnr=`fgconsole`
XDISPLAY=`ps x | grep vt$vtnr | grep -v grep | sed -re 's/(.*\/X.*)\s(:[1234567890]+(\.[1234567890]+)?)/\2/'`
this is working to find DISPLAY, but it did not help to find the owner of that DISPLAY.

but "who" makes it even simpler, because you can do everything with "who":

Code:
vtnr=`fgconsole`
XDISPLAY=`who | grep tty$vtnr | sed -re 's/(.*\()(:.*)(\).*)/\2/'`
XUSER=`who | grep tty$vtnr | awk '{ print $1 }'`
XHOME=`grep ^$XUSER: /etc/passwd | tr ' ' '-' | tr ':' ' ' | cut -f6 -d' '`

export DISPLAY=$XDISPLAY
export XAUTHORITY=/home/$XUSER/.Xauthority
export HOME=$XHOME

your_command_here
I changed my acpi_handler script to work with this. Very nice . Now is possible to set list of allowed users in acpi_handler not to let everyone use keys

Last edited by malo_umoran; 01-20-2009 at 05:49 AM.
 
Old 01-21-2009, 12:35 AM   #9
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by malo_umoran View Post
... because you can do everything with "who":
Hugh. My version of who doesn't report $vtnr for GUIs, just DISPLAY.

Code:
jim@ratel:~$ who
onion    pts/0        Jan 20 22:29 (localhost:10.0)
jim      pts/1        Jan 20 23:23 (192.168.XX.XX)
somebody :0           Jan 20 23:20

jim@ratel:~$ ps -wwf $(cat /tmp/.X0-lock)
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
root     11409 11404  0 23:20 tty7     SLs+   0:00 /usr/X11R6/bin/X :0 -br -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7

EDIT: Perhaps I should mention this is from Ubuntu 6.10 (I picked this up on the zero reply thread ):

Code:
jim@ratel:~$ who --version
who (GNU coreutils) 5.96
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Joseph Arceneaux, David MacKenzie, and Michael Stone.

Last edited by blackhole54; 01-21-2009 at 12:43 AM.
 
Old 01-21-2009, 03:52 AM   #10
malo_umoran
Member
 
Registered: Dec 2003
Distribution: Desktop: Slackware 13.1 &13.37 | Server: Debian 6.0
Posts: 270

Rep: Reputation: 32
I wrote the script on Slackware, but it also works on Ubuntu 8.04. I just tested it.

Code:
yy@max:~/Desktop$ who
yy       tty7         2008-09-04 10:27 (:0)
yy       pts/0        2008-09-04 10:27 (:0.0)
yy       pts/1        2008-10-29 13:15 (:0.0)
yy       pts/2        2008-12-22 14:15 (:0.0)
yy       pts/3        2008-11-05 11:29 (:0.0)
yy       pts/4        2008-11-05 15:20 (:0.0)
who (GNU coreutils) 6.10

I wrote it with who 6.9 on Slackware 12.1.


I have no idea why do you get something like this from who ... maybe just an old version and it will work with a new one?!

192.168. looks like some kind of remote login on your machine.
 
Old 01-21-2009, 10:25 AM   #11
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by malo_umoran View Post
192.168. looks like some kind of remote login on your machine.
Indeed. It was the easiest way for me to run the test! It was a headless machine . Here are the same results, same software, different machine, no remote login (forgive lack of showing command; I just redirected output to file on key drive; same on subsequent results from live CD):

Code:
jim      :0           2009-01-21 08:47
jim      pts/0        2009-01-21 08:48 (:0.0)

Quote:
I have no idea why do you get something like this from who ... maybe just an old version and it will work with a new one?!
Apparently. Below are results on same machine as above (this post) but running live CD of Ubuntu 8.04, which I just happened to have lying around. Afterwards I give output of same who command with --version parameter:

Code:
ubuntu   tty4         2009-01-21 15:53
ubuntu   tty5         2009-01-21 15:53
ubuntu   tty2         2009-01-21 15:53
ubuntu   tty3         2009-01-21 15:53
ubuntu   tty6         2009-01-21 15:53
ubuntu   tty1         2009-01-21 15:54
ubuntu   tty7         2009-01-21 15:54 (:0)
ubuntu   pts/0        2009-01-21 15:56 (:0.0)


who (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Joseph Arceneaux, David MacKenzie, and Michael Stone.
With respect to my previous comment about absense of OP, I wasn't planning on posting back. I broke radio silence to let others know there might an issue wrt the version of who.

Last edited by blackhole54; 01-21-2009 at 11:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
acpid, acpi_handler.sh mexx20 Slackware 3 01-06-2009 07:10 PM
Running 'xfce-session-logout' from acpi script ewolf Linux - Software 3 12-06-2007 11:05 AM
ACPI: 2.6.17.7; nothing in /proc/acpi/fan; fans not running; problem not seen before. zetabill Linux - Kernel 1 07-30-2006 04:27 PM
shell script asking for confirmation? bikov_k Programming 6 11-04-2005 02:16 AM
Bash script - confirmation option satimis Programming 4 10-21-2004 07:29 AM


All times are GMT -5. The time now is 08:32 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration