Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C, sysctl and kern.hostname

C, sysctl and kern.hostname
Thread Tools
Junior Member
Join Date: Aug 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Feb 9, 2004, 09:51 AM
 
I'm coming back to C after a bit of a hiatus, and am working on a simple little program. In part of this program, I want to obtain the hostname of the machine and have thusfar been using sysctl but unfortunately sysctl gives me confusing information. For example:

Code:
#include <stdio.h> #include <stdlib.h> #include <sys/sysctl.h> int main() { int mib[2]; size_t len; char *kern_hostname; len = 0; mib[0] = CTL_KERN; mib[1] = KERN_HOSTNAME; sysctl(mib, 2, NULL, &len, NULL, 0); kern_hostname = malloc(len * sizeof(char)); sysctl(mib, 2, kern_hostname, &len, NULL, 0); printf("%s\n", kern_hostname); free(kern_hostname); return 0; }
This displays nothing, yet a 'sysctl -a' shows the hostname to be 'kern.hostname = xxxxx.local'. In addition, printing out len after the first 'sysctl()' call yields 11, which is the size of my hostname.

Any ideas?
Peace. Love. MacOSX.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Feb 9, 2004, 11:18 AM
 
This code looks solid, so I'm not sure why it isn't working. Possibly, the hostname being returned isn't a c-string, it's just filling the buffer with the characters and not adding the string terminator to the end, and this is confusing printf (although I'd expect a crash if this were what was happening).

Try allocating (len + 1) chars and setting the last one to 0.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Dedicated MacNNer
Join Date: Sep 2001
Location: San Jose CA
Status: Offline
Reply With Quote
Feb 9, 2004, 12:43 PM
 
Hi,

When sysctl(3) is called it expects that the caller specifies the length of the input value (*oldp) using *oldlenp (len in this case). When sysctl completes it puts the actual length of *oldp into *oldlenp.

The problem is that when systcl(3) is called for a string it expects the length to include space for the trailing NULL ('\0'), but when it returns the string length it does not include the extra space required for the trailing NULL ('\0').

The upshot of all this is that if len is incremented after the first call to sysctl() then the program should work.

As an aside, some other things you might want to consider are:

1. check the return value of sysctl(3) and print out errno using strerror(3) if sysctl(3) fails.

2. check the return value from malloc(3) to make sure it is not null.

3. use memset(3) to zero the string before calling sysctl(3) the second time.

HTH,

--ranga
     
Hous68  (op)
Junior Member
Join Date: Aug 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Feb 9, 2004, 03:35 PM
 
Bingo. 'len++;' after the first 'sysctl()' call gives me results. It's strange because I can retrieve other strings from 'sysctl()' the same way and still get results.

Thank you both for the input!
Peace. Love. MacOSX.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2