LCOV - code coverage report
Current view: top level - udev - udevadm-test.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 67 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             :  * Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
       4             :  */
       5             : 
       6             : #include <errno.h>
       7             : #include <getopt.h>
       8             : #include <signal.h>
       9             : #include <stddef.h>
      10             : #include <stdio.h>
      11             : #include <stdlib.h>
      12             : #include <sys/signalfd.h>
      13             : #include <unistd.h>
      14             : 
      15             : #include "sd-device.h"
      16             : 
      17             : #include "device-private.h"
      18             : #include "device-util.h"
      19             : #include "libudev-util.h"
      20             : #include "string-util.h"
      21             : #include "strxcpyx.h"
      22             : #include "udev-builtin.h"
      23             : #include "udev-event.h"
      24             : #include "udevadm.h"
      25             : 
      26             : static const char *arg_action = "add";
      27             : static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
      28             : static char arg_syspath[UTIL_PATH_SIZE] = {};
      29             : 
      30           0 : static int help(void) {
      31             : 
      32           0 :         printf("%s test [OPTIONS] DEVPATH\n\n"
      33             :                "Test an event run.\n\n"
      34             :                "  -h --help                            Show this help\n"
      35             :                "  -V --version                         Show package version\n"
      36             :                "  -a --action=ACTION|help              Set action string\n"
      37             :                "  -N --resolve-names=early|late|never  When to resolve names\n"
      38             :                , program_invocation_short_name);
      39             : 
      40           0 :         return 0;
      41             : }
      42             : 
      43           0 : static int parse_argv(int argc, char *argv[]) {
      44             :         static const struct option options[] = {
      45             :                 { "action",        required_argument, NULL, 'a' },
      46             :                 { "resolve-names", required_argument, NULL, 'N' },
      47             :                 { "version",       no_argument,       NULL, 'V' },
      48             :                 { "help",          no_argument,       NULL, 'h' },
      49             :                 {}
      50             :         };
      51             : 
      52             :         int c;
      53             : 
      54           0 :         while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
      55           0 :                 switch (c) {
      56           0 :                 case 'a': {
      57             :                         DeviceAction a;
      58             : 
      59           0 :                         if (streq(optarg, "help")) {
      60           0 :                                 dump_device_action_table();
      61           0 :                                 return 0;
      62             :                         }
      63             : 
      64           0 :                         a = device_action_from_string(optarg);
      65           0 :                         if (a < 0)
      66           0 :                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
      67             :                                                        "Invalid action '%s'", optarg);
      68             : 
      69           0 :                         arg_action = optarg;
      70           0 :                         break;
      71             :                 }
      72           0 :                 case 'N':
      73           0 :                         arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
      74           0 :                         if (arg_resolve_name_timing < 0)
      75           0 :                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
      76             :                                                        "--resolve-names= must be early, late or never");
      77           0 :                         break;
      78           0 :                 case 'V':
      79           0 :                         return print_version();
      80           0 :                 case 'h':
      81           0 :                         return help();
      82           0 :                 case '?':
      83           0 :                         return -EINVAL;
      84           0 :                 default:
      85           0 :                         assert_not_reached("Unknown option");
      86             :                 }
      87             : 
      88           0 :         if (!argv[optind])
      89           0 :                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
      90             :                                        "syspath parameter missing.");
      91             : 
      92             :         /* add /sys if needed */
      93           0 :         if (!startswith(argv[optind], "/sys"))
      94           0 :                 strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL);
      95             :         else
      96           0 :                 strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]);
      97             : 
      98           0 :         return 1;
      99             : }
     100             : 
     101           0 : int test_main(int argc, char *argv[], void *userdata) {
     102           0 :         _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
     103           0 :         _cleanup_(udev_event_freep) UdevEvent *event = NULL;
     104           0 :         _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
     105             :         const char *cmd, *key, *value;
     106             :         sigset_t mask, sigmask_orig;
     107             :         Iterator i;
     108             :         void *val;
     109             :         int r;
     110             : 
     111           0 :         log_set_max_level(LOG_DEBUG);
     112             : 
     113           0 :         r = parse_argv(argc, argv);
     114           0 :         if (r <= 0)
     115           0 :                 return r;
     116             : 
     117           0 :         printf("This program is for debugging only, it does not run any program\n"
     118             :                "specified by a RUN key. It may show incorrect results, because\n"
     119             :                "some values may be different, or not available at a simulation run.\n"
     120             :                "\n");
     121             : 
     122           0 :         assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0);
     123             : 
     124           0 :         udev_builtin_init();
     125             : 
     126           0 :         r = udev_rules_new(&rules, arg_resolve_name_timing);
     127           0 :         if (r < 0) {
     128           0 :                 log_error_errno(r, "Failed to read udev rules: %m");
     129           0 :                 goto out;
     130             :         }
     131             : 
     132           0 :         r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action);
     133           0 :         if (r < 0) {
     134           0 :                 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
     135           0 :                 goto out;
     136             :         }
     137             : 
     138             :         /* don't read info from the db */
     139           0 :         device_seal(dev);
     140             : 
     141           0 :         event = udev_event_new(dev, 0, NULL);
     142             : 
     143           0 :         assert_se(sigfillset(&mask) >= 0);
     144           0 :         assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0);
     145             : 
     146           0 :         udev_event_execute_rules(event, 60 * USEC_PER_SEC, NULL, rules);
     147             : 
     148           0 :         FOREACH_DEVICE_PROPERTY(dev, key, value)
     149           0 :                 printf("%s=%s\n", key, value);
     150             : 
     151           0 :         ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
     152             :                 char program[UTIL_PATH_SIZE];
     153             : 
     154           0 :                 udev_event_apply_format(event, cmd, program, sizeof(program), false);
     155           0 :                 printf("run: '%s'\n", program);
     156             :         }
     157             : 
     158           0 :         r = 0;
     159           0 : out:
     160           0 :         udev_builtin_exit();
     161           0 :         return r;
     162             : }

Generated by: LCOV version 1.14