On Thu, Apr 13, Christian Boltz wrote:
Am Dienstag, 11. April 2023, 21:46:45 CEST schrieb Thorsten Kukuk:
And a very complicated way to find out, when the user did login :( wtmp is not a reliable way to find out when the current session started, why not using utmp? Or better logind?
My best guess is that aa-notify existed years before logind, and so far reading /var/log/wtmp worked ;-)
utmp probably already existed back then, but I don't know why the original author of aa-notify decided to use wtmp instead.
Even I'm not old enough ;) to know what was first, utmp or wtmp. But since the struct is called "utmp" and not "wtmp", I would assume that utmp is older, or they were introduced at the same time. Maybe he did not use utmp, as the entry get's removed if the user logs out, which is not the case with wtmp? But this would mean it's possible to use the "-l" option for a different user then the one calling the command?
As far as I understand the code, it uses the current login time, not the last one. And the best, robust and correct source for this is systemd-logind. There are several interfaces (DBUS, libsystemd), but I don't know if there is a python interface.
In worst case, it shouldn't be too hard to check the output of a command if it provides json output (which systemd commands usually do).
I had a look at loginctl - list-sessions looks somewhat useful, but unfortunately doesn't include the login time. user-status has a "Since:" line, but unfortunately ignores "-o json" :-(
If I choose the commandline way with json output, do you have a recommendation which command I should use?
To be honest: no idea. I'm always using libsystemd, and systemd v254 will come with a function "sd_uid_get_login_time" for me ;) I don't know if there are python bindings for libsystemd, else maybe they could be created for this function? Or write a C helper application, which does nothing else then calling this function and prints the result in the format you need?
With DBUS, it looks like I'll need /org/freedesktop/login1 ListSessions and/or /org/freedesktop/login1/user/_$UID Timestamp (assuming I understood man org.freedesktop.login1 right)
To be fair: I don't understand the DBUS API of systemd yet...
Or if you really need to use wtmp (which is not reliable), you can use sqlite and query the wtmp.db directly. This should be a very quick and simple task in python, much easier than parsing the wtmp mess...
Agreed about the wtmp parsing mess. OTOH, I'm not too keen to depend on the table layout of the wtmp.db - even if I'd hope that it doesn't change too often, IIRC I've read that it's not recommended to query this database directly, and that the database layout might change.
We could of course do the same above ideas from me for libsystemd with libwtmpdb, but in that case, I would suggest to use libsystemd, that's more reliable. Thorsten -- Thorsten Kukuk, Distinguished Engineer, Senior Architect, Future Technologies SUSE Software Solutions Germany GmbH, Frankenstraße 146, 90461 Nuernberg, Germany Managing Director: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)