San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 26 | |
| 27 | #define LOG_TAG "Vold" |
| 28 | |
| 29 | #include "cutils/log.h" |
| 30 | |
| 31 | #include "VolumeManager.h" |
| 32 | #include "CommandListener.h" |
| 33 | #include "NetlinkManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 34 | #include "DirectVolume.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 35 | #include "cryptfs.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
| 37 | static int process_config(VolumeManager *vm); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 38 | static void coldboot(const char *path); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | |
| 40 | int main() { |
| 41 | |
| 42 | VolumeManager *vm; |
| 43 | CommandListener *cl; |
| 44 | NetlinkManager *nm; |
| 45 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 46 | SLOGI("Vold 2.1 (the revenge) firing up"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 47 | |
| 48 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | |
| 50 | /* Create our singleton managers */ |
| 51 | if (!(vm = VolumeManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 52 | SLOGE("Unable to create VolumeManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | exit(1); |
| 54 | }; |
| 55 | |
| 56 | if (!(nm = NetlinkManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 57 | SLOGE("Unable to create NetlinkManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 58 | exit(1); |
| 59 | }; |
| 60 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 61 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | cl = new CommandListener(); |
| 63 | vm->setBroadcaster((SocketListener *) cl); |
| 64 | nm->setBroadcaster((SocketListener *) cl); |
| 65 | |
| 66 | if (vm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 67 | SLOGE("Unable to start VolumeManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | if (process_config(vm)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 72 | SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (nm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 76 | SLOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | exit(1); |
| 78 | } |
| 79 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 80 | coldboot("/sys/block"); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 81 | // coldboot("/sys/class/switch"); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 82 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Now that we're up, we can respond to commands |
| 85 | */ |
| 86 | if (cl->startListener()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 87 | SLOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | // Eventually we'll become the monitoring thread |
| 92 | while(1) { |
| 93 | sleep(1000); |
| 94 | } |
| 95 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 96 | SLOGI("Vold exiting"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 97 | exit(0); |
| 98 | } |
| 99 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 100 | static void do_coldboot(DIR *d, int lvl) |
| 101 | { |
| 102 | struct dirent *de; |
| 103 | int dfd, fd; |
| 104 | |
| 105 | dfd = dirfd(d); |
| 106 | |
| 107 | fd = openat(dfd, "uevent", O_WRONLY); |
| 108 | if(fd >= 0) { |
| 109 | write(fd, "add\n", 4); |
| 110 | close(fd); |
| 111 | } |
| 112 | |
| 113 | while((de = readdir(d))) { |
| 114 | DIR *d2; |
| 115 | |
| 116 | if (de->d_name[0] == '.') |
| 117 | continue; |
| 118 | |
| 119 | if (de->d_type != DT_DIR && lvl > 0) |
| 120 | continue; |
| 121 | |
| 122 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 123 | if(fd < 0) |
| 124 | continue; |
| 125 | |
| 126 | d2 = fdopendir(fd); |
| 127 | if(d2 == 0) |
| 128 | close(fd); |
| 129 | else { |
| 130 | do_coldboot(d2, lvl + 1); |
| 131 | closedir(d2); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void coldboot(const char *path) |
| 137 | { |
| 138 | DIR *d = opendir(path); |
| 139 | if(d) { |
| 140 | do_coldboot(d, 0); |
| 141 | closedir(d); |
| 142 | } |
| 143 | } |
| 144 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 145 | static int parse_mount_flags(char *mount_flags) |
| 146 | { |
| 147 | char *save_ptr; |
| 148 | int flags = 0; |
| 149 | |
| 150 | if (strcasestr(mount_flags, "encryptable")) { |
| 151 | flags |= VOL_ENCRYPTABLE; |
| 152 | } |
| 153 | |
| 154 | if (strcasestr(mount_flags, "nonremovable")) { |
| 155 | flags |= VOL_NONREMOVABLE; |
| 156 | } |
| 157 | |
| 158 | return flags; |
| 159 | } |
| 160 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 161 | static int process_config(VolumeManager *vm) { |
| 162 | FILE *fp; |
| 163 | int n = 0; |
| 164 | char line[255]; |
| 165 | |
| 166 | if (!(fp = fopen("/etc/vold.fstab", "r"))) { |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | while(fgets(line, sizeof(line), fp)) { |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 171 | const char *delim = " \t"; |
| 172 | char *save_ptr; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 173 | char *type, *label, *mount_point, *mount_flags, *sysfs_path; |
| 174 | int flags; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 175 | |
| 176 | n++; |
| 177 | line[strlen(line)-1] = '\0'; |
| 178 | |
| 179 | if (line[0] == '#' || line[0] == '\0') |
| 180 | continue; |
| 181 | |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 182 | if (!(type = strtok_r(line, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 183 | SLOGE("Error parsing type"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 184 | goto out_syntax; |
| 185 | } |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 186 | if (!(label = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 187 | SLOGE("Error parsing label"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 188 | goto out_syntax; |
| 189 | } |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 190 | if (!(mount_point = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 191 | SLOGE("Error parsing mount point"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 192 | goto out_syntax; |
| 193 | } |
| 194 | |
| 195 | if (!strcmp(type, "dev_mount")) { |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 196 | DirectVolume *dv = NULL; |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 197 | char *part; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 198 | |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 199 | if (!(part = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 200 | SLOGE("Error parsing partition"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 201 | goto out_syntax; |
| 202 | } |
| 203 | if (strcmp(part, "auto") && atoi(part) == 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 204 | SLOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 205 | goto out_syntax; |
| 206 | } |
| 207 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 208 | if (!strcmp(part, "auto")) { |
| 209 | dv = new DirectVolume(vm, label, mount_point, -1); |
| 210 | } else { |
| 211 | dv = new DirectVolume(vm, label, mount_point, atoi(part)); |
| 212 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 213 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 214 | while ((sysfs_path = strtok_r(NULL, delim, &save_ptr))) { |
| 215 | if (*sysfs_path != '/') { |
| 216 | /* If the first character is not a '/', it must be flags */ |
| 217 | break; |
| 218 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 219 | if (dv->addPath(sysfs_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 220 | SLOGE("Failed to add devpath %s to volume %s", sysfs_path, |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 221 | label); |
| 222 | goto out_fail; |
| 223 | } |
| 224 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 225 | |
| 226 | /* If sysfs_path is non-null at this point, then it contains |
| 227 | * the optional flags for this volume |
| 228 | */ |
| 229 | if (sysfs_path) |
| 230 | flags = parse_mount_flags(sysfs_path); |
| 231 | else |
| 232 | flags = 0; |
| 233 | dv->setFlags(flags); |
| 234 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 235 | vm->addVolume(dv); |
| 236 | } else if (!strcmp(type, "map_mount")) { |
| 237 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 238 | SLOGE("Unknown type '%s'", type); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 239 | goto out_syntax; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fclose(fp); |
| 244 | return 0; |
| 245 | |
| 246 | out_syntax: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 247 | SLOGE("Syntax error on config line %d", n); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 248 | errno = -EINVAL; |
| 249 | out_fail: |
| 250 | fclose(fp); |
| 251 | return -1; |
| 252 | } |