Y/N value import/export
PATCH 1/1 Y/N value import/export
Hello-
I am wanting to export a single Y/N value via sysfs like debugfs_create_bool does.
But, I can't find an exported method for parsing/exporting the Y/N values and copying and pasting the debugfs code doesn't seem right. If I was to create my own method there would be three routines for doing this parsing:
1) kernel/params.c: param_set_bool param_get_bool
2) fs/debugfs/file.c: write_file_bool read_file_bool
3) mine
Attached is a trivial patch to create get_bool and set_bool in lib/strings.c. Is that the right place to put this?
Brandon
Compile tested patch.
--- fs/debugfs/file.c | 18 ++---------------- include/linux/string.h | 4 ++++ kernel/params.c | 13 ++----------- lib/string.c | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 27 deletions(-)
Index: linux-mm-clean/lib/string.c =================================--- linux-mm-clean.orig/lib/string.c +++ linux-mm-clean/lib/string.c
@@ -634,3 +634,27 @@ void *memchr(const void *s, int c, size_
}
EXPORT_SYMBOL(memchr);
#endif
+
+int set_bool(int * val, const char * c)
+{
+ /* One of =[yYnN01] */
+ switch (*c) {
+ case 'y': case 'Y': case '1':
+ *val = 1;
+ return 0;
+ case 'n': case 'N': case '0':
+ *val = 0;
+ return 0;
+ }
+ return -EINVAL;
+}
+EXPORT_SYMBOL(set_bool);
+
+char get_bool(int val)
+{
+ if (val)
+ return 'Y';
+ else
+ return 'N';
+}
+EXPORT_SYMBOL(get_bool);
Index: linux-mm-clean/kernel/params.c =================================--- linux-mm-clean.orig/kernel/params.c +++ linux-mm-clean/kernel/params.c
@@ -225,22 +225,13 @@ int param_set_bool(const char *val, stru
/* No equals means "set"... */
if (!val) val = "1";
- /* One of =[yYnN01] */
- switch (val[0]) {
- case 'y': case 'Y': case '1':
- *(int *)kp->arg = 1;
- return 0;
- case 'n': case 'N': case '0':
- *(int *)kp->arg = 0;
- return 0;
- }
- return -EINVAL;
+ return set_bool((int *)kp->arg, val);
}
int param_get_bool(char *buffer, struct kernel_param *kp) {
/* Y and N chosen as being relatively non-coder friendly */
- return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
+ return sprintf(buffer, "%c", get_bool(*(int *)kp->arg));
}
int param_set_invbool(const char *val, struct kernel_param *kp)
Index: linux-mm-clean/fs/debugfs/file.c =================================--- linux-mm-clean.orig/fs/debugfs/file.c +++ linux-mm-clean/fs/debugfs/file.c
@@ -176,10 +176,7 @@ static ssize_t read_file_bool(struct fil
char buf[3];
u32 *val = file->private_data;
- if (*val)
- buf[0] = 'Y';
- else
- buf[0] = 'N';
+ buf[0] = get_bool(*val);
buf[1] = '\n';
buf[2] = 0x00;
return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -197,18 +194,7 @@ static ssize_t write_file_bool(struct fi
return -EFAULT;
- switch (buf[0]) {
- case 'y':
- case 'Y':
- case '1':
- *val = 1;
- break;
- case 'n':
- case 'N':
- case '0':
- *val = 0;
- break;
- }
+ set_bool(val, &buf[0]);
return count;
}
Index: linux-mm-clean/include/linux/string.h =================================--- linux-mm-clean.orig/include/linux/string.h +++ linux-mm-clean/include/linux/string.h
@@ -100,6 +100,10 @@ extern void * memchr(const void *,int,__
extern char *kstrdup(const char *s, gfp_t gfp);
+extern int set_bool(int * val, const char * c);
+
+extern char get_bool(int val);
+
#ifdef __cplusplus
}
#endif
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 2 lines · Brandon Philips
I am wanting to export a single Y/N value via sysfs like
debugfs_create_booldoes.
Why not 0/1 like the rest of the kernel does?
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Brandon Philips
I am wanting to export a single Y/N value via sysfs like
debugfs_create_booldoes.Why not 0/1 like the rest of the kernel does?
I couldn't find an example of a boolean values exported via sysfs. Could you point me to an attr that uses 0/1 values?
Thanks,
Brandon
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 9 lines · Brandon Philips
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Brandon Philips
I am wanting to export a single Y/N value via sysfs like
debugfs_create_booldoes.Why not 0/1 like the rest of the kernel does?
I couldn't find an example of a boolean values exported via sysfs. Could you point me to an attr that uses 0/1 values?
In drivers/pci/pci-sysfs.c, look for broken_parity_status
_______________________________________________
Kernel-janitors mailing list [email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 10 lines · Brandon Philips
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Brandon Philips
I am wanting to export a single Y/N value via sysfs like
debugfs_create_booldoes.Why not 0/1 like the rest of the kernel does?
I couldn't find an example of a boolean values exported via sysfs. Could you point me to an attr that uses 0/1 values?
in net/core/net-sysfs.c :
static ssize_t show_carrier(struct class_device *dev, char *buf)
{
struct net_device *netdev = to_net_dev(dev);
if (netif_running(netdev)) {
return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
}
return -EINVAL;
}Signature · 7 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html _______________________________________________ Kernel-janitors mailing list [email protected] https://lists.osdl.org/mailman/listinfo/kernel-janitors