From: Cyril Hrubis <chrubis@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 1/3] kconfig: add funtion to parse /proc/cmdline
Date: Fri, 8 Mar 2024 17:31:01 +0100 [thread overview]
Message-ID: <Zes9RTFfzvKJsf4N@rei> (raw)
In-Reply-To: <20240308045230.3106549-1-liwang@redhat.com>
Hi!
> +/**
> + * Macro to initialize a tst_kcmdline_param structure with a specified parameter
> + * name and an empty value. This is useful for setting up an array of parameter
> + * structures before parsing the actual command-line arguments.
> + */
> +#define TST_KCMDLINE_INIT(paraname) { \
> + .key = paraname, \
> + .value = "" \
> +}
> +
> +/* Structure for storing command-line parameter key and its corresponding value */
> +struct tst_kcmdline_param {
^
maybe var as short for variable would be better
name
> + const char *key;
> + char value[128];
> +};
> +
> +/**
> + * Parses command-line parameters from /proc/cmdline and stores them in params array
> + * params: The array of tst_kcmdline_param structures to be filled with parsed key-value pairs
> + * params_len: The length of the params array, indicating how many parameters to parse
> + */
> +void tst_kcmdline_parse(struct tst_kcmdline_param params[], size_t params_len);
> +
> #endif /* TST_KCONFIG_H__ */
> diff --git a/lib/tst_kconfig.c b/lib/tst_kconfig.c
> index 595ea4b09..f79dea5c6 100644
> --- a/lib/tst_kconfig.c
> +++ b/lib/tst_kconfig.c
> @@ -565,3 +565,44 @@ char tst_kconfig_get(const char *confname)
>
> return var.choice;
> }
> +
> +void tst_kcmdline_parse(struct tst_kcmdline_param params[], size_t params_len) {
> + FILE *proc_cmdline;
> + char cmdline[4096];
> + char *token, *key, *value;
> +
> + proc_cmdline = fopen("/proc/cmdline", "r");
> + if (proc_cmdline == NULL)
> + tst_brk(TBROK | TERRNO, "Failed to open /proc/cmdline for reading");
> +
> + if (fgets(cmdline, sizeof(cmdline), proc_cmdline) == NULL) {
> + fclose(proc_cmdline);
> +
> + if (feof(proc_cmdline))
> + tst_brk(TBROK, "End-of-File reached on /proc/cmdline without reading any data");
> + else
> + tst_brk(TBROK | TERRNO, "Failed to read from /proc/cmdline");
> + }
> + fclose(proc_cmdline);
Uff, this is ugly. We have FILE and then use it as if we had fd and read
it as a whole. The whole point of FILE is that glibc manages the buffers
and reads so that we can access it character by character without being
slow. It would be way cleaner if we just read the file character by
character building up the key and value while we do that.
Something as (bevare untested):
char buf[128];
size_t buf_pos = 0, i;
int var_id = -1;
f = fopen("/proc/cmdline", "r");
while ((c = fgetc(f)) != EOF) {
switch (c) {
case '=':
buf[buf_pos] = 0;
for (i = 0; i < vars_len; i++)
var_id = i;
break;
case ' ':
buf[buf_pos] = 0;
if (var_id >= 0)
strcpy(vars[var_id].val, buf);
var_id = -1;
break;
default:
if (buf_pos+1 >= sizeof(buf))
//warning?
buf[buf_pos++] = c;
break;
}
}
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2024-03-08 20:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-08 4:52 [LTP] [PATCH 1/3] kconfig: add funtion to parse /proc/cmdline Li Wang
2024-03-08 4:52 ` [LTP] [PATCH 2/3] init_module: To handle kernel module signature enforcement Li Wang
2024-03-08 8:47 ` Petr Vorel
2024-03-09 8:11 ` Li Wang
2024-03-08 4:52 ` [LTP] [PATCH 3/3] stack_clash: make use of tst_kcmdline_parse Li Wang
2024-03-08 8:56 ` Petr Vorel
2024-03-08 7:12 ` [LTP] [PATCH 1/3] kconfig: add funtion to parse /proc/cmdline Petr Vorel
2024-03-08 16:31 ` Cyril Hrubis [this message]
2024-03-09 8:09 ` Li Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Zes9RTFfzvKJsf4N@rei \
--to=chrubis@suse.cz \
--cc=liwang@redhat.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox