Skip to contentSkip to conversation

4096 byte limit to /proc/PID/environ ?

DISCUSSION James Pearson 7 participants 18 messages May 24, 2006 – Sep 6, 2007 1 message not in the archive Proc filesystem

lore
Share & tools
lore
#0 lore raw
Quoted 8 lines · H. Peter Anvin
Quoted 8 lines

I'm not worried about that - more the fact that when I do:

% cat /proc/$$/environ | wc -c
4096
% env | wc -c
7329

Quoted 6 lines
Quoted 2 lines

/proc/PID/environ is truncated ...

Funny enough, I was looking at this yesterday. I think there is a pretty clean solution for it, I just haven't had a chance to attack it yet.

Having a poke about in fs/proc/, I came up with this - probably isn't pretty or clean, but it works ...

James Pearson

include/linux/proc_fs.h 2006-05-24 13:43:55.964159897 +0100 renamed from include/linux/proc_fs.h.dist 2006-05-11 02:56:24.000000000 +0100 +0 -0
@@ -250,7 +250,7 @@
        int type;
        union {
                int (*proc_get_link)(struct inode *, struct dentry **, 

struct vfsmount **);
- int (*proc_read)(struct task_struct *task, char *page);

+               int (*proc_read)(struct task_struct *task, char *page, loff_t *pos);
        } op;
        struct proc_dir_entry *pde;
        struct inode vfs_inode;
fs/proc/base.c 2006-05-24 13:54:26.370965292 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +17 -5
@@ -409,15 +409,27 @@
         (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
         security_ptrace(current,task) == 0))
 
-static int proc_pid_environ(struct task_struct *task, char * buffer)
+static int proc_pid_environ(struct task_struct *task, char * buffer, loff_t *pos)
 {
        int res = 0;
+       int p = *pos;
        struct mm_struct *mm = get_task_mm(task);
+
+       /* proc_pid_environ is a 'special case' - the required data could
+          be larger than a page, so we read sequential chunks of the
+          environment data into the buffer using the supplied offset */
+       if (p < 0)
+               return -EINVAL;
+
        if (mm) {
-               unsigned int len = mm->env_end - mm->env_start;
+               unsigned int len = mm->env_end - (mm->env_start + p);
                if (len > PAGE_SIZE)
                        len = PAGE_SIZE;
-               res = access_process_vm(task, mm->env_start, buffer, len, 0);
+               res = access_process_vm(task, (mm->env_start + p), buffer, len, 0);
+               /* the calling routine (proc_info_read) needs to know we've
+                  used the offset to read the pid data */
+               *pos += res;
+
                if (!ptrace_may_attach(task))
                        res = -ESRCH;
                mmput(mm);
@@ -425,7 +437,7 @@
        return res;
 }
 
-static int proc_pid_cmdline(struct task_struct *task, char * buffer)
+static int proc_pid_cmdline(struct task_struct *task, char * buffer, loff_t *pos)
 {
        int res = 0;
        unsigned int len;
@@ -462,7 +474,7 @@
        return res;
 }
 
-static int proc_pid_auxv(struct task_struct *task, char *buffer)
+static int proc_pid_auxv(struct task_struct *task, char *buffer, loff_t *pos)
 {
        int res = 0;
        struct mm_struct *mm = get_task_mm(task);
@@ -486,7 +498,7 @@
  * 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 task_struct *task, char *buffer, loff_t *pos)

 {
        char *modname;
        const char *sym_name;

@@ -506,7 +518,7 @@
/*
* Provides /proc/PID/schedstat
*/
-static int proc_pid_schedstat(struct task_struct *task, char *buffer) +static int proc_pid_schedstat(struct task_struct *task, char *buffer, loff_t *pos)
{
return sprintf(buffer, "%lu %lu %lu\n",
task->sched_info.cpu_time,
@@ -517,7 +529,7 @@

/* The badness from the OOM killer */
unsigned long badness(struct task_struct *p, unsigned long uptime);
-static int proc_oom_score(struct task_struct *task, char *buffer) +static int proc_oom_score(struct task_struct *task, char *buffer, loff_t *pos)

 {
        unsigned long points;
        struct timespec uptime;
@@ -745,16 +757,30 @@
        unsigned long page;
        ssize_t length;
        struct task_struct *task = proc_task(inode);

+ loff_t pos = *ppos;

        if (count > PROC_BLOCK_SIZE)
                count = PROC_BLOCK_SIZE;
        if (!(page = __get_free_page(GFP_KERNEL)))
                return -ENOMEM;

- length = PROC_I(inode)->op.proc_read(task, (char*)page);
+ length = PROC_I(inode)->op.proc_read(task, (char*)page, &pos);

- if (length >= 0)
- length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
+ if (length >= 0) {
+ if (pos != *ppos) {
+ /* we are using the buffer to store subsequent sections

+                          of the proc_pid data - so the offset into the buffer
+                          will always be 0 - the offset is used to get the
+                          original data */
+                       pos = 0;
+                       length = simple_read_from_buffer(buf, count, &pos, (char *)page, length);
+                       /* need to update the 'real' offset into the data we

+ are reading ... */
+ *ppos += length;
+ }
+ else
+ length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
+ }

        free_page(page);
        return length;

}

fs/proc/internal.h 2006-05-24 13:52:53.568391757 +0100 renamed from fs/proc/internal.h.dist 2006-05-11 02:56:24.000000000 +0100 +0 -0
@@ -32,10 +32,10 @@
 
 extern void create_seq_entry(char *name, mode_t mode, struct 

file_operations *f);
extern int proc_exe_link(struct inode *, struct dentry **, struct
vfsmount **);
-extern int proc_tid_stat(struct task_struct *, char *); -extern int proc_tgid_stat(struct task_struct *, char *); -extern int proc_pid_status(struct task_struct *, char *); -extern int proc_pid_statm(struct task_struct *, char *); +extern int proc_tid_stat(struct task_struct *, char *, loff_t *); +extern int proc_tgid_stat(struct task_struct *, char *, loff_t *); +extern int proc_pid_status(struct task_struct *, char *, loff_t *); +extern int proc_pid_statm(struct task_struct *, char *, loff_t *);

void free_proc_entry(struct proc_dir_entry *de);

fs/proc/array.c 2006-05-24 13:43:09.327905203 +0100 renamed from fs/proc/array.c.dist 2006-05-11 02:56:24.000000000 +0100 +4 -4
@@ -293,7 +293,7 @@
                            cap_t(p->cap_effective));
 }
 
-int proc_pid_status(struct task_struct *task, char * buffer)
+int proc_pid_status(struct task_struct *task, char * buffer, loff_t *pos)
 {
        char * orig = buffer;
        struct mm_struct *mm = get_task_mm(task);
@@ -465,17 +465,17 @@
        return res;
 }
 
-int proc_tid_stat(struct task_struct *task, char * buffer)
+int proc_tid_stat(struct task_struct *task, char * buffer, loff_t *pos)
 {
        return do_task_stat(task, buffer, 0);
 }
 
-int proc_tgid_stat(struct task_struct *task, char * buffer)
+int proc_tgid_stat(struct task_struct *task, char * buffer, loff_t *pos)
 {
        return do_task_stat(task, buffer, 1);
 }
 
-int proc_pid_statm(struct task_struct *task, char *buffer)
+int proc_pid_statm(struct task_struct *task, char *buffer, loff_t *pos)
 {
        int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data 
= 0;
        struct mm_struct *mm = get_task_mm(task);
#1 lore raw

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

-hpa

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 6 lines · H. Peter Anvin

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

Quoted 7 lines

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

The following patch is based on the /proc/PID/mem code appears to work fine.

James Pearson

fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +78 -21
@@ -409,22 +409,6 @@
         (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
         security_ptrace(current,task) == 0))
 
-static int proc_pid_environ(struct task_struct *task, char * buffer)
-{
-       int res = 0;
-       struct mm_struct *mm = get_task_mm(task);
-       if (mm) {
-               unsigned int len = mm->env_end - mm->env_start;
-               if (len > PAGE_SIZE)
-                       len = PAGE_SIZE;
-               res = access_process_vm(task, mm->env_start, buffer, len, 0);
-               if (!ptrace_may_attach(task))
-                       res = -ESRCH;
-               mmput(mm);
-       }
-       return res;
-}
-
 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
 {
        int res = 0;
@@ -897,6 +881,80 @@
        .open           = mem_open,
 };
 
+static ssize_t env_read(struct file * file, char __user * buf,
+                       size_t count, loff_t *ppos)
+{
+       struct task_struct *task = proc_task(file->f_dentry->d_inode);
+       char *page;
+       unsigned long src = *ppos;
+       int ret = -ESRCH;
+       struct mm_struct *mm;
+       size_t max_len;
+
+       if (!ptrace_may_attach(task))
+               goto out;
+
+       ret = -ENOMEM;
+       page = (char *)__get_free_page(GFP_USER);
+       if (!page)
+               goto out;
+
+       ret = 0;
+
+       mm = get_task_mm(task);
+       if (!mm)
+               goto out_free;
+
+       ret = 0;
+       max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
+
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }
+
+               if (this_len > max_len)
+                       this_len = max_len;
+
+               retval = access_process_vm(task, (mm->env_start + src), page, this_len, 0);
+
+               if (!ptrace_may_attach(task)) {
+                       ret = -ESRCH;
+                       break;
+               }
+
+               if (retval <= 0) {
+                       ret = retval;
+                       break;
+               }
+
+               if (copy_to_user(buf, page, retval)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret += retval;
+               src += retval;
+               buf += retval;
+               count -= retval;
+       }
+       *ppos = src;
+
+       mmput(mm);
+out_free:
+       free_page((unsigned long) page);
+out:
+       return ret;
+}
+
+static struct file_operations proc_env_operations = {
+       .read           = env_read,
+};
+
 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
                                size_t count, loff_t *ppos)
 {
@@ -1675,11 +1733,6 @@
                        inode->i_op = &proc_pid_link_inode_operations;
                        ei->op.proc_get_link = proc_root_link;
                        break;
-               case PROC_TID_ENVIRON:
-               case PROC_TGID_ENVIRON:
-                       inode->i_fop = &proc_info_file_operations;
-                       ei->op.proc_read = proc_pid_environ;
-                       break;
                case PROC_TID_AUXV:
                case PROC_TGID_AUXV:
                        inode->i_fop = &proc_info_file_operations;
@@ -1723,6 +1776,10 @@
                        inode->i_op = &proc_mem_inode_operations;
                        inode->i_fop = &proc_mem_operations;
                        break;
+               case PROC_TID_ENVIRON:
+               case PROC_TGID_ENVIRON:
+                       inode->i_fop = &proc_env_operations;
+                       break;
 #ifdef CONFIG_SECCOMP
                case PROC_TID_SECCOMP:
                case PROC_TGID_SECCOMP:
#4 lore raw
Quoted 142 lines · James Pearson
Quoted 6 lines · H. Peter Anvin

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

Quoted 7 lines

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

The following patch is based on the /proc/PID/mem code appears to work fine.

James Pearson

fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +0 -16
@@ -409,22 +409,6 @@
          (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
          security_ptrace(current,task) == 0))
 
-static int proc_pid_environ(struct task_struct *task, char * buffer)
-{
-       int res = 0;
-       struct mm_struct *mm = get_task_mm(task);
-       if (mm) {
-               unsigned int len = mm->env_end - mm->env_start;
-               if (len > PAGE_SIZE)
-                       len = PAGE_SIZE;
-               res = access_process_vm(task, mm->env_start, buffer,
 len, 0);
-               if (!ptrace_may_attach(task))
-                       res = -ESRCH;
-               mmput(mm);
-       }
-       return res;
-}
-
  static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  {
         int res = 0;
fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +74 -0
@@ -897,6 +881,80 @@
         .open           = mem_open,
  };
 
+static ssize_t env_read(struct file * file, char __user * buf,
+                       size_t count, loff_t *ppos)
+{
+       struct task_struct *task = proc_task(file->f_dentry->d_inode);
+       char *page;
+       unsigned long src = *ppos;
+       int ret = -ESRCH;
+       struct mm_struct *mm;
+       size_t max_len;
+
+       if (!ptrace_may_attach(task))
+               goto out;
+
+       ret = -ENOMEM;
+       page = (char *)__get_free_page(GFP_USER);
+       if (!page)
+               goto out;
+
+       ret = 0;
+
+       mm = get_task_mm(task);
+       if (!mm)
+               goto out_free;
+
+       ret = 0;
+       max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
+
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }
+
+               if (this_len > max_len)
+                       this_len = max_len;
+
+               retval = access_process_vm(task, (mm->env_start + src),
 page, this_len, 0);
+
+               if (!ptrace_may_attach(task)) {
+                       ret = -ESRCH;
+                       break;
+               }
+
+               if (retval <= 0) {
+                       ret = retval;
+                       break;
+               }
+
+               if (copy_to_user(buf, page, retval)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret += retval;
+               src += retval;
+               buf += retval;
+               count -= retval;
+       }
+       *ppos = src;
+
+       mmput(mm);
+out_free:
+       free_page((unsigned long) page);
+out:
+       return ret;
+}
+
+static struct file_operations proc_env_operations = {
+       .read           = env_read,
+};
+
  static ssize_t oom_adjust_read(struct file *file, char __user *buf,
                                 size_t count, loff_t *ppos)
  {
fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +0 -5
@@ -1675,11 +1733,6 @@
                         inode->i_op = &proc_pid_link_inode_operations;
                         ei->op.proc_get_link = proc_root_link;
                         break;
-               case PROC_TID_ENVIRON:
-               case PROC_TGID_ENVIRON:
-                       inode->i_fop = &proc_info_file_operations;
-                       ei->op.proc_read = proc_pid_environ;
-                       break;
                 case PROC_TID_AUXV:
                 case PROC_TGID_AUXV:
                         inode->i_fop = &proc_info_file_operations;
@@ -1723,6 +1776,10 @@
                         inode->i_op = &proc_mem_inode_operations;

fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100

Quoted 10 lines
fs/proc/base.c 2006-06-01 13:40:50.865851007 +0100 renamed from fs/proc/base.c.dist 2006-05-11 02:56:24.000000000 +0100 +4 -1
                         inode->i_fop = &proc_mem_operations;
                         break;
+               case PROC_TID_ENVIRON:
+               case PROC_TGID_ENVIRON:
+                       inode->i_fop = &proc_env_operations;
+                       break;
  #ifdef CONFIG_SECCOMP
                 case PROC_TID_SECCOMP:
                 case PROC_TGID_SECCOMP:
-

This thread has gone stale. The PAGE_SIZE limit still exists. Is this solution acceptable?

thanks,
--Guy Streeter

#5 lore raw
Quoted 12 lines · Guy Streeter
Quoted 11 lines · James Pearson
Quoted 10 lines · H. Peter Anvin

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

The following patch is based on the /proc/PID/mem code appears to work fine.

This thread has gone stale. The PAGE_SIZE limit still exists. Is this solution acceptable?

Can we avoid the code duplication?

-hpa

#6 lore raw
Quoted 16 lines · H. Peter Anvin
Quoted 15 lines · Guy Streeter
Quoted 14 lines · James Pearson
Quoted 13 lines · H. Peter Anvin

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

The following patch is based on the /proc/PID/mem code appears to work fine.

This thread has gone stale. The PAGE_SIZE limit still exists. Is this solution acceptable?

Can we avoid the code duplication?

I hope you're not asking me. I don't know the proc fs code well enough to re-write this.

--Guy

#7 lore raw
In reply to H. Peter Anvin
Quoted 23 lines · H. Peter Anvin
Quoted 21 lines · Guy Streeter
Quoted 18 lines · James Pearson
Quoted 6 lines · H. Peter Anvin

I think this is the wrong approach.

Many of these should probably be converted to seq_file, but in the particular case of environ, the right approach is to observe the fact that reading environ is just like reading /proc/PID/mem, except:

Quoted 7 lines

a. the access restrictions are less strict, and
b. there is a range restriction, which needs to be enforced, and c. there is an offset.

Pretty much, take the guts from /proc/PID/mem and generalize it slightly, and you have the code that can run either /proc/PID/mem or /proc/PID/environ.

The following patch is based on the /proc/PID/mem code appears to work fine.

Quoted 6 lines
Quoted 3 lines

This thread has gone stale. The PAGE_SIZE limit still exists. Is this solution acceptable?

Can we avoid the code duplication?

There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

James Pearson

#8 lore raw
Quoted 4 lines · James Pearson
Quoted 3 lines · H. Peter Anvin
Quoted 2 lines · Guy Streeter
Quoted 1 line · James Pearson

H. Peter Anvin wrote:

[...skipped...]

Quoted 10 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

The following patch is based on the /proc/PID/mem code appears to work fine.

This thread has gone stale. The PAGE_SIZE limit still exists. Is this solution acceptable?

Can we avoid the code duplication?

There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:
<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Yep, I'm sure we should not reuse something from mem part, there are some insignificant code duplicating. If we will try to avoid this, it will complicate code and does not bring any advantage.

Signature · 1 line
Anton Arapov, <[email protected]>
#9 lore raw
In reply to James Pearson

Hey guys, the future of this patch is important for me. What do you think, has this patch any chances to be committed to upstream?

Quoted 8 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Signature · 1 line
Anton Arapov, <[email protected]>
#10 lore raw
Quoted 12 lines · Anton Arapov

Hey guys, the future of this patch is important for me. What do you think, has this patch any chances to be committed to upstream?

Quoted 8 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Looks reasonable to me, except for the one overlong line.

-hpa

#11 lore raw
Quoted 12 lines · H. Peter Anvin
Quoted 10 lines · Anton Arapov

Hey guys, the future of this patch is important for me. What do you
think, has this patch any chances to be committed to upstream?

Quoted 5 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Looks reasonable to me, except for the one overlong line.

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

James Pearson

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +81 -23
@@ -199,27 +199,6 @@ static int proc_root_link(struct inode *
         (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
         security_ptrace(current,task) == 0))
 
-static int proc_pid_environ(struct task_struct *task, char * buffer)
-{
-       int res = 0;
-       struct mm_struct *mm = get_task_mm(task);
-       if (mm) {
-               unsigned int len;
-
-               res = -ESRCH;
-               if (!ptrace_may_attach(task))
-                       goto out;
-
-               len  = mm->env_end - mm->env_start;
-               if (len > PAGE_SIZE)
-                       len = PAGE_SIZE;
-               res = access_process_vm(task, mm->env_start, buffer, len, 0);
-out:
-               mmput(mm);
-       }
-       return res;
-}
-
 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
 {
        int res = 0;
@@ -658,6 +637,85 @@ static const struct file_operations proc
        .open           = mem_open,
 };
 
+static ssize_t environ_read(struct file * file, char __user * buf,
+                       size_t count, loff_t *ppos)
+{
+       struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
+       char *page;
+       unsigned long src = *ppos;
+       int ret = -ESRCH;
+       struct mm_struct *mm;
+       size_t max_len;
+
+       if (!task)
+               goto out_no_task;
+
+       if (!ptrace_may_attach(task))
+               goto out;
+
+       ret = -ENOMEM;
+       page = (char *)__get_free_page(GFP_USER);
+       if (!page)
+               goto out;
+
+       ret = 0;
+
+       mm = get_task_mm(task);
+       if (!mm)
+               goto out_free;
+
+       max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
+
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }
+
+               if (this_len > max_len)
+                       this_len = max_len;
+
+               retval = access_process_vm(task, (mm->env_start + src),
+                       page, this_len, 0);
+
+               if (!ptrace_may_attach(task)) {
+                       ret = -ESRCH;
+                       break;
+               }
+
+               if (retval <= 0) {
+                       ret = retval;
+                       break;
+               }
+
+               if (copy_to_user(buf, page, retval)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret += retval;
+               src += retval;
+               buf += retval;
+               count -= retval;
+       }
+       *ppos = src;
+
+       mmput(mm);
+out_free:
+       free_page((unsigned long) page);
+out:
+       put_task_struct(task);
+out_no_task:
+       return ret;
+}
+
+static const struct file_operations proc_environ_operations = {
+       .read           = environ_read,
+};
+
 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
                                size_t count, loff_t *ppos)
 {
@@ -2048,7 +2106,7 @@ static const struct pid_entry tgid_base_
        DIR("task",       S_IRUGO|S_IXUGO, task),
        DIR("fd",         S_IRUSR|S_IXUSR, fd),
        DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
-       INF("environ",    S_IRUSR, pid_environ),
+       REG("environ",    S_IRUSR, environ),
        INF("auxv",       S_IRUSR, pid_auxv),
        INF("status",     S_IRUGO, pid_status),
 #ifdef CONFIG_SCHED_DEBUG
@@ -2335,7 +2393,7 @@ out_no_task:
 static const struct pid_entry tid_base_stuff[] = {
        DIR("fd",        S_IRUSR|S_IXUSR, fd),
        DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
-       INF("environ",   S_IRUSR, pid_environ),
+       REG("environ",   S_IRUSR, environ),
        INF("auxv",      S_IRUSR, pid_auxv),
        INF("status",    S_IRUGO, pid_status),
 #ifdef CONFIG_SCHED_DEBUG
#12 lore raw
Quoted 19 lines · On Wed, 05 Sep 2007 18:00:57 +0100 James Pearson wrote:
Quoted 18 lines · H. Peter Anvin
Quoted 15 lines · Anton Arapov

Hey guys, the future of this patch is important for me. What do you
think, has this patch any chances to be committed to upstream?

Quoted 8 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Quoted 5 lines
Quoted 2 lines

Looks reasonable to me, except for the one overlong line.

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Hi,

a. It needs a changelog that describes the problem and the patch. b. It needs to apply cleanly to a current kernel.
(It does not apply cleanly now due to some odd line breaks [see #1 below.)
c. It needs to use tabs instead of spaces. That will probably help
on item b as well.

linux-2.6.23-rc5> dryrun < ~/fs-proc-read-sizes.patch
4 out of 4 hunks FAILED -- saving rejects to file fs/proc/base.c.rej

Even using patch -l (--ignore-whitespace), it fails with:

linux-2.6.23-rc5> patch -p1 -bl --dry-run --verbose < ~/fs-proc-read-sizes.patch Hmm... Looks like a unified diff to me...
The text leading up to this was:
--------------------------
|--- ./fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100
|+++ ./fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100
--------------------------
Patching file fs/proc/base.c using Plan A...
Hunk #1 FAILED at 199.
Hunk #2 FAILED at 637.
Hunk #3 succeeded at 2106 with fuzz 1.
Hunk #4 succeeded at 2393 with fuzz 1.
2 out of 4 hunks FAILED -- saving rejects to file fs/proc/base.c.rej done

Quoted 12 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +0 -6
@@ -199,27 +199,6 @@ static int proc_root_link(struct inode *
          (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
          security_ptrace(current,task) == 0))
 
-static int proc_pid_environ(struct task_struct *task, char * buffer)
-{
-       int res = 0;
-       struct mm_struct *mm = get_task_mm(task);
-       if (mm) {
-               unsigned int len;

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 static int proc_root_link(struct inode * · line 202

Quoted 10 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +0 -9
-
-               res = -ESRCH;
-               if (!ptrace_may_attach(task))
-                       goto out;
-
-               len  = mm->env_end - mm->env_start;
-               if (len > PAGE_SIZE)
-                       len = PAGE_SIZE;
-               res = access_process_vm(task, mm->env_start, buffer, 
 len, 0);

#1 ^^^^^^^

Quoted 105 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +0 -6
-out:
-               mmput(mm);
-       }
-       return res;
-}
-
  static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  {
         int res = 0;
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +80 -1
@@ -658,6 +637,85 @@ static const struct file_operations proc
         .open           = mem_open,
  };
 
+static ssize_t environ_read(struct file * file, char __user * buf,
+                       size_t count, loff_t *ppos)
+{
+       struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
+       char *page;
+       unsigned long src = *ppos;
+       int ret = -ESRCH;
+       struct mm_struct *mm;
+       size_t max_len;
+
+       if (!task)
+               goto out_no_task;
+
+       if (!ptrace_may_attach(task))
+               goto out;
+
+       ret = -ENOMEM;
+       page = (char *)__get_free_page(GFP_USER);
+       if (!page)
+               goto out;
+
+       ret = 0;
+
+       mm = get_task_mm(task);
+       if (!mm)
+               goto out_free;
+
+       max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
+
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }
+
+               if (this_len > max_len)
+                       this_len = max_len;
+
+               retval = access_process_vm(task, (mm->env_start + src),
+                       page, this_len, 0);
+
+               if (!ptrace_may_attach(task)) {
+                       ret = -ESRCH;
+                       break;
+               }
+
+               if (retval <= 0) {
+                       ret = retval;
+                       break;
+               }
+
+               if (copy_to_user(buf, page, retval)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret += retval;
+               src += retval;
+               buf += retval;
+               count -= retval;
+       }
+       *ppos = src;
+
+       mmput(mm);
+out_free:
+       free_page((unsigned long) page);
+out:
+       put_task_struct(task);
+out_no_task:
+       return ret;
+}
+
+static const struct file_operations proc_environ_operations = {
+       .read           = environ_read,
+};
+
  static ssize_t oom_adjust_read(struct file *file, char __user *buf,
                                 size_t count, loff_t *ppos)
  {
@@ -2048,7 +2106,7 @@ static const struct pid_entry tgid_base_
         DIR("task",       S_IRUGO|S_IXUGO, task),
         DIR("fd",         S_IRUSR|S_IXUSR, fd),
         DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
-       INF("environ",    S_IRUSR, pid_environ),
+       REG("environ",    S_IRUSR, environ),
         INF("auxv",       S_IRUSR, pid_auxv),
         INF("status",     S_IRUGO, pid_status),
  #ifdef CONFIG_SCHED_DEBUG

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 out_no_task:

Quoted 9 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +1 -2
@@ -2335,7 +2393,7 @@ out_no_task:
  static const struct pid_entry tid_base_stuff[] = {
         DIR("fd",        S_IRUSR|S_IXUSR, fd),
         DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
-       INF("environ",   S_IRUSR, pid_environ),
+       REG("environ",   S_IRUSR, environ),
         INF("auxv",      S_IRUSR, pid_auxv),
         INF("status",    S_IRUGO, pid_status),
  #ifdef CONFIG_SCHED_DEBUG
-

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#13 lore raw
Quoted 6 lines · Randy Dunlap
Quoted 2 lines

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Hi,

Quoted 8 lines

a. It needs a changelog that describes the problem and the patch. b. It needs to apply cleanly to a current kernel.
(It does not apply cleanly now due to some odd line breaks [see #1 below.)
c. It needs to use tabs instead of spaces. That will probably help
on item b as well.

linux-2.6.23-rc5> dryrun < ~/fs-proc-read-sizes.patch 4 out of 4 hunks FAILED -- saving rejects to file fs/proc/base.c.rej

I think a 'cut-n-paste' to my mail app mangled the patch - I'll re-submit it 'cleanly' ...

Thanks

James Pearson

#14 lore raw
In reply to James Pearson
Quoted 19 lines · James Pearson
Quoted 18 lines · H. Peter Anvin
Quoted 15 lines · Anton Arapov

Hey guys, the future of this patch is important for me. What do you
think, has this patch any chances to be committed to upstream?

Quoted 8 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Quoted 5 lines
Quoted 2 lines

Looks reasonable to me, except for the one overlong line.

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Remove duplicate ptrace_may_attach() checks, unecessary (), {} and spaces before pointer names -- char *buf.

#15 lore raw
Quoted 22 lines · Alexey Dobriyan
Quoted 20 lines · James Pearson
Quoted 18 lines · H. Peter Anvin
Quoted 15 lines · Anton Arapov

Hey guys, the future of this patch is important for me. What do you think, has this patch any chances to be committed to upstream?

Quoted 8 lines · James Pearson

H. Peter Anvin wrote:
There isn't that much that is duplicated - and there are also bits of the /proc/PID/mem code that are not needed in this case, so I'm not really sure if it is worth doing.

I did submit a patch a few months ago - see:

<http://marc.info/?l=linux-kernel&m=117862109623007&w=2>

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Looks reasonable to me, except for the one overlong line.

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Remove duplicate ptrace_may_attach() checks, unecessary (), {} and spaces before pointer names -- char *buf.

environ_read() in the patch uses ptrace_may_attach() in a similar way as does mem_read(). Given that environ_read() is based on mem_read(), does this mean that duplicate ptrace_may_attach() checks need to be removed from mem_read() as well? Which ptrace_may_attach() needs to be removed?

Thanks

James Pearson

#16 lore raw
In reply to James Pearson
Quoted 3 lines · James Pearson

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Someone has to point out CodingStyle, so I'll ruin everyone's day now :)

Quoted 1 line

+static ssize_t environ_read(struct file * file, char __user * buf,

^ ^
be gone these spaces.

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 static const struct file_operations proc · line 676

Quoted 8 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +8 -0
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }

Be gone, braces.

#17 lore raw
Quoted 10 lines · Jan Engelhardt
Quoted 2 lines · James Pearson

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Someone has to point out CodingStyle, so I'll ruin everyone's day now :)

Quoted 1 line

+static ssize_t environ_read(struct file * file, char __user * buf,

^ ^
be gone these spaces.

Quoted 11 lines

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 static const struct file_operations proc · line 676

Quoted 8 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +8 -0
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }

Be gone, braces.

Right, also please use use checkpatch.pl.

-hpa

#18 lore raw
In reply to Jan Engelhardt
Quoted 7 lines · Jan Engelhardt
Quoted 3 lines · James Pearson

OK, here is the patch (without the long line) against 2.6.23-rc5 - what else needs to be done to get it committed?

Someone has to point out CodingStyle, so I'll ruin everyone's day now :)

Already pointed out! :) By Alexey Dobriyan.

Quoted 17 lines
Quoted 1 line

+static ssize_t environ_read(struct file * file, char __user * buf,

^ ^
be gone these spaces.

fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 static const struct file_operations proc · line 676

Quoted 8 lines
fs/proc/base.c 2007-09-05 14:08:15.762518000 +0100 renamed from fs/proc/base.c.dist 2007-09-01 07:08:24.000000000 +0100 +8 -0
+       while (count > 0) {
+               int this_len, retval;
+
+               this_len = mm->env_end - (mm->env_start + src);
+
+               if (this_len <= 0) {
+                       break;
+               }

Be gone, braces.

Signature · 1 line
Anton Arapov, <[email protected]>