2018-04-01 08:59:03

by Richard Weinberger

[permalink] [raw]
Subject: [PATCH 0/2] cowsay support

As the printk subsystem gains more and more popularity it is time to add
the final missing feature to make it perfect, namely cowsay output.
Currently only one type of cow is supported and the only user is kmsg,
but I can see more use cases for it and the need for different cow types.

Example of /dev/kmsg usage:
[ 1.309561] _________________________________________
[ 1.309561] < systemd[1]: Detected architecture x86-64. >
[ 1.309561] -----------------------------------------
[ 1.309561] \ ^__^
[ 1.309561] \ (oo)\_______
[ 1.309561] (__)\ )\/\
[ 1.309561] ||----w |
[ 1.309561] || ||

Richard Weinberger (2):
lib: vsprintf: Implement %pCOW
printk: Use %pCOW for kmsg

kernel/printk/printk.c | 5 ++++-
lib/vsprintf.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)

--
2.13.6



2018-04-01 08:58:53

by Richard Weinberger

[permalink] [raw]
Subject: [PATCH 2/2] printk: Use %pCOW for kmsg

...userspace likes eye candy.

Signed-off-by: Richard Weinberger <[email protected]>
---
kernel/printk/printk.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index f274fbef821d..42ea0a18081d 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -815,7 +815,10 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
}
}

- printk_emit(facility, level, NULL, 0, "%s", line);
+ if (line[len - 1] == '\n')
+ line[len - 1] = '\0';
+
+ printk_emit(facility, level, NULL, 0, "%pCOW", line);
kfree(buf);
return ret;
}
--
2.13.6


2018-04-01 08:58:53

by Richard Weinberger

[permalink] [raw]
Subject: [PATCH 1/2] lib: vsprintf: Implement %pCOW

Add a new format string to print in cowsay format.

Signed-off-by: Richard Weinberger <[email protected]>
---
lib/vsprintf.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index d7a708f82559..a48df6f1c3f0 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1693,6 +1693,55 @@ static int __init initialize_ptr_random(void)
}
early_initcall(initialize_ptr_random);

+static char *cowsay(char *buf, char *end, void *ptr)
+{
+ static char dashes[] = {[0 ... 256] = '-'};
+ static char unders[] = {[0 ... 256] = '_'};
+ static char spaces[] = {[0 ... 256] = ' '};
+ static struct cow_type {
+ int num_lines;
+ char *cow_lines[];
+ } default_cow = {
+ .num_lines = 5,
+ .cow_lines = {
+ "\\ ^__^",
+ " \\ (oo)\\_______",
+ " (__)\\ )\\/\\",
+ " ||----w |",
+ " || ||",
+ },
+ };
+
+ int i, n;
+ char *orig_buf = buf;
+ char *str = ptr;
+ int len = strlen(str);
+
+ n = snprintf(buf, end - buf, " %.*s\n< %s >\n %.*s\n", len, unders,
+ str, len, dashes);
+ if (n < 0 || buf + n >= end)
+ goto cow_too_fat;
+
+ buf += n;
+
+ for (i = 0; i < default_cow.num_lines; i++) {
+ n = snprintf(buf, end - buf, "%.*s%s\n", len / 2, spaces,
+ default_cow.cow_lines[i]);
+ if (n < 0 || buf + n >= end)
+ goto cow_too_fat;
+
+ buf += n;
+ }
+
+ return buf;
+
+cow_too_fat:
+ n = snprintf(orig_buf, end - orig_buf, "%s\n", str);
+ if (n > 0)
+ orig_buf += n;
+ return orig_buf;
+}
+
/* Maps a pointer to a 32 bit unique identifier. */
static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
{
@@ -1941,6 +1990,9 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
case 'd':
return dentry_name(buf, end, ptr, spec, fmt);
case 'C':
+ if (fmt[1] == 'O' && fmt[2] == 'W')
+ return cowsay(buf, end, ptr);
+
return clock(buf, end, ptr, spec, fmt);
case 'D':
return dentry_name(buf, end,
--
2.13.6


2018-04-01 09:18:21

by Adam Borowski

[permalink] [raw]
Subject: Re: [PATCH 1/2] lib: vsprintf: Implement %pCOW

On Sun, Apr 01, 2018 at 10:56:21AM +0200, Richard Weinberger wrote:
> + .cow_lines = {
> + "\\ ^__^",
> + " \\ (oo)\\_______",
> + " (__)\\ )\\/\\",
> + " ||----w |",
> + " || ||",

Userspace cowsay(6) knows of different cows. What about using those as a
visual indication of message level?


Meow! err... Moo!
--
ᛊᚨᚾᛁᛏᚣ᛫ᛁᛊ᛫ᚠᛟᚱ᛫ᚦᛖ᛫ᚹᛖᚨᚲ

2018-04-01 09:20:22

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH 1/2] lib: vsprintf: Implement %pCOW

Am Sonntag, 1. April 2018, 11:15:49 CEST schrieb Adam Borowski:
> On Sun, Apr 01, 2018 at 10:56:21AM +0200, Richard Weinberger wrote:
> > + .cow_lines = {
> > + "\\ ^__^",
> > + " \\ (oo)\\_______",
> > + " (__)\\ )\\/\\",
> > + " ||----w |",
> > + " || ||",
>
> Userspace cowsay(6) knows of different cows. What about using those as a
> visual indication of message level?

Yeah, this will be implemented for April 1st 2019 if we get funding.
I think of a eBPF cow engine such that usespace can load different cow types.

Thanks,
//richard

2018-04-02 01:58:50

by Sergey Senozhatsky

[permalink] [raw]
Subject: Re: [PATCH 0/2] cowsay support

On (04/01/18 10:56), Richard Weinberger wrote:
> As the printk subsystem gains more and more popularity it is time to add
> the final missing feature to make it perfect, namely cowsay output.
> Currently only one type of cow is supported and the only user is kmsg,
> but I can see more use cases for it and the need for different cow types.
>
> Example of /dev/kmsg usage:
> [ 1.309561] _________________________________________
> [ 1.309561] < systemd[1]: Detected architecture x86-64. >
> [ 1.309561] -----------------------------------------
> [ 1.309561] \ ^__^
> [ 1.309561] \ (oo)\_______
> [ 1.309561] (__)\ )\/\
> [ 1.309561] ||----w |
> [ 1.309561] || ||
>
> Richard Weinberger (2):
> lib: vsprintf: Implement %pCOW
> printk: Use %pCOW for kmsg
>

___________________________________________________________
< Acked-by: Sergey Senozhatsky <[email protected]> >
-----------------------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

-ss

2018-04-02 14:19:39

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 1/2] lib: vsprintf: Implement %pCOW

On Sun, 2018-04-01 at 10:56 +0200, Richard Weinberger wrote:
> Add a new format string to print in cowsay format.
>

Apparently NAK b/c missed test cases!

> Signed-off-by: Richard Weinberger <[email protected]>
> ---
> lib/vsprintf.c | 52
> ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 52 insertions(+)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index d7a708f82559..a48df6f1c3f0 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -1693,6 +1693,55 @@ static int __init initialize_ptr_random(void)
> }
> early_initcall(initialize_ptr_random);
>
> +static char *cowsay(char *buf, char *end, void *ptr)
> +{
> + static char dashes[] = {[0 ... 256] = '-'};
> + static char unders[] = {[0 ... 256] = '_'};
> + static char spaces[] = {[0 ... 256] = ' '};
> + static struct cow_type {
> + int num_lines;
> + char *cow_lines[];
> + } default_cow = {
> + .num_lines = 5,
> + .cow_lines = {
> + "\\ ^__^",
> + " \\ (oo)\\_______",
> + " (__)\\ )\\/\\",
> + " ||----w |",
> + " || ||",
> + },
> + };
> +
> + int i, n;
> + char *orig_buf = buf;
> + char *str = ptr;
> + int len = strlen(str);
> +
> + n = snprintf(buf, end - buf, " %.*s\n< %s >\n %.*s\n", len,
> unders,
> + str, len, dashes);
> + if (n < 0 || buf + n >= end)
> + goto cow_too_fat;
> +
> + buf += n;
> +
> + for (i = 0; i < default_cow.num_lines; i++) {
> + n = snprintf(buf, end - buf, "%.*s%s\n", len / 2,
> spaces,
> + default_cow.cow_lines[i]);
> + if (n < 0 || buf + n >= end)
> + goto cow_too_fat;
> +
> + buf += n;
> + }
> +
> + return buf;
> +
> +cow_too_fat:
> + n = snprintf(orig_buf, end - orig_buf, "%s\n", str);
> + if (n > 0)
> + orig_buf += n;
> + return orig_buf;
> +}
> +
> /* Maps a pointer to a 32 bit unique identifier. */
> static char *ptr_to_id(char *buf, char *end, void *ptr, struct
> printf_spec spec)
> {
> @@ -1941,6 +1990,9 @@ char *pointer(const char *fmt, char *buf, char
> *end, void *ptr,
> case 'd':
> return dentry_name(buf, end, ptr, spec, fmt);
> case 'C':
> + if (fmt[1] == 'O' && fmt[2] == 'W')
> + return cowsay(buf, end, ptr);
> +
> return clock(buf, end, ptr, spec, fmt);
> case 'D':
> return dentry_name(buf, end,

--
Andy Shevchenko <[email protected]>
Intel Finland Oy

2018-04-03 09:17:15

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH 1/2] lib: vsprintf: Implement %pCOW

On Mon 2018-04-02 17:18:12, Andy Shevchenko wrote:
> On Sun, 2018-04-01 at 10:56 +0200, Richard Weinberger wrote:
> > Add a new format string to print in cowsay format.
> >
>
> Apparently NAK b/c missed test cases!

This is really sad. I'll miss the cows.

Moo,
Petr