From owner-freebsd-hackers Sat Oct 13 11: 9:20 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from mail.yadt.co.uk (yadt.demon.co.uk [158.152.4.134]) by hub.freebsd.org (Postfix) with SMTP id 0A80437B401 for ; Sat, 13 Oct 2001 11:09:13 -0700 (PDT) Received: (qmail 10215 invoked from network); 13 Oct 2001 18:09:09 -0000 Received: from gattaca.local.yadt.co.uk (HELO mail.gattaca.yadt.co.uk) (qmailr@10.0.0.2) by xfiles.yadt.co.uk with SMTP; 13 Oct 2001 18:09:09 -0000 Received: (qmail 29947 invoked by uid 1000); 13 Oct 2001 18:09:09 -0000 Date: Sat, 13 Oct 2001 19:09:09 +0100 From: David Taylor To: Paolo Pisati Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: First prg with sysctl Message-ID: <20011013190909.A29541@gattaca.yadt.co.uk> Mail-Followup-To: Paolo Pisati , freebsd-hackers@FreeBSD.ORG References: <20011013194902.A38183@newluxor.skynet.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20011013194902.A38183@newluxor.skynet.org>; from flag@libero.it on Sat, Oct 13, 2001 at 19:49:02 +0200 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sat, 13 Oct 2001, Paolo Pisati wrote: > > Someone can tell me why this piece of code doesn't work? > > #include > #include > #include > #include > #include > #include > #include > > int main(void) { > int mib[5], *count; Here you define 'count' as an uninitialised pointer to an integer. > mib[0]=CTL_NET; > mib[1]=PF_LINK; > mib[2]=NETLINK_GENERIC; > mib[3]=IFMIB_SYSTEM; > mib[4]=IFMIB_IFCOUNT; > > sysctl(mib, 5, count, sizeof(int),(void *)NULL, NULL); Here, in arg three, you pass the value of 'count' as the address to store the count in, so you're storing it in some random address, probably resulting in -1 being returned with errno=EFAULT. In arg 4, you pass 'sizeof(int)' (probably 4) as the address of a size_t containing the length of the value, also probably resulting in EFAULT. In arg 5, casting NULL to (void *) is somewhat redundant. In arg 6, you probably want to use 0, since the arg isn't a pointer. You also don't check the return value for success/failure. > printf("count: %d\n",*count); You then dereference the random piece of memory. Consider something like: +++ sysctl.c Sat Oct 13 19:08:44 2001 @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,7 +8,9 @@ #include int main(void) { - int mib[5], *count; + int mib[5]; + int count; + int len; mib[0]=CTL_NET; mib[1]=PF_LINK; @@ -15,8 +18,12 @@ mib[3]=IFMIB_SYSTEM; mib[4]=IFMIB_IFCOUNT; - sysctl(mib, 5, count, sizeof(int),(void *)NULL, NULL); - - printf("count: %d\n",*count); + len = sizeof(int); + if (sysctl(mib, 5, &count, &len, NULL, 0) == -1) + { + perror("sysctl"); + exit(1); + } + printf("count: %d\n",count); } -- David Taylor davidt@yadt.co.uk To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message