LCOV - code coverage report
Current view: top level - core - mount.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 356 1015 35.1 %
Date: 2019-08-23 13:36:53 Functions: 35 66 53.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 199 984 20.2 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <errno.h>
       4                 :            : #include <signal.h>
       5                 :            : #include <stdio.h>
       6                 :            : #include <sys/epoll.h>
       7                 :            : 
       8                 :            : #include "sd-messages.h"
       9                 :            : 
      10                 :            : #include "alloc-util.h"
      11                 :            : #include "dbus-mount.h"
      12                 :            : #include "dbus-unit.h"
      13                 :            : #include "device.h"
      14                 :            : #include "exit-status.h"
      15                 :            : #include "format-util.h"
      16                 :            : #include "fstab-util.h"
      17                 :            : #include "libmount-util.h"
      18                 :            : #include "log.h"
      19                 :            : #include "manager.h"
      20                 :            : #include "mkdir.h"
      21                 :            : #include "mount-setup.h"
      22                 :            : #include "mount.h"
      23                 :            : #include "mountpoint-util.h"
      24                 :            : #include "parse-util.h"
      25                 :            : #include "path-util.h"
      26                 :            : #include "process-util.h"
      27                 :            : #include "serialize.h"
      28                 :            : #include "special.h"
      29                 :            : #include "string-table.h"
      30                 :            : #include "string-util.h"
      31                 :            : #include "strv.h"
      32                 :            : #include "unit-name.h"
      33                 :            : #include "unit.h"
      34                 :            : 
      35                 :            : #define RETRY_UMOUNT_MAX 32
      36                 :            : 
      37                 :            : static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
      38                 :            :         [MOUNT_DEAD] = UNIT_INACTIVE,
      39                 :            :         [MOUNT_MOUNTING] = UNIT_ACTIVATING,
      40                 :            :         [MOUNT_MOUNTING_DONE] = UNIT_ACTIVATING,
      41                 :            :         [MOUNT_MOUNTED] = UNIT_ACTIVE,
      42                 :            :         [MOUNT_REMOUNTING] = UNIT_RELOADING,
      43                 :            :         [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
      44                 :            :         [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
      45                 :            :         [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
      46                 :            :         [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
      47                 :            :         [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
      48                 :            :         [MOUNT_FAILED] = UNIT_FAILED
      49                 :            : };
      50                 :            : 
      51                 :            : static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
      52                 :            : static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
      53                 :            : static int mount_process_proc_self_mountinfo(Manager *m);
      54                 :            : 
      55                 :        660 : static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
      56         [ -  + ]:        660 :         return IN_SET(state,
      57                 :            :                       MOUNT_MOUNTING,
      58                 :            :                       MOUNT_MOUNTING_DONE,
      59                 :            :                       MOUNT_REMOUNTING,
      60                 :            :                       MOUNT_REMOUNTING_SIGTERM,
      61                 :            :                       MOUNT_REMOUNTING_SIGKILL,
      62                 :            :                       MOUNT_UNMOUNTING,
      63                 :            :                       MOUNT_UNMOUNTING_SIGTERM,
      64                 :            :                       MOUNT_UNMOUNTING_SIGKILL);
      65                 :            : }
      66                 :            : 
      67                 :          0 : static bool mount_is_network(const MountParameters *p) {
      68         [ #  # ]:          0 :         assert(p);
      69                 :            : 
      70         [ #  # ]:          0 :         if (fstab_test_option(p->options, "_netdev\0"))
      71                 :          0 :                 return true;
      72                 :            : 
      73   [ #  #  #  # ]:          0 :         if (p->fstype && fstype_is_network(p->fstype))
      74                 :          0 :                 return true;
      75                 :            : 
      76                 :          0 :         return false;
      77                 :            : }
      78                 :            : 
      79                 :          0 : static bool mount_is_loop(const MountParameters *p) {
      80         [ #  # ]:          0 :         assert(p);
      81                 :            : 
      82         [ #  # ]:          0 :         if (fstab_test_option(p->options, "loop\0"))
      83                 :          0 :                 return true;
      84                 :            : 
      85                 :          0 :         return false;
      86                 :            : }
      87                 :            : 
      88                 :        704 : static bool mount_is_bind(const MountParameters *p) {
      89         [ -  + ]:        704 :         assert(p);
      90                 :            : 
      91         [ -  + ]:        704 :         if (fstab_test_option(p->options, "bind\0" "rbind\0"))
      92                 :          0 :                 return true;
      93                 :            : 
      94   [ +  -  -  + ]:        704 :         if (p->fstype && STR_IN_SET(p->fstype, "bind", "rbind"))
      95                 :          0 :                 return true;
      96                 :            : 
      97                 :        704 :         return false;
      98                 :            : }
      99                 :            : 
     100                 :        132 : static bool mount_is_bound_to_device(const Mount *m) {
     101                 :            :         const MountParameters *p;
     102                 :            : 
     103         [ -  + ]:        132 :         if (m->from_fragment)
     104                 :          0 :                 return true;
     105                 :            : 
     106                 :        132 :         p = &m->parameters_proc_self_mountinfo;
     107                 :        132 :         return fstab_test_option(p->options, "x-systemd.device-bound\0");
     108                 :            : }
     109                 :            : 
     110                 :          0 : static bool mount_needs_quota(const MountParameters *p) {
     111         [ #  # ]:          0 :         assert(p);
     112                 :            : 
     113                 :            :         /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
     114                 :            :          * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
     115   [ #  #  #  # ]:          0 :         if (p->fstype && fstype_is_network(p->fstype))
     116                 :          0 :                 return false;
     117                 :            : 
     118         [ #  # ]:          0 :         if (mount_is_bind(p))
     119                 :          0 :                 return false;
     120                 :            : 
     121                 :          0 :         return fstab_test_option(p->options,
     122                 :            :                                  "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
     123                 :            : }
     124                 :            : 
     125                 :       1788 : static void mount_init(Unit *u) {
     126                 :       1788 :         Mount *m = MOUNT(u);
     127                 :            : 
     128         [ -  + ]:       1788 :         assert(u);
     129         [ -  + ]:       1788 :         assert(u->load_state == UNIT_STUB);
     130                 :            : 
     131                 :       1788 :         m->timeout_usec = u->manager->default_timeout_start_usec;
     132                 :            : 
     133                 :       1788 :         m->exec_context.std_output = u->manager->default_std_output;
     134                 :       1788 :         m->exec_context.std_error = u->manager->default_std_error;
     135                 :            : 
     136                 :       1788 :         m->directory_mode = 0755;
     137                 :            : 
     138                 :            :         /* We need to make sure that /usr/bin/mount is always called
     139                 :            :          * in the same process group as us, so that the autofs kernel
     140                 :            :          * side doesn't send us another mount request while we are
     141                 :            :          * already trying to comply its last one. */
     142                 :       1788 :         m->exec_context.same_pgrp = true;
     143                 :            : 
     144                 :       1788 :         m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
     145                 :            : 
     146                 :       1788 :         u->ignore_on_isolate = true;
     147                 :       1788 : }
     148                 :            : 
     149                 :          0 : static int mount_arm_timer(Mount *m, usec_t usec) {
     150                 :            :         int r;
     151                 :            : 
     152         [ #  # ]:          0 :         assert(m);
     153                 :            : 
     154         [ #  # ]:          0 :         if (m->timer_event_source) {
     155                 :          0 :                 r = sd_event_source_set_time(m->timer_event_source, usec);
     156         [ #  # ]:          0 :                 if (r < 0)
     157                 :          0 :                         return r;
     158                 :            : 
     159                 :          0 :                 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
     160                 :            :         }
     161                 :            : 
     162         [ #  # ]:          0 :         if (usec == USEC_INFINITY)
     163                 :          0 :                 return 0;
     164                 :            : 
     165                 :          0 :         r = sd_event_add_time(
     166         [ #  # ]:          0 :                         UNIT(m)->manager->event,
     167                 :            :                         &m->timer_event_source,
     168                 :            :                         CLOCK_MONOTONIC,
     169                 :            :                         usec, 0,
     170                 :            :                         mount_dispatch_timer, m);
     171         [ #  # ]:          0 :         if (r < 0)
     172                 :          0 :                 return r;
     173                 :            : 
     174                 :          0 :         (void) sd_event_source_set_description(m->timer_event_source, "mount-timer");
     175                 :            : 
     176                 :          0 :         return 0;
     177                 :            : }
     178                 :            : 
     179                 :       2448 : static void mount_unwatch_control_pid(Mount *m) {
     180         [ -  + ]:       2448 :         assert(m);
     181                 :            : 
     182         [ +  - ]:       2448 :         if (m->control_pid <= 0)
     183                 :       2448 :                 return;
     184                 :            : 
     185         [ #  # ]:          0 :         unit_unwatch_pid(UNIT(m), m->control_pid);
     186                 :          0 :         m->control_pid = 0;
     187                 :            : }
     188                 :            : 
     189                 :       3576 : static void mount_parameters_done(MountParameters *p) {
     190         [ -  + ]:       3576 :         assert(p);
     191                 :            : 
     192                 :       3576 :         p->what = mfree(p->what);
     193                 :       3576 :         p->options = mfree(p->options);
     194                 :       3576 :         p->fstype = mfree(p->fstype);
     195                 :       3576 : }
     196                 :            : 
     197                 :       1788 : static void mount_done(Unit *u) {
     198                 :       1788 :         Mount *m = MOUNT(u);
     199                 :            : 
     200         [ -  + ]:       1788 :         assert(m);
     201                 :            : 
     202                 :       1788 :         m->where = mfree(m->where);
     203                 :            : 
     204                 :       1788 :         mount_parameters_done(&m->parameters_proc_self_mountinfo);
     205                 :       1788 :         mount_parameters_done(&m->parameters_fragment);
     206                 :            : 
     207                 :       1788 :         m->exec_runtime = exec_runtime_unref(m->exec_runtime, false);
     208                 :       1788 :         exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
     209                 :       1788 :         m->control_command = NULL;
     210                 :            : 
     211                 :       1788 :         dynamic_creds_unref(&m->dynamic_creds);
     212                 :            : 
     213                 :       1788 :         mount_unwatch_control_pid(m);
     214                 :            : 
     215                 :       1788 :         m->timer_event_source = sd_event_source_unref(m->timer_event_source);
     216                 :       1788 : }
     217                 :            : 
     218                 :       1364 : _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
     219         [ -  + ]:       1364 :         assert(m);
     220                 :            : 
     221         [ -  + ]:       1364 :         if (m->from_fragment)
     222                 :          0 :                 return &m->parameters_fragment;
     223                 :            : 
     224                 :       1364 :         return NULL;
     225                 :            : }
     226                 :            : 
     227                 :       1064 : _pure_ static MountParameters* get_mount_parameters(Mount *m) {
     228         [ -  + ]:       1064 :         assert(m);
     229                 :            : 
     230         [ +  - ]:       1064 :         if (m->from_proc_self_mountinfo)
     231                 :       1064 :                 return &m->parameters_proc_self_mountinfo;
     232                 :            : 
     233                 :          0 :         return get_mount_parameters_fragment(m);
     234                 :            : }
     235                 :            : 
     236                 :        660 : static int update_parameters_proc_self_mountinfo(
     237                 :            :                 Mount *m,
     238                 :            :                 const char *what,
     239                 :            :                 const char *options,
     240                 :            :                 const char *fstype) {
     241                 :            : 
     242                 :            :         MountParameters *p;
     243                 :            :         int r, q, w;
     244                 :            : 
     245                 :        660 :         p = &m->parameters_proc_self_mountinfo;
     246                 :            : 
     247                 :        660 :         r = free_and_strdup(&p->what, what);
     248         [ -  + ]:        660 :         if (r < 0)
     249                 :          0 :                 return r;
     250                 :            : 
     251                 :        660 :         q = free_and_strdup(&p->options, options);
     252         [ -  + ]:        660 :         if (q < 0)
     253                 :          0 :                 return q;
     254                 :            : 
     255                 :        660 :         w = free_and_strdup(&p->fstype, fstype);
     256         [ -  + ]:        660 :         if (w < 0)
     257                 :          0 :                 return w;
     258                 :            : 
     259   [ -  +  #  #  :        660 :         return r > 0 || q > 0 || w > 0;
                   #  # ]
     260                 :            : }
     261                 :            : 
     262                 :        704 : static int mount_add_mount_dependencies(Mount *m) {
     263                 :            :         MountParameters *pm;
     264                 :            :         Unit *other;
     265                 :            :         Iterator i;
     266                 :            :         Set *s;
     267                 :            :         int r;
     268                 :            : 
     269         [ -  + ]:        704 :         assert(m);
     270                 :            : 
     271         [ +  + ]:        704 :         if (!path_equal(m->where, "/")) {
     272         [ +  - ]:        616 :                 _cleanup_free_ char *parent = NULL;
     273                 :            : 
     274                 :            :                 /* Adds in links to other mount points that might lie further up in the hierarchy */
     275                 :            : 
     276                 :        616 :                 parent = dirname_malloc(m->where);
     277         [ -  + ]:        616 :                 if (!parent)
     278                 :          0 :                         return -ENOMEM;
     279                 :            : 
     280         [ +  - ]:        616 :                 r = unit_require_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT);
     281         [ -  + ]:        616 :                 if (r < 0)
     282                 :          0 :                         return r;
     283                 :            :         }
     284                 :            : 
     285                 :            :         /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
     286                 :            :          * or a loop mount) to be available. */
     287                 :        704 :         pm = get_mount_parameters_fragment(m);
     288   [ -  +  #  # ]:        704 :         if (pm && pm->what &&
     289   [ #  #  #  # ]:          0 :             path_is_absolute(pm->what) &&
     290   [ #  #  #  # ]:          0 :             (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) {
     291                 :            : 
     292         [ #  # ]:          0 :                 r = unit_require_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE);
     293         [ #  # ]:          0 :                 if (r < 0)
     294                 :          0 :                         return r;
     295                 :            :         }
     296                 :            : 
     297                 :            :         /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
     298         [ +  - ]:        704 :         s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
     299         [ +  + ]:       1716 :         SET_FOREACH(other, s, i) {
     300                 :            : 
     301         [ -  + ]:       1012 :                 if (other->load_state != UNIT_LOADED)
     302                 :          0 :                         continue;
     303                 :            : 
     304   [ +  -  -  + ]:       1012 :                 if (other == UNIT(m))
     305                 :          0 :                         continue;
     306                 :            : 
     307         [ +  - ]:       1012 :                 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true, UNIT_DEPENDENCY_PATH);
     308         [ -  + ]:       1012 :                 if (r < 0)
     309                 :          0 :                         return r;
     310                 :            : 
     311   [ +  -  -  + ]:       1012 :                 if (UNIT(m)->fragment_path) {
     312                 :            :                         /* If we have fragment configuration, then make this dependency required */
     313         [ #  # ]:          0 :                         r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true, UNIT_DEPENDENCY_PATH);
     314         [ #  # ]:          0 :                         if (r < 0)
     315                 :          0 :                                 return r;
     316                 :            :                 }
     317                 :            :         }
     318                 :            : 
     319                 :        704 :         return 0;
     320                 :            : }
     321                 :            : 
     322                 :        704 : static int mount_add_device_dependencies(Mount *m) {
     323                 :            :         UnitDependencyMask mask;
     324                 :            :         MountParameters *p;
     325                 :            :         UnitDependency dep;
     326                 :            :         int r;
     327                 :            : 
     328         [ -  + ]:        704 :         assert(m);
     329                 :            : 
     330                 :        704 :         p = get_mount_parameters(m);
     331         [ -  + ]:        704 :         if (!p)
     332                 :          0 :                 return 0;
     333                 :            : 
     334         [ -  + ]:        704 :         if (!p->what)
     335                 :          0 :                 return 0;
     336                 :            : 
     337         [ -  + ]:        704 :         if (mount_is_bind(p))
     338                 :          0 :                 return 0;
     339                 :            : 
     340         [ +  + ]:        704 :         if (!is_device_path(p->what))
     341                 :        484 :                 return 0;
     342                 :            : 
     343                 :            :         /* /dev/root is a really weird thing, it's not a real device,
     344                 :            :          * but just a path the kernel exports for the root file system
     345                 :            :          * specified on the kernel command line. Ignore it here. */
     346         [ -  + ]:        220 :         if (path_equal(p->what, "/dev/root"))
     347                 :          0 :                 return 0;
     348                 :            : 
     349         [ +  + ]:        220 :         if (path_equal(m->where, "/"))
     350                 :         88 :                 return 0;
     351                 :            : 
     352                 :            :         /* Mount units from /proc/self/mountinfo are not bound to devices
     353                 :            :          * by default since they're subject to races when devices are
     354                 :            :          * unplugged. But the user can still force this dep with an
     355                 :            :          * appropriate option (or udev property) so the mount units are
     356                 :            :          * automatically stopped when the device disappears suddenly. */
     357         [ -  + ]:        132 :         dep = mount_is_bound_to_device(m) ? UNIT_BINDS_TO : UNIT_REQUIRES;
     358                 :            : 
     359                 :            :         /* We always use 'what' from /proc/self/mountinfo if mounted */
     360         [ +  - ]:        132 :         mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT : UNIT_DEPENDENCY_FILE;
     361                 :            : 
     362         [ +  - ]:        132 :         r = unit_add_node_dependency(UNIT(m), p->what, false, dep, mask);
     363         [ -  + ]:        132 :         if (r < 0)
     364                 :          0 :                 return r;
     365                 :            : 
     366                 :        132 :         return 0;
     367                 :            : }
     368                 :            : 
     369                 :        704 : static int mount_add_quota_dependencies(Mount *m) {
     370                 :            :         UnitDependencyMask mask;
     371                 :            :         MountParameters *p;
     372                 :            :         int r;
     373                 :            : 
     374         [ -  + ]:        704 :         assert(m);
     375                 :            : 
     376   [ +  -  +  - ]:        704 :         if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
     377                 :        704 :                 return 0;
     378                 :            : 
     379                 :          0 :         p = get_mount_parameters_fragment(m);
     380         [ #  # ]:          0 :         if (!p)
     381                 :          0 :                 return 0;
     382                 :            : 
     383         [ #  # ]:          0 :         if (!mount_needs_quota(p))
     384                 :          0 :                 return 0;
     385                 :            : 
     386         [ #  # ]:          0 :         mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT;
     387                 :            : 
     388         [ #  # ]:          0 :         r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, true, mask);
     389         [ #  # ]:          0 :         if (r < 0)
     390                 :          0 :                 return r;
     391                 :            : 
     392         [ #  # ]:          0 :         r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, true, mask);
     393         [ #  # ]:          0 :         if (r < 0)
     394                 :          0 :                 return r;
     395                 :            : 
     396                 :          0 :         return 0;
     397                 :            : }
     398                 :            : 
     399                 :       1020 : static bool mount_is_extrinsic(Mount *m) {
     400                 :            :         MountParameters *p;
     401         [ -  + ]:       1020 :         assert(m);
     402                 :            : 
     403                 :            :         /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
     404                 :            :          * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
     405                 :            :          * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
     406                 :            : 
     407   [ +  -  +  - ]:       1020 :         if (!MANAGER_IS_SYSTEM(UNIT(m)->manager)) /* We only automatically manage mounts if we are in system mode */
     408                 :       1020 :                 return true;
     409                 :            : 
     410   [ #  #  #  #  :          0 :         if (PATH_IN_SET(m->where,  /* Don't bother with the OS data itself */
             #  #  #  # ]
     411                 :            :                         "/",
     412                 :            :                         "/usr"))
     413                 :          0 :                 return true;
     414                 :            : 
     415         [ #  # ]:          0 :         if (PATH_STARTSWITH_SET(m->where,
     416                 :            :                                 "/run/initramfs",    /* This should stay around from before we boot until after we shutdown */
     417                 :            :                                 "/proc",             /* All of this is API VFS */
     418                 :            :                                 "/sys",              /* … dito … */
     419                 :            :                                 "/dev"))             /* … dito … */
     420                 :          0 :                 return true;
     421                 :            : 
     422                 :            :         /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
     423                 :          0 :         p = get_mount_parameters(m);
     424   [ #  #  #  #  :          0 :         if (p && fstab_test_option(p->options, "x-initrd.mount\0") && !in_initrd())
                   #  # ]
     425                 :          0 :                 return true;
     426                 :            : 
     427                 :          0 :         return false;
     428                 :            : }
     429                 :            : 
     430                 :        704 : static int mount_add_default_dependencies(Mount *m) {
     431                 :            :         const char *after, *before;
     432                 :            :         UnitDependencyMask mask;
     433                 :            :         MountParameters *p;
     434                 :            :         bool nofail;
     435                 :            :         int r;
     436                 :            : 
     437         [ -  + ]:        704 :         assert(m);
     438                 :            : 
     439   [ +  -  +  + ]:        704 :         if (!UNIT(m)->default_dependencies)
     440                 :         44 :                 return 0;
     441                 :            : 
     442                 :            :         /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
     443                 :            :          * mounted the whole time, since our system is on it.  Also, don't bother with anything mounted below virtual
     444                 :            :          * file systems, it's also going to be virtual, and hence not worth the effort. */
     445         [ +  - ]:        660 :         if (mount_is_extrinsic(m))
     446                 :        660 :                 return 0;
     447                 :            : 
     448                 :          0 :         p = get_mount_parameters(m);
     449         [ #  # ]:          0 :         if (!p)
     450                 :          0 :                 return 0;
     451                 :            : 
     452         [ #  # ]:          0 :         mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_DEFAULT;
     453   [ #  #  #  # ]:          0 :         nofail = m->from_fragment ? fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0") : false;
     454                 :            : 
     455         [ #  # ]:          0 :         if (mount_is_network(p)) {
     456                 :            :                 /* We order ourselves after network.target. This is
     457                 :            :                  * primarily useful at shutdown: services that take
     458                 :            :                  * down the network should order themselves before
     459                 :            :                  * network.target, so that they are shut down only
     460                 :            :                  * after this mount unit is stopped. */
     461                 :            : 
     462         [ #  # ]:          0 :                 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, true, mask);
     463         [ #  # ]:          0 :                 if (r < 0)
     464                 :          0 :                         return r;
     465                 :            : 
     466                 :            :                 /* We pull in network-online.target, and order
     467                 :            :                  * ourselves after it. This is useful at start-up to
     468                 :            :                  * actively pull in tools that want to be started
     469                 :            :                  * before we start mounting network file systems, and
     470                 :            :                  * whose purpose it is to delay this until the network
     471                 :            :                  * is "up". */
     472                 :            : 
     473         [ #  # ]:          0 :                 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, true, mask);
     474         [ #  # ]:          0 :                 if (r < 0)
     475                 :          0 :                         return r;
     476                 :            : 
     477                 :          0 :                 after = SPECIAL_REMOTE_FS_PRE_TARGET;
     478                 :          0 :                 before = SPECIAL_REMOTE_FS_TARGET;
     479                 :            :         } else {
     480                 :          0 :                 after = SPECIAL_LOCAL_FS_PRE_TARGET;
     481                 :          0 :                 before = SPECIAL_LOCAL_FS_TARGET;
     482                 :            :         }
     483                 :            : 
     484         [ #  # ]:          0 :         if (!nofail) {
     485         [ #  # ]:          0 :                 r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, true, mask);
     486         [ #  # ]:          0 :                 if (r < 0)
     487                 :          0 :                         return r;
     488                 :            :         }
     489                 :            : 
     490         [ #  # ]:          0 :         r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, true, mask);
     491         [ #  # ]:          0 :         if (r < 0)
     492                 :          0 :                 return r;
     493                 :            : 
     494         [ #  # ]:          0 :         r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, mask);
     495         [ #  # ]:          0 :         if (r < 0)
     496                 :          0 :                 return r;
     497                 :            : 
     498                 :            :         /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
     499         [ #  # ]:          0 :         if (streq_ptr(p->fstype, "tmpfs")) {
     500         [ #  # ]:          0 :                 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, true, mask);
     501         [ #  # ]:          0 :                 if (r < 0)
     502                 :          0 :                         return r;
     503                 :            :         }
     504                 :            : 
     505                 :          0 :         return 0;
     506                 :            : }
     507                 :            : 
     508                 :        660 : static int mount_verify(Mount *m) {
     509                 :        660 :         _cleanup_free_ char *e = NULL;
     510                 :            :         MountParameters *p;
     511                 :            :         int r;
     512                 :            : 
     513         [ -  + ]:        660 :         assert(m);
     514                 :            : 
     515   [ +  -  -  + ]:        660 :         if (UNIT(m)->load_state != UNIT_LOADED)
     516                 :          0 :                 return 0;
     517                 :            : 
     518   [ +  -  -  +  :        660 :         if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual)
             #  #  #  # ]
     519                 :          0 :                 return -ENOENT;
     520                 :            : 
     521                 :        660 :         r = unit_name_from_path(m->where, ".mount", &e);
     522         [ -  + ]:        660 :         if (r < 0)
     523   [ #  #  #  # ]:          0 :                 return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
     524                 :            : 
     525   [ +  -  -  + ]:        660 :         if (!unit_has_name(UNIT(m), e)) {
     526   [ #  #  #  # ]:          0 :                 log_unit_error(UNIT(m), "Where= setting doesn't match unit name. Refusing.");
     527                 :          0 :                 return -ENOEXEC;
     528                 :            :         }
     529                 :            : 
     530   [ +  -  -  + ]:        660 :         if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
     531   [ #  #  #  # ]:          0 :                 log_unit_error(UNIT(m), "Cannot create mount unit for API file system %s. Refusing.", m->where);
     532                 :          0 :                 return -ENOEXEC;
     533                 :            :         }
     534                 :            : 
     535                 :        660 :         p = get_mount_parameters_fragment(m);
     536   [ -  +  #  # ]:        660 :         if (p && !p->what) {
     537   [ #  #  #  # ]:          0 :                 log_unit_error(UNIT(m), "What= setting is missing. Refusing.");
     538                 :          0 :                 return -ENOEXEC;
     539                 :            :         }
     540                 :            : 
     541   [ -  +  #  # ]:        660 :         if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
     542   [ #  #  #  # ]:          0 :                 log_unit_error(UNIT(m), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
     543                 :          0 :                 return -ENOEXEC;
     544                 :            :         }
     545                 :            : 
     546                 :        660 :         return 0;
     547                 :            : }
     548                 :            : 
     549                 :        704 : static int mount_add_extras(Mount *m) {
     550         [ +  - ]:        704 :         Unit *u = UNIT(m);
     551                 :            :         int r;
     552                 :            : 
     553         [ -  + ]:        704 :         assert(m);
     554                 :            : 
     555                 :            :         /* Note: this call might be called after we already have been loaded once (and even when it has already been
     556                 :            :          * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
     557                 :            :          * to run with an already set up unit. */
     558                 :            : 
     559         [ -  + ]:        704 :         if (u->fragment_path)
     560                 :          0 :                 m->from_fragment = true;
     561                 :            : 
     562         [ -  + ]:        704 :         if (!m->where) {
     563                 :          0 :                 r = unit_name_to_path(u->id, &m->where);
     564         [ #  # ]:          0 :                 if (r < 0)
     565                 :          0 :                         return r;
     566                 :            :         }
     567                 :            : 
     568                 :        704 :         path_simplify(m->where, false);
     569                 :            : 
     570         [ +  + ]:        704 :         if (!u->description) {
     571                 :        660 :                 r = unit_set_description(u, m->where);
     572         [ -  + ]:        660 :                 if (r < 0)
     573                 :          0 :                         return r;
     574                 :            :         }
     575                 :            : 
     576                 :        704 :         r = mount_add_device_dependencies(m);
     577         [ -  + ]:        704 :         if (r < 0)
     578                 :          0 :                 return r;
     579                 :            : 
     580                 :        704 :         r = mount_add_mount_dependencies(m);
     581         [ -  + ]:        704 :         if (r < 0)
     582                 :          0 :                 return r;
     583                 :            : 
     584                 :        704 :         r = mount_add_quota_dependencies(m);
     585         [ -  + ]:        704 :         if (r < 0)
     586                 :          0 :                 return r;
     587                 :            : 
     588                 :        704 :         r = unit_patch_contexts(u);
     589         [ -  + ]:        704 :         if (r < 0)
     590                 :          0 :                 return r;
     591                 :            : 
     592                 :        704 :         r = unit_add_exec_dependencies(u, &m->exec_context);
     593         [ -  + ]:        704 :         if (r < 0)
     594                 :          0 :                 return r;
     595                 :            : 
     596                 :        704 :         r = unit_set_default_slice(u);
     597         [ -  + ]:        704 :         if (r < 0)
     598                 :          0 :                 return r;
     599                 :            : 
     600                 :        704 :         r = mount_add_default_dependencies(m);
     601         [ -  + ]:        704 :         if (r < 0)
     602                 :          0 :                 return r;
     603                 :            : 
     604                 :        704 :         return 0;
     605                 :            : }
     606                 :            : 
     607                 :       1788 : static int mount_load_root_mount(Unit *u) {
     608         [ -  + ]:       1788 :         assert(u);
     609                 :            : 
     610         [ +  + ]:       1788 :         if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
     611                 :       1744 :                 return 0;
     612                 :            : 
     613                 :         44 :         u->perpetual = true;
     614                 :         44 :         u->default_dependencies = false;
     615                 :            : 
     616                 :            :         /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
     617                 :         44 :         MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL;
     618                 :         44 :         MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL;
     619                 :            : 
     620         [ -  + ]:         44 :         if (!u->description)
     621                 :          0 :                 u->description = strdup("Root Mount");
     622                 :            : 
     623                 :         44 :         return 1;
     624                 :            : }
     625                 :            : 
     626                 :       1788 : static int mount_load(Unit *u) {
     627                 :       1788 :         Mount *m = MOUNT(u);
     628                 :            :         int r, q, w;
     629                 :            : 
     630         [ -  + ]:       1788 :         assert(u);
     631         [ -  + ]:       1788 :         assert(u->load_state == UNIT_STUB);
     632                 :            : 
     633                 :       1788 :         r = mount_load_root_mount(u);
     634                 :            : 
     635   [ +  +  -  + ]:       1788 :         if (m->from_proc_self_mountinfo || u->perpetual)
     636                 :        660 :                 q = unit_load_fragment_and_dropin_optional(u);
     637                 :            :         else
     638                 :       1128 :                 q = unit_load_fragment_and_dropin(u);
     639                 :            : 
     640                 :            :         /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
     641                 :            :          * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
     642                 :            :          * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
     643                 :            :          * 'slice' field for a unit once it is active. */
     644   [ +  +  +  -  :       1788 :         if (u->load_state == UNIT_LOADED || m->from_proc_self_mountinfo || u->perpetual)
                   -  + ]
     645                 :        660 :                 w = mount_add_extras(m);
     646                 :            :         else
     647                 :       1128 :                 w = 0;
     648                 :            : 
     649         [ -  + ]:       1788 :         if (r < 0)
     650                 :          0 :                 return r;
     651         [ +  + ]:       1788 :         if (q < 0)
     652                 :       1128 :                 return q;
     653         [ -  + ]:        660 :         if (w < 0)
     654                 :          0 :                 return w;
     655                 :            : 
     656                 :        660 :         return mount_verify(m);
     657                 :            : }
     658                 :            : 
     659                 :        660 : static void mount_set_state(Mount *m, MountState state) {
     660                 :            :         MountState old_state;
     661         [ -  + ]:        660 :         assert(m);
     662                 :            : 
     663         [ +  - ]:        660 :         if (m->state != state)
     664         [ +  - ]:        660 :                 bus_unit_send_pending_change_signal(UNIT(m), false);
     665                 :            : 
     666                 :        660 :         old_state = m->state;
     667                 :        660 :         m->state = state;
     668                 :            : 
     669         [ +  - ]:        660 :         if (!MOUNT_STATE_WITH_PROCESS(state)) {
     670                 :        660 :                 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
     671                 :        660 :                 mount_unwatch_control_pid(m);
     672                 :        660 :                 m->control_command = NULL;
     673                 :        660 :                 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
     674                 :            :         }
     675                 :            : 
     676         [ +  - ]:        660 :         if (state != old_state)
     677   [ +  -  +  - ]:        660 :                 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
     678                 :            : 
     679                 :        660 :         unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state],
     680         [ +  - ]:        660 :                     m->reload_result == MOUNT_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE);
     681                 :        660 : }
     682                 :            : 
     683                 :       1716 : static int mount_coldplug(Unit *u) {
     684                 :       1716 :         Mount *m = MOUNT(u);
     685                 :       1716 :         MountState new_state = MOUNT_DEAD;
     686                 :            :         int r;
     687                 :            : 
     688         [ -  + ]:       1716 :         assert(m);
     689         [ -  + ]:       1716 :         assert(m->state == MOUNT_DEAD);
     690                 :            : 
     691         [ +  + ]:       1716 :         if (m->deserialized_state != m->state)
     692                 :         44 :                 new_state = m->deserialized_state;
     693         [ +  + ]:       1672 :         else if (m->from_proc_self_mountinfo)
     694                 :        616 :                 new_state = MOUNT_MOUNTED;
     695                 :            : 
     696         [ +  + ]:       1716 :         if (new_state == m->state)
     697                 :       1056 :                 return 0;
     698                 :            : 
     699   [ -  +  #  # ]:        660 :         if (m->control_pid > 0 &&
     700         [ #  # ]:          0 :             pid_is_unwaited(m->control_pid) &&
     701                 :          0 :             MOUNT_STATE_WITH_PROCESS(new_state)) {
     702                 :            : 
     703         [ #  # ]:          0 :                 r = unit_watch_pid(UNIT(m), m->control_pid, false);
     704         [ #  # ]:          0 :                 if (r < 0)
     705                 :          0 :                         return r;
     706                 :            : 
     707                 :          0 :                 r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
     708         [ #  # ]:          0 :                 if (r < 0)
     709                 :          0 :                         return r;
     710                 :            :         }
     711                 :            : 
     712   [ -  +  +  - ]:        660 :         if (!IN_SET(new_state, MOUNT_DEAD, MOUNT_FAILED)) {
     713                 :        660 :                 (void) unit_setup_dynamic_creds(u);
     714                 :        660 :                 (void) unit_setup_exec_runtime(u);
     715                 :            :         }
     716                 :            : 
     717                 :        660 :         mount_set_state(m, new_state);
     718                 :        660 :         return 0;
     719                 :            : }
     720                 :            : 
     721                 :        360 : static void mount_dump(Unit *u, FILE *f, const char *prefix) {
     722                 :            :         char buf[FORMAT_TIMESPAN_MAX];
     723                 :        360 :         Mount *m = MOUNT(u);
     724                 :            :         MountParameters *p;
     725                 :            : 
     726         [ -  + ]:        360 :         assert(m);
     727         [ -  + ]:        360 :         assert(f);
     728                 :            : 
     729                 :        360 :         p = get_mount_parameters(m);
     730                 :            : 
     731   [ +  -  +  -  :       2160 :         fprintf(f,
                   +  - ]
     732                 :            :                 "%sMount State: %s\n"
     733                 :            :                 "%sResult: %s\n"
     734                 :            :                 "%sWhere: %s\n"
     735                 :            :                 "%sWhat: %s\n"
     736                 :            :                 "%sFile System Type: %s\n"
     737                 :            :                 "%sOptions: %s\n"
     738                 :            :                 "%sFrom /proc/self/mountinfo: %s\n"
     739                 :            :                 "%sFrom fragment: %s\n"
     740                 :            :                 "%sExtrinsic: %s\n"
     741                 :            :                 "%sDirectoryMode: %04o\n"
     742                 :            :                 "%sSloppyOptions: %s\n"
     743                 :            :                 "%sLazyUnmount: %s\n"
     744                 :            :                 "%sForceUnmount: %s\n"
     745                 :            :                 "%sTimeoutSec: %s\n",
     746                 :            :                 prefix, mount_state_to_string(m->state),
     747                 :            :                 prefix, mount_result_to_string(m->result),
     748                 :            :                 prefix, m->where,
     749                 :        360 :                 prefix, p ? strna(p->what) : "n/a",
     750                 :        360 :                 prefix, p ? strna(p->fstype) : "n/a",
     751                 :        360 :                 prefix, p ? strna(p->options) : "n/a",
     752                 :        360 :                 prefix, yes_no(m->from_proc_self_mountinfo),
     753                 :        360 :                 prefix, yes_no(m->from_fragment),
     754                 :        360 :                 prefix, yes_no(mount_is_extrinsic(m)),
     755                 :            :                 prefix, m->directory_mode,
     756                 :        360 :                 prefix, yes_no(m->sloppy_options),
     757                 :        360 :                 prefix, yes_no(m->lazy_unmount),
     758                 :        360 :                 prefix, yes_no(m->force_unmount),
     759                 :            :                 prefix, format_timespan(buf, sizeof(buf), m->timeout_usec, USEC_PER_SEC));
     760                 :            : 
     761         [ -  + ]:        360 :         if (m->control_pid > 0)
     762                 :          0 :                 fprintf(f,
     763                 :            :                         "%sControl PID: "PID_FMT"\n",
     764                 :            :                         prefix, m->control_pid);
     765                 :            : 
     766                 :        360 :         exec_context_dump(&m->exec_context, f, prefix);
     767                 :        360 :         kill_context_dump(&m->kill_context, f, prefix);
     768                 :        360 :         cgroup_context_dump(&m->cgroup_context, f, prefix);
     769                 :        360 : }
     770                 :            : 
     771                 :          0 : static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
     772                 :            : 
     773                 :          0 :         _cleanup_(exec_params_clear) ExecParameters exec_params = {
     774                 :            :                 .flags     = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
     775                 :            :                 .stdin_fd  = -1,
     776                 :            :                 .stdout_fd = -1,
     777                 :            :                 .stderr_fd = -1,
     778                 :            :                 .exec_fd   = -1,
     779                 :            :         };
     780                 :            :         pid_t pid;
     781                 :            :         int r;
     782                 :            : 
     783         [ #  # ]:          0 :         assert(m);
     784         [ #  # ]:          0 :         assert(c);
     785         [ #  # ]:          0 :         assert(_pid);
     786                 :            : 
     787         [ #  # ]:          0 :         r = unit_prepare_exec(UNIT(m));
     788         [ #  # ]:          0 :         if (r < 0)
     789                 :          0 :                 return r;
     790                 :            : 
     791                 :          0 :         r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
     792         [ #  # ]:          0 :         if (r < 0)
     793                 :          0 :                 return r;
     794                 :            : 
     795         [ #  # ]:          0 :         r = unit_set_exec_params(UNIT(m), &exec_params);
     796         [ #  # ]:          0 :         if (r < 0)
     797                 :          0 :                 return r;
     798                 :            : 
     799                 :          0 :         r = exec_spawn(UNIT(m),
     800                 :            :                        c,
     801         [ #  # ]:          0 :                        &m->exec_context,
     802                 :            :                        &exec_params,
     803                 :            :                        m->exec_runtime,
     804                 :            :                        &m->dynamic_creds,
     805                 :            :                        &pid);
     806         [ #  # ]:          0 :         if (r < 0)
     807                 :          0 :                 return r;
     808                 :            : 
     809         [ #  # ]:          0 :         r = unit_watch_pid(UNIT(m), pid, true);
     810         [ #  # ]:          0 :         if (r < 0)
     811                 :          0 :                 return r;
     812                 :            : 
     813                 :          0 :         *_pid = pid;
     814                 :            : 
     815                 :          0 :         return 0;
     816                 :            : }
     817                 :            : 
     818                 :          0 : static void mount_enter_dead(Mount *m, MountResult f) {
     819         [ #  # ]:          0 :         assert(m);
     820                 :            : 
     821         [ #  # ]:          0 :         if (m->result == MOUNT_SUCCESS)
     822                 :          0 :                 m->result = f;
     823                 :            : 
     824         [ #  # ]:          0 :         unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
     825         [ #  # ]:          0 :         mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
     826                 :            : 
     827                 :          0 :         m->exec_runtime = exec_runtime_unref(m->exec_runtime, true);
     828                 :            : 
     829         [ #  # ]:          0 :         exec_context_destroy_runtime_directory(&m->exec_context, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
     830                 :            : 
     831         [ #  # ]:          0 :         unit_unref_uid_gid(UNIT(m), true);
     832                 :            : 
     833                 :          0 :         dynamic_creds_destroy(&m->dynamic_creds);
     834                 :            : 
     835                 :            :         /* Any dependencies based on /proc/self/mountinfo are now stale */
     836         [ #  # ]:          0 :         unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
     837                 :          0 : }
     838                 :            : 
     839                 :          0 : static void mount_enter_mounted(Mount *m, MountResult f) {
     840         [ #  # ]:          0 :         assert(m);
     841                 :            : 
     842         [ #  # ]:          0 :         if (m->result == MOUNT_SUCCESS)
     843                 :          0 :                 m->result = f;
     844                 :            : 
     845                 :          0 :         mount_set_state(m, MOUNT_MOUNTED);
     846                 :          0 : }
     847                 :            : 
     848                 :          0 : static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
     849         [ #  # ]:          0 :         assert(m);
     850                 :            : 
     851                 :            :         /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
     852                 :            :          * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
     853                 :            :          * ultimately we just mirror the kernel's internal state on this. */
     854                 :            : 
     855         [ #  # ]:          0 :         if (m->from_proc_self_mountinfo)
     856                 :          0 :                 mount_enter_mounted(m, f);
     857                 :            :         else
     858                 :          0 :                 mount_enter_dead(m, f);
     859                 :          0 : }
     860                 :            : 
     861                 :          0 : static int state_to_kill_operation(MountState state) {
     862      [ #  #  # ]:          0 :         switch (state) {
     863                 :            : 
     864                 :          0 :         case MOUNT_REMOUNTING_SIGTERM:
     865                 :            :         case MOUNT_UNMOUNTING_SIGTERM:
     866                 :          0 :                 return KILL_TERMINATE;
     867                 :            : 
     868                 :          0 :         case MOUNT_REMOUNTING_SIGKILL:
     869                 :            :         case MOUNT_UNMOUNTING_SIGKILL:
     870                 :          0 :                 return KILL_KILL;
     871                 :            : 
     872                 :          0 :         default:
     873                 :          0 :                 return _KILL_OPERATION_INVALID;
     874                 :            :         }
     875                 :            : }
     876                 :            : 
     877                 :          0 : static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
     878                 :            :         int r;
     879                 :            : 
     880         [ #  # ]:          0 :         assert(m);
     881                 :            : 
     882         [ #  # ]:          0 :         if (m->result == MOUNT_SUCCESS)
     883                 :          0 :                 m->result = f;
     884                 :            : 
     885                 :          0 :         r = unit_kill_context(
     886         [ #  # ]:          0 :                         UNIT(m),
     887                 :            :                         &m->kill_context,
     888                 :          0 :                         state_to_kill_operation(state),
     889                 :            :                         -1,
     890                 :            :                         m->control_pid,
     891                 :            :                         false);
     892         [ #  # ]:          0 :         if (r < 0)
     893                 :          0 :                 goto fail;
     894                 :            : 
     895         [ #  # ]:          0 :         if (r > 0) {
     896                 :          0 :                 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
     897         [ #  # ]:          0 :                 if (r < 0)
     898                 :          0 :                         goto fail;
     899                 :            : 
     900                 :          0 :                 mount_set_state(m, state);
     901   [ #  #  #  # ]:          0 :         } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
     902                 :          0 :                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
     903   [ #  #  #  # ]:          0 :         else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
     904                 :          0 :                 mount_enter_mounted(m, MOUNT_SUCCESS);
     905   [ #  #  #  # ]:          0 :         else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
     906                 :          0 :                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
     907                 :            :         else
     908                 :          0 :                 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
     909                 :            : 
     910                 :          0 :         return;
     911                 :            : 
     912                 :          0 : fail:
     913   [ #  #  #  # ]:          0 :         log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
     914                 :          0 :         mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
     915                 :            : }
     916                 :            : 
     917                 :          0 : static void mount_enter_unmounting(Mount *m) {
     918                 :            :         int r;
     919                 :            : 
     920         [ #  # ]:          0 :         assert(m);
     921                 :            : 
     922                 :            :         /* Start counting our attempts */
     923   [ #  #  #  # ]:          0 :         if (!IN_SET(m->state,
     924                 :            :                     MOUNT_UNMOUNTING,
     925                 :            :                     MOUNT_UNMOUNTING_SIGTERM,
     926                 :            :                     MOUNT_UNMOUNTING_SIGKILL))
     927                 :          0 :                 m->n_retry_umount = 0;
     928                 :            : 
     929                 :          0 :         m->control_command_id = MOUNT_EXEC_UNMOUNT;
     930                 :          0 :         m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
     931                 :            : 
     932                 :          0 :         r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
     933   [ #  #  #  # ]:          0 :         if (r >= 0 && m->lazy_unmount)
     934                 :          0 :                 r = exec_command_append(m->control_command, "-l", NULL);
     935   [ #  #  #  # ]:          0 :         if (r >= 0 && m->force_unmount)
     936                 :          0 :                 r = exec_command_append(m->control_command, "-f", NULL);
     937         [ #  # ]:          0 :         if (r < 0)
     938                 :          0 :                 goto fail;
     939                 :            : 
     940                 :          0 :         mount_unwatch_control_pid(m);
     941                 :            : 
     942                 :          0 :         r = mount_spawn(m, m->control_command, &m->control_pid);
     943         [ #  # ]:          0 :         if (r < 0)
     944                 :          0 :                 goto fail;
     945                 :            : 
     946                 :          0 :         mount_set_state(m, MOUNT_UNMOUNTING);
     947                 :            : 
     948                 :          0 :         return;
     949                 :            : 
     950                 :          0 : fail:
     951   [ #  #  #  # ]:          0 :         log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
     952                 :          0 :         mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
     953                 :            : }
     954                 :            : 
     955                 :          0 : static void mount_enter_mounting(Mount *m) {
     956                 :            :         int r;
     957                 :            :         MountParameters *p;
     958                 :            : 
     959         [ #  # ]:          0 :         assert(m);
     960                 :            : 
     961         [ #  # ]:          0 :         r = unit_fail_if_noncanonical(UNIT(m), m->where);
     962         [ #  # ]:          0 :         if (r < 0)
     963                 :          0 :                 goto fail;
     964                 :            : 
     965                 :          0 :         (void) mkdir_p_label(m->where, m->directory_mode);
     966                 :            : 
     967         [ #  # ]:          0 :         unit_warn_if_dir_nonempty(UNIT(m), m->where);
     968         [ #  # ]:          0 :         unit_warn_leftover_processes(UNIT(m));
     969                 :            : 
     970                 :          0 :         m->control_command_id = MOUNT_EXEC_MOUNT;
     971                 :          0 :         m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
     972                 :            : 
     973                 :            :         /* Create the source directory for bind-mounts if needed */
     974                 :          0 :         p = get_mount_parameters_fragment(m);
     975   [ #  #  #  # ]:          0 :         if (p && mount_is_bind(p))
     976                 :          0 :                 (void) mkdir_p_label(p->what, m->directory_mode);
     977                 :            : 
     978         [ #  # ]:          0 :         if (p) {
     979         [ #  # ]:          0 :                 _cleanup_free_ char *opts = NULL;
     980                 :            : 
     981                 :          0 :                 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
     982         [ #  # ]:          0 :                 if (r < 0)
     983                 :          0 :                         goto fail;
     984                 :            : 
     985                 :          0 :                 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
     986   [ #  #  #  # ]:          0 :                 if (r >= 0 && m->sloppy_options)
     987                 :          0 :                         r = exec_command_append(m->control_command, "-s", NULL);
     988   [ #  #  #  # ]:          0 :                 if (r >= 0 && p->fstype)
     989                 :          0 :                         r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
     990   [ #  #  #  # ]:          0 :                 if (r >= 0 && !isempty(opts))
     991                 :          0 :                         r = exec_command_append(m->control_command, "-o", opts, NULL);
     992                 :            :         } else
     993                 :          0 :                 r = -ENOENT;
     994         [ #  # ]:          0 :         if (r < 0)
     995                 :          0 :                 goto fail;
     996                 :            : 
     997                 :          0 :         mount_unwatch_control_pid(m);
     998                 :            : 
     999                 :          0 :         r = mount_spawn(m, m->control_command, &m->control_pid);
    1000         [ #  # ]:          0 :         if (r < 0)
    1001                 :          0 :                 goto fail;
    1002                 :            : 
    1003                 :          0 :         mount_set_state(m, MOUNT_MOUNTING);
    1004                 :            : 
    1005                 :          0 :         return;
    1006                 :            : 
    1007                 :          0 : fail:
    1008   [ #  #  #  # ]:          0 :         log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
    1009                 :          0 :         mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
    1010                 :            : }
    1011                 :            : 
    1012                 :          0 : static void mount_set_reload_result(Mount *m, MountResult result) {
    1013         [ #  # ]:          0 :         assert(m);
    1014                 :            : 
    1015                 :            :         /* Only store the first error we encounter */
    1016         [ #  # ]:          0 :         if (m->reload_result != MOUNT_SUCCESS)
    1017                 :          0 :                 return;
    1018                 :            : 
    1019                 :          0 :         m->reload_result = result;
    1020                 :            : }
    1021                 :            : 
    1022                 :          0 : static void mount_enter_remounting(Mount *m) {
    1023                 :            :         int r;
    1024                 :            :         MountParameters *p;
    1025                 :            : 
    1026         [ #  # ]:          0 :         assert(m);
    1027                 :            : 
    1028                 :            :         /* Reset reload result when we are about to start a new remount operation */
    1029                 :          0 :         m->reload_result = MOUNT_SUCCESS;
    1030                 :            : 
    1031                 :          0 :         m->control_command_id = MOUNT_EXEC_REMOUNT;
    1032                 :          0 :         m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
    1033                 :            : 
    1034                 :          0 :         p = get_mount_parameters_fragment(m);
    1035         [ #  # ]:          0 :         if (p) {
    1036                 :            :                 const char *o;
    1037                 :            : 
    1038         [ #  # ]:          0 :                 if (p->options)
    1039   [ #  #  #  #  :          0 :                         o = strjoina("remount,", p->options);
          #  #  #  #  #  
                #  #  # ]
    1040                 :            :                 else
    1041                 :          0 :                         o = "remount";
    1042                 :            : 
    1043                 :          0 :                 r = exec_command_set(m->control_command, MOUNT_PATH,
    1044                 :            :                                      p->what, m->where,
    1045                 :            :                                      "-o", o, NULL);
    1046   [ #  #  #  # ]:          0 :                 if (r >= 0 && m->sloppy_options)
    1047                 :          0 :                         r = exec_command_append(m->control_command, "-s", NULL);
    1048   [ #  #  #  # ]:          0 :                 if (r >= 0 && p->fstype)
    1049                 :          0 :                         r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
    1050                 :            :         } else
    1051                 :          0 :                 r = -ENOENT;
    1052         [ #  # ]:          0 :         if (r < 0)
    1053                 :          0 :                 goto fail;
    1054                 :            : 
    1055                 :          0 :         mount_unwatch_control_pid(m);
    1056                 :            : 
    1057                 :          0 :         r = mount_spawn(m, m->control_command, &m->control_pid);
    1058         [ #  # ]:          0 :         if (r < 0)
    1059                 :          0 :                 goto fail;
    1060                 :            : 
    1061                 :          0 :         mount_set_state(m, MOUNT_REMOUNTING);
    1062                 :            : 
    1063                 :          0 :         return;
    1064                 :            : 
    1065                 :          0 : fail:
    1066   [ #  #  #  # ]:          0 :         log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
    1067                 :          0 :         mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
    1068                 :          0 :         mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
    1069                 :            : }
    1070                 :            : 
    1071                 :          0 : static void mount_cycle_clear(Mount *m) {
    1072         [ #  # ]:          0 :         assert(m);
    1073                 :            : 
    1074                 :            :         /* Clear all state we shall forget for this new cycle */
    1075                 :            : 
    1076                 :          0 :         m->result = MOUNT_SUCCESS;
    1077                 :          0 :         m->reload_result = MOUNT_SUCCESS;
    1078                 :          0 :         exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
    1079         [ #  # ]:          0 :         UNIT(m)->reset_accounting = true;
    1080                 :          0 : }
    1081                 :            : 
    1082                 :          0 : static int mount_start(Unit *u) {
    1083                 :          0 :         Mount *m = MOUNT(u);
    1084                 :            :         int r;
    1085                 :            : 
    1086         [ #  # ]:          0 :         assert(m);
    1087                 :            : 
    1088                 :            :         /* We cannot fulfill this request right now, try again later
    1089                 :            :          * please! */
    1090   [ #  #  #  # ]:          0 :         if (IN_SET(m->state,
    1091                 :            :                    MOUNT_UNMOUNTING,
    1092                 :            :                    MOUNT_UNMOUNTING_SIGTERM,
    1093                 :            :                    MOUNT_UNMOUNTING_SIGKILL))
    1094                 :          0 :                 return -EAGAIN;
    1095                 :            : 
    1096                 :            :         /* Already on it! */
    1097         [ #  # ]:          0 :         if (m->state == MOUNT_MOUNTING)
    1098                 :          0 :                 return 0;
    1099                 :            : 
    1100   [ #  #  #  # ]:          0 :         assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
    1101                 :            : 
    1102                 :          0 :         r = unit_test_start_limit(u);
    1103         [ #  # ]:          0 :         if (r < 0) {
    1104                 :          0 :                 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
    1105                 :          0 :                 return r;
    1106                 :            :         }
    1107                 :            : 
    1108                 :          0 :         r = unit_acquire_invocation_id(u);
    1109         [ #  # ]:          0 :         if (r < 0)
    1110                 :          0 :                 return r;
    1111                 :            : 
    1112                 :          0 :         mount_cycle_clear(m);
    1113                 :          0 :         mount_enter_mounting(m);
    1114                 :            : 
    1115                 :          0 :         return 1;
    1116                 :            : }
    1117                 :            : 
    1118                 :          0 : static int mount_stop(Unit *u) {
    1119                 :          0 :         Mount *m = MOUNT(u);
    1120                 :            : 
    1121         [ #  # ]:          0 :         assert(m);
    1122                 :            : 
    1123   [ #  #  #  #  :          0 :         switch (m->state) {
                   #  # ]
    1124                 :            : 
    1125                 :          0 :         case MOUNT_UNMOUNTING:
    1126                 :            :         case MOUNT_UNMOUNTING_SIGKILL:
    1127                 :            :         case MOUNT_UNMOUNTING_SIGTERM:
    1128                 :            :                 /* Already on it */
    1129                 :          0 :                 return 0;
    1130                 :            : 
    1131                 :          0 :         case MOUNT_MOUNTING:
    1132                 :            :         case MOUNT_MOUNTING_DONE:
    1133                 :            :         case MOUNT_REMOUNTING:
    1134                 :            :                 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
    1135                 :          0 :                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
    1136                 :          0 :                 return 0;
    1137                 :            : 
    1138                 :          0 :         case MOUNT_REMOUNTING_SIGTERM:
    1139                 :            :                 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
    1140                 :          0 :                 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
    1141                 :          0 :                 return 0;
    1142                 :            : 
    1143                 :          0 :         case MOUNT_REMOUNTING_SIGKILL:
    1144                 :            :                 /* as above */
    1145                 :          0 :                 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
    1146                 :          0 :                 return 0;
    1147                 :            : 
    1148                 :          0 :         case MOUNT_MOUNTED:
    1149                 :          0 :                 mount_enter_unmounting(m);
    1150                 :          0 :                 return 1;
    1151                 :            : 
    1152                 :          0 :         default:
    1153                 :          0 :                 assert_not_reached("Unexpected state.");
    1154                 :            :         }
    1155                 :            : }
    1156                 :            : 
    1157                 :          0 : static int mount_reload(Unit *u) {
    1158                 :          0 :         Mount *m = MOUNT(u);
    1159                 :            : 
    1160         [ #  # ]:          0 :         assert(m);
    1161         [ #  # ]:          0 :         assert(m->state == MOUNT_MOUNTED);
    1162                 :            : 
    1163                 :          0 :         mount_enter_remounting(m);
    1164                 :            : 
    1165                 :          0 :         return 1;
    1166                 :            : }
    1167                 :            : 
    1168                 :          0 : static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
    1169                 :          0 :         Mount *m = MOUNT(u);
    1170                 :            : 
    1171         [ #  # ]:          0 :         assert(m);
    1172         [ #  # ]:          0 :         assert(f);
    1173         [ #  # ]:          0 :         assert(fds);
    1174                 :            : 
    1175                 :          0 :         (void) serialize_item(f, "state", mount_state_to_string(m->state));
    1176                 :          0 :         (void) serialize_item(f, "result", mount_result_to_string(m->result));
    1177                 :          0 :         (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
    1178                 :          0 :         (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
    1179                 :            : 
    1180         [ #  # ]:          0 :         if (m->control_pid > 0)
    1181                 :          0 :                 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
    1182                 :            : 
    1183         [ #  # ]:          0 :         if (m->control_command_id >= 0)
    1184                 :          0 :                 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
    1185                 :            : 
    1186                 :          0 :         return 0;
    1187                 :            : }
    1188                 :            : 
    1189                 :          0 : static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
    1190                 :          0 :         Mount *m = MOUNT(u);
    1191                 :            :         int r;
    1192                 :            : 
    1193         [ #  # ]:          0 :         assert(u);
    1194         [ #  # ]:          0 :         assert(key);
    1195         [ #  # ]:          0 :         assert(value);
    1196         [ #  # ]:          0 :         assert(fds);
    1197                 :            : 
    1198         [ #  # ]:          0 :         if (streq(key, "state")) {
    1199                 :            :                 MountState state;
    1200                 :            : 
    1201         [ #  # ]:          0 :                 if ((state = mount_state_from_string(value)) < 0)
    1202         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse state value: %s", value);
    1203                 :            :                 else
    1204                 :          0 :                         m->deserialized_state = state;
    1205                 :            : 
    1206         [ #  # ]:          0 :         } else if (streq(key, "result")) {
    1207                 :            :                 MountResult f;
    1208                 :            : 
    1209                 :          0 :                 f = mount_result_from_string(value);
    1210         [ #  # ]:          0 :                 if (f < 0)
    1211         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse result value: %s", value);
    1212         [ #  # ]:          0 :                 else if (f != MOUNT_SUCCESS)
    1213                 :          0 :                         m->result = f;
    1214                 :            : 
    1215         [ #  # ]:          0 :         } else if (streq(key, "reload-result")) {
    1216                 :            :                 MountResult f;
    1217                 :            : 
    1218                 :          0 :                 f = mount_result_from_string(value);
    1219         [ #  # ]:          0 :                 if (f < 0)
    1220         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse reload result value: %s", value);
    1221         [ #  # ]:          0 :                 else if (f != MOUNT_SUCCESS)
    1222                 :          0 :                         m->reload_result = f;
    1223                 :            : 
    1224         [ #  # ]:          0 :         } else if (streq(key, "n-retry-umount")) {
    1225                 :            : 
    1226                 :          0 :                 r = safe_atou(value, &m->n_retry_umount);
    1227         [ #  # ]:          0 :                 if (r < 0)
    1228         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse n-retry-umount value: %s", value);
    1229                 :            : 
    1230         [ #  # ]:          0 :         } else if (streq(key, "control-pid")) {
    1231                 :            : 
    1232         [ #  # ]:          0 :                 if (parse_pid(value, &m->control_pid) < 0)
    1233         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse control-pid value: %s", value);
    1234                 :            : 
    1235         [ #  # ]:          0 :         } else if (streq(key, "control-command")) {
    1236                 :            :                 MountExecCommand id;
    1237                 :            : 
    1238                 :          0 :                 id = mount_exec_command_from_string(value);
    1239         [ #  # ]:          0 :                 if (id < 0)
    1240         [ #  # ]:          0 :                         log_unit_debug(u, "Failed to parse exec-command value: %s", value);
    1241                 :            :                 else {
    1242                 :          0 :                         m->control_command_id = id;
    1243                 :          0 :                         m->control_command = m->exec_command + id;
    1244                 :            :                 }
    1245                 :            :         } else
    1246         [ #  # ]:          0 :                 log_unit_debug(u, "Unknown serialization key: %s", key);
    1247                 :            : 
    1248                 :          0 :         return 0;
    1249                 :            : }
    1250                 :            : 
    1251                 :      10262 : _pure_ static UnitActiveState mount_active_state(Unit *u) {
    1252         [ -  + ]:      10262 :         assert(u);
    1253                 :            : 
    1254                 :      10262 :         return state_translation_table[MOUNT(u)->state];
    1255                 :            : }
    1256                 :            : 
    1257                 :          0 : _pure_ static const char *mount_sub_state_to_string(Unit *u) {
    1258         [ #  # ]:          0 :         assert(u);
    1259                 :            : 
    1260                 :          0 :         return mount_state_to_string(MOUNT(u)->state);
    1261                 :            : }
    1262                 :            : 
    1263                 :       2344 : _pure_ static bool mount_may_gc(Unit *u) {
    1264                 :       2344 :         Mount *m = MOUNT(u);
    1265                 :            : 
    1266         [ -  + ]:       2344 :         assert(m);
    1267                 :            : 
    1268         [ +  + ]:       2344 :         if (m->from_proc_self_mountinfo)
    1269                 :        616 :                 return false;
    1270                 :            : 
    1271                 :       1728 :         return true;
    1272                 :            : }
    1273                 :            : 
    1274                 :          0 : static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
    1275                 :          0 :         Mount *m = MOUNT(u);
    1276                 :            :         MountResult f;
    1277                 :            : 
    1278         [ #  # ]:          0 :         assert(m);
    1279         [ #  # ]:          0 :         assert(pid >= 0);
    1280                 :            : 
    1281         [ #  # ]:          0 :         if (pid != m->control_pid)
    1282                 :          0 :                 return;
    1283                 :            : 
    1284                 :            :         /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
    1285                 :            :          * they established/remove a mount. This is important when mounting, but even more so when unmounting
    1286                 :            :          * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
    1287                 :            :          * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
    1288                 :            :          * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
    1289                 :            :          * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
    1290                 :            :          * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
    1291                 :            :          * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
    1292                 :            :          * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
    1293                 :            :          * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
    1294                 :            :          * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
    1295                 :            :          * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
    1296                 :            :          * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
    1297                 :            :          * /proc/self/mountinfo changes before our mount/umount exits. */
    1298                 :          0 :         (void) mount_process_proc_self_mountinfo(u->manager);
    1299                 :            : 
    1300                 :          0 :         m->control_pid = 0;
    1301                 :            : 
    1302         [ #  # ]:          0 :         if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
    1303                 :          0 :                 f = MOUNT_SUCCESS;
    1304         [ #  # ]:          0 :         else if (code == CLD_EXITED)
    1305                 :          0 :                 f = MOUNT_FAILURE_EXIT_CODE;
    1306         [ #  # ]:          0 :         else if (code == CLD_KILLED)
    1307                 :          0 :                 f = MOUNT_FAILURE_SIGNAL;
    1308         [ #  # ]:          0 :         else if (code == CLD_DUMPED)
    1309                 :          0 :                 f = MOUNT_FAILURE_CORE_DUMP;
    1310                 :            :         else
    1311                 :          0 :                 assert_not_reached("Unknown code");
    1312                 :            : 
    1313   [ #  #  #  # ]:          0 :         if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
    1314                 :          0 :                 mount_set_reload_result(m, f);
    1315         [ #  # ]:          0 :         else if (m->result == MOUNT_SUCCESS)
    1316                 :          0 :                 m->result = f;
    1317                 :            : 
    1318         [ #  # ]:          0 :         if (m->control_command) {
    1319                 :          0 :                 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
    1320                 :            : 
    1321                 :          0 :                 m->control_command = NULL;
    1322                 :          0 :                 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
    1323                 :            :         }
    1324                 :            : 
    1325                 :          0 :         unit_log_process_exit(
    1326                 :            :                         u,
    1327                 :            :                         "Mount process",
    1328                 :            :                         mount_exec_command_to_string(m->control_command_id),
    1329                 :            :                         f == MOUNT_SUCCESS,
    1330                 :            :                         code, status);
    1331                 :            : 
    1332                 :            :         /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
    1333                 :            :          * before we process the SIGCHLD for the mount command. */
    1334                 :            : 
    1335   [ #  #  #  #  :          0 :         switch (m->state) {
                   #  # ]
    1336                 :            : 
    1337                 :          0 :         case MOUNT_MOUNTING:
    1338                 :            :                 /* Our mount point has not appeared in mountinfo.  Something went wrong. */
    1339                 :            : 
    1340         [ #  # ]:          0 :                 if (f == MOUNT_SUCCESS) {
    1341                 :            :                         /* Either /bin/mount has an unexpected definition of success,
    1342                 :            :                          * or someone raced us and we lost. */
    1343   [ #  #  #  # ]:          0 :                         log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
    1344                 :          0 :                         f = MOUNT_FAILURE_PROTOCOL;
    1345                 :            :                 }
    1346                 :          0 :                 mount_enter_dead(m, f);
    1347                 :          0 :                 break;
    1348                 :            : 
    1349                 :          0 :         case MOUNT_MOUNTING_DONE:
    1350                 :          0 :                 mount_enter_mounted(m, f);
    1351                 :          0 :                 break;
    1352                 :            : 
    1353                 :          0 :         case MOUNT_REMOUNTING:
    1354                 :            :         case MOUNT_REMOUNTING_SIGTERM:
    1355                 :            :         case MOUNT_REMOUNTING_SIGKILL:
    1356                 :          0 :                 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
    1357                 :          0 :                 break;
    1358                 :            : 
    1359                 :          0 :         case MOUNT_UNMOUNTING:
    1360                 :            : 
    1361   [ #  #  #  # ]:          0 :                 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
    1362                 :            : 
    1363                 :            :                         /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
    1364                 :            :                          * stacked on top of each other. We might exceed the timeout specified by the user overall,
    1365                 :            :                          * but we will stop as soon as any one umount times out. */
    1366                 :            : 
    1367         [ #  # ]:          0 :                         if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
    1368         [ #  # ]:          0 :                                 log_unit_debug(u, "Mount still present, trying again.");
    1369                 :          0 :                                 m->n_retry_umount++;
    1370                 :          0 :                                 mount_enter_unmounting(m);
    1371                 :            :                         } else {
    1372         [ #  # ]:          0 :                                 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
    1373                 :          0 :                                 mount_enter_mounted(m, f);
    1374                 :            :                         }
    1375                 :            :                 } else
    1376                 :          0 :                         mount_enter_dead_or_mounted(m, f);
    1377                 :            : 
    1378                 :          0 :                 break;
    1379                 :            : 
    1380                 :          0 :         case MOUNT_UNMOUNTING_SIGKILL:
    1381                 :            :         case MOUNT_UNMOUNTING_SIGTERM:
    1382                 :          0 :                 mount_enter_dead_or_mounted(m, f);
    1383                 :          0 :                 break;
    1384                 :            : 
    1385                 :          0 :         default:
    1386                 :          0 :                 assert_not_reached("Uh, control process died at wrong time.");
    1387                 :            :         }
    1388                 :            : 
    1389                 :            :         /* Notify clients about changed exit status */
    1390                 :          0 :         unit_add_to_dbus_queue(u);
    1391                 :            : }
    1392                 :            : 
    1393                 :          0 : static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
    1394                 :          0 :         Mount *m = MOUNT(userdata);
    1395                 :            : 
    1396         [ #  # ]:          0 :         assert(m);
    1397         [ #  # ]:          0 :         assert(m->timer_event_source == source);
    1398                 :            : 
    1399   [ #  #  #  #  :          0 :         switch (m->state) {
             #  #  #  # ]
    1400                 :            : 
    1401                 :          0 :         case MOUNT_MOUNTING:
    1402                 :            :         case MOUNT_MOUNTING_DONE:
    1403   [ #  #  #  # ]:          0 :                 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
    1404                 :          0 :                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
    1405                 :          0 :                 break;
    1406                 :            : 
    1407                 :          0 :         case MOUNT_REMOUNTING:
    1408   [ #  #  #  # ]:          0 :                 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
    1409                 :          0 :                 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
    1410                 :          0 :                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
    1411                 :          0 :                 break;
    1412                 :            : 
    1413                 :          0 :         case MOUNT_REMOUNTING_SIGTERM:
    1414                 :          0 :                 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
    1415                 :            : 
    1416         [ #  # ]:          0 :                 if (m->kill_context.send_sigkill) {
    1417   [ #  #  #  # ]:          0 :                         log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
    1418                 :          0 :                         mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
    1419                 :            :                 } else {
    1420   [ #  #  #  # ]:          0 :                         log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
    1421                 :          0 :                         mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
    1422                 :            :                 }
    1423                 :          0 :                 break;
    1424                 :            : 
    1425                 :          0 :         case MOUNT_REMOUNTING_SIGKILL:
    1426                 :          0 :                 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
    1427                 :            : 
    1428   [ #  #  #  # ]:          0 :                 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
    1429                 :          0 :                 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
    1430                 :          0 :                 break;
    1431                 :            : 
    1432                 :          0 :         case MOUNT_UNMOUNTING:
    1433   [ #  #  #  # ]:          0 :                 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
    1434                 :          0 :                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
    1435                 :          0 :                 break;
    1436                 :            : 
    1437                 :          0 :         case MOUNT_UNMOUNTING_SIGTERM:
    1438         [ #  # ]:          0 :                 if (m->kill_context.send_sigkill) {
    1439   [ #  #  #  # ]:          0 :                         log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
    1440                 :          0 :                         mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
    1441                 :            :                 } else {
    1442   [ #  #  #  # ]:          0 :                         log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
    1443                 :          0 :                         mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
    1444                 :            :                 }
    1445                 :          0 :                 break;
    1446                 :            : 
    1447                 :          0 :         case MOUNT_UNMOUNTING_SIGKILL:
    1448   [ #  #  #  # ]:          0 :                 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
    1449                 :          0 :                 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
    1450                 :          0 :                 break;
    1451                 :            : 
    1452                 :          0 :         default:
    1453                 :          0 :                 assert_not_reached("Timeout at wrong time.");
    1454                 :            :         }
    1455                 :            : 
    1456                 :          0 :         return 0;
    1457                 :            : }
    1458                 :            : 
    1459                 :        616 : static int mount_setup_new_unit(
    1460                 :            :                 Manager *m,
    1461                 :            :                 const char *name,
    1462                 :            :                 const char *what,
    1463                 :            :                 const char *where,
    1464                 :            :                 const char *options,
    1465                 :            :                 const char *fstype,
    1466                 :            :                 MountProcFlags *ret_flags,
    1467                 :            :                 Unit **ret) {
    1468                 :            : 
    1469                 :        616 :         _cleanup_(unit_freep) Unit *u = NULL;
    1470                 :            :         int r;
    1471                 :            : 
    1472         [ -  + ]:        616 :         assert(m);
    1473         [ -  + ]:        616 :         assert(name);
    1474         [ -  + ]:        616 :         assert(ret_flags);
    1475         [ -  + ]:        616 :         assert(ret);
    1476                 :            : 
    1477                 :        616 :         r = unit_new_for_name(m, sizeof(Mount), name, &u);
    1478         [ -  + ]:        616 :         if (r < 0)
    1479                 :          0 :                 return r;
    1480                 :            : 
    1481                 :        616 :         r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
    1482         [ -  + ]:        616 :         if (r < 0)
    1483                 :          0 :                 return r;
    1484                 :            : 
    1485                 :        616 :         r = free_and_strdup(&MOUNT(u)->where, where);
    1486         [ -  + ]:        616 :         if (r < 0)
    1487                 :          0 :                 return r;
    1488                 :            : 
    1489                 :        616 :         r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
    1490         [ -  + ]:        616 :         if (r < 0)
    1491                 :          0 :                 return r;
    1492                 :            : 
    1493                 :            :         /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
    1494                 :            :          * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
    1495                 :            :          * to.*/
    1496                 :        616 :         MOUNT(u)->from_proc_self_mountinfo = true;
    1497                 :            : 
    1498                 :            :         /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
    1499                 :            :          * loaded in now. */
    1500                 :        616 :         unit_add_to_load_queue(u);
    1501                 :            : 
    1502                 :        616 :         *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
    1503                 :        616 :         *ret = TAKE_PTR(u);
    1504                 :        616 :         return 0;
    1505                 :            : }
    1506                 :            : 
    1507                 :         44 : static int mount_setup_existing_unit(
    1508                 :            :                 Unit *u,
    1509                 :            :                 const char *what,
    1510                 :            :                 const char *where,
    1511                 :            :                 const char *options,
    1512                 :            :                 const char *fstype,
    1513                 :            :                 MountProcFlags *ret_flags) {
    1514                 :            : 
    1515                 :         44 :         MountProcFlags flags = MOUNT_PROC_IS_MOUNTED;
    1516                 :            :         int r;
    1517                 :            : 
    1518         [ -  + ]:         44 :         assert(u);
    1519         [ -  + ]:         44 :         assert(flags);
    1520                 :            : 
    1521         [ +  - ]:         44 :         if (!MOUNT(u)->where) {
    1522                 :         44 :                 MOUNT(u)->where = strdup(where);
    1523         [ -  + ]:         44 :                 if (!MOUNT(u)->where)
    1524                 :          0 :                         return -ENOMEM;
    1525                 :            :         }
    1526                 :            : 
    1527                 :         44 :         r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
    1528         [ -  + ]:         44 :         if (r < 0)
    1529                 :          0 :                 return r;
    1530         [ +  - ]:         44 :         if (r > 0)
    1531                 :         44 :                 flags |= MOUNT_PROC_JUST_CHANGED;
    1532                 :            : 
    1533   [ -  +  #  # ]:         44 :         if (!MOUNT(u)->from_proc_self_mountinfo || FLAGS_SET(MOUNT(u)->proc_flags, MOUNT_PROC_JUST_MOUNTED))
    1534                 :         44 :                 flags |= MOUNT_PROC_JUST_MOUNTED;
    1535                 :            : 
    1536                 :         44 :         MOUNT(u)->from_proc_self_mountinfo = true;
    1537                 :            : 
    1538   [ -  +  -  + ]:         44 :         if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
    1539                 :            :                 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
    1540                 :            :                  * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
    1541                 :          0 :                 u->load_state = UNIT_LOADED;
    1542                 :          0 :                 u->load_error = 0;
    1543                 :            : 
    1544                 :          0 :                 flags |= MOUNT_PROC_JUST_CHANGED;
    1545                 :            :         }
    1546                 :            : 
    1547         [ +  - ]:         44 :         if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
    1548                 :            :                 /* If things changed, then make sure that all deps are regenerated. Let's
    1549                 :            :                  * first remove all automatic deps, and then add in the new ones. */
    1550                 :            : 
    1551                 :         44 :                 unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
    1552                 :            : 
    1553                 :         44 :                 r = mount_add_extras(MOUNT(u));
    1554         [ -  + ]:         44 :                 if (r < 0)
    1555                 :          0 :                         return r;
    1556                 :            :         }
    1557                 :            : 
    1558                 :         44 :         *ret_flags = flags;
    1559                 :         44 :         return 0;
    1560                 :            : }
    1561                 :            : 
    1562                 :       1716 : static int mount_setup_unit(
    1563                 :            :                 Manager *m,
    1564                 :            :                 const char *what,
    1565                 :            :                 const char *where,
    1566                 :            :                 const char *options,
    1567                 :            :                 const char *fstype,
    1568                 :            :                 bool set_flags) {
    1569                 :            : 
    1570                 :       1716 :         _cleanup_free_ char *e = NULL;
    1571                 :            :         MountProcFlags flags;
    1572                 :            :         Unit *u;
    1573                 :            :         int r;
    1574                 :            : 
    1575         [ -  + ]:       1716 :         assert(m);
    1576         [ -  + ]:       1716 :         assert(what);
    1577         [ -  + ]:       1716 :         assert(where);
    1578         [ -  + ]:       1716 :         assert(options);
    1579         [ -  + ]:       1716 :         assert(fstype);
    1580                 :            : 
    1581                 :            :         /* Ignore API mount points. They should never be referenced in
    1582                 :            :          * dependencies ever. */
    1583   [ +  +  +  + ]:       1716 :         if (mount_point_is_api(where) || mount_point_ignore(where))
    1584                 :       1012 :                 return 0;
    1585                 :            : 
    1586         [ +  + ]:        704 :         if (streq(fstype, "autofs"))
    1587                 :         44 :                 return 0;
    1588                 :            : 
    1589                 :            :         /* probably some kind of swap, ignore */
    1590         [ -  + ]:        660 :         if (!is_path(where))
    1591                 :          0 :                 return 0;
    1592                 :            : 
    1593                 :        660 :         r = unit_name_from_path(where, ".mount", &e);
    1594         [ -  + ]:        660 :         if (r < 0)
    1595         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to generate unit name from path '%s': %m", where);
    1596                 :            : 
    1597                 :        660 :         u = manager_get_unit(m, e);
    1598         [ +  + ]:        660 :         if (u)
    1599                 :         44 :                 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
    1600                 :            :         else
    1601                 :            :                 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
    1602                 :            :                  * by the sysadmin having called mount(8) directly. */
    1603                 :        616 :                 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
    1604         [ -  + ]:        660 :         if (r < 0)
    1605         [ #  # ]:          0 :                 return log_warning_errno(r, "Failed to set up mount unit: %m");
    1606                 :            : 
    1607                 :            :         /* If the mount changed properties or state, let's notify our clients */
    1608         [ +  - ]:        660 :         if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
    1609                 :        660 :                 unit_add_to_dbus_queue(u);
    1610                 :            : 
    1611         [ -  + ]:        660 :         if (set_flags)
    1612                 :          0 :                 MOUNT(u)->proc_flags = flags;
    1613                 :            : 
    1614                 :        660 :         return 0;
    1615                 :            : }
    1616                 :            : 
    1617                 :         44 : static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
    1618                 :         44 :         _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
    1619                 :         44 :         _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
    1620                 :            :         int r;
    1621                 :            : 
    1622         [ -  + ]:         44 :         assert(m);
    1623                 :            : 
    1624                 :         44 :         r = libmount_parse(NULL, NULL, &table, &iter);
    1625         [ -  + ]:         44 :         if (r < 0)
    1626         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
    1627                 :            : 
    1628                 :       1716 :         for (;;) {
    1629                 :            :                 struct libmnt_fs *fs;
    1630                 :            :                 const char *device, *path, *options, *fstype;
    1631                 :            : 
    1632                 :       1760 :                 r = mnt_table_next_fs(table, iter, &fs);
    1633         [ +  + ]:       1760 :                 if (r == 1)
    1634                 :         44 :                         break;
    1635         [ -  + ]:       1716 :                 if (r < 0)
    1636         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
    1637                 :            : 
    1638                 :       1716 :                 device = mnt_fs_get_source(fs);
    1639                 :       1716 :                 path = mnt_fs_get_target(fs);
    1640                 :       1716 :                 options = mnt_fs_get_options(fs);
    1641                 :       1716 :                 fstype = mnt_fs_get_fstype(fs);
    1642                 :            : 
    1643   [ +  -  -  + ]:       1716 :                 if (!device || !path)
    1644                 :          0 :                         continue;
    1645                 :            : 
    1646                 :       1716 :                 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
    1647                 :            : 
    1648                 :       1716 :                 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
    1649                 :            :         }
    1650                 :            : 
    1651                 :         44 :         return 0;
    1652                 :            : }
    1653                 :            : 
    1654                 :         56 : static void mount_shutdown(Manager *m) {
    1655         [ -  + ]:         56 :         assert(m);
    1656                 :            : 
    1657                 :         56 :         m->mount_event_source = sd_event_source_unref(m->mount_event_source);
    1658                 :            : 
    1659                 :         56 :         mnt_unref_monitor(m->mount_monitor);
    1660                 :         56 :         m->mount_monitor = NULL;
    1661                 :         56 : }
    1662                 :            : 
    1663                 :          0 : static int mount_get_timeout(Unit *u, usec_t *timeout) {
    1664                 :          0 :         Mount *m = MOUNT(u);
    1665                 :            :         usec_t t;
    1666                 :            :         int r;
    1667                 :            : 
    1668         [ #  # ]:          0 :         if (!m->timer_event_source)
    1669                 :          0 :                 return 0;
    1670                 :            : 
    1671                 :          0 :         r = sd_event_source_get_time(m->timer_event_source, &t);
    1672         [ #  # ]:          0 :         if (r < 0)
    1673                 :          0 :                 return r;
    1674         [ #  # ]:          0 :         if (t == USEC_INFINITY)
    1675                 :          0 :                 return 0;
    1676                 :            : 
    1677                 :          0 :         *timeout = t;
    1678                 :          0 :         return 1;
    1679                 :            : }
    1680                 :            : 
    1681                 :         44 : static void mount_enumerate_perpetual(Manager *m) {
    1682                 :            :         Unit *u;
    1683                 :            :         int r;
    1684                 :            : 
    1685         [ -  + ]:         44 :         assert(m);
    1686                 :            : 
    1687                 :            :         /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
    1688                 :            :          * unconditionally synthesize it here and mark it as perpetual. */
    1689                 :            : 
    1690                 :         44 :         u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
    1691         [ +  - ]:         44 :         if (!u) {
    1692                 :         44 :                 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
    1693         [ -  + ]:         44 :                 if (r < 0) {
    1694         [ #  # ]:          0 :                         log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
    1695                 :          0 :                         return;
    1696                 :            :                 }
    1697                 :            :         }
    1698                 :            : 
    1699                 :         44 :         u->perpetual = true;
    1700                 :         44 :         MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
    1701                 :            : 
    1702                 :         44 :         unit_add_to_load_queue(u);
    1703                 :         44 :         unit_add_to_dbus_queue(u);
    1704                 :            : }
    1705                 :            : 
    1706                 :          0 : static bool mount_is_mounted(Mount *m) {
    1707         [ #  # ]:          0 :         assert(m);
    1708                 :            : 
    1709   [ #  #  #  #  :          0 :         return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
                   #  # ]
    1710                 :            : }
    1711                 :            : 
    1712                 :         44 : static void mount_enumerate(Manager *m) {
    1713                 :            :         int r;
    1714                 :            : 
    1715         [ -  + ]:         44 :         assert(m);
    1716                 :            : 
    1717                 :         44 :         mnt_init_debug(0);
    1718                 :            : 
    1719         [ +  - ]:         44 :         if (!m->mount_monitor) {
    1720                 :            :                 int fd;
    1721                 :            : 
    1722                 :         44 :                 m->mount_monitor = mnt_new_monitor();
    1723         [ -  + ]:         44 :                 if (!m->mount_monitor) {
    1724                 :          0 :                         log_oom();
    1725                 :          0 :                         goto fail;
    1726                 :            :                 }
    1727                 :            : 
    1728                 :         44 :                 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
    1729         [ -  + ]:         44 :                 if (r < 0) {
    1730         [ #  # ]:          0 :                         log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
    1731                 :          0 :                         goto fail;
    1732                 :            :                 }
    1733                 :            : 
    1734                 :         44 :                 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
    1735         [ -  + ]:         44 :                 if (r < 0) {
    1736         [ #  # ]:          0 :                         log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
    1737                 :          0 :                         goto fail;
    1738                 :            :                 }
    1739                 :            : 
    1740                 :            :                 /* mnt_unref_monitor() will close the fd */
    1741                 :         44 :                 fd = r = mnt_monitor_get_fd(m->mount_monitor);
    1742         [ -  + ]:         44 :                 if (r < 0) {
    1743         [ #  # ]:          0 :                         log_error_errno(r, "Failed to acquire watch file descriptor: %m");
    1744                 :          0 :                         goto fail;
    1745                 :            :                 }
    1746                 :            : 
    1747                 :         44 :                 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
    1748         [ -  + ]:         44 :                 if (r < 0) {
    1749         [ #  # ]:          0 :                         log_error_errno(r, "Failed to watch mount file descriptor: %m");
    1750                 :          0 :                         goto fail;
    1751                 :            :                 }
    1752                 :            : 
    1753                 :         44 :                 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
    1754         [ -  + ]:         44 :                 if (r < 0) {
    1755         [ #  # ]:          0 :                         log_error_errno(r, "Failed to adjust mount watch priority: %m");
    1756                 :          0 :                         goto fail;
    1757                 :            :                 }
    1758                 :            : 
    1759                 :         44 :                 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
    1760                 :            :         }
    1761                 :            : 
    1762                 :         44 :         r = mount_load_proc_self_mountinfo(m, false);
    1763         [ -  + ]:         44 :         if (r < 0)
    1764                 :          0 :                 goto fail;
    1765                 :            : 
    1766                 :         44 :         return;
    1767                 :            : 
    1768                 :          0 : fail:
    1769                 :          0 :         mount_shutdown(m);
    1770                 :            : }
    1771                 :            : 
    1772                 :          0 : static int drain_libmount(Manager *m) {
    1773                 :          0 :         bool rescan = false;
    1774                 :            :         int r;
    1775                 :            : 
    1776         [ #  # ]:          0 :         assert(m);
    1777                 :            : 
    1778                 :            :         /* Drain all events and verify that the event is valid.
    1779                 :            :          *
    1780                 :            :          * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
    1781                 :            :          * may generate event which is irrelevant for us.
    1782                 :            :          *
    1783                 :            :          * error: r < 0; valid: r == 0, false positive: r == 1 */
    1784                 :            :         do {
    1785                 :          0 :                 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
    1786         [ #  # ]:          0 :                 if (r < 0)
    1787         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to drain libmount events: %m");
    1788         [ #  # ]:          0 :                 if (r == 0)
    1789                 :          0 :                         rescan = true;
    1790         [ #  # ]:          0 :         } while (r == 0);
    1791                 :            : 
    1792                 :          0 :         return rescan;
    1793                 :            : }
    1794                 :            : 
    1795                 :          0 : static int mount_process_proc_self_mountinfo(Manager *m) {
    1796                 :          0 :         _cleanup_set_free_free_ Set *around = NULL, *gone = NULL;
    1797                 :            :         const char *what;
    1798                 :            :         Iterator i;
    1799                 :            :         Unit *u;
    1800                 :            :         int r;
    1801                 :            : 
    1802         [ #  # ]:          0 :         assert(m);
    1803                 :            : 
    1804                 :          0 :         r = drain_libmount(m);
    1805         [ #  # ]:          0 :         if (r <= 0)
    1806                 :          0 :                 return r;
    1807                 :            : 
    1808                 :          0 :         r = mount_load_proc_self_mountinfo(m, true);
    1809         [ #  # ]:          0 :         if (r < 0) {
    1810                 :            :                 /* Reset flags, just in case, for later calls */
    1811         [ #  # ]:          0 :                 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
    1812                 :          0 :                         MOUNT(u)->proc_flags = 0;
    1813                 :            : 
    1814                 :          0 :                 return 0;
    1815                 :            :         }
    1816                 :            : 
    1817                 :          0 :         manager_dispatch_load_queue(m);
    1818                 :            : 
    1819         [ #  # ]:          0 :         LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
    1820                 :          0 :                 Mount *mount = MOUNT(u);
    1821                 :            : 
    1822         [ #  # ]:          0 :                 if (!mount_is_mounted(mount)) {
    1823                 :            : 
    1824                 :            :                         /* A mount point is not around right now. It
    1825                 :            :                          * might be gone, or might never have
    1826                 :            :                          * existed. */
    1827                 :            : 
    1828         [ #  # ]:          0 :                         if (mount->from_proc_self_mountinfo &&
    1829         [ #  # ]:          0 :                             mount->parameters_proc_self_mountinfo.what) {
    1830                 :            : 
    1831                 :            :                                 /* Remember that this device might just have disappeared */
    1832   [ #  #  #  # ]:          0 :                                 if (set_ensure_allocated(&gone, &path_hash_ops) < 0 ||
    1833                 :          0 :                                     set_put_strdup(gone, mount->parameters_proc_self_mountinfo.what) < 0)
    1834                 :          0 :                                         log_oom(); /* we don't care too much about OOM here... */
    1835                 :            :                         }
    1836                 :            : 
    1837                 :          0 :                         mount->from_proc_self_mountinfo = false;
    1838         [ #  # ]:          0 :                         assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
    1839                 :            : 
    1840         [ #  # ]:          0 :                         switch (mount->state) {
    1841                 :            : 
    1842                 :          0 :                         case MOUNT_MOUNTED:
    1843                 :            :                                 /* This has just been unmounted by somebody else, follow the state change. */
    1844                 :          0 :                                 mount_enter_dead(mount, MOUNT_SUCCESS);
    1845                 :          0 :                                 break;
    1846                 :            : 
    1847                 :          0 :                         default:
    1848                 :          0 :                                 break;
    1849                 :            :                         }
    1850                 :            : 
    1851         [ #  # ]:          0 :                 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
    1852                 :            : 
    1853                 :            :                         /* A mount point was added or changed */
    1854                 :            : 
    1855      [ #  #  # ]:          0 :                         switch (mount->state) {
    1856                 :            : 
    1857                 :          0 :                         case MOUNT_DEAD:
    1858                 :            :                         case MOUNT_FAILED:
    1859                 :            : 
    1860                 :            :                                 /* This has just been mounted by somebody else, follow the state change, but let's
    1861                 :            :                                  * generate a new invocation ID for this implicitly and automatically. */
    1862                 :          0 :                                 (void) unit_acquire_invocation_id(u);
    1863                 :          0 :                                 mount_cycle_clear(mount);
    1864                 :          0 :                                 mount_enter_mounted(mount, MOUNT_SUCCESS);
    1865                 :          0 :                                 break;
    1866                 :            : 
    1867                 :          0 :                         case MOUNT_MOUNTING:
    1868                 :          0 :                                 mount_set_state(mount, MOUNT_MOUNTING_DONE);
    1869                 :          0 :                                 break;
    1870                 :            : 
    1871                 :          0 :                         default:
    1872                 :            :                                 /* Nothing really changed, but let's
    1873                 :            :                                  * issue an notification call
    1874                 :            :                                  * nonetheless, in case somebody is
    1875                 :            :                                  * waiting for this. (e.g. file system
    1876                 :            :                                  * ro/rw remounts.) */
    1877                 :          0 :                                 mount_set_state(mount, mount->state);
    1878                 :          0 :                                 break;
    1879                 :            :                         }
    1880                 :          0 :                 }
    1881                 :            : 
    1882   [ #  #  #  # ]:          0 :                 if (mount_is_mounted(mount) &&
    1883                 :          0 :                     mount->from_proc_self_mountinfo &&
    1884         [ #  # ]:          0 :                     mount->parameters_proc_self_mountinfo.what) {
    1885                 :            :                         /* Track devices currently used */
    1886                 :            : 
    1887   [ #  #  #  # ]:          0 :                         if (set_ensure_allocated(&around, &path_hash_ops) < 0 ||
    1888                 :          0 :                             set_put_strdup(around, mount->parameters_proc_self_mountinfo.what) < 0)
    1889                 :          0 :                                 log_oom();
    1890                 :            :                 }
    1891                 :            : 
    1892                 :            :                 /* Reset the flags for later calls */
    1893                 :          0 :                 mount->proc_flags = 0;
    1894                 :            :         }
    1895                 :            : 
    1896         [ #  # ]:          0 :         SET_FOREACH(what, gone, i) {
    1897         [ #  # ]:          0 :                 if (set_contains(around, what))
    1898                 :          0 :                         continue;
    1899                 :            : 
    1900                 :            :                 /* Let the device units know that the device is no longer mounted */
    1901                 :          0 :                 device_found_node(m, what, 0, DEVICE_FOUND_MOUNT);
    1902                 :            :         }
    1903                 :            : 
    1904                 :          0 :         return 0;
    1905                 :            : }
    1906                 :            : 
    1907                 :          0 : static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
    1908                 :          0 :         Manager *m = userdata;
    1909                 :            : 
    1910         [ #  # ]:          0 :         assert(m);
    1911         [ #  # ]:          0 :         assert(revents & EPOLLIN);
    1912                 :            : 
    1913                 :          0 :         return mount_process_proc_self_mountinfo(m);
    1914                 :            : }
    1915                 :            : 
    1916                 :          0 : static void mount_reset_failed(Unit *u) {
    1917                 :          0 :         Mount *m = MOUNT(u);
    1918                 :            : 
    1919         [ #  # ]:          0 :         assert(m);
    1920                 :            : 
    1921         [ #  # ]:          0 :         if (m->state == MOUNT_FAILED)
    1922                 :          0 :                 mount_set_state(m, MOUNT_DEAD);
    1923                 :            : 
    1924                 :          0 :         m->result = MOUNT_SUCCESS;
    1925                 :          0 :         m->reload_result = MOUNT_SUCCESS;
    1926                 :          0 : }
    1927                 :            : 
    1928                 :          0 : static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
    1929                 :          0 :         Mount *m = MOUNT(u);
    1930                 :            : 
    1931         [ #  # ]:          0 :         assert(m);
    1932                 :            : 
    1933                 :          0 :         return unit_kill_common(u, who, signo, -1, m->control_pid, error);
    1934                 :            : }
    1935                 :            : 
    1936                 :          0 : static int mount_control_pid(Unit *u) {
    1937                 :          0 :         Mount *m = MOUNT(u);
    1938                 :            : 
    1939         [ #  # ]:          0 :         assert(m);
    1940                 :            : 
    1941                 :          0 :         return m->control_pid;
    1942                 :            : }
    1943                 :            : 
    1944                 :            : static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
    1945                 :            :         [MOUNT_EXEC_MOUNT] = "ExecMount",
    1946                 :            :         [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
    1947                 :            :         [MOUNT_EXEC_REMOUNT] = "ExecRemount",
    1948                 :            : };
    1949                 :            : 
    1950   [ +  +  +  + ]:         40 : DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
    1951                 :            : 
    1952                 :            : static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
    1953                 :            :         [MOUNT_SUCCESS] = "success",
    1954                 :            :         [MOUNT_FAILURE_RESOURCES] = "resources",
    1955                 :            :         [MOUNT_FAILURE_TIMEOUT] = "timeout",
    1956                 :            :         [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
    1957                 :            :         [MOUNT_FAILURE_SIGNAL] = "signal",
    1958                 :            :         [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
    1959                 :            :         [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
    1960                 :            :         [MOUNT_FAILURE_PROTOCOL] = "protocol",
    1961                 :            : };
    1962                 :            : 
    1963   [ +  +  +  + ]:        440 : DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
    1964                 :            : 
    1965                 :            : const UnitVTable mount_vtable = {
    1966                 :            :         .object_size = sizeof(Mount),
    1967                 :            :         .exec_context_offset = offsetof(Mount, exec_context),
    1968                 :            :         .cgroup_context_offset = offsetof(Mount, cgroup_context),
    1969                 :            :         .kill_context_offset = offsetof(Mount, kill_context),
    1970                 :            :         .exec_runtime_offset = offsetof(Mount, exec_runtime),
    1971                 :            :         .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
    1972                 :            : 
    1973                 :            :         .sections =
    1974                 :            :                 "Unit\0"
    1975                 :            :                 "Mount\0"
    1976                 :            :                 "Install\0",
    1977                 :            :         .private_section = "Mount",
    1978                 :            : 
    1979                 :            :         .init = mount_init,
    1980                 :            :         .load = mount_load,
    1981                 :            :         .done = mount_done,
    1982                 :            : 
    1983                 :            :         .coldplug = mount_coldplug,
    1984                 :            : 
    1985                 :            :         .dump = mount_dump,
    1986                 :            : 
    1987                 :            :         .start = mount_start,
    1988                 :            :         .stop = mount_stop,
    1989                 :            :         .reload = mount_reload,
    1990                 :            : 
    1991                 :            :         .kill = mount_kill,
    1992                 :            : 
    1993                 :            :         .serialize = mount_serialize,
    1994                 :            :         .deserialize_item = mount_deserialize_item,
    1995                 :            : 
    1996                 :            :         .active_state = mount_active_state,
    1997                 :            :         .sub_state_to_string = mount_sub_state_to_string,
    1998                 :            : 
    1999                 :            :         .may_gc = mount_may_gc,
    2000                 :            : 
    2001                 :            :         .sigchld_event = mount_sigchld_event,
    2002                 :            : 
    2003                 :            :         .reset_failed = mount_reset_failed,
    2004                 :            : 
    2005                 :            :         .control_pid = mount_control_pid,
    2006                 :            : 
    2007                 :            :         .bus_vtable = bus_mount_vtable,
    2008                 :            :         .bus_set_property = bus_mount_set_property,
    2009                 :            :         .bus_commit_properties = bus_mount_commit_properties,
    2010                 :            : 
    2011                 :            :         .get_timeout = mount_get_timeout,
    2012                 :            : 
    2013                 :            :         .can_transient = true,
    2014                 :            : 
    2015                 :            :         .enumerate_perpetual = mount_enumerate_perpetual,
    2016                 :            :         .enumerate = mount_enumerate,
    2017                 :            :         .shutdown = mount_shutdown,
    2018                 :            : 
    2019                 :            :         .status_message_formats = {
    2020                 :            :                 .starting_stopping = {
    2021                 :            :                         [0] = "Mounting %s...",
    2022                 :            :                         [1] = "Unmounting %s...",
    2023                 :            :                 },
    2024                 :            :                 .finished_start_job = {
    2025                 :            :                         [JOB_DONE]       = "Mounted %s.",
    2026                 :            :                         [JOB_FAILED]     = "Failed to mount %s.",
    2027                 :            :                         [JOB_TIMEOUT]    = "Timed out mounting %s.",
    2028                 :            :                 },
    2029                 :            :                 .finished_stop_job = {
    2030                 :            :                         [JOB_DONE]       = "Unmounted %s.",
    2031                 :            :                         [JOB_FAILED]     = "Failed unmounting %s.",
    2032                 :            :                         [JOB_TIMEOUT]    = "Timed out unmounting %s.",
    2033                 :            :                 },
    2034                 :            :         },
    2035                 :            : };

Generated by: LCOV version 1.14