How to list info about processes using c in freebsd?

Viewed 265

I want to list all processes in FreeBSD and I have this code below, which uses kvm, but it does not know what KVM_NO_FILES is, and I can't figure how to fix it. If there is another way of doing it, please do share.

#include <stdio.h>
#include <kvm.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/sysctl.h>

int
main(void)
{
    char errbuf[_POSIX2_LINE_MAX];
    kvm_t *kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
    int nentries = 0;
    struct kinfo_proc *kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
    int i;
    for (i = 0; i < nentries; ++i) {
        printf("%s\n", kinfo[i].p_comm);
    }
      return 0;
}

And I get this error:

root@freebsd:- # cc -lkvm main.c 
main.c:11:53: error: use of undeclared identifier 'KVM_NO_FILES' 
kvm_t *kernel = kvm_openfiles(NULL. NULL. NULL, HVM_NO_FILES, errbuf): 
main.c:13:71: error: invalid application of 'sizeof' to an incomplete type 
'struct kinfo_proc' 
...= kvm_getprocs(kernel, HERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nen... 
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc' 
struct kinfo_proc; 
main.c:18:29: error: subscript of pointer to incomplete type 'struct kinfo_proc' 
printf("Xs\n", kinfolil.p_comm): 
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc' 
struct kinfo_proc; 
3 errors generated. 
0 Answers
Related