]> Git Repo - linux.git/blobdiff - lib/string_helpers.c
mfd: cros-ec: Increase maximum mkbp event size
[linux.git] / lib / string_helpers.c
index aa00c9f989eec4ac56eefa34e7f4e2ad76ca70e9..29c490e5d47858ce18da95d63608a147f9b1ecd7 100644 (file)
@@ -10,6 +10,9 @@
 #include <linux/export.h>
 #include <linux/ctype.h>
 #include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/limits.h>
+#include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/string_helpers.h>
@@ -562,3 +565,64 @@ char *kstrdup_quotable(const char *src, gfp_t gfp)
        return dst;
 }
 EXPORT_SYMBOL_GPL(kstrdup_quotable);
+
+/*
+ * Returns allocated NULL-terminated string containing process
+ * command line, with inter-argument NULLs replaced with spaces,
+ * and other special characters escaped.
+ */
+char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
+{
+       char *buffer, *quoted;
+       int i, res;
+
+       buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
+       if (!buffer)
+               return NULL;
+
+       res = get_cmdline(task, buffer, PAGE_SIZE - 1);
+       buffer[res] = '\0';
+
+       /* Collapse trailing NULLs, leave res pointing to last non-NULL. */
+       while (--res >= 0 && buffer[res] == '\0')
+               ;
+
+       /* Replace inter-argument NULLs. */
+       for (i = 0; i <= res; i++)
+               if (buffer[i] == '\0')
+                       buffer[i] = ' ';
+
+       /* Make sure result is printable. */
+       quoted = kstrdup_quotable(buffer, gfp);
+       kfree(buffer);
+       return quoted;
+}
+EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);
+
+/*
+ * Returns allocated NULL-terminated string containing pathname,
+ * with special characters escaped, able to be safely logged. If
+ * there is an error, the leading character will be "<".
+ */
+char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
+{
+       char *temp, *pathname;
+
+       if (!file)
+               return kstrdup("<unknown>", gfp);
+
+       /* We add 11 spaces for ' (deleted)' to be appended */
+       temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
+       if (!temp)
+               return kstrdup("<no_memory>", gfp);
+
+       pathname = file_path(file, temp, PATH_MAX + 11);
+       if (IS_ERR(pathname))
+               pathname = kstrdup("<too_long>", gfp);
+       else
+               pathname = kstrdup_quotable(pathname, gfp);
+
+       kfree(temp);
+       return pathname;
+}
+EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
This page took 0.033196 seconds and 4 git commands to generate.