Re: [PATCH 1/2] lib/procutils: general purpose procfs parsing functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, May 02, 2011 at 10:04:13PM -0300, Davidlohr Bueso wrote:
> From: Davidlohr Bueso <dave@xxxxxxx>
> Date: Mon, 2 May 2011 21:48:05 -0300
> 
> This patch introduces the first function in the file,
> proc_getthreads(), which obtains the threads for a given PID.

 Cool!

 What about to use more talkative a easily extendible API:

        struct proc_tasks *ts;
        pid_t tid;

        ts = proc_open_tasks(<pid>);

        while(proc_next_tid(ts, &tid) == 0) {
            ...
        }

        proc_close_tasks(ts);

where 
    - /proc/#/tasks 'DIR *' is in 'struct proc_tasks'
    - opendir() in proc_open_tasks()
    - readdir() in proc_next_tid()
    - closedir() in  proc_close_tasks()

one day we can easily extend this API and add for example

    proc_next_task_cpuset(struct proc_tasks *, pid_t *tid, cpu_set_t *)

or whatever. It's also a simple way how to avoid the realloc().

> +	char path[18]; /* lenght of /proc/<5digits-pid>/task/ */

pid_t is __S32_TYPE, so it would be better to use something larger.

> +	sprintf(path, "/proc/%d/task/", pid);
> +
> +	dir = opendir(path);
> +	if (!dir)
> +		goto ret;
> +
> +	while ((d = readdir(dir))) {
> +		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
> +			continue;
> +		list = (i == 0) ? malloc(sizeof(pid_t)) : 
> +			realloc(list, (i + 1) * sizeof(pid_t));

 unnecessary, realloc(NULL, <size>) works like malloc(<size>);

> +		list[i++] = (pid_t) strtol(d->d_name, (char **) NULL, 10);

 this should be more paranoid ;-)

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux