proc: convert /proc/$PID/wchan to seq_file interface
[cascardo/linux.git] / fs / proc / base.c
index 2d696b0..353f287 100644 (file)
  */
 
 struct pid_entry {
-       char *name;
+       const char *name;
        int len;
        umode_t mode;
        const struct inode_operations *iop;
@@ -200,27 +200,32 @@ static int proc_root_link(struct dentry *dentry, struct path *path)
        return result;
 }
 
-static int proc_pid_cmdline(struct task_struct *task, char *buffer)
+static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
+                           struct pid *pid, struct task_struct *task)
 {
-       return get_cmdline(task, buffer, PAGE_SIZE);
+       /*
+        * Rely on struct seq_operations::show() being called once
+        * per internal buffer allocation. See single_open(), traverse().
+        */
+       BUG_ON(m->size < PAGE_SIZE);
+       m->count += get_cmdline(task, m->buf, PAGE_SIZE);
+       return 0;
 }
 
-static int proc_pid_auxv(struct task_struct *task, char *buffer)
+static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
+                        struct pid *pid, struct task_struct *task)
 {
        struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
-       int res = PTR_ERR(mm);
        if (mm && !IS_ERR(mm)) {
                unsigned int nwords = 0;
                do {
                        nwords += 2;
                } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
-               res = nwords * sizeof(mm->saved_auxv[0]);
-               if (res > PAGE_SIZE)
-                       res = PAGE_SIZE;
-               memcpy(buffer, mm->saved_auxv, res);
+               seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
                mmput(mm);
-       }
-       return res;
+               return 0;
+       } else
+               return PTR_ERR(mm);
 }
 
 
@@ -229,7 +234,8 @@ static int proc_pid_auxv(struct task_struct *task, char *buffer)
  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  * Returns the resolved symbol.  If that fails, simply return the address.
  */
-static int proc_pid_wchan(struct task_struct *task, char *buffer)
+static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
+                         struct pid *pid, struct task_struct *task)
 {
        unsigned long wchan;
        char symname[KSYM_NAME_LEN];
@@ -240,9 +246,9 @@ static int proc_pid_wchan(struct task_struct *task, char *buffer)
                if (!ptrace_may_access(task, PTRACE_MODE_READ))
                        return 0;
                else
-                       return sprintf(buffer, "%lu", wchan);
+                       return seq_printf(m, "%lu", wchan);
        else
-               return sprintf(buffer, "%s", symname);
+               return seq_printf(m, "%s", symname);
 }
 #endif /* CONFIG_KALLSYMS */
 
@@ -418,8 +424,8 @@ static int proc_oom_score(struct task_struct *task, char *buffer)
 }
 
 struct limit_names {
-       char *name;
-       char *unit;
+       const char *name;
+       const char *unit;
 };
 
 static const struct limit_names lnames[RLIM_NLIMITS] = {
@@ -442,12 +448,11 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 };
 
 /* Display limits for a process */
-static int proc_pid_limits(struct task_struct *task, char *buffer)
+static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
+                          struct pid *pid, struct task_struct *task)
 {
        unsigned int i;
-       int count = 0;
        unsigned long flags;
-       char *bufptr = buffer;
 
        struct rlimit rlim[RLIM_NLIMITS];
 
@@ -459,35 +464,34 @@ static int proc_pid_limits(struct task_struct *task, char *buffer)
        /*
         * print the file header
         */
-       count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
+       seq_printf(m, "%-25s %-20s %-20s %-10s\n",
                        "Limit", "Soft Limit", "Hard Limit", "Units");
 
        for (i = 0; i < RLIM_NLIMITS; i++) {
                if (rlim[i].rlim_cur == RLIM_INFINITY)
-                       count += sprintf(&bufptr[count], "%-25s %-20s ",
+                       seq_printf(m, "%-25s %-20s ",
                                         lnames[i].name, "unlimited");
                else
-                       count += sprintf(&bufptr[count], "%-25s %-20lu ",
+                       seq_printf(m, "%-25s %-20lu ",
                                         lnames[i].name, rlim[i].rlim_cur);
 
                if (rlim[i].rlim_max == RLIM_INFINITY)
-                       count += sprintf(&bufptr[count], "%-20s ", "unlimited");
+                       seq_printf(m, "%-20s ", "unlimited");
                else
-                       count += sprintf(&bufptr[count], "%-20lu ",
-                                        rlim[i].rlim_max);
+                       seq_printf(m, "%-20lu ", rlim[i].rlim_max);
 
                if (lnames[i].unit)
-                       count += sprintf(&bufptr[count], "%-10s\n",
-                                        lnames[i].unit);
+                       seq_printf(m, "%-10s\n", lnames[i].unit);
                else
-                       count += sprintf(&bufptr[count], "\n");
+                       seq_putc(m, '\n');
        }
 
-       return count;
+       return 0;
 }
 
 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
-static int proc_pid_syscall(struct task_struct *task, char *buffer)
+static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
+                           struct pid *pid, struct task_struct *task)
 {
        long nr;
        unsigned long args[6], sp, pc;
@@ -496,11 +500,11 @@ static int proc_pid_syscall(struct task_struct *task, char *buffer)
                return res;
 
        if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
-               res = sprintf(buffer, "running\n");
+               seq_puts(m, "running\n");
        else if (nr < 0)
-               res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
+               seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
        else
-               res = sprintf(buffer,
+               seq_printf(m,
                       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
                       nr,
                       args[0], args[1], args[2], args[3], args[4], args[5],
@@ -2056,7 +2060,7 @@ static int show_timer(struct seq_file *m, void *v)
        struct k_itimer *timer;
        struct timers_private *tp = m->private;
        int notify;
-       static char *nstr[] = {
+       static const char * const nstr[] = {
                [SIGEV_SIGNAL] = "signal",
                [SIGEV_NONE] = "none",
                [SIGEV_THREAD] = "thread",
@@ -2449,7 +2453,7 @@ static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
 
 #ifdef CONFIG_USER_NS
 static int proc_id_map_open(struct inode *inode, struct file *file,
-       struct seq_operations *seq_ops)
+       const struct seq_operations *seq_ops)
 {
        struct user_namespace *ns = NULL;
        struct task_struct *task;
@@ -2557,10 +2561,10 @@ static const struct pid_entry tgid_base_stuff[] = {
        DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
 #endif
        REG("environ",    S_IRUSR, proc_environ_operations),
-       INF("auxv",       S_IRUSR, proc_pid_auxv),
+       ONE("auxv",       S_IRUSR, proc_pid_auxv),
        ONE("status",     S_IRUGO, proc_pid_status),
        ONE("personality", S_IRUSR, proc_pid_personality),
-       INF("limits",     S_IRUGO, proc_pid_limits),
+       ONE("limits",     S_IRUGO, proc_pid_limits),
 #ifdef CONFIG_SCHED_DEBUG
        REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
 #endif
@@ -2569,9 +2573,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #endif
        REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
-       INF("syscall",    S_IRUSR, proc_pid_syscall),
+       ONE("syscall",    S_IRUSR, proc_pid_syscall),
 #endif
-       INF("cmdline",    S_IRUGO, proc_pid_cmdline),
+       ONE("cmdline",    S_IRUGO, proc_pid_cmdline),
        ONE("stat",       S_IRUGO, proc_tgid_stat),
        ONE("statm",      S_IRUGO, proc_pid_statm),
        REG("maps",       S_IRUGO, proc_pid_maps_operations),
@@ -2594,7 +2598,7 @@ static const struct pid_entry tgid_base_stuff[] = {
        DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
 #endif
 #ifdef CONFIG_KALLSYMS
-       INF("wchan",      S_IRUGO, proc_pid_wchan),
+       ONE("wchan",      S_IRUGO, proc_pid_wchan),
 #endif
 #ifdef CONFIG_STACKTRACE
        ONE("stack",      S_IRUSR, proc_pid_stack),
@@ -2780,12 +2784,12 @@ out:
 
 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
 {
-       int result = 0;
+       int result = -ENOENT;
        struct task_struct *task;
        unsigned tgid;
        struct pid_namespace *ns;
 
-       tgid = name_to_int(dentry);
+       tgid = name_to_int(&dentry->d_name);
        if (tgid == ~0U)
                goto out;
 
@@ -2896,18 +2900,18 @@ static const struct pid_entry tid_base_stuff[] = {
        DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
        DIR("ns",        S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
        REG("environ",   S_IRUSR, proc_environ_operations),
-       INF("auxv",      S_IRUSR, proc_pid_auxv),
+       ONE("auxv",      S_IRUSR, proc_pid_auxv),
        ONE("status",    S_IRUGO, proc_pid_status),
        ONE("personality", S_IRUSR, proc_pid_personality),
-       INF("limits",    S_IRUGO, proc_pid_limits),
+       ONE("limits",    S_IRUGO, proc_pid_limits),
 #ifdef CONFIG_SCHED_DEBUG
        REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
 #endif
        REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
-       INF("syscall",   S_IRUSR, proc_pid_syscall),
+       ONE("syscall",   S_IRUSR, proc_pid_syscall),
 #endif
-       INF("cmdline",   S_IRUGO, proc_pid_cmdline),
+       ONE("cmdline",   S_IRUGO, proc_pid_cmdline),
        ONE("stat",      S_IRUGO, proc_tid_stat),
        ONE("statm",     S_IRUGO, proc_pid_statm),
        REG("maps",      S_IRUGO, proc_tid_maps_operations),
@@ -2932,7 +2936,7 @@ static const struct pid_entry tid_base_stuff[] = {
        DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
 #endif
 #ifdef CONFIG_KALLSYMS
-       INF("wchan",     S_IRUGO, proc_pid_wchan),
+       ONE("wchan",     S_IRUGO, proc_pid_wchan),
 #endif
 #ifdef CONFIG_STACKTRACE
        ONE("stack",      S_IRUSR, proc_pid_stack),
@@ -3033,7 +3037,7 @@ static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry
        if (!leader)
                goto out_no_task;
 
-       tid = name_to_int(dentry);
+       tid = name_to_int(&dentry->d_name);
        if (tid == ~0U)
                goto out;