Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Jul 2016 16:34:57 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r303151 - in head/sys: dev/pty kern sys
Message-ID:  <201607211634.u6LGYvIH038339@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Thu Jul 21 16:34:56 2016
New Revision: 303151
URL: https://svnweb.freebsd.org/changeset/base/303151

Log:
  Provide counter_warning(9) KPI which allows to issue limited number of
  warnings for some kernel events, mostly intended for the use of
  obsoleted or otherwise undersired interfaces.
  
  This is an abstracted and race-expelled code from compat pty driver.
  
  Requested and reviewed by:	jhb
  Sponsored by:	The FreeBSD Foundation
  MFC after:	1 week
  Differential revision:	https://reviews.freebsd.org/D7270

Modified:
  head/sys/dev/pty/pty.c
  head/sys/kern/subr_prf.c
  head/sys/sys/systm.h

Modified: head/sys/dev/pty/pty.c
==============================================================================
--- head/sys/dev/pty/pty.c	Thu Jul 21 15:48:41 2016	(r303150)
+++ head/sys/dev/pty/pty.c	Thu Jul 21 16:34:56 2016	(r303151)
@@ -52,10 +52,10 @@ __FBSDID("$FreeBSD$");
  * binary emulation.
  */
 
-static unsigned int pty_warningcnt = 1;
+static unsigned pty_warningcnt = 1;
 SYSCTL_UINT(_kern, OID_AUTO, tty_pty_warningcnt, CTLFLAG_RW,
-	&pty_warningcnt, 0,
-	"Warnings that will be triggered upon legacy PTY allocation");
+    &pty_warningcnt, 0,
+    "Warnings that will be triggered upon legacy PTY allocation");
 
 static int
 ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
@@ -77,12 +77,7 @@ ptydev_fdopen(struct cdev *dev, int ffla
 	}
 
 	/* Raise a warning when a legacy PTY has been allocated. */
-	if (pty_warningcnt > 0) {
-		pty_warningcnt--;
-		log(LOG_INFO, "pid %d (%s) is using legacy pty devices%s\n",
-		    td->td_proc->p_pid, td->td_name,
-		    pty_warningcnt ? "" : " - not logging anymore");
-	}
+	counted_warning(&pty_warningcnt, "is using legacy pty devices");
 
 	return (0);
 }

Modified: head/sys/kern/subr_prf.c
==============================================================================
--- head/sys/kern/subr_prf.c	Thu Jul 21 15:48:41 2016	(r303150)
+++ head/sys/kern/subr_prf.c	Thu Jul 21 16:34:56 2016	(r303151)
@@ -1196,3 +1196,22 @@ sbuf_hexdump(struct sbuf *sb, const void
 	}
 }
 
+void
+counted_warning(unsigned *counter, const char *msg)
+{
+	struct thread *td;
+	unsigned c;
+
+	for (;;) {
+		c = *counter;
+		if (c == 0)
+			break;
+		if (atomic_cmpset_int(counter, c, c - 1)) {
+			td = curthread;
+			log(LOG_INFO, "pid %d (%s) %s%s\n",
+			    td->td_proc->p_pid, td->td_name, msg,
+			    c > 1 ? "" : " - not logging anymore");
+			break;
+		}
+	}
+}

Modified: head/sys/sys/systm.h
==============================================================================
--- head/sys/sys/systm.h	Thu Jul 21 15:48:41 2016	(r303150)
+++ head/sys/sys/systm.h	Thu Jul 21 16:34:56 2016	(r303151)
@@ -447,4 +447,6 @@ void	intr_prof_stack_use(struct thread *
 
 extern void (*softdep_ast_cleanup)(void);
 
+void counted_warning(unsigned *counter, const char *msg);
+
 #endif /* !_SYS_SYSTM_H_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201607211634.u6LGYvIH038339>