1

I am having one "small" problem :)

If I run command

loginctl list-session

I will get output that will have columns session, uid, user and seat. So I have session here as c6, c2, c4, etc.

Also, I have a process with some id, and I want to know in what session is it running?

for example

cat /proc/<pid>/sessionid

will return some number like 4294967295 which is completely different that loginctl session.

Now my main question here is this: How can I get loginctl session id, from process id.

Yes, I know that I can run this

ps aux | grep -i <pid> | awk '{print $1}'

and get user, and then map this user to loginctl and get session id... but I don't think this is the right solution. For example, is there only one instance of UserA in loginctl? Because I can see that there are few instances of lightdm (x display manager) and I cannot be sure which session id is correct.

And yes, I will implement this in C++ (c++11), so I will also accept c++ answers :)

Thank you.

Regards, golobitch

  • What if some process in a logind session calls setsid ? Then a logind session would contain two kernel sessions. I believe Iogind session is more like a cgroup, I am not quite sure about that when using logind (or say, systemd as your house keeper), it will maintain a strict relationship between kernel sessions and systemd managed cgroups. I also think using dbus to get a pid list of a logind session might be a better idea. – 炸鱼薯条德里克 Sep 18 '18 at 12:52
1

have you tried to pass your ID from /proc/pid/sessionid to loginctl show-session ID But in my case it shows the sam numbers.

someuser@somemachine-test ~ $ cat /proc/self/sessionid
9293

someuser@somemachine-test ~ $ loginctl list-sessions
   SESSION        UID USER             SEAT
      9293      10002 someuser

1 sessions listed.
someuser@somemachine-test ~ $ loginctl show-session 9293
Id=9293
User=10002
Name=someuser
Timestamp=Tue 2018-09-18 13:24:08 CEST
TimestampMonotonic=3614939245544
VTNr=0
Remote=yes
RemoteHost=172.21.98.41
Service=sshd
Scope=session-9293.scope
Leader=8290
Audit=9293
Type=tty
Class=user
Active=yes
State=active
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no

And I can also find it by name of cgourp:

someuser@somemachine-test ~ $ cat /proc/self/cgroup
11:pids:/
10:memory:/user.slice
9:freezer:/
8:hugetlb:/
7:devices:/
6:cpuacct,cpu:/user.slice
5:blkio:/
4:perf_event:/
3:cpuset:/
2:net_prio,net_cls:/
1:name=systemd:/user.slice/user-10002.slice/session-9293.scope

For c++ there is DBus Interface in systemd-logind and also C library sd_login. But unfortunately I don't have any experience with that.

  • Hi Alexander and thank you for you answer. Yes, I tried and passed my id from /proc/<pid>/sessionid to loginctl show-session ID, but no success. I got error "Failed to get session. No session '4294967295' known. But I like your second solution. However, can you point me a little bit in the right direction? What are those numbers in /cgroup (that first numbers (1, 2, 3,...))? I will maybe use this – golobich Sep 18 '18 at 11:51
  • You can find info in man cgroups by search for '/proc/[pid]/cgroup' - man7.org/linux/man-pages/man7/cgroups.7.html – Alexander Sep 18 '18 at 12:23
  • Have you seen this ? It would be easier for you to play with dbus. freedesktop.org/wiki/Software/systemd/logind – Alexander Sep 18 '18 at 12:30
  • Yes Alexander, you are right! I must have overlooked that GetSessionByPID method :) so yes, if I say busctl org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager GetSessionByPID u <pid> then I get object path for proper session. Thank you. I will accept your answer, but please incldue this dbus solution in your answer as well. And yes, I am using sd-bus in C++ – golobich Sep 18 '18 at 14:18
  • 1
    there is also int sd_pid_get_session(pid_t pid, char **session); function in <systemd/sd-login.h> – Alexander Sep 18 '18 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.