コード例 #1
0
ファイル: ev-document-links.c プロジェクト: JosephMcc/xreader
gint
ev_document_links_get_link_page (EvDocumentLinks *document_links,
				 EvLink          *link)
{
	EvLinkDest *dest;

	dest = get_link_dest (link);

	return dest ? ev_document_links_get_dest_page (document_links, dest) : -1;
}
コード例 #2
0
ファイル: ev-document-links.c プロジェクト: JosephMcc/xreader
gchar *
ev_document_links_get_link_page_label (EvDocumentLinks *document_links,
				       EvLink          *link)
{
	EvLinkDest *dest;

	dest = get_link_dest (link);

	return dest ? ev_document_links_get_dest_page_label (document_links, dest) : NULL;
}
コード例 #3
0
ファイル: pid.c プロジェクト: rdebath/sgt
static struct pidset get_processes(void)
{
    struct dirent *de;
    struct pidset ret;
    DIR *d;

    pidset_init(&ret);

    d = opendir("/proc");
    if (!d) {
        perror("/proc: open\n");
        exit(1);
    }
    while ((de = readdir(d)) != NULL) {
        int pid;
        char *cmdline, *status, *exe;
        int cmdlinelen;
        const char **argv;
        char filename[256];
        struct procdata *proc;

        const char *name = de->d_name;
        if (name[strspn(name, "0123456789")])
            continue;

        /*
         * The filename is numeric, i.e. we've found a pid. Try to
         * retrieve all the information we want about it.
         *
         * We expect this will fail every so often for random reasons,
         * e.g. if the pid has disappeared between us fetching a list
         * of them and trying to read their command lines. In that
         * situation, we won't bother reporting errors: we'll just
         * drop this pid and silently move on to the next one.
         */
        pid = atoi(name);
        assert(pid >= 0 && pid < PIDMAX);

        sprintf(filename, "/proc/%d/cmdline", pid);
        if ((cmdline = get_contents(filename, &cmdlinelen)) == NULL)
            continue;

        sprintf(filename, "/proc/%d/status", pid);
        if ((status = get_contents(filename, NULL)) == NULL) {
            free(cmdline);
            continue;
        }

        sprintf(filename, "/proc/%d/exe", pid);
        exe = get_link_dest(filename);
        /* This may fail, if the process isn't ours, but we continue
         * anyway. */

        /*
         * Now we've got all our raw data out of /proc. Process it
         * into the internal representation we're going to use in the
         * process-selection logic.
         */
        proc = (struct procdata *)malloc(sizeof(struct procdata));
        if (!proc) {
            fprintf(stderr, "pid: out of memory\n");
            exit(1);
        }
        proc->pid = pid;
        proc->exe = exe;

        /*
         * cmdline contains a list of NUL-terminated strings. Scan
         * them to get the argv pointers.
         */
        {
            const char *p;
            int nargs;

            /* Count the arguments. */
            nargs = 0;
            for (p = cmdline; p < cmdline + cmdlinelen; p += strlen(p)+1)
                nargs++;

            /* Allocate space for the pointers. */
            argv = (const char **)malloc((nargs+1) * sizeof(char *));
            proc->argv = argv;
            if (!argv) {
                fprintf(stderr, "pid: out of memory\n");
                exit(1);
            }

            /* Store the pointers. */
            proc->argc = 0;
            for (p = cmdline; p < cmdline + cmdlinelen; p += strlen(p)+1)
                argv[proc->argc++] = p;

            /* Trailing NULL to match standard argv lists, just in case. */
            assert(proc->argc == nargs);
            argv[proc->argc] = NULL;
        }

        /*
         * Scan status for the uid and the parent pid. This file
         * contains a list of \n-terminated lines of text.
         */
        {
            const char *p;
            int got_ppid = 0, got_uid = 0;

            p = status;
            while (p && *p) {
                if (!got_ppid && sscanf(p, "PPid: %d", &proc->ppid) == 1)
                    got_ppid = 1;
                if (!got_uid && sscanf(p, "Uid: %*d %d", &proc->uid) == 1)
                    got_uid = 1;

                /*
                 * Advance to next line.
                 */
                p = strchr(p, '\n');
                if (p) p++;
            }

            if (!got_uid || !got_ppid) { /* arrgh, abort everything so far */
                free(cmdline);
                free(exe);
                free(status);
                free(argv);
                free(proc);
                continue;
            }
        }

        /*
         * If we get here, we've got everything we need. Add the
         * process to the list of things we can usefully work
         * with.
         */
        procs[pid] = proc;
        pidset_add(&ret, pid);
    }
    closedir(d);

    return ret;
}