BSD-Process

 view release on metacpan or  search on metacpan

Process.xs  view on Meta::CPAN


MODULE = BSD::Process   PACKAGE = BSD::Process

PROTOTYPES: ENABLE

short
max_kernel_groups()
    CODE:
#if __FreeBSD_version < 500000
        RETVAL = 0;
#else
        RETVAL = KI_NGROUPS;
#endif
    OUTPUT:
        RETVAL

SV *
_info(int pid, int resolve)
    PREINIT:
        /* TODO: int pid should be pid_t pid */
        size_t len;
        struct kinfo_proc ki;
        HV *h;

    CODE:
        /* use the sysctl approach instead of using a kernel
         * descriptor, makes for a bit less housekeeping.
         */
        if (proc_info_mib[0] == -1) {
            len = sizeof(proc_info_mib)/sizeof(proc_info_mib[0]);
            if (sysctlnametomib("kern.proc.pid", proc_info_mib, &len) == -1) {
                warn( "kern.proc.pid is corrupt\n");
                XSRETURN_UNDEF;
            }
        }
        proc_info_mib[3] = pid;
        len = sizeof(ki);
        if (sysctl(proc_info_mib, sizeof(proc_info_mib)/sizeof(proc_info_mib[0]), &ki, &len, NULL, 0) == -1) {
            /* process identified by pid has probably exited */
            XSRETURN_UNDEF;
        }
        h = _procinfo( &ki, resolve );
        RETVAL = newRV((SV *)h);

    OUTPUT:
        RETVAL

void
_list(int request, int param)
    PREINIT:
#ifdef dXSTARG
    dXSTARG;
#else
    dTARGET;
#endif
        struct kinfo_proc *kip;
        kvm_t *kd;
        int nr;
        char errbuf[_POSIX2_LINE_MAX];
        const char *nlistf, *memf;
    PPCODE:
        nlistf = memf = PATH_DEV_NULL;
        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
        kip = _proc_request(kd, request, param, &nr);
        if (kip) {
            int p;
            for (p = 0; p < nr; ++kip, ++p) {
#if PERL_API_VERSION == 5 && PERL_VERSION == 6
                EXTEND(SP,1);
                XPUSHi(kip->PID_FIELD);
#else
                mPUSHi(kip->PID_FIELD);
#endif
            }
            kvm_close(kd);
        }
        else {
            warn("kvm error in list(): %s\n", kvm_geterr(kd));
            XSRETURN_UNDEF;
        }
        XSRETURN(nr);

HV *
_all(int resolve, int request, int param)
    PREINIT:
        struct kinfo_proc *kip;
        kvm_t *kd;
        int nr;
        char errbuf[_POSIX2_LINE_MAX];
        char pidbuf[16];
        const char *nlistf, *memf;
        HV *h;
        HV *package;
        HV *out;
        int p;

    CODE:
        nlistf = memf = PATH_DEV_NULL;
        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
        kip = _proc_request(kd, request, param, &nr);

        if (!kip) {
            warn("kvm error in all(): %s\n", kvm_geterr(kd));
            XSRETURN_UNDEF;
        }

        out = (HV *)sv_2mortal((SV *)newHV());
        package = gv_stashpv("BSD::Process", 0);

        RETVAL = out;
        for (p = 0; p < nr; ++kip, ++p) {
            h = _procinfo( kip, resolve );
            hv_store(h, "_resolve", 8, newSViv(resolve), 0);
            hv_store(h, "_pid",     4, newSViv(kip->PID_FIELD), 0);
            sprintf( pidbuf, "%d", kip->PID_FIELD);
            hv_store(out, pidbuf, strlen(pidbuf),
                sv_bless(newRV((SV *)h), package), 0);
        }
        kvm_close(kd);

    OUTPUT:



( run in 1.376 second using v1.01-cache-2.11-cpan-71847e10f99 )