Author: trasz
Date: Sat Jul 11 13:11:54 2020
New Revision: 363094
URL: https://svnweb.freebsd.org/changeset/base/363094

Log:
  Make linprocfs(5) report correct tty number in /proc/<PID>/stat.
  Fixes sudo (sudo-1.8.21p2-3ubuntu1.2); previously would fail
  with "sudo: no tty present and no askpass program specified".
  
  Reviewed by:  kib, emaste
  MFC after:    2 weeks
  Sponsored by: The FreeBSD Foundation
  Differential Revision:        https://reviews.freebsd.org/D25588

Modified:
  head/sys/compat/linprocfs/linprocfs.c

Modified: head/sys/compat/linprocfs/linprocfs.c
==============================================================================
--- head/sys/compat/linprocfs/linprocfs.c       Sat Jul 11 13:08:16 2020        
(r363093)
+++ head/sys/compat/linprocfs/linprocfs.c       Sat Jul 11 13:11:54 2020        
(r363094)
@@ -767,6 +767,32 @@ linprocfs_doloadavg(PFS_FILL_ARGS)
        return (0);
 }
 
+static int
+linprocfs_get_tty_nr(struct proc *p)
+{
+       struct session *sp;
+       const char *ttyname;
+       int error, major, minor, nr;
+
+       PROC_LOCK_ASSERT(p, MA_OWNED);
+       sx_assert(&proctree_lock, SX_LOCKED);
+
+       if ((p->p_flag & P_CONTROLT) == 0)
+               return (-1);
+
+       sp = p->p_pgrp->pg_session;
+       if (sp == NULL)
+               return (-1);
+
+       ttyname = devtoname(sp->s_ttyp->t_dev);
+       error = linux_driver_get_major_minor(ttyname, &major, &minor);
+       if (error != 0)
+               return (-1);
+
+       nr = makedev(major, minor);
+       return (nr);
+}
+
 /*
  * Filler function for proc/pid/stat
  */
@@ -777,12 +803,14 @@ linprocfs_doprocstat(PFS_FILL_ARGS)
        struct timeval boottime;
        char state;
        static int ratelimit = 0;
+       int tty_nr;
        vm_offset_t startcode, startdata;
 
        getboottime(&boottime);
        sx_slock(&proctree_lock);
        PROC_LOCK(p);
        fill_kinfo_proc(p, &kp);
+       tty_nr = linprocfs_get_tty_nr(p);
        sx_sunlock(&proctree_lock);
        if (p->p_vmspace) {
           startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
@@ -809,10 +837,7 @@ linprocfs_doprocstat(PFS_FILL_ARGS)
        PS_ADD("pgrp",          "%d",   p->p_pgid);
        PS_ADD("session",       "%d",   p->p_session->s_sid);
        PROC_UNLOCK(p);
-       if (kp.ki_tdev == NODEV)
-               PS_ADD("tty",   "%s",   "-1");
-       else
-               PS_ADD("tty",           "%ju",  (uintmax_t)kp.ki_tdev);
+       PS_ADD("tty",           "%d",   tty_nr);
        PS_ADD("tpgid",         "%d",   kp.ki_tpgid);
        PS_ADD("flags",         "%u",   0); /* XXX */
        PS_ADD("minflt",        "%lu",  kp.ki_rusage.ru_minflt);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to