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

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

 



On Tue, 2011-05-03 at 10:44 +0200, Karel Zak wrote:
> 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()
> 

OK, you want this fancier than I thought :) My initial focus was
something quick and simple, ala strutils.

> 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/ */

I guess we can use something based on BUFSIZ (like BUFSIZ/2?). No need
to make it too big.
> 
> 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 ;-)

Something like strtol_or_err()?

Thanks,
Davidlohr

--
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