diff options
author | Oleg Nesterov <oleg@tv-sign.ru> | 2006-06-26 03:26:01 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-06-26 12:58:26 -0400 |
commit | a872ff0cb2218dc9688b990c5ccda064dc40946b (patch) | |
tree | ff94ae63e668f0229fc7aeed220def7e9708db3a /fs/proc | |
parent | cc288738c9ae3c64d3c50b86604044d1f6d22941 (diff) |
[PATCH] simplify/fix first_tid()
first_tid:
/* If nr exceeds the number of threads there is nothing todo */
if (nr) {
if (nr >= get_nr_threads(leader))
goto done;
}
This is not reliable: sub-threads can exit after this check, so the
'for' loop below can overlap and proc_task_readdir() can return an
already filldir'ed dirents.
for (; pos && pid_alive(pos); pos = next_thread(pos)) {
if (--nr > 0)
continue;
Off-by-one error, will return 'leader' when nr == 1.
This patch tries to fix these problems and simplify the code.
Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'fs/proc')
-rw-r--r-- | fs/proc/base.c | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c index 6092a6e2c5a9..5ee46d3a5cac 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c | |||
@@ -2227,38 +2227,34 @@ int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) | |||
2227 | static struct task_struct *first_tid(struct task_struct *leader, | 2227 | static struct task_struct *first_tid(struct task_struct *leader, |
2228 | int tid, int nr) | 2228 | int tid, int nr) |
2229 | { | 2229 | { |
2230 | struct task_struct *pos = NULL; | 2230 | struct task_struct *pos; |
2231 | 2231 | ||
2232 | rcu_read_lock(); | 2232 | rcu_read_lock(); |
2233 | /* Attempt to start with the pid of a thread */ | 2233 | /* Attempt to start with the pid of a thread */ |
2234 | if (tid && (nr > 0)) { | 2234 | if (tid && (nr > 0)) { |
2235 | pos = find_task_by_pid(tid); | 2235 | pos = find_task_by_pid(tid); |
2236 | if (pos && (pos->group_leader != leader)) | 2236 | if (pos && (pos->group_leader == leader)) |
2237 | pos = NULL; | 2237 | goto found; |
2238 | if (pos) | ||
2239 | nr = 0; | ||
2240 | } | 2238 | } |
2241 | 2239 | ||
2242 | /* If nr exceeds the number of threads there is nothing todo */ | 2240 | /* If nr exceeds the number of threads there is nothing todo */ |
2243 | if (nr) { | 2241 | pos = NULL; |
2244 | if (nr >= get_nr_threads(leader)) | 2242 | if (nr && nr >= get_nr_threads(leader)) |
2245 | goto done; | 2243 | goto out; |
2246 | } | ||
2247 | 2244 | ||
2248 | /* If we haven't found our starting place yet start with the | 2245 | /* If we haven't found our starting place yet start |
2249 | * leader and walk nr threads forward. | 2246 | * with the leader and walk nr threads forward. |
2250 | */ | 2247 | */ |
2251 | if (!pos && (nr >= 0)) | 2248 | for (pos = leader; nr > 0; --nr) { |
2252 | pos = leader; | 2249 | pos = next_thread(pos); |
2253 | 2250 | if (pos == leader) { | |
2254 | for (; pos && pid_alive(pos); pos = next_thread(pos)) { | 2251 | pos = NULL; |
2255 | if (--nr > 0) | 2252 | goto out; |
2256 | continue; | 2253 | } |
2257 | get_task_struct(pos); | ||
2258 | goto done; | ||
2259 | } | 2254 | } |
2260 | pos = NULL; | 2255 | found: |
2261 | done: | 2256 | get_task_struct(pos); |
2257 | out: | ||
2262 | rcu_read_unlock(); | 2258 | rcu_read_unlock(); |
2263 | return pos; | 2259 | return pos; |
2264 | } | 2260 | } |