site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com Thread-index: AciRIu4fpStDDWtTShK9gpkyP6XaMAB0Md0Q I cannot see the definition of struct filelist on Mac 10.4.7. Is it one of the private structure that we cannot use ? Regards, Manish -----Original Message----- From: darwin-kernel-bounces+manish_chaturvedi=persistent.co.in@lists.apple.com [mailto:darwin-kernel-bounces+manish_chaturvedi=persistent.co.in@lists.apple .com] On Behalf Of Terry Lambert Sent: Saturday, March 29, 2008 3:58 AM To: anukriti_shrimal Cc: darwin-kernel@lists.apple.com Subject: Re: Which structure does sysctl fill when KERN_FILE parameter is given as input on Mac Os X 10.4.7? 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 (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/manish_chaturvedi%40per sistent.co.in This email sent to manish_chaturvedi@persistent.co.in _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... This email sent to site_archiver@lists.apple.com