Check if parent process expired in libkvm

Hi there,
I'm trying to write a code in C (with libkvm) that checks, for all process, if the parent process exists.
This library seems to set to 1 (init) the ki_ppid field for all process with parent expired, is it true? There is a way to get the real ppid?
Thanks
MadHatter

P.S.
this is the code, mostly taken from ps.c
Code:
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <limits.h>
#include <fcntl.h>

int main(){
	kvm_t *kd;
	char errbuf[_POSIX2_LINE_MAX];
	int nentries;
	int p;
	struct kinfo_proc *kp;
	kd = kvm_openfiles(NULL,"/dev/null",NULL,O_RDONLY,errbuf);
	if(kd == 0)
		errx(1,"%s",errbuf);
	kp = kvm_getprocs(kd,KERN_PROC_PROC,0,&nentries);
	for(p=0; p<nentries;p++){
		printf("%5d %s parent id %d",kp[p].ki_pid,kp[p].ki_comm,kp[p].ki_ppid);
		if(kp[p].ki_ppid==1)
			printf(" parent proc not exsits");
		printf("\n");
	}
		
	kvm_close(kd);
}
 
Back
Top