Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <stdarg.h> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 29 | #include "cutils/misc.h" |
| 30 | #include "cutils/properties.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 31 | #include "edify/expr.h" |
| 32 | #include "minzip/DirUtil.h" |
| 33 | #include "mtdutils/mounts.h" |
| 34 | #include "mtdutils/mtdutils.h" |
| 35 | #include "updater.h" |
| 36 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 37 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 38 | // mount(type, location, mount_point) |
| 39 | // |
| 40 | // what: type="MTD" location="<partition>" to mount a yaffs2 filesystem |
| 41 | // type="vfat" location="/dev/block/<whatever>" to mount a device |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 42 | char* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 43 | char* result = NULL; |
| 44 | if (argc != 3) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 45 | return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 46 | } |
| 47 | char* type; |
| 48 | char* location; |
| 49 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 50 | if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | if (strlen(type) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 55 | ErrorAbort(state, "type argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 56 | goto done; |
| 57 | } |
| 58 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 59 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 60 | goto done; |
| 61 | } |
| 62 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 63 | ErrorAbort(state, "mount_point argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 64 | goto done; |
| 65 | } |
| 66 | |
| 67 | mkdir(mount_point, 0755); |
| 68 | |
| 69 | if (strcmp(type, "MTD") == 0) { |
| 70 | mtd_scan_partitions(); |
| 71 | const MtdPartition* mtd; |
| 72 | mtd = mtd_find_partition_by_name(location); |
| 73 | if (mtd == NULL) { |
| 74 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 75 | name, location); |
| 76 | result = strdup(""); |
| 77 | goto done; |
| 78 | } |
| 79 | if (mtd_mount_partition(mtd, mount_point, "yaffs2", 0 /* rw */) != 0) { |
| 80 | fprintf(stderr, "mtd mount of %s failed: %s\n", |
| 81 | location, strerror(errno)); |
| 82 | result = strdup(""); |
| 83 | goto done; |
| 84 | } |
| 85 | result = mount_point; |
| 86 | } else { |
| 87 | if (mount(location, mount_point, type, |
| 88 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 89 | fprintf(stderr, "%s: failed to mount %s at %s: %s\n", |
| 90 | name, location, mount_point, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 91 | result = strdup(""); |
| 92 | } else { |
| 93 | result = mount_point; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | done: |
| 98 | free(type); |
| 99 | free(location); |
| 100 | if (result != mount_point) free(mount_point); |
| 101 | return result; |
| 102 | } |
| 103 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 104 | |
| 105 | // is_mounted(mount_point) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 106 | char* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 107 | char* result = NULL; |
| 108 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 109 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 110 | } |
| 111 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 112 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
| 115 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 116 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 117 | goto done; |
| 118 | } |
| 119 | |
| 120 | scan_mounted_volumes(); |
| 121 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 122 | if (vol == NULL) { |
| 123 | result = strdup(""); |
| 124 | } else { |
| 125 | result = mount_point; |
| 126 | } |
| 127 | |
| 128 | done: |
| 129 | if (result != mount_point) free(mount_point); |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 134 | char* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 135 | char* result = NULL; |
| 136 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 137 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 138 | } |
| 139 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 140 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 144 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 145 | goto done; |
| 146 | } |
| 147 | |
| 148 | scan_mounted_volumes(); |
| 149 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 150 | if (vol == NULL) { |
| 151 | fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point); |
| 152 | result = strdup(""); |
| 153 | } else { |
| 154 | unmount_mounted_volume(vol); |
| 155 | result = mount_point; |
| 156 | } |
| 157 | |
| 158 | done: |
| 159 | if (result != mount_point) free(mount_point); |
| 160 | return result; |
| 161 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 162 | |
| 163 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 164 | // format(type, location) |
| 165 | // |
| 166 | // type="MTD" location=partition |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 167 | char* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 168 | char* result = NULL; |
| 169 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 170 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 171 | } |
| 172 | char* type; |
| 173 | char* location; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 174 | if (ReadArgs(state, argv, 2, &type, &location) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | if (strlen(type) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 179 | ErrorAbort(state, "type argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 180 | goto done; |
| 181 | } |
| 182 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 183 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 184 | goto done; |
| 185 | } |
| 186 | |
| 187 | if (strcmp(type, "MTD") == 0) { |
| 188 | mtd_scan_partitions(); |
| 189 | const MtdPartition* mtd = mtd_find_partition_by_name(location); |
| 190 | if (mtd == NULL) { |
| 191 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 192 | name, location); |
| 193 | result = strdup(""); |
| 194 | goto done; |
| 195 | } |
| 196 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 197 | if (ctx == NULL) { |
| 198 | fprintf(stderr, "%s: can't write \"%s\"", name, location); |
| 199 | result = strdup(""); |
| 200 | goto done; |
| 201 | } |
| 202 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 203 | mtd_write_close(ctx); |
| 204 | fprintf(stderr, "%s: failed to erase \"%s\"", name, location); |
| 205 | result = strdup(""); |
| 206 | goto done; |
| 207 | } |
| 208 | if (mtd_write_close(ctx) != 0) { |
| 209 | fprintf(stderr, "%s: failed to close \"%s\"", name, location); |
| 210 | result = strdup(""); |
| 211 | goto done; |
| 212 | } |
| 213 | result = location; |
| 214 | } else { |
| 215 | fprintf(stderr, "%s: unsupported type \"%s\"", name, type); |
| 216 | } |
| 217 | |
| 218 | done: |
| 219 | free(type); |
| 220 | if (result != location) free(location); |
| 221 | return result; |
| 222 | } |
| 223 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 224 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 225 | char* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 226 | char** paths = malloc(argc * sizeof(char*)); |
| 227 | int i; |
| 228 | for (i = 0; i < argc; ++i) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 229 | paths[i] = Evaluate(state, argv[i]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 230 | if (paths[i] == NULL) { |
| 231 | int j; |
| 232 | for (j = 0; j < i; ++i) { |
| 233 | free(paths[j]); |
| 234 | } |
| 235 | free(paths); |
| 236 | return NULL; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
| 241 | |
| 242 | int success = 0; |
| 243 | for (i = 0; i < argc; ++i) { |
| 244 | if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0) |
| 245 | ++success; |
| 246 | free(paths[i]); |
| 247 | } |
| 248 | free(paths); |
| 249 | |
| 250 | char buffer[10]; |
| 251 | sprintf(buffer, "%d", success); |
| 252 | return strdup(buffer); |
| 253 | } |
| 254 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 255 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 256 | char* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 257 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 258 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 259 | } |
| 260 | char* frac_str; |
| 261 | char* sec_str; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 262 | if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | double frac = strtod(frac_str, NULL); |
| 267 | int sec = strtol(sec_str, NULL, 10); |
| 268 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 269 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 270 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
| 271 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 272 | free(sec_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 273 | return frac_str; |
| 274 | } |
| 275 | |
| 276 | char* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 277 | if (argc != 1) { |
| 278 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 279 | } |
| 280 | char* frac_str; |
| 281 | if (ReadArgs(state, argv, 1, &frac_str) < 0) { |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | double frac = strtod(frac_str, NULL); |
| 286 | |
| 287 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 288 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
| 289 | |
| 290 | return frac_str; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 293 | // package_extract_dir(package_path, destination_path) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 294 | char* PackageExtractDirFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 295 | int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 296 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 297 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 298 | } |
| 299 | char* zip_path; |
| 300 | char* dest_path; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 301 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 302 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 303 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 304 | |
| 305 | // To create a consistent system image, never use the clock for timestamps. |
| 306 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 307 | |
| 308 | bool success = mzExtractRecursive(za, zip_path, dest_path, |
| 309 | MZ_EXTRACT_FILES_ONLY, ×tamp, |
| 310 | NULL, NULL); |
| 311 | free(zip_path); |
| 312 | free(dest_path); |
| 313 | return strdup(success ? "t" : ""); |
| 314 | } |
| 315 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 316 | |
| 317 | // package_extract_file(package_path, destination_path) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 318 | char* PackageExtractFileFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 319 | int argc, Expr* argv[]) { |
| 320 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 321 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 322 | } |
| 323 | char* zip_path; |
| 324 | char* dest_path; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 325 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 326 | |
| 327 | bool success = false; |
| 328 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 329 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 330 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 331 | if (entry == NULL) { |
| 332 | fprintf(stderr, "%s: no %s in package\n", name, zip_path); |
| 333 | goto done; |
| 334 | } |
| 335 | |
| 336 | FILE* f = fopen(dest_path, "wb"); |
| 337 | if (f == NULL) { |
| 338 | fprintf(stderr, "%s: can't open %s for write: %s\n", |
| 339 | name, dest_path, strerror(errno)); |
| 340 | goto done; |
| 341 | } |
| 342 | success = mzExtractZipEntryToFile(za, entry, fileno(f)); |
| 343 | fclose(f); |
| 344 | |
| 345 | done: |
| 346 | free(zip_path); |
| 347 | free(dest_path); |
| 348 | return strdup(success ? "t" : ""); |
| 349 | } |
| 350 | |
| 351 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 352 | // symlink target src1 src2 ... |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 353 | // unlinks any previously existing src1, src2, etc before creating symlinks. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 354 | char* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 355 | if (argc == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 356 | return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 357 | } |
| 358 | char* target; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 359 | target = Evaluate(state, argv[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 360 | if (target == NULL) return NULL; |
| 361 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 362 | char** srcs = ReadVarArgs(state, argc-1, argv+1); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 363 | if (srcs == NULL) { |
| 364 | free(target); |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | int i; |
| 369 | for (i = 0; i < argc-1; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 370 | if (unlink(srcs[i]) < 0) { |
| 371 | if (errno != ENOENT) { |
| 372 | fprintf(stderr, "%s: failed to remove %s: %s\n", |
| 373 | name, srcs[i], strerror(errno)); |
| 374 | } |
| 375 | } |
| 376 | if (symlink(target, srcs[i]) < 0) { |
| 377 | fprintf(stderr, "%s: failed to symlink %s to %s: %s\n", |
| 378 | name, srcs[i], target, strerror(errno)); |
| 379 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 380 | free(srcs[i]); |
| 381 | } |
| 382 | free(srcs); |
| 383 | return strdup(""); |
| 384 | } |
| 385 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 386 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 387 | char* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 388 | char* result = NULL; |
| 389 | bool recursive = (strcmp(name, "set_perm_recursive") == 0); |
| 390 | |
| 391 | int min_args = 4 + (recursive ? 1 : 0); |
| 392 | if (argc < min_args) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 393 | return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 396 | char** args = ReadVarArgs(state, argc, argv); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 397 | if (args == NULL) return NULL; |
| 398 | |
| 399 | char* end; |
| 400 | int i; |
| 401 | |
| 402 | int uid = strtoul(args[0], &end, 0); |
| 403 | if (*end != '\0' || args[0][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 404 | ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 405 | goto done; |
| 406 | } |
| 407 | |
| 408 | int gid = strtoul(args[1], &end, 0); |
| 409 | if (*end != '\0' || args[1][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 410 | ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 411 | goto done; |
| 412 | } |
| 413 | |
| 414 | if (recursive) { |
| 415 | int dir_mode = strtoul(args[2], &end, 0); |
| 416 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 417 | ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 418 | goto done; |
| 419 | } |
| 420 | |
| 421 | int file_mode = strtoul(args[3], &end, 0); |
| 422 | if (*end != '\0' || args[3][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 423 | ErrorAbort(state, "%s: \"%s\" not a valid filemode", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 424 | name, args[3]); |
| 425 | goto done; |
| 426 | } |
| 427 | |
| 428 | for (i = 4; i < argc; ++i) { |
| 429 | dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode); |
| 430 | } |
| 431 | } else { |
| 432 | int mode = strtoul(args[2], &end, 0); |
| 433 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 434 | ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 435 | goto done; |
| 436 | } |
| 437 | |
Doug Zongker | 0bbfe3d | 2009-06-25 13:37:31 -0700 | [diff] [blame] | 438 | for (i = 3; i < argc; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 439 | if (chown(args[i], uid, gid) < 0) { |
| 440 | fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n", |
| 441 | name, args[i], uid, gid, strerror(errno)); |
| 442 | } |
| 443 | if (chmod(args[i], mode) < 0) { |
| 444 | fprintf(stderr, "%s: chmod of %s to %o failed: %s\n", |
| 445 | name, args[i], mode, strerror(errno)); |
| 446 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | result = strdup(""); |
| 450 | |
| 451 | done: |
| 452 | for (i = 0; i < argc; ++i) { |
| 453 | free(args[i]); |
| 454 | } |
| 455 | free(args); |
| 456 | |
| 457 | return result; |
| 458 | } |
| 459 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 460 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 461 | char* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 462 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 463 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 464 | } |
| 465 | char* key; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 466 | key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 467 | if (key == NULL) return NULL; |
| 468 | |
| 469 | char value[PROPERTY_VALUE_MAX]; |
| 470 | property_get(key, value, ""); |
| 471 | free(key); |
| 472 | |
| 473 | return strdup(value); |
| 474 | } |
| 475 | |
| 476 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 477 | // file_getprop(file, key) |
| 478 | // |
| 479 | // interprets 'file' as a getprop-style file (key=value pairs, one |
| 480 | // per line, # comment lines and blank lines okay), and returns the value |
| 481 | // for 'key' (or "" if it isn't defined). |
| 482 | char* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 483 | char* result = NULL; |
| 484 | char* buffer = NULL; |
| 485 | char* filename; |
| 486 | char* key; |
| 487 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | struct stat st; |
| 492 | if (stat(filename, &st) < 0) { |
| 493 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", |
| 494 | name, filename, strerror(errno)); |
| 495 | goto done; |
| 496 | } |
| 497 | |
| 498 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 499 | |
| 500 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 501 | ErrorAbort(state, "%s too large for %s (max %d)", |
| 502 | filename, name, MAX_FILE_GETPROP_SIZE); |
| 503 | goto done; |
| 504 | } |
| 505 | |
| 506 | buffer = malloc(st.st_size+1); |
| 507 | if (buffer == NULL) { |
| 508 | ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1); |
| 509 | goto done; |
| 510 | } |
| 511 | |
| 512 | FILE* f = fopen(filename, "rb"); |
| 513 | if (f == NULL) { |
| 514 | ErrorAbort(state, "%s: failed to open %s: %s", |
| 515 | name, filename, strerror(errno)); |
| 516 | goto done; |
| 517 | } |
| 518 | |
| 519 | if (fread(buffer, 1, st.st_size, f) != st.st_size) { |
| 520 | ErrorAbort(state, "%s: failed to read %d bytes from %s", |
| 521 | name, st.st_size+1, filename); |
| 522 | fclose(f); |
| 523 | goto done; |
| 524 | } |
| 525 | buffer[st.st_size] = '\0'; |
| 526 | |
| 527 | fclose(f); |
| 528 | |
| 529 | char* line = strtok(buffer, "\n"); |
| 530 | do { |
| 531 | // skip whitespace at start of line |
| 532 | while (*line && isspace(*line)) ++line; |
| 533 | |
| 534 | // comment or blank line: skip to next line |
| 535 | if (*line == '\0' || *line == '#') continue; |
| 536 | |
| 537 | char* equal = strchr(line, '='); |
| 538 | if (equal == NULL) { |
| 539 | ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?", |
| 540 | name, line, filename); |
| 541 | goto done; |
| 542 | } |
| 543 | |
| 544 | // trim whitespace between key and '=' |
| 545 | char* key_end = equal-1; |
| 546 | while (key_end > line && isspace(*key_end)) --key_end; |
| 547 | key_end[1] = '\0'; |
| 548 | |
| 549 | // not the key we're looking for |
| 550 | if (strcmp(key, line) != 0) continue; |
| 551 | |
| 552 | // skip whitespace after the '=' to the start of the value |
| 553 | char* val_start = equal+1; |
| 554 | while(*val_start && isspace(*val_start)) ++val_start; |
| 555 | |
| 556 | // trim trailing whitespace |
| 557 | char* val_end = val_start + strlen(val_start)-1; |
| 558 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 559 | val_end[1] = '\0'; |
| 560 | |
| 561 | result = strdup(val_start); |
| 562 | break; |
| 563 | |
| 564 | } while ((line = strtok(NULL, "\n"))); |
| 565 | |
| 566 | if (result == NULL) result = strdup(""); |
| 567 | |
| 568 | done: |
| 569 | free(filename); |
| 570 | free(key); |
| 571 | free(buffer); |
| 572 | return result; |
| 573 | } |
| 574 | |
| 575 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 576 | static bool write_raw_image_cb(const unsigned char* data, |
| 577 | int data_len, void* ctx) { |
| 578 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 579 | if (r == data_len) return true; |
| 580 | fprintf(stderr, "%s\n", strerror(errno)); |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | // write_raw_image(file, partition) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 585 | char* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 586 | char* result = NULL; |
| 587 | |
| 588 | char* partition; |
| 589 | char* filename; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 590 | if (ReadArgs(state, argv, 2, &filename, &partition) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 595 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 596 | goto done; |
| 597 | } |
| 598 | if (strlen(filename) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 599 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 600 | goto done; |
| 601 | } |
| 602 | |
| 603 | mtd_scan_partitions(); |
| 604 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 605 | if (mtd == NULL) { |
| 606 | fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition); |
| 607 | result = strdup(""); |
| 608 | goto done; |
| 609 | } |
| 610 | |
| 611 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 612 | if (ctx == NULL) { |
| 613 | fprintf(stderr, "%s: can't write mtd partition \"%s\"\n", |
| 614 | name, partition); |
| 615 | result = strdup(""); |
| 616 | goto done; |
| 617 | } |
| 618 | |
| 619 | bool success; |
| 620 | |
| 621 | FILE* f = fopen(filename, "rb"); |
| 622 | if (f == NULL) { |
| 623 | fprintf(stderr, "%s: can't open %s: %s\n", |
| 624 | name, filename, strerror(errno)); |
| 625 | result = strdup(""); |
| 626 | goto done; |
| 627 | } |
| 628 | |
| 629 | success = true; |
| 630 | char* buffer = malloc(BUFSIZ); |
| 631 | int read; |
| 632 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 633 | int wrote = mtd_write_data(ctx, buffer, read); |
| 634 | success = success && (wrote == read); |
| 635 | if (!success) { |
| 636 | fprintf(stderr, "mtd_write_data to %s failed: %s\n", |
| 637 | partition, strerror(errno)); |
| 638 | } |
| 639 | } |
| 640 | free(buffer); |
| 641 | fclose(f); |
| 642 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 643 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 644 | fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition); |
| 645 | } |
| 646 | if (mtd_write_close(ctx) != 0) { |
| 647 | fprintf(stderr, "%s: error closing write of %s\n", name, partition); |
| 648 | } |
| 649 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 650 | printf("%s %s partition from %s\n", |
| 651 | success ? "wrote" : "failed to write", partition, filename); |
| 652 | |
| 653 | result = success ? partition : strdup(""); |
| 654 | |
| 655 | done: |
| 656 | if (result != partition) free(partition); |
| 657 | free(filename); |
| 658 | return result; |
| 659 | } |
| 660 | |
| 661 | // write_firmware_image(file, partition) |
| 662 | // |
| 663 | // partition is "radio" or "hboot" |
| 664 | // file is not used until after updater exits |
| 665 | // |
| 666 | // TODO: this should live in some HTC-specific library |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 667 | char* WriteFirmwareImageFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 668 | int argc, Expr* argv[]) { |
| 669 | char* result = NULL; |
| 670 | |
| 671 | char* partition; |
| 672 | char* filename; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 673 | if (ReadArgs(state, argv, 2, &filename, &partition) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 678 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 679 | goto done; |
| 680 | } |
| 681 | if (strlen(filename) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 682 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 683 | goto done; |
| 684 | } |
| 685 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 686 | FILE* cmd = ((UpdaterInfo*)(state->cookie))->cmd_pipe; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 687 | fprintf(cmd, "firmware %s %s\n", partition, filename); |
| 688 | |
| 689 | printf("will write %s firmware from %s\n", partition, filename); |
| 690 | result = partition; |
| 691 | |
| 692 | done: |
| 693 | if (result != partition) free(partition); |
| 694 | free(filename); |
| 695 | return result; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | extern int applypatch(int argc, char** argv); |
| 700 | |
| 701 | // apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1:patch, ...) |
| 702 | // apply_patch_check(file, sha1, ...) |
| 703 | // apply_patch_space(bytes) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 704 | char* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 705 | printf("in applypatchfn (%s)\n", name); |
| 706 | |
| 707 | char* prepend = NULL; |
| 708 | if (strstr(name, "check") != NULL) { |
| 709 | prepend = "-c"; |
| 710 | } else if (strstr(name, "space") != NULL) { |
| 711 | prepend = "-s"; |
| 712 | } |
| 713 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 714 | char** args = ReadVarArgs(state, argc, argv); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 715 | if (args == NULL) return NULL; |
| 716 | |
| 717 | // insert the "program name" argv[0] and a copy of the "prepend" |
| 718 | // string (if any) at the start of the args. |
| 719 | |
| 720 | int extra = 1 + (prepend != NULL ? 1 : 0); |
| 721 | char** temp = malloc((argc+extra) * sizeof(char*)); |
| 722 | memcpy(temp+extra, args, argc * sizeof(char*)); |
| 723 | temp[0] = strdup("updater"); |
| 724 | if (prepend) { |
| 725 | temp[1] = strdup(prepend); |
| 726 | } |
| 727 | free(args); |
| 728 | args = temp; |
| 729 | argc += extra; |
| 730 | |
| 731 | printf("calling applypatch\n"); |
| 732 | fflush(stdout); |
| 733 | int result = applypatch(argc, args); |
| 734 | printf("applypatch returned %d\n", result); |
| 735 | |
| 736 | int i; |
| 737 | for (i = 0; i < argc; ++i) { |
| 738 | free(args[i]); |
| 739 | } |
| 740 | free(args); |
| 741 | |
| 742 | switch (result) { |
| 743 | case 0: return strdup("t"); |
| 744 | case 1: return strdup(""); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 745 | default: return ErrorAbort(state, "applypatch couldn't parse args"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 749 | char* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 750 | char** args = ReadVarArgs(state, argc, argv); |
| 751 | if (args == NULL) { |
| 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | int size = 0; |
| 756 | int i; |
| 757 | for (i = 0; i < argc; ++i) { |
| 758 | size += strlen(args[i]); |
| 759 | } |
| 760 | char* buffer = malloc(size+1); |
| 761 | size = 0; |
| 762 | for (i = 0; i < argc; ++i) { |
| 763 | strcpy(buffer+size, args[i]); |
| 764 | size += strlen(args[i]); |
| 765 | free(args[i]); |
| 766 | } |
| 767 | free(args); |
| 768 | buffer[size] = '\0'; |
| 769 | |
| 770 | char* line = strtok(buffer, "\n"); |
| 771 | while (line) { |
| 772 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 773 | "ui_print %s\n", line); |
| 774 | line = strtok(NULL, "\n"); |
| 775 | } |
| 776 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 777 | |
| 778 | return buffer; |
| 779 | } |
| 780 | |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 781 | char* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 782 | if (argc < 1) { |
| 783 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 784 | } |
| 785 | char** args = ReadVarArgs(state, argc, argv); |
| 786 | if (args == NULL) { |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 791 | memcpy(args2, args, sizeof(char*) * argc); |
| 792 | args2[argc] = NULL; |
| 793 | |
| 794 | fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc); |
| 795 | |
| 796 | pid_t child = fork(); |
| 797 | if (child == 0) { |
| 798 | execv(args2[0], args2); |
| 799 | fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno)); |
| 800 | _exit(1); |
| 801 | } |
| 802 | int status; |
| 803 | waitpid(child, &status, 0); |
| 804 | if (WIFEXITED(status)) { |
| 805 | if (WEXITSTATUS(status) != 0) { |
| 806 | fprintf(stderr, "run_program: child exited with status %d\n", |
| 807 | WEXITSTATUS(status)); |
| 808 | } |
| 809 | } else if (WIFSIGNALED(status)) { |
| 810 | fprintf(stderr, "run_program: child terminated by signal %d\n", |
| 811 | WTERMSIG(status)); |
| 812 | } |
| 813 | |
| 814 | int i; |
| 815 | for (i = 0; i < argc; ++i) { |
| 816 | free(args[i]); |
| 817 | } |
| 818 | free(args); |
| 819 | free(args2); |
| 820 | |
| 821 | char buffer[20]; |
| 822 | sprintf(buffer, "%d", status); |
| 823 | |
| 824 | return strdup(buffer); |
| 825 | } |
| 826 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 827 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 828 | void RegisterInstallFunctions() { |
| 829 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 830 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 831 | RegisterFunction("unmount", UnmountFn); |
| 832 | RegisterFunction("format", FormatFn); |
| 833 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 834 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 835 | RegisterFunction("delete", DeleteFn); |
| 836 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 837 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 838 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 839 | RegisterFunction("symlink", SymlinkFn); |
| 840 | RegisterFunction("set_perm", SetPermFn); |
| 841 | RegisterFunction("set_perm_recursive", SetPermFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 842 | |
| 843 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 844 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 845 | RegisterFunction("write_raw_image", WriteRawImageFn); |
| 846 | RegisterFunction("write_firmware_image", WriteFirmwareImageFn); |
| 847 | |
| 848 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 849 | RegisterFunction("apply_patch_check", ApplyPatchFn); |
| 850 | RegisterFunction("apply_patch_space", ApplyPatchFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 851 | |
| 852 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 853 | |
| 854 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 855 | } |