You are not logged in.

#1 2012-10-20 20:26:56

Psycho_zs
Member
Registered: 2010-05-24
Posts: 18

Is there a universal way to identify X session from the inside?

Hi everyone!

I wrote some bash daemons designed to be started by xdg autostart mechanism.
The problem is: unlike X clients, bash scripts do not exit on logout from X session or when X is killed.
subproblem: script needs to do some housekeeping when exiting.

I'm looking for DE/DM-independent way of telling if current user X session still exist, or not. Or a way to catch the end of session.

Testing for $DISPLAY is meaningless
Testing for X PID is unreliable (for example, xdm reuses same X process for itself and user sessions)
Not every session manager gives its PID to child processes as PPID.
env of script started via xdg autostart differs across DEs/DMs.

I've made a very dirty workaround: start empty dummy conky and use it as an indicator that session still exist.
But there should be a cleaner way!

Offline

#2 2012-10-21 06:05:20

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 218

Re: Is there a universal way to identify X session from the inside?

I have fbpanel installed on my system.  Included with this package is a script named 'xlogout' see below.  Perhaps there is something in this that may help you.

#!/bin/bash

# xlogout - logs user out of its X session
# Linux specific since uses /proc

# get display name without screen number
[ -z "$DISPLAY" ] && exit 1
DPY=${DISPLAY:1}
DPY=${DPY/.*/}
echo "DPY=${DPY}"

# get X pid
XPID=`< /tmp/.X${DPY}-lock`
XPID=`echo $XPID`
echo "XPID=$XPID"

# get pid of xdm (or gdm, kdm, etc). usually it's parent of X
XDMPID=`ps -o ppid --pid=$XPID | awk '{if (FNR != 1) print $1}'`
echo "XDMPID=$XDMPID"

# recursivly find child of xdm that was started in home dir - 
# it's user's session start up script
function pid_scan()
{

    rm -f $PF
    while [ $# != 0 ]; do
        ps --no-headers -o pid --ppid=$1 >> $PF
        shift
    done
    for pid in `< $PF`; do
        if cwd=`ls -al /proc/$pid/cwd 2>/dev/null`; then
            cwd=`sed 's/.*-> //' <<< $cwd`
            [ "$cwd" == "$HOME" ] && echo $pid && return
        fi
    done
    pids=`< $PF`
    [ -n "$pids" ] && pid_scan `< $PF`;
}

PF=/tmp/$$-pids
SPID=`pid_scan $XDMPID`
rm -f $PF

[ -z "$SPID" ] && exit 1
echo "Session start up script"
ps -o uid,pid,ppid,sess,cmd --pid $SPID
kill -SIGTERM -$SPID

Offline

#3 2012-10-21 06:44:20

brebs
Member
Registered: 2007-04-03
Posts: 3,427

Re: Is there a universal way to identify X session from the inside?

Psycho_zs wrote:

or when X is killed.

That should be easy. Just use a wrapper for startx, e.g. ~/bin/startx

/usr/bin/startx
do-whatever-you-want-at-end

Offline

#4 2012-10-21 07:15:19

Psycho_zs
Member
Registered: 2010-05-24
Posts: 18

Re: Is there a universal way to identify X session from the inside?

rockin turtle wrote:

Included with this package is a script named 'xlogout'

Wow! that's a nice one! Works even when no DM is used, detects xinitrc as $XDMPID.

brebs wrote:

Just use a wrapper for startx.

That`s not from the inside.

Offline

Board footer

Powered by FluxBB