Hello,

enclosed is a patch making the output of who --mesg more reliable when 
compiled with --with-tty-group.  The issue was discussed before at

https://bugzilla.redhat.com/454261

and later at

http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/14022

Any comments are welcome!

Kamil
From df522e97bd36b498cf75f4a7edd978f83388cdfd Mon Sep 17 00:00:00 2001
From: Kamil Dudka <[email protected]>
Date: Wed, 20 Jan 2010 16:15:37 +0100
Subject: [PATCH] who: --mesg now respects also group of a TTY

... when compiled with --with-tty-group
* src/who.c (is_tty_writable): A new function returning true if a TTY
device is writable by group.  Additionally it checks the group to be
the same as TTY_GROUP_NAME when compiled with --with-tty-group.
* m4/jm-macros.m4: Introduce a new configure options --with-tty-group.
* NEWS: Mention the change.
---
 NEWS            |    5 +++++
 m4/jm-macros.m4 |   17 +++++++++++++++++
 src/who.c       |   24 +++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/NEWS b/NEWS
index 530ff95..de310bd 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** New features
+
+  who --mesg now respects also group of a TTY when compiled with
+  --with-tty-group
+
 
 * Noteworthy changes in release 8.4 (2010-01-13) [stable]
 
diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4
index 2713827..1e55db1 100644
--- a/m4/jm-macros.m4
+++ b/m4/jm-macros.m4
@@ -144,6 +144,23 @@ AC_DEFUN([coreutils_MACROS],
     ])
 
   AC_REQUIRE([AM_LANGINFO_CODESET])
+
+  # configure options --with-tty-group/--without-tty-group
+  AC_ARG_WITH([tty-group],
+    AS_HELP_STRING([--with-tty-group[[[=NAME]]]],
+      [group used by system for TTYs, "tty" when not specified]
+      [ (default: do not rely on any group used for TTYs)]),
+    [tty_group_name=$withval],
+    [tty_group_name=no])
+
+  if test "x$tty_group_name" != xno; then
+    if test "x$tty_group_name" = xyes; then
+      tty_group_name=tty
+    fi
+    AC_MSG_NOTICE([TTY group used by system set to "$tty_group_name"])
+    AC_DEFINE_UNQUOTED([TTY_GROUP_NAME], ["$tty_group_name"],
+      [group used by system for TTYs])
+  fi
 ])
 
 AC_DEFUN([gl_CHECK_ALL_HEADERS],
diff --git a/src/who.c b/src/who.c
index f71db3b..62bd948 100644
--- a/src/who.c
+++ b/src/who.c
@@ -37,6 +37,10 @@
 #include "hard-locale.h"
 #include "quote.h"
 
+#ifdef TTY_GROUP_NAME
+# include <grp.h>
+#endif
+
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "who"
 
@@ -308,6 +312,24 @@ print_line (int userlen, const char *user, const char state,
   free (x_exitstr);
 }
 
+#ifdef TTY_GROUP_NAME
+static bool
+is_tty_writable (const struct stat *pstat)
+{
+  struct group *ttygr = getgrnam (TTY_GROUP_NAME);
+  if (!ttygr || (pstat->st_gid != ttygr->gr_gid))
+    return false;
+
+  return pstat->st_mode & S_IWGRP;
+}
+#else
+static bool
+is_tty_writable (const struct stat *pstat)
+{
+  return pstat->st_mode & S_IWGRP;
+}
+#endif
+
 /* Send properly parsed USER_PROCESS info to print_line.  The most
    recent boot time is BOOTTIME. */
 static void
@@ -346,7 +368,7 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
 
   if (stat (line, &stats) == 0)
     {
-      mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
+      mesg = is_tty_writable (&stats) ? '+' : '-';
       last_change = stats.st_atime;
     }
   else
-- 
1.6.2.5

Reply via email to