LCOV - code coverage report
Current view: top level - test - test-udev.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 0 56 0.0 %
Date: 2019-08-23 13:36:53 Functions: 0 3 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 72 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : /***
       3                 :            :   Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
       4                 :            : ***/
       5                 :            : 
       6                 :            : #include <errno.h>
       7                 :            : #include <sched.h>
       8                 :            : #include <stdio.h>
       9                 :            : #include <stdlib.h>
      10                 :            : #include <sys/mount.h>
      11                 :            : #include <sys/signalfd.h>
      12                 :            : #include <unistd.h>
      13                 :            : 
      14                 :            : #include "build.h"
      15                 :            : #include "device-private.h"
      16                 :            : #include "fs-util.h"
      17                 :            : #include "log.h"
      18                 :            : #include "main-func.h"
      19                 :            : #include "missing.h"
      20                 :            : #include "mkdir.h"
      21                 :            : #include "selinux-util.h"
      22                 :            : #include "signal-util.h"
      23                 :            : #include "string-util.h"
      24                 :            : #include "tests.h"
      25                 :            : #include "udev-event.h"
      26                 :            : 
      27                 :          0 : static int fake_filesystems(void) {
      28                 :            :         static const struct fakefs {
      29                 :            :                 const char *src;
      30                 :            :                 const char *target;
      31                 :            :                 const char *error;
      32                 :            :                 bool ignore_mount_error;
      33                 :            :         } fakefss[] = {
      34                 :            :                 { "test/tmpfs/sys", "/sys",                    "Failed to mount test /sys",                        false },
      35                 :            :                 { "test/tmpfs/dev", "/dev",                    "Failed to mount test /dev",                        false },
      36                 :            :                 { "test/run",       "/run",                    "Failed to mount test /run",                        false },
      37                 :            :                 { "test/run",       "/etc/udev/rules.d",       "Failed to mount empty /etc/udev/rules.d",          true },
      38                 :            :                 { "test/run",       UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
      39                 :            :         };
      40                 :            :         unsigned i;
      41                 :            : 
      42         [ #  # ]:          0 :         if (unshare(CLONE_NEWNS) < 0)
      43         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to call unshare(): %m");
      44                 :            : 
      45         [ #  # ]:          0 :         if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
      46         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to mount / as private: %m");
      47                 :            : 
      48         [ #  # ]:          0 :         for (i = 0; i < ELEMENTSOF(fakefss); i++)
      49         [ #  # ]:          0 :                 if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) {
      50   [ #  #  #  # ]:          0 :                         log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error);
      51         [ #  # ]:          0 :                         if (!fakefss[i].ignore_mount_error)
      52                 :          0 :                                 return -errno;
      53                 :            :                 }
      54                 :            : 
      55                 :          0 :         return 0;
      56                 :            : }
      57                 :            : 
      58                 :          0 : static int run(int argc, char *argv[]) {
      59                 :          0 :         _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
      60                 :          0 :         _cleanup_(udev_event_freep) UdevEvent *event = NULL;
      61                 :          0 :         _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
      62                 :            :         const char *devpath, *devname, *action;
      63                 :            :         int r;
      64                 :            : 
      65                 :          0 :         test_setup_logging(LOG_INFO);
      66                 :            : 
      67   [ #  #  #  # ]:          0 :         if (!IN_SET(argc, 2, 3)) {
      68         [ #  # ]:          0 :                 log_error("This program needs one or two arguments, %d given", argc - 1);
      69                 :          0 :                 return -EINVAL;
      70                 :            :         }
      71                 :            : 
      72                 :          0 :         r = fake_filesystems();
      73         [ #  # ]:          0 :         if (r < 0)
      74                 :          0 :                 return r;
      75                 :            : 
      76         [ #  # ]:          0 :         if (argc == 2) {
      77         [ #  # ]:          0 :                 if (!streq(argv[1], "check")) {
      78         [ #  # ]:          0 :                         log_error("Unknown argument: %s", argv[1]);
      79                 :          0 :                         return -EINVAL;
      80                 :            :                 }
      81                 :            : 
      82                 :          0 :                 return 0;
      83                 :            :         }
      84                 :            : 
      85         [ #  # ]:          0 :         log_debug("version %s", GIT_VERSION);
      86                 :          0 :         mac_selinux_init();
      87                 :            : 
      88                 :          0 :         action = argv[1];
      89                 :          0 :         devpath = argv[2];
      90                 :            : 
      91         [ #  # ]:          0 :         assert_se(udev_rules_new(&rules, RESOLVE_NAME_EARLY) == 0);
      92                 :            : 
      93   [ #  #  #  #  :          0 :         const char *syspath = strjoina("/sys", devpath);
          #  #  #  #  #  
                #  #  # ]
      94                 :          0 :         r = device_new_from_synthetic_event(&dev, syspath, action);
      95         [ #  # ]:          0 :         if (r < 0)
      96         [ #  # ]:          0 :                 return log_debug_errno(r, "Failed to open device '%s'", devpath);
      97                 :            : 
      98         [ #  # ]:          0 :         assert_se(event = udev_event_new(dev, 0, NULL));
      99                 :            : 
     100         [ #  # ]:          0 :         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
     101                 :            : 
     102                 :            :         /* do what devtmpfs usually provides us */
     103         [ #  # ]:          0 :         if (sd_device_get_devname(dev, &devname) >= 0) {
     104                 :            :                 const char *subsystem;
     105                 :          0 :                 mode_t mode = 0600;
     106                 :            : 
     107   [ #  #  #  # ]:          0 :                 if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
     108                 :          0 :                         mode |= S_IFBLK;
     109                 :            :                 else
     110                 :          0 :                         mode |= S_IFCHR;
     111                 :            : 
     112         [ #  # ]:          0 :                 if (!streq(action, "remove")) {
     113                 :          0 :                         dev_t devnum = makedev(0, 0);
     114                 :            : 
     115                 :          0 :                         (void) mkdir_parents_label(devname, 0755);
     116                 :          0 :                         (void) sd_device_get_devnum(dev, &devnum);
     117         [ #  # ]:          0 :                         if (mknod(devname, mode, devnum) < 0)
     118         [ #  # ]:          0 :                                 return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
     119                 :            :                 } else {
     120         [ #  # ]:          0 :                         if (unlink(devname) < 0)
     121         [ #  # ]:          0 :                                 return log_error_errno(errno, "unlink('%s') failed: %m", devname);
     122                 :          0 :                         (void) rmdir_parents(devname, "/");
     123                 :            :                 }
     124                 :            :         }
     125                 :            : 
     126                 :          0 :         udev_event_execute_rules(event, 3 * USEC_PER_SEC, NULL, rules);
     127                 :          0 :         udev_event_execute_run(event, 3 * USEC_PER_SEC);
     128                 :            : 
     129                 :          0 :         return 0;
     130                 :            : }
     131                 :            : 
     132                 :          0 : DEFINE_MAIN_FUNCTION(run);

Generated by: LCOV version 1.14