[LTP] [PATCH 1/2] testcase: lib: Create tst_getconf to replace "getconf"

Li Wang liwang@redhat.com
Wed Jun 6 06:11:25 CEST 2018


Mylène Josserand <mylene.josserand@bootlin.com> wrote:

> In some system, "getconf" application may not be installed.
> Some tests are using it to retrieve some variables such as the
> page size (PAGESIZE).
>
> Create a tst_getconf binary that use sysconf() function to retrieve
> these variables instead of relying on "getconf" application that
> may not be available.
> Add also this new helper in the documentation.
>
> Example:
> pagesize=`tst_getconf PAGESIZE`
>
> Signed-off-by: Mylène Josserand <mylene.josserand@bootlin.com>
> ​[...]
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <string.h>
> +
> +static void print_help(void)
> +{
> +       printf("Usage: tst_getconf variable\n\n");
> +       printf("       variable: can be PAGESIZE or _NPROCESSORS_ONLN (for
> the moment)\n");
> +       printf("example: tst_getconf PAGESIZE\n");
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +       int opt;
> +
> +       while ((opt = getopt(argc, argv, ":h")) != -1) {
> +               switch (opt) {
> +               case 'h':
> +                       print_help();
> +                       return 0;
> +               default:
> +                       print_help();
> +                       return 1;
> +               }
> +       }
>

​What about set argc limitation here?​

​    if (argc != 2) {
        print_help();
        return 1;
    }

​

> +
> +       if (optind >= argc) {
> +               fprintf(stderr, "ERROR: Expected variable argument\n\n");
> +               print_help();
> +               return 1;
> +       }
> +
> +       if (!strcmp(argv[optind], "_NPROCESSORS_ONLN"))
> +               printf("%ld", sysconf(_SC_NPROCESSORS_ONLN));
> +       else if (!strcmp(argv[optind],"PAGESIZE"))
> +               printf("%ld", sysconf(_SC_PAGE_SIZE));
>

​We'd better add a default check for invalid parameter here, assume that if
someone gives
a typo in use this tst_getconf, it's not easy to find the error.

# ./tst_getconf _NPROCESSORS_ONL  <--- typo here
# echo $?
0



> +
> +       return 0;
> +}
> --
> 2.11.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>



-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20180606/c8ecab59/attachment.html>


More information about the ltp mailing list