struct tgid_iter {
unsigned int tgid;
struct task_struct *task;
- struct pid_namespace *pid_ns;
+ struct pid_namespace *const pid_ns;
};
-static struct tgid_iter next_tgid(struct tgid_iter iter)
+static struct tgid_iter
+make_tgid_iter(unsigned int init_tgid, struct pid_namespace *pid_ns)
{
- struct pid *pid;
+ return (struct tgid_iter){
+ .tgid = init_tgid - 1,
+ .pid_ns = pid_ns,
+ };
+}
+
+static bool next_tgid(struct tgid_iter *it)
+{
+ if (it->task) {
+ put_task_struct(it->task);
+ it->task = NULL;
+ }
- if (iter.task)
- put_task_struct(iter.task);
rcu_read_lock();
-retry:
- iter.task = NULL;
- pid = find_ge_pid(iter.tgid, iter.pid_ns);
- if (pid) {
- iter.tgid = pid_nr_ns(pid, iter.pid_ns);
- iter.task = pid_task(pid, PIDTYPE_TGID);
- if (!iter.task) {
- iter.tgid += 1;
- goto retry;
+ while (1) {
+ it->tgid += 1;
+ const auto pid = find_ge_pid(it->tgid, it->pid_ns);
+ if (pid) {
+ it->tgid = pid_nr_ns(pid, it->pid_ns);
+ it->task = pid_task(pid, PIDTYPE_TGID);
+ if (it->task) {
+ get_task_struct(it->task);
+ rcu_read_unlock();
+ return true;
+ }
+ } else {
+ rcu_read_unlock();
+ return false;
}
- get_task_struct(iter.task);
}
- rcu_read_unlock();
- return iter;
}
#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
/* for the /proc/ directory itself, after non-process stuff has been done */
int proc_pid_readdir(struct file *file, struct dir_context *ctx)
{
- struct tgid_iter iter;
struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb);
struct pid_namespace *pid_ns = proc_pid_ns(file_inode(file)->i_sb);
loff_t pos = ctx->pos;
return 0;
ctx->pos = pos = pos + 1;
}
- iter.tgid = pos - TGID_OFFSET;
- iter.task = NULL;
- iter.pid_ns = pid_ns;
- for (iter = next_tgid(iter);
- iter.task;
- iter.tgid += 1, iter = next_tgid(iter)) {
+
+ auto iter = make_tgid_iter(pos - TGID_OFFSET, pid_ns);
+ while (next_tgid(&iter)) {
char name[10 + 1];
unsigned int len;