Re: Which structure does sysctl fill when KERN_FILE parameter is given as input on Mac Os X 10.4.7?
Re: Which structure does sysctl fill when KERN_FILE parameter is given as input on Mac Os X 10.4.7?
- Subject: Re: Which structure does sysctl fill when KERN_FILE parameter is given as input on Mac Os X 10.4.7?
- From: Terry Lambert <email@hidden>
- Date: Fri, 28 Mar 2008 15:28:18 -0700
On Mar 27, 2008, at 11:38 PM, anukriti_shrimal wrote:
Hi All,
I am currently working on Mac Os X 10.4.7.
I m trying to retrieve the list of files opened by a process using
sysctl(). The code snippet that I’ve written is :
void* pBuff = NULL ;
mib[0] = CTL_KERN;
mib[1] = KERN_FILE;
size = sizeof(struct filedesc) ; // This is what I
tried. This struct is defined in file sys/filedesc.h
pBuff = malloc(size) ;
while (sysctl((int *)mib, 2, pBuff, &size, NULL, 0) <
0)
{
if (errno == ENOMEM)
{
printf("Not sufficient memory
allocated\n");
}
size *= 2 ;
if ((arguments = realloc(pBuff, size)) ==
NULL)
{
printf("Realloc error\n");
}
printf("Error in getting file struct %s\n
and error no is %d\n", strerror(errno),errno) ;
}
This code compiles and fills the buffer pBuff.
Now my question is that the buffer pBuff should to mapped to which
structure on Mac 10.4.7??
I have found that it fills struct file or struct xfile on Free BSD.
But on Mac Os, I’ve tried it with struct extern_file and struct
filedesc, but both didn’t work. Why are the two structs ‘file’ and
‘xfile’ not exposed on Mac 10.4.7?
If none of the above, then which struct is getting filled by the
above sysctl call?
I know that there is an alternative way to do the same task, i.e.,
through lsof. But my project constraints don’t let me use that.
Also, when I saw the ktrace dump of lsof, I found that it also uses
sysctl.
Is there some other way to do the same task?
Not portably, if you are unwilling to use lsof. We will probably
break you every release and potentially every SU. Hope you are OK
with that, since you are using undocumented and unsupported SPI by
using that sysctl.
If you need to know what it packs in the buffer by default on 10.4.7,
it's a struct filelist followed by N struct extern_file's.
Your best bet when you have questions like these is to look at the xnu
sources for the OS version you are interested in, since, as I implied,
this can change from version to version of the OS.
Unless your constraint is specifically "don't popen lsof", I would
recommend using lsof.
-- Terry _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden