Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 9 Jun 2019 03:31:08 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r348827 - stable/12/lib/libnetgraph
Message-ID:  <201906090331.x593V8Kg056459@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Sun Jun  9 03:31:07 2019
New Revision: 348827
URL: https://svnweb.freebsd.org/changeset/base/348827

Log:
  MFC r347439:
  Atomically update the global gMsgId in libnetgraph.
  
  PR:	234442

Modified:
  stable/12/lib/libnetgraph/msg.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/lib/libnetgraph/msg.c
==============================================================================
--- stable/12/lib/libnetgraph/msg.c	Sun Jun  9 03:29:26 2019	(r348826)
+++ stable/12/lib/libnetgraph/msg.c	Sun Jun  9 03:31:07 2019	(r348827)
@@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <stdarg.h>
+#include <stdatomic.h>
 #include <netgraph/ng_message.h>
 #include <netgraph/ng_socket.h>
 
@@ -51,7 +52,7 @@ __FBSDID("$FreeBSD$");
 #include "internal.h"
 
 /* Next message token value */
-static int	gMsgId;
+static _Atomic(unsigned int) gMsgId;
 
 /* For delivering both messages and replies */
 static int	NgDeliverMsg(int cs, const char *path,
@@ -72,9 +73,7 @@ NgSendMsg(int cs, const char *path,
 	memset(&msg, 0, sizeof(msg));
 	msg.header.version = NG_VERSION;
 	msg.header.typecookie = cookie;
-	if (++gMsgId < 0)
-		gMsgId = 1;
-	msg.header.token = gMsgId;
+	msg.header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
 	msg.header.flags = NGF_ORIG;
 	msg.header.cmd = cmd;
 	snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
@@ -143,9 +142,7 @@ NgSendAsciiMsg(int cs, const char *path, const char *f
 
 	/* Now send binary version */
 	binary = (struct ng_mesg *)reply->data;
-	if (++gMsgId < 0)
-		gMsgId = 1;
-	binary->header.token = gMsgId;
+	binary->header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
 	binary->header.version = NG_VERSION;
 	if (NgDeliverMsg(cs,
 	    path, binary, binary->data, binary->header.arglen) < 0) {



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