Commit a0a07b87 authored by Alexey Dobriyan's avatar Alexey Dobriyan Committed by Linus Torvalds

proc: less code duplication in /proc/*/cmdline

After staring at this code for a while I've figured using small 2-entry
array describing ARGV and ENVP is the way to address code duplication
critique.

Link: http://lkml.kernel.org/r/20170105185724.GA12027@avx2Signed-off-by: default avatarAlexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 4e4a7fb7
...@@ -292,16 +292,27 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf, ...@@ -292,16 +292,27 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
} }
} else { } else {
/* /*
* Command line (1 string) occupies ARGV and maybe * Command line (1 string) occupies ARGV and
* extends into ENVP. * extends into ENVP.
*/ */
if (len1 + len2 <= *pos) struct {
goto skip_argv_envp; unsigned long p;
if (len1 <= *pos) unsigned long len;
goto skip_argv; } cmdline[2] = {
{ .p = arg_start, .len = len1 },
{ .p = env_start, .len = len2 },
};
loff_t pos1 = *pos;
unsigned int i;
p = arg_start + *pos; i = 0;
len = len1 - *pos; while (i < 2 && pos1 >= cmdline[i].len) {
pos1 -= cmdline[i].len;
i++;
}
while (i < 2) {
p = cmdline[i].p + pos1;
len = cmdline[i].len - pos1;
while (count > 0 && len > 0) { while (count > 0 && len > 0) {
unsigned int _count, l; unsigned int _count, l;
int nr_read; int nr_read;
...@@ -339,54 +350,11 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf, ...@@ -339,54 +350,11 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
if (final) if (final)
goto out_free_page; goto out_free_page;
} }
skip_argv:
/*
* Command line (1 string) occupies ARGV and
* extends into ENVP.
*/
if (len1 <= *pos) {
p = env_start + *pos - len1;
len = len1 + len2 - *pos;
} else {
p = env_start;
len = len2;
}
while (count > 0 && len > 0) {
unsigned int _count, l;
int nr_read;
bool final;
_count = min3(count, len, PAGE_SIZE); /* Only first chunk can be read partially. */
nr_read = access_remote_vm(mm, p, page, _count, 0); pos1 = 0;
if (nr_read < 0) i++;
rv = nr_read;
if (nr_read <= 0)
goto out_free_page;
/* Find EOS. */
final = false;
l = strnlen(page, nr_read);
if (l < nr_read) {
nr_read = l;
final = true;
}
if (copy_to_user(buf, page, nr_read)) {
rv = -EFAULT;
goto out_free_page;
}
p += nr_read;
len -= nr_read;
buf += nr_read;
count -= nr_read;
rv += nr_read;
if (final)
goto out_free_page;
} }
skip_argv_envp:
;
} }
out_free_page: out_free_page:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment