File: | build-scan/../src/shared/switch-root.c |
Warning: | line 127, column 25 Value stored to 'old_root_fd' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | /*** |
3 | Copyright © 2012 Harald Hoyer |
4 | ***/ |
5 | |
6 | #include <errno(*__errno_location ()).h> |
7 | #include <fcntl.h> |
8 | #include <limits.h> |
9 | #include <stdbool.h> |
10 | #include <stdio.h> |
11 | #include <sys/mount.h> |
12 | #include <sys/stat.h> |
13 | #include <unistd.h> |
14 | |
15 | #include "base-filesystem.h" |
16 | #include "fd-util.h" |
17 | #include "fs-util.h" |
18 | #include "log.h" |
19 | #include "missing.h" |
20 | #include "mkdir.h" |
21 | #include "mount-util.h" |
22 | #include "path-util.h" |
23 | #include "rm-rf.h" |
24 | #include "stdio-util.h" |
25 | #include "string-util.h" |
26 | #include "strv.h" |
27 | #include "switch-root.h" |
28 | #include "user-util.h" |
29 | #include "util.h" |
30 | |
31 | int switch_root(const char *new_root, |
32 | const char *old_root_after, /* path below the new root, where to place the old root after the transition */ |
33 | bool_Bool unmount_old_root, |
34 | unsigned long mount_flags) { /* MS_MOVE or MS_BIND */ |
35 | |
36 | _cleanup_free___attribute__((cleanup(freep))) char *resolved_old_root_after = NULL((void*)0); |
37 | _cleanup_close___attribute__((cleanup(closep))) int old_root_fd = -1; |
38 | bool_Bool old_root_remove; |
39 | const char *i; |
40 | int r; |
41 | |
42 | assert(new_root)do { if ((__builtin_expect(!!(!(new_root)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("new_root"), "../src/shared/switch-root.c" , 42, __PRETTY_FUNCTION__); } while (0); |
43 | assert(old_root_after)do { if ((__builtin_expect(!!(!(old_root_after)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("old_root_after"), "../src/shared/switch-root.c" , 43, __PRETTY_FUNCTION__); } while (0); |
44 | |
45 | if (path_equal(new_root, "/")) |
46 | return 0; |
47 | |
48 | /* Check if we shall remove the contents of the old root */ |
49 | old_root_remove = in_initrd(); |
50 | if (old_root_remove) { |
51 | old_root_fd = open("/", O_RDONLY00|O_NONBLOCK04000|O_CLOEXEC02000000|O_NOCTTY0400|O_DIRECTORY0200000); |
52 | if (old_root_fd < 0) |
53 | return log_error_errno(errno, "Failed to open root directory: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 53, __func__ , "Failed to open root directory: %m") : -abs(_e); }); |
54 | } |
55 | |
56 | /* Determine where we shall place the old root after the transition */ |
57 | r = chase_symlinks(old_root_after, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved_old_root_after); |
58 | if (r < 0) |
59 | return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, old_root_after)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/shared/switch-root.c", 59, __func__, "Failed to resolve %s/%s: %m" , new_root, old_root_after) : -abs(_e); }); |
60 | if (r == 0) /* Doesn't exist yet. Let's create it */ |
61 | (void) mkdir_p_label(resolved_old_root_after, 0755); |
62 | |
63 | /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted MS_SHARED. Hence |
64 | * remount them MS_PRIVATE here as a work-around. |
65 | * |
66 | * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */ |
67 | if (mount(NULL((void*)0), "/", NULL((void*)0), MS_RECMS_REC|MS_PRIVATEMS_PRIVATE, NULL((void*)0)) < 0) |
68 | return log_error_errno(errno, "Failed to set \"/\" mount propagation to private: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 68, __func__ , "Failed to set \"/\" mount propagation to private: %m") : - abs(_e); }); |
69 | |
70 | FOREACH_STRING(i, "/sys", "/dev", "/run", "/proc")for (char **_l = ({ char **_ll = ((char**) ((const char*[]) { "/sys", "/dev", "/run", "/proc", ((void*)0) })); i = _ll ? _ll [0] : ((void*)0); _ll; }); _l && *_l; i = ({ _l ++; _l [0]; })) { |
71 | _cleanup_free___attribute__((cleanup(freep))) char *chased = NULL((void*)0); |
72 | |
73 | r = chase_symlinks(i, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &chased); |
74 | if (r < 0) |
75 | return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, i)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/shared/switch-root.c", 75, __func__, "Failed to resolve %s/%s: %m" , new_root, i) : -abs(_e); }); |
76 | if (r > 0) { |
77 | /* Already exists. Let's see if it is a mount point already. */ |
78 | r = path_is_mount_point(chased, NULL((void*)0), 0); |
79 | if (r < 0) |
80 | return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", chased)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/shared/switch-root.c", 80, __func__, "Failed to determine whether %s is a mount point: %m" , chased) : -abs(_e); }); |
81 | if (r > 0) /* If it is already mounted, then do nothing */ |
82 | continue; |
83 | } else |
84 | /* Doesn't exist yet? */ |
85 | (void) mkdir_p_label(chased, 0755); |
86 | |
87 | if (mount(i, chased, NULL((void*)0), mount_flags, NULL((void*)0)) < 0) |
88 | return log_error_errno(r, "Failed to mount %s to %s: %m", i, chased)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/shared/switch-root.c", 88, __func__, "Failed to mount %s to %s: %m" , i, chased) : -abs(_e); }); |
89 | } |
90 | |
91 | /* Do not fail if base_filesystem_create() fails. Not all switch roots are like base_filesystem_create() wants |
92 | * them to look like. They might even boot, if they are RO and don't have the FS layout. Just ignore the error |
93 | * and switch_root() nevertheless. */ |
94 | (void) base_filesystem_create(new_root, UID_INVALID((uid_t) -1), GID_INVALID((gid_t) -1)); |
95 | |
96 | if (chdir(new_root) < 0) |
97 | return log_error_errno(errno, "Failed to change directory to %s: %m", new_root)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 97, __func__ , "Failed to change directory to %s: %m", new_root) : -abs(_e ); }); |
98 | |
99 | /* We first try a pivot_root() so that we can umount the old root dir. In many cases (i.e. where rootfs is /), |
100 | * that's not possible however, and hence we simply overmount root */ |
101 | if (pivot_rootmissing_pivot_root(new_root, resolved_old_root_after) >= 0) { |
102 | |
103 | /* Immediately get rid of the old root, if detach_oldroot is set. |
104 | * Since we are running off it we need to do this lazily. */ |
105 | if (unmount_old_root) { |
106 | r = umount_recursive(old_root_after, MNT_DETACHMNT_DETACH); |
107 | if (r < 0) |
108 | log_warning_errno(r, "Failed to unmount old root directory tree, ignoring: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/shared/switch-root.c", 108, __func__, "Failed to unmount old root directory tree, ignoring: %m" ) : -abs(_e); }); |
109 | } |
110 | |
111 | } else if (mount(new_root, "/", NULL((void*)0), MS_MOVEMS_MOVE, NULL((void*)0)) < 0) |
112 | return log_error_errno(errno, "Failed to move %s to /: %m", new_root)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 112, __func__ , "Failed to move %s to /: %m", new_root) : -abs(_e); }); |
113 | |
114 | if (chroot(".") < 0) |
115 | return log_error_errno(errno, "Failed to change root: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 115, __func__ , "Failed to change root: %m") : -abs(_e); }); |
116 | |
117 | if (chdir("/") < 0) |
118 | return log_error_errno(errno, "Failed to change directory: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 118, __func__ , "Failed to change directory: %m") : -abs(_e); }); |
119 | |
120 | if (old_root_fd >= 0) { |
121 | struct stat rb; |
122 | |
123 | if (fstat(old_root_fd, &rb) < 0) |
124 | log_warning_errno(errno, "Failed to stat old root directory, leaving: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/shared/switch-root.c", 124, __func__ , "Failed to stat old root directory, leaving: %m") : -abs(_e ); }); |
125 | else { |
126 | (void) rm_rf_children(old_root_fd, 0, &rb); |
127 | old_root_fd = -1; |
Value stored to 'old_root_fd' is never read | |
128 | } |
129 | } |
130 | |
131 | return 0; |
132 | } |