From: Thorsten Kukuk Date: Wed, 31 May 2023 06:55:33 +0000 (+0200) Subject: wall: query logind for list of users with tty (#2088) X-Git-Tag: v2.40-rc1~274^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=87fcd95ac5ad18e471b17f448daa55b2ce2fac6a;p=thirdparty%2Futil-linux.git wall: query logind for list of users with tty (#2088) --- diff --git a/configure.ac b/configure.ac index d631b062d2..17d3ab703b 100644 --- a/configure.ac +++ b/configure.ac @@ -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 ]) ) ]) AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$have_systemd" = xyes]) diff --git a/meson.build b/meson.build index f0ebe07414..66d90e15ae 100644 --- a/meson.build +++ b/meson.build @@ -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) diff --git a/term-utils/Makemodule.am b/term-utils/Makemodule.am index 07004d43ca..1542320422 100644 --- a/term-utils/Makemodule.am +++ b/term-utils/Makemodule.am @@ -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:: diff --git a/term-utils/wall.c b/term-utils/wall.c index c601d3e5b7..313b1bdee5 100644 --- a/term-utils/wall.c +++ b/term-utils/wall.c @@ -61,6 +61,11 @@ #include #include +#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1 +# include +# include +#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);