LCOV - code coverage report
Current view: top level - udev - udevadm-test-builtin.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 44 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: GPL-2.0+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <getopt.h>
       5             : #include <stddef.h>
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : 
       9             : #include "log.h"
      10             : #include "udev-builtin.h"
      11             : #include "udevadm.h"
      12             : #include "udevadm-util.h"
      13             : 
      14             : static const char *arg_command = NULL;
      15             : static const char *arg_syspath = NULL;
      16             : 
      17           0 : static int help(void) {
      18           0 :         printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
      19             :                "Test a built-in command.\n\n"
      20             :                "  -h --help     Print this message\n"
      21             :                "  -V --version  Print version of the program\n\n"
      22             :                "Commands:\n"
      23             :                , program_invocation_short_name);
      24             : 
      25           0 :         udev_builtin_list();
      26             : 
      27           0 :         return 0;
      28             : }
      29             : 
      30           0 : static int parse_argv(int argc, char *argv[]) {
      31             :         static const struct option options[] = {
      32             :                 { "version", no_argument, NULL, 'V' },
      33             :                 { "help",    no_argument, NULL, 'h' },
      34             :                 {}
      35             :         };
      36             : 
      37             :         int c;
      38             : 
      39           0 :         while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
      40           0 :                 switch (c) {
      41           0 :                 case 'V':
      42           0 :                         return print_version();
      43           0 :                 case 'h':
      44           0 :                         return help();
      45           0 :                 case '?':
      46           0 :                         return -EINVAL;
      47           0 :                 default:
      48           0 :                         assert_not_reached("Unknown option");
      49             :                 }
      50             : 
      51           0 :         arg_command = argv[optind++];
      52           0 :         if (!arg_command)
      53           0 :                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
      54             :                                        "Command missing.");
      55             : 
      56           0 :         arg_syspath = argv[optind++];
      57           0 :         if (!arg_syspath)
      58           0 :                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
      59             :                                        "syspath missing.");
      60             : 
      61           0 :         return 1;
      62             : }
      63             : 
      64           0 : int builtin_main(int argc, char *argv[], void *userdata) {
      65           0 :         _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
      66             :         UdevBuiltinCommand cmd;
      67             :         int r;
      68             : 
      69           0 :         log_set_max_level(LOG_DEBUG);
      70             : 
      71           0 :         r = parse_argv(argc, argv);
      72           0 :         if (r <= 0)
      73           0 :                 return r;
      74             : 
      75           0 :         udev_builtin_init();
      76             : 
      77           0 :         cmd = udev_builtin_lookup(arg_command);
      78           0 :         if (cmd < 0) {
      79           0 :                 log_error("Unknown command '%s'", arg_command);
      80           0 :                 r = -EINVAL;
      81           0 :                 goto finish;
      82             :         }
      83             : 
      84           0 :         r = find_device(arg_syspath, "/sys", &dev);
      85           0 :         if (r < 0) {
      86           0 :                 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
      87           0 :                 goto finish;
      88             :         }
      89             : 
      90           0 :         r = udev_builtin_run(dev, cmd, arg_command, true);
      91           0 :         if (r < 0)
      92           0 :                 log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
      93             : 
      94           0 : finish:
      95           0 :         udev_builtin_exit();
      96           0 :         return r;
      97             : }

Generated by: LCOV version 1.14