Saturday, April 27, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 4482  / 2 Years ago, sat, february 26, 2022, 11:51:01

I would like to use a script to find out the username - of the user who is currently on the physical console (who has command of the keyboard, mouse and graphical display)?



I do not just want to know who runs X (as the current console can be switched to another terminal and another user).






To clarify:

Commands like w, who etc. show who is logged at which terminal. This is simple. I would like to know, however, who owns currently the physical console (display, keyboard etc.). For example, if two users run displays :0 and :1, and I can switch between the displays with ctrl+alt+7 and ctrl+alt+8, I would like to know which user currently has the connection between his or her display and the actual, physical screen.






One more clarification:

I am not interested in knowing my own username. I have a script, running as another user or root, that needs to know who uses the physical display. There can be several X sessions running by different users, some of them virtualized, some of them connected to a remote X server, but I am only interested in the one that is actually displaying output on the monitor.






Yet another clarification:

Whatever the command is, the output must be different after pressing Ctrl+Alt+F1 (switching to another virtual console).


More From » command-line

 Answers
0

I'm afraid I can't give you a specific answer (because I don't know the tech well), but I believe you can find it out using D-Bus and ConsoleKit.



For example, when I switch from X to a VT and back, I get this listening on dbus-monitor:



$ dbus-monitor --system | grep ConsoleKit
signal sender=:1.16 -> dest=(null destination) serial=19039 path=/org/freedesktop/ConsoleKit/Session2; interface=org.freedesktop.ConsoleKit.Session; member=ActiveChanged
signal sender=:1.16 -> dest=(null destination) serial=19040 path=/org/freedesktop/ConsoleKit/Seat1; interface=org.freedesktop.ConsoleKit.Seat; member=ActiveSessionChanged
signal sender=:1.16 -> dest=(null destination) serial=19041 path=/org/freedesktop/ConsoleKit/Session2; interface=org.freedesktop.ConsoleKit.Session; member=ActiveChanged
signal sender=:1.16 -> dest=(null destination) serial=19042 path=/org/freedesktop/ConsoleKit/Seat1; interface=org.freedesktop.ConsoleKit.Seat; member=ActiveSessionChanged
string "/org/freedesktop/ConsoleKit/Session2"


Specifically, you could use the GetActiveSession method. Here's how with dbus-send (may require sudo):



$ dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Seat1 org.freedesktop.ConsoleKit.Seat.GetActiveSession


Then, depending on your application, you'll want to check out the returned object's properties, which should be an object path to a Session.


[#35250] Monday, February 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ndeecru

Total Points: 109
Total Questions: 128
Total Answers: 117

Location: Czech Republic
Member since Thu, Aug 11, 2022
2 Years ago
;