Ejemplo n.º 1
0
QString QLockFilePrivate::processNameByPid(qint64 pid)
{
#if defined(Q_OS_OSX)
    char name[1024];
    proc_name(pid, name, sizeof(name) / sizeof(char));
    return QFile::decodeName(name);
#elif defined(Q_OS_LINUX)
    if (!QFile::exists(QStringLiteral("/proc/version")))
        return QString();
    char exePath[64];
    char buf[PATH_MAX + 1];
    sprintf(exePath, "/proc/%lld/exe", pid);
    size_t len = (size_t)readlink(exePath, buf, sizeof(buf));
    if (len >= sizeof(buf)) {
        // The pid is gone. Return some invalid process name to fail the test.
        return QStringLiteral("/ERROR/");
    }
    buf[len] = 0;
    return QFileInfo(QFile::decodeName(buf)).fileName();
#elif defined(Q_OS_BSD4) && !defined(Q_OS_IOS)
    kinfo_proc *proc = kinfo_getproc(pid);
    if (!proc)
        return QString();
    QString name = QFile::decodeName(proc->ki_comm);
    free(proc);
    return name;
#else
    return QString();
#endif
}
Ejemplo n.º 2
0
const char *proc_maps(pid_t pid, size_t *start, size_t *end, int *exe_self)
{
    static struct kinfo_vmentry *freep = NULL;
    static unsigned int i, cnt;
    static char *exe_name;

    /* first, init */
    if (freep == NULL) {
        struct kinfo_proc *ki = kinfo_getproc(pid);
        if (ki == NULL) {
            perror("Error in get process info");
            exit(6);
        }
        freep = procstat_getvmmap(procstat_open_sysctl(), ki, &cnt);
        exe_name = ki->ki_comm;
    }

    while (i < cnt) {
        struct kinfo_vmentry *kve = &freep[i++];
        if ((kve->kve_protection & KVME_PROT_EXEC) && kve->kve_path[0] == '/') {
            *start = kve->kve_start;
            *end = kve->kve_end;

            if (exe_self != NULL) {
                *exe_self = (strcmp(exe_name, strrchr(kve->kve_path, '/') + 1) == 0);
            }
            return kve->kve_path;
        }
    }

    i = 0;
    free(freep);
    freep = NULL;
    return NULL;
}
Ejemplo n.º 3
0
QString QLockFilePrivate::processNameByPid(qint64 pid)
{
#if defined(Q_OS_OSX)
    char name[1024];
    proc_name(pid, name, sizeof(name) / sizeof(char));
    return QFile::decodeName(name);
#elif defined(Q_OS_LINUX)
    if (!QFile::exists(QStringLiteral("/proc/version")))
        return QString();
    char exePath[64];
    char buf[PATH_MAX + 1];
    sprintf(exePath, "/proc/%lld/exe", pid);
    size_t len = (size_t)readlink(exePath, buf, sizeof(buf));
    if (len >= sizeof(buf)) {
        // The pid is gone. Return some invalid process name to fail the test.
        return QStringLiteral("/ERROR/");
    }
    buf[len] = 0;
    return QFileInfo(QFile::decodeName(buf)).fileName();
#elif defined(Q_OS_BSD4) && !defined(Q_OS_IOS)
# if defined(__GLIBC__) && defined(__FreeBSD_kernel__)
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
    size_t len = 0;
    if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0)
        return QString();
    kinfo_proc *proc = static_cast<kinfo_proc *>(malloc(len));
# else
    kinfo_proc *proc = kinfo_getproc(pid);
# endif
    if (!proc)
        return QString();
# if defined(__GLIBC__) && defined(__FreeBSD_kernel__)
    if (sysctl(mib, 4, proc, &len, NULL, 0) < 0) {
        free(proc);
        return QString();
    }
    if (proc->ki_pid != pid) {
        free(proc);
        return QString();
    }
# endif
    QString name = QFile::decodeName(proc->ki_comm);
    free(proc);
    return name;
#else
    Q_UNUSED(pid);
    return QString();
#endif
}
Ejemplo n.º 4
0
char *qemu_get_pid_name(pid_t pid)
{
    char *name = NULL;

#if defined(__FreeBSD__)
    /* BSDs don't have /proc, but they provide a nice substitute */
    struct kinfo_proc *proc = kinfo_getproc(pid);

    if (proc) {
        name = g_strdup(proc->ki_comm);
        free(proc);
    }
#else
    /* Assume a system with reasonable procfs */
    char *pid_path;
    size_t len;

    pid_path = g_strdup_printf("/proc/%d/cmdline", pid);
    g_file_get_contents(pid_path, &name, &len, NULL);
    g_free(pid_path);
#endif

    return name;
}