blob: ff4dd5829db9aa4a539d4d13a39369e75b416c71 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
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 Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Doug Zongker8edb00c2009-06-11 17:21:44 -070037#include "cutils/misc.h"
38#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070039#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070041#include "minzip/DirUtil.h"
42#include "mtdutils/mounts.h"
43#include "mtdutils/mtdutils.h"
44#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080045#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000046#include "flashutils/flashutils.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070047
Doug Zongker3d177d02010-07-01 09:18:44 -070048#ifdef USE_EXT4
49#include "make_ext4fs.h"
50#endif
51
52// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070053//
Doug Zongker3d177d02010-07-01 09:18:44 -070054// fs_type="yaffs2" partition_type="MTD" location=partition
55// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080056Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070057 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070058 if (argc != 4) {
59 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 }
Doug Zongker3d177d02010-07-01 09:18:44 -070061 char* fs_type;
62 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070063 char* location;
64 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070065 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
66 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070067 return NULL;
68 }
69
Doug Zongker3d177d02010-07-01 09:18:44 -070070 if (strlen(fs_type) == 0) {
71 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
72 goto done;
73 }
74 if (strlen(partition_type) == 0) {
75 ErrorAbort(state, "partition_type argument to %s() can't be empty",
76 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070077 goto done;
78 }
79 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070080 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070081 goto done;
82 }
83 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070084 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070085 goto done;
86 }
87
Stephen Smalley779701d2012-02-09 14:13:23 -050088 char *secontext = NULL;
89
90 if (sehandle) {
91 selabel_lookup(sehandle, &secontext, mount_point, 0755);
92 setfscreatecon(secontext);
93 }
Stephen Smalley779701d2012-02-09 14:13:23 -050094
Doug Zongker9931f7f2009-06-10 14:11:53 -070095 mkdir(mount_point, 0755);
96
Stephen Smalley779701d2012-02-09 14:13:23 -050097 if (secontext) {
98 freecon(secontext);
99 setfscreatecon(NULL);
100 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500101
Doug Zongker3d177d02010-07-01 09:18:44 -0700102 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700103 mtd_scan_partitions();
104 const MtdPartition* mtd;
105 mtd = mtd_find_partition_by_name(location);
106 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700107 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700108 name, location);
109 result = strdup("");
110 goto done;
111 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700112 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700113 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 location, strerror(errno));
115 result = strdup("");
116 goto done;
117 }
118 result = mount_point;
119 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700120 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700122 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700123 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700124 result = strdup("");
125 } else {
126 result = mount_point;
127 }
128 }
129
130done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700131 free(fs_type);
132 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133 free(location);
134 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800135 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700136}
137
Doug Zongker8edb00c2009-06-11 17:21:44 -0700138
139// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800140Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700141 char* result = NULL;
142 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700144 }
145 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700146 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700147 return NULL;
148 }
149 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700150 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700151 goto done;
152 }
153
154 scan_mounted_volumes();
155 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
156 if (vol == NULL) {
157 result = strdup("");
158 } else {
159 result = mount_point;
160 }
161
162done:
163 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800164 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700165}
166
167
Doug Zongker512536a2010-02-17 16:11:44 -0800168Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 char* result = NULL;
170 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 }
173 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 return NULL;
176 }
177 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700178 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 goto done;
180 }
181
182 scan_mounted_volumes();
183 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
184 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700185 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 result = strdup("");
187 } else {
188 unmount_mounted_volume(vol);
189 result = mount_point;
190 }
191
192done:
193 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800194 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700195}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196
197
Stephen Smalley779701d2012-02-09 14:13:23 -0500198// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700199//
Stephen Smalley779701d2012-02-09 14:13:23 -0500200// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
201// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800202// if fs_size == 0, then make_ext4fs uses the entire partition.
203// if fs_size > 0, that is the size to use
204// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800205Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400207 if (argc != 5) {
208 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700209 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700210 char* fs_type;
211 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800213 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400214 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500215
Stephen Smalley779701d2012-02-09 14:13:23 -0500216 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
217 return NULL;
218 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700219
Doug Zongker3d177d02010-07-01 09:18:44 -0700220 if (strlen(fs_type) == 0) {
221 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
222 goto done;
223 }
224 if (strlen(partition_type) == 0) {
225 ErrorAbort(state, "partition_type argument to %s() can't be empty",
226 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 goto done;
228 }
229 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 goto done;
232 }
233
Stephen Smalley516e4e22012-04-03 13:35:11 -0400234 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500235 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
236 goto done;
237 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500238
Doug Zongker3d177d02010-07-01 09:18:44 -0700239 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 mtd_scan_partitions();
241 const MtdPartition* mtd = mtd_find_partition_by_name(location);
242 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700243 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 name, location);
245 result = strdup("");
246 goto done;
247 }
248 MtdWriteContext* ctx = mtd_write_partition(mtd);
249 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700250 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 result = strdup("");
252 goto done;
253 }
254 if (mtd_erase_blocks(ctx, -1) == -1) {
255 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700256 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 result = strdup("");
258 goto done;
259 }
260 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700261 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700262 result = strdup("");
263 goto done;
264 }
265 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700266#ifdef USE_EXT4
267 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500268 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700269 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700270 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700271 name, status, location);
272 result = strdup("");
273 goto done;
274 }
275 result = location;
276#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700278 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700279 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280 }
281
282done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700283 free(fs_type);
284 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800286 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700287}
288
Doug Zongker8edb00c2009-06-11 17:21:44 -0700289
Doug Zongker512536a2010-02-17 16:11:44 -0800290Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291 char** paths = malloc(argc * sizeof(char*));
292 int i;
293 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700294 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700295 if (paths[i] == NULL) {
296 int j;
297 for (j = 0; j < i; ++i) {
298 free(paths[j]);
299 }
300 free(paths);
301 return NULL;
302 }
303 }
304
305 bool recursive = (strcmp(name, "delete_recursive") == 0);
306
307 int success = 0;
308 for (i = 0; i < argc; ++i) {
309 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
310 ++success;
311 free(paths[i]);
312 }
313 free(paths);
314
315 char buffer[10];
316 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800317 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318}
319
Doug Zongker8edb00c2009-06-11 17:21:44 -0700320
Doug Zongker512536a2010-02-17 16:11:44 -0800321Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700323 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 }
325 char* frac_str;
326 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700327 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 return NULL;
329 }
330
331 double frac = strtod(frac_str, NULL);
332 int sec = strtol(sec_str, NULL, 10);
333
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700334 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
336
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800338 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700339}
340
Doug Zongker512536a2010-02-17 16:11:44 -0800341Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700342 if (argc != 1) {
343 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
344 }
345 char* frac_str;
346 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
347 return NULL;
348 }
349
350 double frac = strtod(frac_str, NULL);
351
352 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
353 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
354
Doug Zongker512536a2010-02-17 16:11:44 -0800355 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356}
357
Doug Zongker8edb00c2009-06-11 17:21:44 -0700358// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800359Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700360 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363 }
364 char* zip_path;
365 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700366 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700367
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700368 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369
370 // To create a consistent system image, never use the clock for timestamps.
371 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
372
373 bool success = mzExtractRecursive(za, zip_path, dest_path,
374 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500375 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376 free(zip_path);
377 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800378 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379}
380
Doug Zongker8edb00c2009-06-11 17:21:44 -0700381
382// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800383// or
384// package_extract_file(package_path)
385// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800386// function (the char* returned is actually a FileContents*).
387Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700388 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800389 if (argc != 1 && argc != 2) {
390 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
391 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700392 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800394 if (argc == 2) {
395 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700396
Doug Zongker6aece332010-02-01 14:40:12 -0800397 char* zip_path;
398 char* dest_path;
399 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
400
401 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
402 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
403 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700404 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800405 goto done2;
406 }
407
408 FILE* f = fopen(dest_path, "wb");
409 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700410 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800411 name, dest_path, strerror(errno));
412 goto done2;
413 }
414 success = mzExtractZipEntryToFile(za, entry, fileno(f));
415 fclose(f);
416
417 done2:
418 free(zip_path);
419 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800420 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800421 } else {
422 // The one-argument version returns the contents of the file
423 // as the result.
424
425 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800426 Value* v = malloc(sizeof(Value));
427 v->type = VAL_BLOB;
428 v->size = -1;
429 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800430
431 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
432
433 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
434 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
435 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700436 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800437 goto done1;
438 }
439
Doug Zongker512536a2010-02-17 16:11:44 -0800440 v->size = mzGetZipEntryUncompLen(entry);
441 v->data = malloc(v->size);
442 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700443 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800444 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800445 goto done1;
446 }
447
Doug Zongker512536a2010-02-17 16:11:44 -0800448 success = mzExtractZipEntryToBuffer(za, entry,
449 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800450
451 done1:
452 free(zip_path);
453 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800454 free(v->data);
455 v->data = NULL;
456 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800457 }
Doug Zongker512536a2010-02-17 16:11:44 -0800458 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700459 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700460}
461
Doug Zongkera23075f2012-08-06 16:19:09 -0700462// Create all parent directories of name, if necessary.
463static int make_parents(char* name) {
464 char* p;
465 for (p = name + (strlen(name)-1); p > name; --p) {
466 if (*p != '/') continue;
467 *p = '\0';
468 if (make_parents(name) < 0) return -1;
469 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700470 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700471 *p = '/';
472 if (result == 0 || errno == EEXIST) {
473 // successfully created or already existed; we're done
474 return 0;
475 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700476 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700477 return -1;
478 }
479 }
480 return 0;
481}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700482
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700484// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800485Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700487 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 }
489 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700490 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700491 if (target == NULL) return NULL;
492
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700493 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 if (srcs == NULL) {
495 free(target);
496 return NULL;
497 }
498
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700499 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700500 int i;
501 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700502 if (unlink(srcs[i]) < 0) {
503 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700504 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700505 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700506 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700507 }
508 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700509 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700510 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700511 name, srcs[i], target);
512 ++bad;
513 }
Doug Zongker60babf82009-09-18 15:11:24 -0700514 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700515 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700516 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700517 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700518 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700519 free(srcs[i]);
520 }
521 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700522 if (bad) {
523 return ErrorAbort(state, "%s: some symlinks failed", name);
524 }
Doug Zongker512536a2010-02-17 16:11:44 -0800525 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526}
527
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528
Doug Zongker512536a2010-02-17 16:11:44 -0800529Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700530 char* result = NULL;
531 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
532
533 int min_args = 4 + (recursive ? 1 : 0);
534 if (argc < min_args) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700535 return ErrorAbort(state, "%s() expects %d+ args, got %d",
536 name, min_args, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700537 }
538
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700539 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700540 if (args == NULL) return NULL;
541
542 char* end;
543 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700544 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700545
546 int uid = strtoul(args[0], &end, 0);
547 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700548 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700549 goto done;
550 }
551
552 int gid = strtoul(args[1], &end, 0);
553 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700554 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700555 goto done;
556 }
557
558 if (recursive) {
559 int dir_mode = strtoul(args[2], &end, 0);
560 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700561 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700562 goto done;
563 }
564
565 int file_mode = strtoul(args[3], &end, 0);
566 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700567 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700568 name, args[3]);
569 goto done;
570 }
571
572 for (i = 4; i < argc; ++i) {
573 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
574 }
575 } else {
576 int mode = strtoul(args[2], &end, 0);
577 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700578 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579 goto done;
580 }
581
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700582 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700583 if (chown(args[i], uid, gid) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700584 printf("%s: chown of %s to %d %d failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700585 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700586 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700587 }
588 if (chmod(args[i], mode) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700589 printf("%s: chmod of %s to %o failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700590 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700591 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700592 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593 }
594 }
595 result = strdup("");
596
597done:
598 for (i = 0; i < argc; ++i) {
599 free(args[i]);
600 }
601 free(args);
602
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700603 if (bad) {
604 free(result);
605 return ErrorAbort(state, "%s: some changes failed", name);
606 }
Doug Zongker512536a2010-02-17 16:11:44 -0800607 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700608}
609
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700610struct perm_parsed_args {
611 bool has_uid;
612 uid_t uid;
613 bool has_gid;
614 gid_t gid;
615 bool has_mode;
616 mode_t mode;
617 bool has_fmode;
618 mode_t fmode;
619 bool has_dmode;
620 mode_t dmode;
621 bool has_selabel;
622 char* selabel;
623 bool has_capabilities;
624 uint64_t capabilities;
625};
626
627static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
628 int i;
629 struct perm_parsed_args parsed;
630 int bad = 0;
631 static int max_warnings = 20;
632
633 memset(&parsed, 0, sizeof(parsed));
634
635 for (i = 1; i < argc; i += 2) {
636 if (strcmp("uid", args[i]) == 0) {
637 int64_t uid;
638 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
639 parsed.uid = uid;
640 parsed.has_uid = true;
641 } else {
642 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
643 bad++;
644 }
645 continue;
646 }
647 if (strcmp("gid", args[i]) == 0) {
648 int64_t gid;
649 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
650 parsed.gid = gid;
651 parsed.has_gid = true;
652 } else {
653 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
654 bad++;
655 }
656 continue;
657 }
658 if (strcmp("mode", args[i]) == 0) {
659 int32_t mode;
660 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
661 parsed.mode = mode;
662 parsed.has_mode = true;
663 } else {
664 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
665 bad++;
666 }
667 continue;
668 }
669 if (strcmp("dmode", args[i]) == 0) {
670 int32_t mode;
671 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
672 parsed.dmode = mode;
673 parsed.has_dmode = true;
674 } else {
675 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
676 bad++;
677 }
678 continue;
679 }
680 if (strcmp("fmode", args[i]) == 0) {
681 int32_t mode;
682 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
683 parsed.fmode = mode;
684 parsed.has_fmode = true;
685 } else {
686 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
687 bad++;
688 }
689 continue;
690 }
691 if (strcmp("capabilities", args[i]) == 0) {
692 int64_t capabilities;
693 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
694 parsed.capabilities = capabilities;
695 parsed.has_capabilities = true;
696 } else {
697 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
698 bad++;
699 }
700 continue;
701 }
702 if (strcmp("selabel", args[i]) == 0) {
703 if (args[i+1][0] != '\0') {
704 parsed.selabel = args[i+1];
705 parsed.has_selabel = true;
706 } else {
707 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
708 bad++;
709 }
710 continue;
711 }
712 if (max_warnings != 0) {
713 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
714 max_warnings--;
715 if (max_warnings == 0) {
716 printf("ParsedPermArgs: suppressing further warnings\n");
717 }
718 }
719 }
720 return parsed;
721}
722
723static int ApplyParsedPerms(
724 const char* filename,
725 const struct stat *statptr,
726 struct perm_parsed_args parsed)
727{
728 int bad = 0;
729
Nick Kraleviche4612512013-09-10 15:34:19 -0700730 /* ignore symlinks */
731 if (S_ISLNK(statptr->st_mode)) {
732 return 0;
733 }
734
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700735 if (parsed.has_uid) {
736 if (chown(filename, parsed.uid, -1) < 0) {
737 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
738 filename, parsed.uid, strerror(errno));
739 bad++;
740 }
741 }
742
743 if (parsed.has_gid) {
744 if (chown(filename, -1, parsed.gid) < 0) {
745 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
746 filename, parsed.gid, strerror(errno));
747 bad++;
748 }
749 }
750
751 if (parsed.has_mode) {
752 if (chmod(filename, parsed.mode) < 0) {
753 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
754 filename, parsed.mode, strerror(errno));
755 bad++;
756 }
757 }
758
759 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
760 if (chmod(filename, parsed.dmode) < 0) {
761 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
762 filename, parsed.dmode, strerror(errno));
763 bad++;
764 }
765 }
766
767 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
768 if (chmod(filename, parsed.fmode) < 0) {
769 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
770 filename, parsed.fmode, strerror(errno));
771 bad++;
772 }
773 }
774
775 if (parsed.has_selabel) {
776 // TODO: Don't silently ignore ENOTSUP
777 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
778 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
779 filename, parsed.selabel, strerror(errno));
780 bad++;
781 }
782 }
783
784 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
785 if (parsed.capabilities == 0) {
786 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
787 // Report failure unless it's ENODATA (attribute not set)
788 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
789 filename, parsed.capabilities, strerror(errno));
790 bad++;
791 }
792 } else {
793 struct vfs_cap_data cap_data;
794 memset(&cap_data, 0, sizeof(cap_data));
795 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
796 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
797 cap_data.data[0].inheritable = 0;
798 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
799 cap_data.data[1].inheritable = 0;
800 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
801 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
802 filename, parsed.capabilities, strerror(errno));
803 bad++;
804 }
805 }
806 }
807
808 return bad;
809}
810
811// nftw doesn't allow us to pass along context, so we need to use
812// global variables. *sigh*
813static struct perm_parsed_args recursive_parsed_args;
814
815static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
816 int fileflags, struct FTW *pfwt) {
817 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
818}
819
820static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
821 int i;
822 int bad = 0;
823 static int nwarnings = 0;
824 struct stat sb;
825 Value* result = NULL;
826
827 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
828
829 if ((argc % 2) != 1) {
830 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
831 name, argc);
832 }
833
834 char** args = ReadVarArgs(state, argc, argv);
835 if (args == NULL) return NULL;
836
837 if (lstat(args[0], &sb) == -1) {
838 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
839 goto done;
840 }
841
842 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
843
844 if (recursive) {
845 recursive_parsed_args = parsed;
846 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
847 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
848 } else {
849 bad += ApplyParsedPerms(args[0], &sb, parsed);
850 }
851
852done:
853 for (i = 0; i < argc; ++i) {
854 free(args[i]);
855 }
856 free(args);
857
858 if (result != NULL) {
859 return result;
860 }
861
862 if (bad > 0) {
863 return ErrorAbort(state, "%s: some changes failed", name);
864 }
865
866 return StringValue(strdup(""));
867}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700868
Doug Zongker512536a2010-02-17 16:11:44 -0800869Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700870 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700871 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700872 }
873 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700874 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700875 if (key == NULL) return NULL;
876
877 char value[PROPERTY_VALUE_MAX];
878 property_get(key, value, "");
879 free(key);
880
Doug Zongker512536a2010-02-17 16:11:44 -0800881 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700882}
883
884
Doug Zongker47cace92009-06-18 10:11:50 -0700885// file_getprop(file, key)
886//
887// interprets 'file' as a getprop-style file (key=value pairs, one
888// per line, # comment lines and blank lines okay), and returns the value
889// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800890Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700891 char* result = NULL;
892 char* buffer = NULL;
893 char* filename;
894 char* key;
895 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
896 return NULL;
897 }
898
899 struct stat st;
900 if (stat(filename, &st) < 0) {
901 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
902 name, filename, strerror(errno));
903 goto done;
904 }
905
906#define MAX_FILE_GETPROP_SIZE 65536
907
908 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
909 ErrorAbort(state, "%s too large for %s (max %d)",
910 filename, name, MAX_FILE_GETPROP_SIZE);
911 goto done;
912 }
913
914 buffer = malloc(st.st_size+1);
915 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700916 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700917 goto done;
918 }
919
920 FILE* f = fopen(filename, "rb");
921 if (f == NULL) {
922 ErrorAbort(state, "%s: failed to open %s: %s",
923 name, filename, strerror(errno));
924 goto done;
925 }
926
927 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700928 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700929 name, st.st_size+1, filename);
930 fclose(f);
931 goto done;
932 }
933 buffer[st.st_size] = '\0';
934
935 fclose(f);
936
937 char* line = strtok(buffer, "\n");
938 do {
939 // skip whitespace at start of line
940 while (*line && isspace(*line)) ++line;
941
942 // comment or blank line: skip to next line
943 if (*line == '\0' || *line == '#') continue;
944
945 char* equal = strchr(line, '=');
946 if (equal == NULL) {
947 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
948 name, line, filename);
949 goto done;
950 }
951
952 // trim whitespace between key and '='
953 char* key_end = equal-1;
954 while (key_end > line && isspace(*key_end)) --key_end;
955 key_end[1] = '\0';
956
957 // not the key we're looking for
958 if (strcmp(key, line) != 0) continue;
959
960 // skip whitespace after the '=' to the start of the value
961 char* val_start = equal+1;
962 while(*val_start && isspace(*val_start)) ++val_start;
963
964 // trim trailing whitespace
965 char* val_end = val_start + strlen(val_start)-1;
966 while (val_end > val_start && isspace(*val_end)) --val_end;
967 val_end[1] = '\0';
968
969 result = strdup(val_start);
970 break;
971
972 } while ((line = strtok(NULL, "\n")));
973
974 if (result == NULL) result = strdup("");
975
976 done:
977 free(filename);
978 free(key);
979 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800980 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700981}
982
983
Doug Zongker8edb00c2009-06-11 17:21:44 -0700984static bool write_raw_image_cb(const unsigned char* data,
985 int data_len, void* ctx) {
986 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
987 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700988 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700989 return false;
990}
991
Doug Zongker179b2d92011-04-12 15:49:04 -0700992// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800993Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700994 char* result = NULL;
995
Doug Zongker179b2d92011-04-12 15:49:04 -0700996 Value* partition_value;
997 Value* contents;
998 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700999 return NULL;
1000 }
1001
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001002 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001003 if (partition_value->type != VAL_STRING) {
1004 ErrorAbort(state, "partition argument to %s must be string", name);
1005 goto done;
1006 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001007 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001008 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001009 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001010 goto done;
1011 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001012 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001013 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001014 goto done;
1015 }
1016
Dees_Troy76a1d412013-04-20 13:54:35 +00001017 char* filename = contents->data;
1018 if (0 == restore_raw_partition(NULL, partition, filename))
1019 result = strdup(partition);
1020 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001021 result = strdup("");
1022 goto done;
1023 }
1024
Doug Zongker8edb00c2009-06-11 17:21:44 -07001025done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001026 if (result != partition) FreeValue(partition_value);
1027 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001028 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001029}
1030
Doug Zongker8edb00c2009-06-11 17:21:44 -07001031// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001032Value* ApplyPatchSpaceFn(const char* name, State* state,
1033 int argc, Expr* argv[]) {
1034 char* bytes_str;
1035 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1036 return NULL;
1037 }
1038
1039 char* endptr;
1040 size_t bytes = strtol(bytes_str, &endptr, 10);
1041 if (bytes == 0 && endptr == bytes_str) {
1042 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1043 name, bytes_str);
1044 free(bytes_str);
1045 return NULL;
1046 }
1047
1048 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1049}
1050
1051
1052// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -08001053Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001054 if (argc < 6 || (argc % 2) == 1) {
1055 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1056 "even number, got %d",
1057 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001058 }
1059
Doug Zongkerc4351c72010-02-22 14:46:32 -08001060 char* source_filename;
1061 char* target_filename;
1062 char* target_sha1;
1063 char* target_size_str;
1064 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1065 &target_sha1, &target_size_str) < 0) {
1066 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001067 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068
Doug Zongkerc4351c72010-02-22 14:46:32 -08001069 char* endptr;
1070 size_t target_size = strtol(target_size_str, &endptr, 10);
1071 if (target_size == 0 && endptr == target_size_str) {
1072 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1073 name, target_size_str);
1074 free(source_filename);
1075 free(target_filename);
1076 free(target_sha1);
1077 free(target_size_str);
1078 return NULL;
1079 }
1080
1081 int patchcount = (argc-4) / 2;
1082 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001083
1084 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001085 for (i = 0; i < patchcount; ++i) {
1086 if (patches[i*2]->type != VAL_STRING) {
1087 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1088 break;
1089 }
1090 if (patches[i*2+1]->type != VAL_BLOB) {
1091 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1092 break;
1093 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001094 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001095 if (i != patchcount) {
1096 for (i = 0; i < patchcount*2; ++i) {
1097 FreeValue(patches[i]);
1098 }
1099 free(patches);
1100 return NULL;
1101 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102
Doug Zongkerc4351c72010-02-22 14:46:32 -08001103 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1104 for (i = 0; i < patchcount; ++i) {
1105 patch_sha_str[i] = patches[i*2]->data;
1106 patches[i*2]->data = NULL;
1107 FreeValue(patches[i*2]);
1108 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001109 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001110
1111 int result = applypatch(source_filename, target_filename,
1112 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001113 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001114
1115 for (i = 0; i < patchcount; ++i) {
1116 FreeValue(patches[i]);
1117 }
1118 free(patch_sha_str);
1119 free(patches);
1120
1121 return StringValue(strdup(result == 0 ? "t" : ""));
1122}
1123
1124// apply_patch_check(file, [sha1_1, ...])
1125Value* ApplyPatchCheckFn(const char* name, State* state,
1126 int argc, Expr* argv[]) {
1127 if (argc < 1) {
1128 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1129 name, argc);
1130 }
1131
1132 char* filename;
1133 if (ReadArgs(state, argv, 1, &filename) < 0) {
1134 return NULL;
1135 }
1136
1137 int patchcount = argc-1;
1138 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1139
1140 int result = applypatch_check(filename, patchcount, sha1s);
1141
1142 int i;
1143 for (i = 0; i < patchcount; ++i) {
1144 free(sha1s[i]);
1145 }
1146 free(sha1s);
1147
1148 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001149}
1150
Doug Zongker512536a2010-02-17 16:11:44 -08001151Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001152 char** args = ReadVarArgs(state, argc, argv);
1153 if (args == NULL) {
1154 return NULL;
1155 }
1156
1157 int size = 0;
1158 int i;
1159 for (i = 0; i < argc; ++i) {
1160 size += strlen(args[i]);
1161 }
1162 char* buffer = malloc(size+1);
1163 size = 0;
1164 for (i = 0; i < argc; ++i) {
1165 strcpy(buffer+size, args[i]);
1166 size += strlen(args[i]);
1167 free(args[i]);
1168 }
1169 free(args);
1170 buffer[size] = '\0';
1171
1172 char* line = strtok(buffer, "\n");
1173 while (line) {
1174 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1175 "ui_print %s\n", line);
1176 line = strtok(NULL, "\n");
1177 }
1178 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1179
Doug Zongker512536a2010-02-17 16:11:44 -08001180 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001181}
1182
Doug Zongkerd0181b82011-10-19 10:51:12 -07001183Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1184 if (argc != 0) {
1185 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1186 }
1187 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1188 return StringValue(strdup("t"));
1189}
1190
Doug Zongker512536a2010-02-17 16:11:44 -08001191Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001192 if (argc < 1) {
1193 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1194 }
1195 char** args = ReadVarArgs(state, argc, argv);
1196 if (args == NULL) {
1197 return NULL;
1198 }
1199
1200 char** args2 = malloc(sizeof(char*) * (argc+1));
1201 memcpy(args2, args, sizeof(char*) * argc);
1202 args2[argc] = NULL;
1203
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001204 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001205
1206 pid_t child = fork();
1207 if (child == 0) {
1208 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001209 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001210 _exit(1);
1211 }
1212 int status;
1213 waitpid(child, &status, 0);
1214 if (WIFEXITED(status)) {
1215 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001216 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001217 WEXITSTATUS(status));
1218 }
1219 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001220 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001221 WTERMSIG(status));
1222 }
1223
1224 int i;
1225 for (i = 0; i < argc; ++i) {
1226 free(args[i]);
1227 }
1228 free(args);
1229 free(args2);
1230
1231 char buffer[20];
1232 sprintf(buffer, "%d", status);
1233
Doug Zongker512536a2010-02-17 16:11:44 -08001234 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001235}
1236
Doug Zongker512536a2010-02-17 16:11:44 -08001237// Take a sha-1 digest and return it as a newly-allocated hex string.
1238static char* PrintSha1(uint8_t* digest) {
1239 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1240 int i;
1241 const char* alphabet = "0123456789abcdef";
1242 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1243 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1244 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1245 }
1246 buffer[i*2] = '\0';
1247 return buffer;
1248}
1249
1250// sha1_check(data)
1251// to return the sha1 of the data (given in the format returned by
1252// read_file).
1253//
1254// sha1_check(data, sha1_hex, [sha1_hex, ...])
1255// returns the sha1 of the file if it matches any of the hex
1256// strings passed, or "" if it does not equal any of them.
1257//
1258Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1259 if (argc < 1) {
1260 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1261 }
1262
1263 Value** args = ReadValueVarArgs(state, argc, argv);
1264 if (args == NULL) {
1265 return NULL;
1266 }
1267
1268 if (args[0]->size < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001269 printf("%s(): no file contents received", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001270 return StringValue(strdup(""));
1271 }
1272 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001273 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001274 FreeValue(args[0]);
1275
1276 if (argc == 1) {
1277 return StringValue(PrintSha1(digest));
1278 }
1279
1280 int i;
1281 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1282 for (i = 1; i < argc; ++i) {
1283 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001284 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001285 name, i);
1286 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1287 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001288 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1289 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001290 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1291 break;
1292 }
1293 FreeValue(args[i]);
1294 }
1295 if (i >= argc) {
1296 // Didn't match any of the hex strings; return false.
1297 return StringValue(strdup(""));
1298 }
1299 // Found a match; free all the remaining arguments and return the
1300 // matched one.
1301 int j;
1302 for (j = i+1; j < argc; ++j) {
1303 FreeValue(args[j]);
1304 }
1305 return args[i];
1306}
1307
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001308// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001309// is actually a FileContents*).
1310Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1311 if (argc != 1) {
1312 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1313 }
1314 char* filename;
1315 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1316
1317 Value* v = malloc(sizeof(Value));
1318 v->type = VAL_BLOB;
1319
1320 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001321 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001322 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1323 name, filename, strerror(errno));
1324 free(filename);
1325 free(v);
1326 free(fc.data);
1327 return NULL;
1328 }
1329
1330 v->size = fc.size;
1331 v->data = (char*)fc.data;
1332
1333 free(filename);
1334 return v;
1335}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001336
Doug Zongker9931f7f2009-06-10 14:11:53 -07001337void RegisterInstallFunctions() {
1338 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001339 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001340 RegisterFunction("unmount", UnmountFn);
1341 RegisterFunction("format", FormatFn);
1342 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001343 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001344 RegisterFunction("delete", DeleteFn);
1345 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001346 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1347 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001348 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001349
1350 // Maybe, at some future point, we can delete these functions? They have been
1351 // replaced by perm_set and perm_set_recursive.
Doug Zongker9931f7f2009-06-10 14:11:53 -07001352 RegisterFunction("set_perm", SetPermFn);
1353 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001354
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001355 // Usage:
1356 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1357 // Example:
1358 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1359 RegisterFunction("set_metadata", SetMetadataFn);
1360
1361 // Usage:
1362 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1363 // Example:
1364 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1365 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1366
Doug Zongker8edb00c2009-06-11 17:21:44 -07001367 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001368 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001369 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001370
1371 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001372 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1373 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001374
Doug Zongker512536a2010-02-17 16:11:44 -08001375 RegisterFunction("read_file", ReadFileFn);
1376 RegisterFunction("sha1_check", Sha1CheckFn);
1377
Doug Zongkerd0181b82011-10-19 10:51:12 -07001378 RegisterFunction("wipe_cache", WipeCacheFn);
1379
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001380 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001381
1382 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001383}