 |
 |
C, sysctl and kern.hostname
|
 |
|
 |
|
Junior Member
Join Date: Aug 2001
Location: Chicago, IL
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Aug 2001
Location: Chicago, IL
Status:
Offline
|
|
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.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|