Commit a90902531a06a030a252a07fbff7f45a189a64fe

Authored by William Roberts
Committed by Eric Paris
1 parent 147d2601d8

mm: Create utility function for accessing a tasks commandline value

introduce get_cmdline() for retreiving the value of a processes
proc/self/cmdline value.

Acked-by: David Rientjes <rientjes@google.com>
Acked-by: Stephen Smalley <sds@tycho.nsa.gov>
Acked-by: Richard Guy Briggs <rgb@redhat.com>

Signed-off-by: William Roberts <wroberts@tresys.com>
Signed-off-by: Eric Paris <eparis@redhat.com>

Showing 2 changed files with 49 additions and 0 deletions Side-by-side Diff

... ... @@ -1134,6 +1134,7 @@
1134 1134 int set_page_dirty(struct page *page);
1135 1135 int set_page_dirty_lock(struct page *page);
1136 1136 int clear_page_dirty_for_io(struct page *page);
  1137 +int get_cmdline(struct task_struct *task, char *buffer, int buflen);
1137 1138  
1138 1139 /* Is the vma a continuation of the stack vma above it? */
1139 1140 static inline int vma_growsdown(struct vm_area_struct *vma, unsigned long addr)
... ... @@ -413,6 +413,54 @@
413 413 * sysctl_overcommit_ratio / 100) + total_swap_pages;
414 414 }
415 415  
  416 +/**
  417 + * get_cmdline() - copy the cmdline value to a buffer.
  418 + * @task: the task whose cmdline value to copy.
  419 + * @buffer: the buffer to copy to.
  420 + * @buflen: the length of the buffer. Larger cmdline values are truncated
  421 + * to this length.
  422 + * Returns the size of the cmdline field copied. Note that the copy does
  423 + * not guarantee an ending NULL byte.
  424 + */
  425 +int get_cmdline(struct task_struct *task, char *buffer, int buflen)
  426 +{
  427 + int res = 0;
  428 + unsigned int len;
  429 + struct mm_struct *mm = get_task_mm(task);
  430 + if (!mm)
  431 + goto out;
  432 + if (!mm->arg_end)
  433 + goto out_mm; /* Shh! No looking before we're done */
  434 +
  435 + len = mm->arg_end - mm->arg_start;
  436 +
  437 + if (len > buflen)
  438 + len = buflen;
  439 +
  440 + res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  441 +
  442 + /*
  443 + * If the nul at the end of args has been overwritten, then
  444 + * assume application is using setproctitle(3).
  445 + */
  446 + if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
  447 + len = strnlen(buffer, res);
  448 + if (len < res) {
  449 + res = len;
  450 + } else {
  451 + len = mm->env_end - mm->env_start;
  452 + if (len > buflen - res)
  453 + len = buflen - res;
  454 + res += access_process_vm(task, mm->env_start,
  455 + buffer+res, len, 0);
  456 + res = strnlen(buffer, res);
  457 + }
  458 + }
  459 +out_mm:
  460 + mmput(mm);
  461 +out:
  462 + return res;
  463 +}
416 464  
417 465 /* Tracepoints definitions. */
418 466 EXPORT_TRACEPOINT_SYMBOL(kmalloc);