]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
wall: query logind for list of users with tty (#2088)
authorThorsten Kukuk <kukuk@suse.com>
Wed, 31 May 2023 06:55:33 +0000 (08:55 +0200)
committerThorsten Kukuk <kukuk@suse.com>
Wed, 31 May 2023 06:55:33 +0000 (08:55 +0200)
configure.ac
meson.build
term-utils/Makemodule.am
term-utils/wall.c

index d631b062d280ee31cc05a69464d979641972480c..17d3ab703b4f7293f29e3e87b3819c78071e7e7a 100644 (file)
@@ -2491,6 +2491,7 @@ AS_IF([test "x$with_systemd" != xno], [
     [*:yes],
        AC_DEFINE([HAVE_LIBSYSTEMD], [1], [Define if libsystemd is available])
        AC_DEFINE([USE_SYSTEMD], [1], [Define if systemd support is wanted ])
+       AC_CHECK_DECLS([sd_session_get_username], [], [], [#include <systemd/sd-login.h>])
   )
 ])
 AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$have_systemd" = xyes])
index f0ebe074144e96624aa0e519ce2ac0d287a59593..66d90e15ae849890592c94070b978d4ea43638a7 100644 (file)
@@ -311,6 +311,11 @@ lib_systemd = dependency(
   required : get_option('systemd'))
 conf.set('HAVE_LIBSYSTEMD', lib_systemd.found() ? 1 : false)
 
+have = cc.has_function(
+  'sd_session_get_username',
+  dependencies : lib_systemd)
+conf.set('HAVE_DECL_SD_SESSION_GET_USERNAME', have ? 1 : false)
+
 lib_udev = dependency(
   'libudev',
   required : get_option('systemd'))
@@ -2316,7 +2321,7 @@ exe = executable(
   agetty_sources,
   include_directories : includes,
   link_with : [lib_common, logindefs_c],
-  dependencies : BSD ? lib_util : [],
+  dependencies : [BSD ? lib_util : [], lib_systemd],
   install_dir : sbindir,
   install : opt,
   build_by_default : opt)
@@ -2362,6 +2367,7 @@ exe = executable(
   wall_sources,
   include_directories : includes,
   link_with : [lib_common],
+  dependencies : [lib_systemd],
   install_dir : usrbin_exec_dir,
   install : opt,
   build_by_default : opt)
@@ -2380,6 +2386,7 @@ exe = executable(
   write_sources,
   include_directories : includes,
   link_with : [lib_common],
+  dependencies : [lib_systemd],
   install_dir : usrbin_exec_dir,
   install : opt,
   build_by_default : opt)
index 07004d43ca1175f22d0d57aee704674d1287f3f8..15423204222392eed576f4f0604a21fda97f71a1 100644 (file)
@@ -98,6 +98,10 @@ dist_noinst_DATA += term-utils/wall.1.adoc
 wall_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS)
 wall_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
 wall_LDADD = $(LDADD) libcommon.la
+if HAVE_SYSTEMD
+wall_LDADD += $(SYSTEMD_LIBS)
+wall_CFLAGS += $(SYSTEMD_CFLAGS)
+endif
 if USE_TTY_GROUP
 if MAKEINSTALL_DO_CHOWN
 install-exec-hook-wall::
index c601d3e5b73582dfaa20edf04b2a61e8384a7f1f..313b1bdee5622c6d32097f288f93b1278f0ab751 100644 (file)
 #include <sys/types.h>
 #include <grp.h>
 
+#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
+# include <systemd/sd-login.h>
+# include <systemd/sd-daemon.h>
+#endif
+
 #include "nls.h"
 #include "xalloc.h"
 #include "strutils.h"
@@ -246,6 +251,37 @@ int main(int argc, char **argv)
 
        iov.iov_base = mbuf;
        iov.iov_len = mbufsize;
+#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
+       if (sd_booted() > 0) {
+               char **sessions_list;
+               int sessions;
+
+               sessions = sd_get_sessions(&sessions_list);
+               if (sessions < 0)
+                       errx(EXIT_FAILURE, _("error getting sessions: %s"),
+                               strerror(-sessions));
+
+               for (int i = 0; i < sessions; i++) {
+                       char *name, *tty;
+                       int r;
+
+                       if ((r = sd_session_get_username(sessions_list[i], &name)) < 0)
+                               errx(EXIT_FAILURE, _("get user name failed: %s"), strerror (-r));
+
+                       if (!(group_buf && !is_gr_member(name, group_buf))) {
+                               if (sd_session_get_tty(sessions_list[i], &tty) >= 0) {
+                                       if ((p = ttymsg(&iov, 1, tty, timeout)) != NULL)
+                                               warnx("%s", p);
+
+                                       free(tty);
+                               }
+                       }
+                       free(name);
+                       free(sessions_list[i]);
+               }
+               free(sessions_list);
+       } else {
+#endif
        while((utmpptr = getutxent())) {
                if (!utmpptr->ut_user[0])
                        continue;
@@ -269,6 +305,9 @@ int main(int argc, char **argv)
                        warnx("%s", p);
        }
        endutxent();
+#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
+       }
+#endif
        free(mbuf);
        free_group_workspace(group_buf);
        exit(EXIT_SUCCESS);