excluding "." and ".." directories when calculating tids.
Signed-off-by: Gui Jianfeng <[email protected]>
---
tools/perf/util/thread.c | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..4f71d1c 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -17,6 +17,8 @@ int find_all_tid(int pid, pid_t ** all_tid)
sprintf(name, "/proc/%d/task", pid);
items = scandir(name, &namelist, NULL, NULL);
+ /* Excluding "." and ".." directories! */
+ items -= 2;
if (items <= 0)
return -ENOENT;
*all_tid = malloc(sizeof(pid_t) * items);
--
1.6.5.2
Gui Jianfeng wrote:
> excluding "." and ".." directories when calculating tids.
Please ignore this one, will post an updated version.
Thanks,
Gui
>
> Signed-off-by: Gui Jianfeng <[email protected]>
> ---
> tools/perf/util/thread.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 1f7ecd4..4f71d1c 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -17,6 +17,8 @@ int find_all_tid(int pid, pid_t ** all_tid)
>
> sprintf(name, "/proc/%d/task", pid);
> items = scandir(name, &namelist, NULL, NULL);
> + /* Excluding "." and ".." directories! */
> + items -= 2;
> if (items <= 0)
> return -ENOENT;
> *all_tid = malloc(sizeof(pid_t) * items);
Introduce a filter function to skip "." and ".." directories when calculating
tid number.
Signed-off-by: Gui Jianfeng <[email protected]>
---
tools/perf/util/thread.c | 11 ++++++++++-
1 files changed, 10 insertions(+), 1 deletions(-)
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..9a448b4 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -7,6 +7,15 @@
#include "util.h"
#include "debug.h"
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+ if (dir->d_name[0] == '.')
+ return 0;
+ else
+ return 1;
+}
+
int find_all_tid(int pid, pid_t ** all_tid)
{
char name[256];
@@ -16,7 +25,7 @@ int find_all_tid(int pid, pid_t ** all_tid)
int i;
sprintf(name, "/proc/%d/task", pid);
- items = scandir(name, &namelist, NULL, NULL);
+ items = scandir(name, &namelist, filter, NULL);
if (items <= 0)
return -ENOENT;
*all_tid = malloc(sizeof(pid_t) * items);
--
1.6.5.2
On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
> Introduce a filter function to skip "." and ".." directories when calculating
> tid number.
>
> Signed-off-by: Gui Jianfeng <[email protected]>
> ---
> tools/perf/util/thread.c | 11 ++++++++++-
> 1 files changed, 10 insertions(+), 1 deletions(-)
>
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 1f7ecd4..9a448b4 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -7,6 +7,15 @@
> #include "util.h"
> #include "debug.h"
>
> +/* Skip "." and ".." directories */
> +static int filter(const struct dirent *dir)
> +{
> + if (dir->d_name[0] == '.')
> + return 0;
> + else
> + return 1;
> +}
> +
>
Is this safe? Can you _never_ have a d_name with a leading dot, like '
.hidden' ??
Maybe should
if(dir->d_name[0] == '.' && (dir->d_name[1] == '\0' || (dir->d_name[1]
== '.' && dir->d_name[2] == '\0')))
Admittedly I don't think it happens in the current procfs, but I'd want
to be careful regardless.
Adam Schrotenboer wrote:
> On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
>> Introduce a filter function to skip "." and ".." directories when calculating
>> tid number.
>>
>> Signed-off-by: Gui Jianfeng <[email protected]>
>> ---
>> tools/perf/util/thread.c | 11 ++++++++++-
>> 1 files changed, 10 insertions(+), 1 deletions(-)
>>
>> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
>> index 1f7ecd4..9a448b4 100644
>> --- a/tools/perf/util/thread.c
>> +++ b/tools/perf/util/thread.c
>> @@ -7,6 +7,15 @@
>> #include "util.h"
>> #include "debug.h"
>>
>> +/* Skip "." and ".." directories */
>> +static int filter(const struct dirent *dir)
>> +{
>> + if (dir->d_name[0] == '.')
>> + return 0;
>> + else
>> + return 1;
>> +}
>> +
>>
>
> Is this safe? Can you _never_ have a d_name with a leading dot, like '
> .hidden' ??
> Maybe should
> if(dir->d_name[0] == '.' && (dir->d_name[1] == '\0' || (dir->d_name[1]
> == '.' && dir->d_name[2] == '\0')))
>
> Admittedly I don't think it happens in the current procfs, but I'd want
> to be careful regardless.
Actually, we only care the numeral directories. So, even if there's a ".hidden",
it's fine to filter out this directory. Just keep things simple here.
Thanks,
Gui
>
>
Em Thu, Jun 17, 2010 at 11:11:35AM +0800, Gui Jianfeng escreveu:
> Adam Schrotenboer wrote:
> > On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
> >> Introduce a filter function to skip "." and ".." directories when calculating
> >> tid number.
> >>
> >> +/* Skip "." and ".." directories */
> >> +static int filter(const struct dirent *dir)
> >> +{
> >> + if (dir->d_name[0] == '.')
> >> + return 0;
> >> + else
> >> + return 1;
> >
> > Is this safe? Can you _never_ have a d_name with a leading dot, like '
> > .hidden' ??
> > Admittedly I don't think it happens in the current procfs, but I'd want
> > to be careful regardless.
>
> Actually, we only care the numeral directories. So, even if there's a ".hidden",
> it's fine to filter out this directory. Just keep things simple here.
Agreed, applying to perf/core, thanks,
- Arnaldo
Commit-ID: c214909b36efec632432acdcbfacdd46a6e11370
Gitweb: http://git.kernel.org/tip/c214909b36efec632432acdcbfacdd46a6e11370
Author: Gui Jianfeng <[email protected]>
AuthorDate: Wed, 16 Jun 2010 13:21:44 +0800
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 1 Jul 2010 14:02:38 -0300
perf tools: Fix find tids routine by excluding "." and ".."
Introduce a filter function to skip "." and ".." directories when calculating
tid number, otherwise tid 0 will be included in the all_tid result array.
Cc: Ingo Molnar <[email protected]>
LKML-Reference: <[email protected]>
Signed-off-by: Gui Jianfeng <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/thread.c | 11 ++++++++++-
1 files changed, 10 insertions(+), 1 deletions(-)
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..9a448b4 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -7,6 +7,15 @@
#include "util.h"
#include "debug.h"
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+ if (dir->d_name[0] == '.')
+ return 0;
+ else
+ return 1;
+}
+
int find_all_tid(int pid, pid_t ** all_tid)
{
char name[256];
@@ -16,7 +25,7 @@ int find_all_tid(int pid, pid_t ** all_tid)
int i;
sprintf(name, "/proc/%d/task", pid);
- items = scandir(name, &namelist, NULL, NULL);
+ items = scandir(name, &namelist, filter, NULL);
if (items <= 0)
return -ENOENT;
*all_tid = malloc(sizeof(pid_t) * items);