Subject: Re: gethostname and getdomainname
To: Christos Zoulas <christos@zoulas.com>
From: Ignatios Souvatzis <is@jocelyn.rhein.de>
List: tech-userlevel
Date: 11/14/1999 22:01:52
--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=us-ascii

On Sun, Nov 14, 1999 at 09:29:10PM +0100, Ignatios Souvatzis wrote:

> I really think we should also swallow the error there.
> 
> > I still think though that it is better
> > to make sysctl return a non-null-terminated string.
> 
> Thats fine with me. 
> 
> To summarize, I would
> 
> - fix sysctl so that it returns the truncated string, non-null-terminated,
>   and create an appropriate error. I think copyoutstr will do the job nicely
>   for us.

Didn't need to. First, it returns an errno not documented in sysctl(3), 2nd,
I need to explictly get at the length anyhow (for passing it back), so it 
can use copyout, like before.

Here's the kern_sysctl.c patch for review:

	-is

--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="kern_sysctl.patch"

Index: kern_sysctl.c
===================================================================
RCS file: /cvsroot/syssrc/sys/kern/kern_sysctl.c,v
retrieving revision 1.54
diff -u -r1.54 kern_sysctl.c
--- kern_sysctl.c	1999/11/12 16:10:16	1.54
+++ kern_sysctl.c	1999/11/14 21:01:28
@@ -767,16 +767,27 @@
 	char *str;
 	int maxlen;
 {
-	int len, error = 0;
+	int len, error = 0, err2 = 0;
 
-	len = strlen(str) + 1;
-	if (oldp && *oldlenp < len)
-		return (ENOMEM);
 	if (newp && newlen >= maxlen)
 		return (EINVAL);
-	*oldlenp = len;
-	if (oldp)
-		error = copyout(str, oldp, len);
+
+	if (oldlenp) {
+		len = strlen(str) + 1;
+
+		if (*oldlenp < len) {
+			err2 = ENOMEM;
+			len = *oldlenp;
+		} else
+			*oldlenp = len;
+
+		if (oldp)
+			error = copyout(str, oldp, len);
+
+		if (error == 0)
+			error = err2;
+	}
+
 	if (error == 0 && newp) {
 		error = copyin(newp, str, newlen);
 		str[newlen] = 0;
@@ -794,16 +805,27 @@
 	void *newp;
 	char *str;
 {
-	int len, error = 0;
+	int len, error = 0, err2 = 0;
 
-	len = strlen(str) + 1;
-	if (oldp && *oldlenp < len)
-		return (ENOMEM);
 	if (newp)
 		return (EPERM);
-	*oldlenp = len;
-	if (oldp)
-		error = copyout(str, oldp, len);
+
+	if (oldlenp) {
+		len = strlen(str) + 1;
+
+		if (*oldlenp < len) {
+			err2 = ENOMEM;
+			len = *oldlenp;
+		} else
+			*oldlenp = len;
+
+		if (oldp)
+			error = copyout(str, oldp, len);
+
+		if (!error)
+			error = err2;
+	}
+
 	return (error);
 }
 

--IJpNTDwzlM2Ie8A6--