summaryrefslogtreecommitdiffstats
path: root/lib/string_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string_helpers.c')
-rw-r--r--lib/string_helpers.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index aa00c9f989ee..b16ee85aaf87 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -10,6 +10,7 @@
10#include <linux/export.h> 10#include <linux/export.h>
11#include <linux/ctype.h> 11#include <linux/ctype.h>
12#include <linux/errno.h> 12#include <linux/errno.h>
13#include <linux/mm.h>
13#include <linux/slab.h> 14#include <linux/slab.h>
14#include <linux/string.h> 15#include <linux/string.h>
15#include <linux/string_helpers.h> 16#include <linux/string_helpers.h>
@@ -562,3 +563,36 @@ char *kstrdup_quotable(const char *src, gfp_t gfp)
562 return dst; 563 return dst;
563} 564}
564EXPORT_SYMBOL_GPL(kstrdup_quotable); 565EXPORT_SYMBOL_GPL(kstrdup_quotable);
566
567/*
568 * Returns allocated NULL-terminated string containing process
569 * command line, with inter-argument NULLs replaced with spaces,
570 * and other special characters escaped.
571 */
572char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
573{
574 char *buffer, *quoted;
575 int i, res;
576
577 buffer = kmalloc(PAGE_SIZE, GFP_TEMPORARY);
578 if (!buffer)
579 return NULL;
580
581 res = get_cmdline(task, buffer, PAGE_SIZE - 1);
582 buffer[res] = '\0';
583
584 /* Collapse trailing NULLs, leave res pointing to last non-NULL. */
585 while (--res >= 0 && buffer[res] == '\0')
586 ;
587
588 /* Replace inter-argument NULLs. */
589 for (i = 0; i <= res; i++)
590 if (buffer[i] == '\0')
591 buffer[i] = ' ';
592
593 /* Make sure result is printable. */
594 quoted = kstrdup_quotable(buffer, gfp);
595 kfree(buffer);
596 return quoted;
597}
598EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);