WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] how to find active X11 session's user and display?  (Read 9268 times)

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
[Solved] how to find active X11 session's user and display?
« on: November 25, 2020, 06:01:55 AM »
I'm running TCL11 64-bit on an x86_64 laptop. I have   Xorg-7.7.tcz  and  fluxbox.tcz  loaded. Also, I use the  user=bruno  boot command. For the purpose of this exercise (so that two different users are logged in--one in an X session, one in a console), I pressed Ctrl+Alt+F1 and logged in as  tc  in tty1.

Therefore, in this situation, the "right answers" are that the X11 session's user is  bruno  and the display happens to be  :0.0  . How would a shell script find this information?

To my surprise, if I run the  who  command in tty1 I don't even see evidence that user  bruno  is logged in:

Code: [Select]
tc@box:~$ who
tc              tty1            00:00   Nov 25 08:33:42 

The  last  command is similarly unhelpful.

The  ps  command is not helpful, either, because the X server is started by  root  and not by  bruno:
Code: [Select]
bruno@box:~$ ps -ef | grep X
 6462 root     /usr/local/lib/xorg/Xorg -nolisten tcp
17653 bruno    grep X

I found this https://unix.stackexchange.com/a/117099 but in TCL Xorg's PPID is 1 so the display manager approach is not going to work.

If X is running then one can assume that the X session's user is "cat /etc/sysconfig/tcuser" and that the display is ":0.0" but I don't want to make assumptions.

So how would a shell script running in TCL find active X11 session's user and display, without making assumptions?
« Last Edit: November 25, 2020, 01:10:53 PM by Rich »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #1 on: November 25, 2020, 06:11:58 AM »
Hi, GNUser!

The same mess under Xfbdev. Xfbdev started by tc. then exit from console and login as jb. who in console shows jb only. ps in terminal under X shows jb only, unless they are started by tc.

But ps in console shows all processes, run under X, with correct user - tc.
And ps from X terminal shows the same right data.
Newly started terminal under X is started from tc.
 
« Last Edit: November 25, 2020, 06:17:51 AM by jazzbiker »

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
Re: how to find active X11 session's user and display?
« Reply #2 on: November 25, 2020, 06:18:02 AM »
But ps in console shows all processes, run under X, with correct user - tc.
Hi, jazzbiker. It would be convenient if GUI applications were children of X (i.e., if GUI apps' PPID were Xorg's PID) but it seems that Xorg has no children.

I need to find a solution that would work even if script were running in the background and not in an X terminal emulator.
« Last Edit: November 25, 2020, 06:20:12 AM by GNUser »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #3 on: November 25, 2020, 06:20:46 AM »
In my case under Xfbdev newly started terminals are children of my window manager - dwm.
« Last Edit: November 25, 2020, 06:23:51 AM by jazzbiker »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #4 on: November 25, 2020, 06:28:18 AM »
Maybe read /etc/sysconfig/desktop and ps | grep it ?

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
Re: how to find active X11 session's user and display?
« Reply #5 on: November 25, 2020, 06:50:53 AM »
Yes, that would give us the GUI user's name (unlike X, the WM/DE runs as regular user). Can we find a more universal (i.e., non-TCL-specific) solution? You'd think that X itself would be able to give us this information.

Once we have the GUI username, you'd think it would be trivial to find that user's display but these do not work:
Code: [Select]
# su - bruno -c 'echo $DISPLAY'
# su - bruno -c 'env | grep DISPLAY'
« Last Edit: November 25, 2020, 07:04:01 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11225
Re: how to find active X11 session's user and display?
« Reply #6 on: November 25, 2020, 07:09:49 AM »
Hi GNUser
Can multiple users be logged in under Tinycore?
... For the purpose of this exercise (so that two different users are logged in--one in an X session, one in a console), I pressed Ctrl+Alt+F1 and logged in as  tc  in tty1.

 ----- Snip -----

To my surprise, if I run the  who  command in tty1 I don't even see evidence that user  bruno  is logged in:

Code: [Select]
tc@box:~$ who
tc              tty1            00:00   Nov 25 08:33:42
...
Now, if you hit  Ctrl+Alt+F2  and open a new terminal, does  who  show bruno is still logged in?

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #7 on: November 25, 2020, 07:10:50 AM »
Ok, the second in order logged user can't penetrate into the first in order user's environment. You will need separate process inside X which will answer its $DISPLAY into some file (preliminarily opened) in case some signal (USR2?) will be recieved.

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #8 on: November 25, 2020, 07:15:56 AM »
Hi, Rich!

Isn't it funny?:
Code: [Select]
tc@box:~$ whoami
tc
tc@box:~$ who
jb              tty1            00:11   Nov 25 16:07:39
tc@box:~$

It's citation from within Xfbdev terminal. Probably it is busybox's jokes.

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
Re: how to find active X11 session's user and display?
« Reply #9 on: November 25, 2020, 07:17:42 AM »
Hi, Rich. Yes, it seems multiple users can be logged in.

If I press Ctrl+Alt+F1 and login as  tc  (for the purposes of demonstration), then press Ctrl+Alt+F2 to return to my desktop, then launch a GUI terminal emulator, here's what I see when I run "who":

Code: [Select]
bruno@box:~$ who
tc              tty1            00:31   Nov 25 09:35:49 
bruno           pts/0           00:00   Nov 25 10:12:55  :0.0

If I leave that terminal emulator open, then open another one, I get this:
Code: [Select]
bruno@box:~$ who
tc              tty1            00:31   Nov 25 09:35:49 
bruno           pts/0           00:00   Nov 25 10:12:55  :0.0
bruno           pts/1           00:00   Nov 25 10:13:09  :0.0

So it seems that each terminal emulator in X runs a login shell.

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
Re: how to find active X11 session's user and display?
« Reply #10 on: November 25, 2020, 07:33:13 AM »
I can confirm jazzbiker's strange observation.

I created this shell script and bound it to a keyboard shortcut:

Code: [Select]
#!/bin/sh
who >>/home/bruno/log.txt
echo "" >>/home/bruno/log.txt
whoami >>/home/bruno/log.txt

This is what /home/bruno/log.txt looks like:
Code: [Select]
tc              tty1            00:45   Nov 25 09:35:49 

bruno

This is very strange. I know for a fact that I (user "bruno") am using my desktop inside of Xorg running in tty2. Yet "who" does not even see me as being logged in.

« Last Edit: November 25, 2020, 07:34:48 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11225
Re: how to find active X11 session's user and display?
« Reply #11 on: November 25, 2020, 07:38:36 AM »
Hi Hi GNUser
How about:
Code: [Select]
tc@E310:~$ busybox who -a
tc              tty1            00:33   Nov 18 09:43:22 
tc              pts/0           09:40   Nov 18 09:43:34  :0.0
tc              pts/1           11:18   Nov 23 00:22:40  :0.0
tc              pts/2           old     Nov 18 09:44:27  :0.0
tc              pts/3           00:00   Nov 25 10:35:32  :0.0
tc              pts/4           ?       Nov 24 10:00:36  :0.0
tc              pts/5           ?       Nov 23 19:42:19  :0.0
tc              pts/6           ?       Nov 24 09:11:00  :0.0
tc              pts/7           ?       Nov 24 09:56:31  :0.0

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1345
Re: how to find active X11 session's user and display?
« Reply #12 on: November 25, 2020, 07:45:08 AM »
Hi, Rich. I get the same output as before.

Try closing all your GUI terminal emulators in your X session (in tty2). Then switch to tty1 and run "busybox who -a" and you'll see that, as far as "who" is concerned, there's nobody logged into tty2.

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #13 on: November 25, 2020, 07:46:26 AM »
Foe me nothing changed
Code: [Select]
tc@box:~$ whoami
tc
tc@box:~$ busybox who -a
jb              tty1            00:00   Nov 25 16:07:39
tc@box:~$
Xfbdev, dwm, citation from newly openet terminal

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: how to find active X11 session's user and display?
« Reply #14 on: November 25, 2020, 08:15:54 AM »
Jokes continues. X started as tc, then in tty1 tc exited and logined as jb. Then in X opened new terminal:
Code: [Select]
tc@box:~$ su - jb -c 'echo $DISPLAY'
Password:

tc@box:~$ echo $DISPLAY
:0.0

in tty1
Code: [Select]
jb@box:~$ echo $DISPLAY

jb@box:~$ su - tc -c 'echo $DISPLAY'

jb@box:~$
tc can acces jb's environment, but jb can't access tc's.