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" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
| 36 | static int process_config(VolumeManager *vm); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 37 | static void coldboot(const char *path); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | |
| 39 | int main() { |
| 40 | |
| 41 | VolumeManager *vm; |
| 42 | CommandListener *cl; |
| 43 | NetlinkManager *nm; |
| 44 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 45 | SLOGI("Vold 2.1 (the revenge) firing up"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 46 | |
| 47 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 48 | |
| 49 | /* Create our singleton managers */ |
| 50 | if (!(vm = VolumeManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 51 | SLOGE("Unable to create VolumeManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 52 | exit(1); |
| 53 | }; |
| 54 | |
| 55 | if (!(nm = NetlinkManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 56 | SLOGE("Unable to create NetlinkManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | exit(1); |
| 58 | }; |
| 59 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 60 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | cl = new CommandListener(); |
| 62 | vm->setBroadcaster((SocketListener *) cl); |
| 63 | nm->setBroadcaster((SocketListener *) cl); |
| 64 | |
| 65 | if (vm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 66 | SLOGE("Unable to start VolumeManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 67 | exit(1); |
| 68 | } |
| 69 | |
| 70 | if (process_config(vm)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 71 | SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (nm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 75 | SLOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 76 | exit(1); |
| 77 | } |
| 78 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 79 | coldboot("/sys/block"); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 80 | // coldboot("/sys/class/switch"); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 81 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Now that we're up, we can respond to commands |
| 84 | */ |
| 85 | if (cl->startListener()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 86 | SLOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | exit(1); |
| 88 | } |
| 89 | |
| 90 | // Eventually we'll become the monitoring thread |
| 91 | while(1) { |
| 92 | sleep(1000); |
| 93 | } |
| 94 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 95 | SLOGI("Vold exiting"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 96 | exit(0); |
| 97 | } |
| 98 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 99 | static void do_coldboot(DIR *d, int lvl) |
| 100 | { |
| 101 | struct dirent *de; |
| 102 | int dfd, fd; |
| 103 | |
| 104 | dfd = dirfd(d); |
| 105 | |
| 106 | fd = openat(dfd, "uevent", O_WRONLY); |
| 107 | if(fd >= 0) { |
| 108 | write(fd, "add\n", 4); |
| 109 | close(fd); |
| 110 | } |
| 111 | |
| 112 | while((de = readdir(d))) { |
| 113 | DIR *d2; |
| 114 | |
| 115 | if (de->d_name[0] == '.') |
| 116 | continue; |
| 117 | |
| 118 | if (de->d_type != DT_DIR && lvl > 0) |
| 119 | continue; |
| 120 | |
| 121 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 122 | if(fd < 0) |
| 123 | continue; |
| 124 | |
| 125 | d2 = fdopendir(fd); |
| 126 | if(d2 == 0) |
| 127 | close(fd); |
| 128 | else { |
| 129 | do_coldboot(d2, lvl + 1); |
| 130 | closedir(d2); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void coldboot(const char *path) |
| 136 | { |
| 137 | DIR *d = opendir(path); |
| 138 | if(d) { |
| 139 | do_coldboot(d, 0); |
| 140 | closedir(d); |
| 141 | } |
| 142 | } |
| 143 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 144 | static int process_config(VolumeManager *vm) { |
| 145 | FILE *fp; |
| 146 | int n = 0; |
| 147 | char line[255]; |
| 148 | |
| 149 | if (!(fp = fopen("/etc/vold.fstab", "r"))) { |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | while(fgets(line, sizeof(line), fp)) { |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 154 | const char *delim = " \t"; |
| 155 | char *save_ptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 156 | char *type, *label, *mount_point; |
| 157 | |
| 158 | n++; |
| 159 | line[strlen(line)-1] = '\0'; |
| 160 | |
| 161 | if (line[0] == '#' || line[0] == '\0') |
| 162 | continue; |
| 163 | |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 164 | if (!(type = strtok_r(line, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 165 | SLOGE("Error parsing type"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 166 | goto out_syntax; |
| 167 | } |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 168 | if (!(label = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 169 | SLOGE("Error parsing label"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 170 | goto out_syntax; |
| 171 | } |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 172 | if (!(mount_point = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 173 | SLOGE("Error parsing mount point"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 174 | goto out_syntax; |
| 175 | } |
| 176 | |
| 177 | if (!strcmp(type, "dev_mount")) { |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 178 | DirectVolume *dv = NULL; |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 179 | char *part; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 180 | |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 181 | if (!(part = strtok_r(NULL, delim, &save_ptr))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 182 | SLOGE("Error parsing partition"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 183 | goto out_syntax; |
| 184 | } |
| 185 | if (strcmp(part, "auto") && atoi(part) == 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 186 | 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] | 187 | goto out_syntax; |
| 188 | } |
| 189 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 190 | if (!strcmp(part, "auto")) { |
| 191 | dv = new DirectVolume(vm, label, mount_point, -1); |
| 192 | } else { |
| 193 | dv = new DirectVolume(vm, label, mount_point, atoi(part)); |
| 194 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 195 | |
Jinho You | 74ca25a | 2010-11-15 14:02:32 +0800 | [diff] [blame] | 196 | while (char *sysfs_path = strtok_r(NULL, delim, &save_ptr)) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | if (dv->addPath(sysfs_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 198 | SLOGE("Failed to add devpath %s to volume %s", sysfs_path, |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 199 | label); |
| 200 | goto out_fail; |
| 201 | } |
| 202 | } |
| 203 | vm->addVolume(dv); |
| 204 | } else if (!strcmp(type, "map_mount")) { |
| 205 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 206 | SLOGE("Unknown type '%s'", type); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 207 | goto out_syntax; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fclose(fp); |
| 212 | return 0; |
| 213 | |
| 214 | out_syntax: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 215 | SLOGE("Syntax error on config line %d", n); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 216 | errno = -EINVAL; |
| 217 | out_fail: |
| 218 | fclose(fp); |
| 219 | return -1; |
| 220 | } |