blob: 422a1bb1e2f3cc2913450d3969ac00c1c3f02964 [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 Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080048#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080049#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070050
Doug Zongker3d177d02010-07-01 09:18:44 -070051#ifdef USE_EXT4
52#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080053#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070054#endif
55
Michael Runged4a63422014-10-22 19:48:41 -070056void uiPrint(State* state, char* buffer) {
57 char* line = strtok(buffer, "\n");
58 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
59 while (line) {
60 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
61 line = strtok(NULL, "\n");
62 }
63 fprintf(ui->cmd_pipe, "ui_print\n");
Tao Baob6918c72015-05-19 17:02:16 -070064
65 // The recovery will only print the contents to screen for pipe command
66 // ui_print. We need to dump the contents to stderr (which has been
67 // redirected to the log file) directly.
Tao Bao1eb90032015-06-03 09:49:02 -070068 fprintf(stderr, "%s", buffer);
Michael Runged4a63422014-10-22 19:48:41 -070069}
70
71__attribute__((__format__(printf, 2, 3))) __nonnull((2))
72void uiPrintf(State* state, const char* format, ...) {
73 char error_msg[1024];
74 va_list ap;
75 va_start(ap, format);
76 vsnprintf(error_msg, sizeof(error_msg), format, ap);
77 va_end(ap);
78 uiPrint(state, error_msg);
79}
80
Doug Zongker52b40362014-02-10 15:30:30 -080081// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070082char* PrintSha1(const uint8_t* digest) {
Tao Bao485b6372015-06-23 23:23:33 -070083 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080084 const char* alphabet = "0123456789abcdef";
Tao Bao485b6372015-06-23 23:23:33 -070085 size_t i;
Doug Zongker52b40362014-02-10 15:30:30 -080086 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
87 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
88 buffer[i*2+1] = alphabet[digest[i] & 0xf];
89 }
90 buffer[i*2] = '\0';
91 return buffer;
92}
93
Doug Zongker3d177d02010-07-01 09:18:44 -070094// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070095//
Doug Zongker3d177d02010-07-01 09:18:44 -070096// fs_type="yaffs2" partition_type="MTD" location=partition
97// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080098Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070099 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700100 if (argc != 4 && argc != 5) {
101 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700103 char* fs_type;
104 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700105 char* location;
106 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700107 char* mount_options;
108 bool has_mount_options;
109 if (argc == 5) {
110 has_mount_options = true;
111 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
112 &location, &mount_point, &mount_options) < 0) {
113 return NULL;
114 }
115 } else {
116 has_mount_options = false;
117 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700118 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700119 return NULL;
120 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 }
122
Doug Zongker3d177d02010-07-01 09:18:44 -0700123 if (strlen(fs_type) == 0) {
124 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
125 goto done;
126 }
127 if (strlen(partition_type) == 0) {
128 ErrorAbort(state, "partition_type argument to %s() can't be empty",
129 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130 goto done;
131 }
132 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700133 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 goto done;
135 }
136 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138 goto done;
139 }
140
Tao Bao485b6372015-06-23 23:23:33 -0700141 {
142 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500143
Tao Bao485b6372015-06-23 23:23:33 -0700144 if (sehandle) {
145 selabel_lookup(sehandle, &secontext, mount_point, 0755);
146 setfscreatecon(secontext);
147 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500148
Tao Bao485b6372015-06-23 23:23:33 -0700149 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150
Tao Bao485b6372015-06-23 23:23:33 -0700151 if (secontext) {
152 freecon(secontext);
153 setfscreatecon(NULL);
154 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500155 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500156
Doug Zongker3d177d02010-07-01 09:18:44 -0700157 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158 mtd_scan_partitions();
159 const MtdPartition* mtd;
160 mtd = mtd_find_partition_by_name(location);
161 if (mtd == NULL) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700162 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163 name, location);
164 result = strdup("");
165 goto done;
166 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700167 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700168 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 location, strerror(errno));
170 result = strdup("");
171 goto done;
172 }
173 result = mount_point;
174 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700175 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700176 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
177 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700178 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700179 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 result = strdup("");
181 } else {
182 result = mount_point;
183 }
184 }
185
186done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700187 free(fs_type);
188 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189 free(location);
190 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700191 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800192 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193}
194
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195
196// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800197Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700198 char* result = NULL;
199 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700200 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700201 }
202 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700203 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700204 return NULL;
205 }
206 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700207 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700208 goto done;
209 }
210
211 scan_mounted_volumes();
Tao Bao485b6372015-06-23 23:23:33 -0700212 {
213 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
214 if (vol == NULL) {
215 result = strdup("");
216 } else {
217 result = mount_point;
218 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219 }
220
221done:
222 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800223 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700224}
225
226
Doug Zongker512536a2010-02-17 16:11:44 -0800227Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 char* result = NULL;
229 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 }
232 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700233 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700234 return NULL;
235 }
236 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700237 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 goto done;
239 }
240
241 scan_mounted_volumes();
Tao Bao485b6372015-06-23 23:23:33 -0700242 {
243 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
244 if (vol == NULL) {
245 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
246 result = strdup("");
247 } else {
248 int ret = unmount_mounted_volume(vol);
249 if (ret != 0) {
250 uiPrintf(state, "unmount of %s failed (%d): %s\n",
251 mount_point, ret, strerror(errno));
252 }
253 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700254 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255 }
256
257done:
258 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800259 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700260}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700261
JP Abgrall37aedb32014-06-16 19:07:39 -0700262static int exec_cmd(const char* path, char* const argv[]) {
263 int status;
264 pid_t child;
265 if ((child = vfork()) == 0) {
266 execv(path, argv);
267 _exit(-1);
268 }
269 waitpid(child, &status, 0);
270 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
271 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
272 }
273 return WEXITSTATUS(status);
274}
275
Doug Zongker8edb00c2009-06-11 17:21:44 -0700276
Stephen Smalley779701d2012-02-09 14:13:23 -0500277// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278//
Stephen Smalley779701d2012-02-09 14:13:23 -0500279// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
280// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700281// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
282// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800283// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700284// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800285Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400287 if (argc != 5) {
288 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700290 char* fs_type;
291 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800293 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400294 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500295
Stephen Smalley779701d2012-02-09 14:13:23 -0500296 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
297 return NULL;
298 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299
Doug Zongker3d177d02010-07-01 09:18:44 -0700300 if (strlen(fs_type) == 0) {
301 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
302 goto done;
303 }
304 if (strlen(partition_type) == 0) {
305 ErrorAbort(state, "partition_type argument to %s() can't be empty",
306 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307 goto done;
308 }
309 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700310 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 goto done;
312 }
313
Stephen Smalley516e4e22012-04-03 13:35:11 -0400314 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500315 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
316 goto done;
317 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500318
Doug Zongker3d177d02010-07-01 09:18:44 -0700319 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700320 mtd_scan_partitions();
321 const MtdPartition* mtd = mtd_find_partition_by_name(location);
322 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700323 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 name, location);
325 result = strdup("");
326 goto done;
327 }
328 MtdWriteContext* ctx = mtd_write_partition(mtd);
329 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700330 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 result = strdup("");
332 goto done;
333 }
334 if (mtd_erase_blocks(ctx, -1) == -1) {
335 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700336 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 result = strdup("");
338 goto done;
339 }
340 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 result = strdup("");
343 goto done;
344 }
345 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700346#ifdef USE_EXT4
347 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500348 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700349 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700350 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700351 name, status, location);
352 result = strdup("");
353 goto done;
354 }
355 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700356 } else if (strcmp(fs_type, "f2fs") == 0) {
357 char *num_sectors;
358 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
359 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
360 result = strdup("");
361 goto done;
362 }
363 const char *f2fs_path = "/sbin/mkfs.f2fs";
364 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
365 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
366 free(num_sectors);
367 if (status != 0) {
368 printf("%s: mkfs.f2fs failed (%d) on %s",
369 name, status, location);
370 result = strdup("");
371 goto done;
372 }
373 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700374#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700376 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700377 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378 }
379
380done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700381 free(fs_type);
382 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700383 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800384 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700385}
386
Michael Rungece7ca712013-11-06 17:42:20 -0800387Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
388 char* result = NULL;
389 if (argc != 2) {
390 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
391 }
392
393 char* src_name;
394 char* dst_name;
395
396 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
397 return NULL;
398 }
399 if (strlen(src_name) == 0) {
400 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
401 goto done;
402 }
403 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700404 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800405 goto done;
406 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700407 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700408 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700409 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700410 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
411 // File was already moved
412 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700413 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700414 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800415 src_name, dst_name, strerror(errno));
416 } else {
417 result = dst_name;
418 }
419
420done:
421 free(src_name);
422 if (result != dst_name) free(dst_name);
423 return StringValue(result);
424}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700425
Doug Zongker512536a2010-02-17 16:11:44 -0800426Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao485b6372015-06-23 23:23:33 -0700427 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
428 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700429 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700430 if (paths[i] == NULL) {
431 int j;
432 for (j = 0; j < i; ++i) {
433 free(paths[j]);
434 }
435 free(paths);
436 return NULL;
437 }
438 }
439
440 bool recursive = (strcmp(name, "delete_recursive") == 0);
441
442 int success = 0;
Tao Bao485b6372015-06-23 23:23:33 -0700443 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
445 ++success;
446 free(paths[i]);
447 }
448 free(paths);
449
450 char buffer[10];
451 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800452 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453}
454
Doug Zongker8edb00c2009-06-11 17:21:44 -0700455
Doug Zongker512536a2010-02-17 16:11:44 -0800456Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700458 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 }
460 char* frac_str;
461 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700462 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 return NULL;
464 }
465
466 double frac = strtod(frac_str, NULL);
467 int sec = strtol(sec_str, NULL, 10);
468
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700469 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
471
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800473 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700474}
475
Doug Zongker512536a2010-02-17 16:11:44 -0800476Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700477 if (argc != 1) {
478 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
479 }
480 char* frac_str;
481 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
482 return NULL;
483 }
484
485 double frac = strtod(frac_str, NULL);
486
487 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
488 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
489
Doug Zongker512536a2010-02-17 16:11:44 -0800490 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700491}
492
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800494Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700495 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700497 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700498 }
499 char* zip_path;
500 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700501 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700502
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700503 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700504
505 // To create a consistent system image, never use the clock for timestamps.
506 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
507
508 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000509 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500510 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511 free(zip_path);
512 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800513 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514}
515
Doug Zongker8edb00c2009-06-11 17:21:44 -0700516
517// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800518// or
519// package_extract_file(package_path)
520// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800521// function (the char* returned is actually a FileContents*).
522Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700523 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700524 if (argc < 1 || argc > 2) {
525 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800526 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700527 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700529
Doug Zongker5f875bf2014-08-22 14:53:43 -0700530 if (argc == 2) {
531 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800532
533 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700534
Doug Zongker6aece332010-02-01 14:40:12 -0800535 char* zip_path;
536 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700537 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800538
Doug Zongker6aece332010-02-01 14:40:12 -0800539 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
540 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700541 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800542 goto done2;
543 }
544
Tao Bao485b6372015-06-23 23:23:33 -0700545 {
546 FILE* f = fopen(dest_path, "wb");
547 if (f == NULL) {
548 printf("%s: can't open %s for write: %s\n",
549 name, dest_path, strerror(errno));
550 goto done2;
551 }
552 success = mzExtractZipEntryToFile(za, entry, fileno(f));
553 fclose(f);
Doug Zongker6aece332010-02-01 14:40:12 -0800554 }
Doug Zongker6aece332010-02-01 14:40:12 -0800555
556 done2:
557 free(zip_path);
558 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800559 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800560 } else {
561 // The one-argument version returns the contents of the file
562 // as the result.
563
564 char* zip_path;
Tao Bao485b6372015-06-23 23:23:33 -0700565 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800566 v->type = VAL_BLOB;
567 v->size = -1;
568 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800569
570 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
571
572 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
573 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
574 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700575 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800576 goto done1;
577 }
578
Doug Zongker512536a2010-02-17 16:11:44 -0800579 v->size = mzGetZipEntryUncompLen(entry);
Tao Bao485b6372015-06-23 23:23:33 -0700580 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800581 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700582 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800583 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800584 goto done1;
585 }
586
Doug Zongker512536a2010-02-17 16:11:44 -0800587 success = mzExtractZipEntryToBuffer(za, entry,
588 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800589
590 done1:
591 free(zip_path);
592 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800593 free(v->data);
594 v->data = NULL;
595 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800596 }
Doug Zongker512536a2010-02-17 16:11:44 -0800597 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700598 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700599}
600
Doug Zongkera23075f2012-08-06 16:19:09 -0700601// Create all parent directories of name, if necessary.
602static int make_parents(char* name) {
603 char* p;
604 for (p = name + (strlen(name)-1); p > name; --p) {
605 if (*p != '/') continue;
606 *p = '\0';
607 if (make_parents(name) < 0) return -1;
608 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700609 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700610 *p = '/';
611 if (result == 0 || errno == EEXIST) {
612 // successfully created or already existed; we're done
613 return 0;
614 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700615 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700616 return -1;
617 }
618 }
619 return 0;
620}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700621
Doug Zongker9931f7f2009-06-10 14:11:53 -0700622// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700623// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800624Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700626 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700627 }
628 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700629 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 if (target == NULL) return NULL;
631
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700632 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700633 if (srcs == NULL) {
634 free(target);
635 return NULL;
636 }
637
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700638 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700639 int i;
640 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700641 if (unlink(srcs[i]) < 0) {
642 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700643 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700644 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700645 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700646 }
647 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700648 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700649 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700650 name, srcs[i], target);
651 ++bad;
652 }
Doug Zongker60babf82009-09-18 15:11:24 -0700653 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700654 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700655 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700656 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700657 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658 free(srcs[i]);
659 }
660 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700661 if (bad) {
662 return ErrorAbort(state, "%s: some symlinks failed", name);
663 }
Doug Zongker512536a2010-02-17 16:11:44 -0800664 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700665}
666
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700667struct perm_parsed_args {
668 bool has_uid;
669 uid_t uid;
670 bool has_gid;
671 gid_t gid;
672 bool has_mode;
673 mode_t mode;
674 bool has_fmode;
675 mode_t fmode;
676 bool has_dmode;
677 mode_t dmode;
678 bool has_selabel;
679 char* selabel;
680 bool has_capabilities;
681 uint64_t capabilities;
682};
683
Michael Runged4a63422014-10-22 19:48:41 -0700684static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700685 int i;
686 struct perm_parsed_args parsed;
687 int bad = 0;
688 static int max_warnings = 20;
689
690 memset(&parsed, 0, sizeof(parsed));
691
692 for (i = 1; i < argc; i += 2) {
693 if (strcmp("uid", args[i]) == 0) {
694 int64_t uid;
695 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
696 parsed.uid = uid;
697 parsed.has_uid = true;
698 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700699 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700700 bad++;
701 }
702 continue;
703 }
704 if (strcmp("gid", args[i]) == 0) {
705 int64_t gid;
706 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
707 parsed.gid = gid;
708 parsed.has_gid = true;
709 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700710 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700711 bad++;
712 }
713 continue;
714 }
715 if (strcmp("mode", args[i]) == 0) {
716 int32_t mode;
717 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
718 parsed.mode = mode;
719 parsed.has_mode = true;
720 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700721 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700722 bad++;
723 }
724 continue;
725 }
726 if (strcmp("dmode", args[i]) == 0) {
727 int32_t mode;
728 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
729 parsed.dmode = mode;
730 parsed.has_dmode = true;
731 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700732 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700733 bad++;
734 }
735 continue;
736 }
737 if (strcmp("fmode", args[i]) == 0) {
738 int32_t mode;
739 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
740 parsed.fmode = mode;
741 parsed.has_fmode = true;
742 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700743 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700744 bad++;
745 }
746 continue;
747 }
748 if (strcmp("capabilities", args[i]) == 0) {
749 int64_t capabilities;
750 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
751 parsed.capabilities = capabilities;
752 parsed.has_capabilities = true;
753 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700754 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700755 bad++;
756 }
757 continue;
758 }
759 if (strcmp("selabel", args[i]) == 0) {
760 if (args[i+1][0] != '\0') {
761 parsed.selabel = args[i+1];
762 parsed.has_selabel = true;
763 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700765 bad++;
766 }
767 continue;
768 }
769 if (max_warnings != 0) {
770 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
771 max_warnings--;
772 if (max_warnings == 0) {
773 printf("ParsedPermArgs: suppressing further warnings\n");
774 }
775 }
776 }
777 return parsed;
778}
779
780static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700781 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700782 const char* filename,
783 const struct stat *statptr,
784 struct perm_parsed_args parsed)
785{
786 int bad = 0;
787
Nick Kralevich68802412014-10-23 20:36:42 -0700788 if (parsed.has_selabel) {
789 if (lsetfilecon(filename, parsed.selabel) != 0) {
790 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
791 filename, parsed.selabel, strerror(errno));
792 bad++;
793 }
794 }
795
Nick Kraleviche4612512013-09-10 15:34:19 -0700796 /* ignore symlinks */
797 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700798 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700799 }
800
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700801 if (parsed.has_uid) {
802 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700803 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
804 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 bad++;
806 }
807 }
808
809 if (parsed.has_gid) {
810 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700811 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
812 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 bad++;
814 }
815 }
816
817 if (parsed.has_mode) {
818 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700819 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
820 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700821 bad++;
822 }
823 }
824
825 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
826 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700827 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
828 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700829 bad++;
830 }
831 }
832
833 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
834 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700835 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700836 filename, parsed.fmode, strerror(errno));
837 bad++;
838 }
839 }
840
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
842 if (parsed.capabilities == 0) {
843 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
844 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700845 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700846 filename, parsed.capabilities, strerror(errno));
847 bad++;
848 }
849 } else {
850 struct vfs_cap_data cap_data;
851 memset(&cap_data, 0, sizeof(cap_data));
852 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
853 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
854 cap_data.data[0].inheritable = 0;
855 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
856 cap_data.data[1].inheritable = 0;
857 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700858 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
859 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700860 bad++;
861 }
862 }
863 }
864
865 return bad;
866}
867
868// nftw doesn't allow us to pass along context, so we need to use
869// global variables. *sigh*
870static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700871static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872
873static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
874 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700875 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700876}
877
878static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700879 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700880 struct stat sb;
881 Value* result = NULL;
882
883 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
884
885 if ((argc % 2) != 1) {
Tao Bao485b6372015-06-23 23:23:33 -0700886 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700887 }
888
889 char** args = ReadVarArgs(state, argc, argv);
890 if (args == NULL) return NULL;
891
892 if (lstat(args[0], &sb) == -1) {
893 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
894 goto done;
895 }
896
Tao Bao485b6372015-06-23 23:23:33 -0700897 {
898 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899
Tao Bao485b6372015-06-23 23:23:33 -0700900 if (recursive) {
901 recursive_parsed_args = parsed;
902 recursive_state = state;
903 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
904 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
905 recursive_state = NULL;
906 } else {
907 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
908 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700909 }
910
911done:
Tao Bao485b6372015-06-23 23:23:33 -0700912 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700913 free(args[i]);
914 }
915 free(args);
916
917 if (result != NULL) {
918 return result;
919 }
920
921 if (bad > 0) {
922 return ErrorAbort(state, "%s: some changes failed", name);
923 }
924
925 return StringValue(strdup(""));
926}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700927
Doug Zongker512536a2010-02-17 16:11:44 -0800928Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700929 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700930 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700931 }
Tao Bao485b6372015-06-23 23:23:33 -0700932 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700933 if (key == NULL) return NULL;
934
935 char value[PROPERTY_VALUE_MAX];
936 property_get(key, value, "");
937 free(key);
938
Doug Zongker512536a2010-02-17 16:11:44 -0800939 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700940}
941
942
Doug Zongker47cace92009-06-18 10:11:50 -0700943// file_getprop(file, key)
944//
945// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700946// per line. # comment lines,blank lines, lines without '=' ignored),
947// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800948Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700949 char* result = NULL;
950 char* buffer = NULL;
951 char* filename;
952 char* key;
953 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
954 return NULL;
955 }
956
957 struct stat st;
958 if (stat(filename, &st) < 0) {
Tao Bao485b6372015-06-23 23:23:33 -0700959 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700960 goto done;
961 }
962
963#define MAX_FILE_GETPROP_SIZE 65536
964
965 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao485b6372015-06-23 23:23:33 -0700966 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700967 goto done;
968 }
969
Tao Bao485b6372015-06-23 23:23:33 -0700970 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700971 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700972 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700973 goto done;
974 }
975
Tao Bao485b6372015-06-23 23:23:33 -0700976 FILE* f;
977 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700978 if (f == NULL) {
Tao Bao485b6372015-06-23 23:23:33 -0700979 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700980 goto done;
981 }
982
983 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700984 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700985 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700986 fclose(f);
987 goto done;
988 }
989 buffer[st.st_size] = '\0';
990
991 fclose(f);
992
Tao Bao485b6372015-06-23 23:23:33 -0700993 char* line;
994 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700995 do {
996 // skip whitespace at start of line
997 while (*line && isspace(*line)) ++line;
998
999 // comment or blank line: skip to next line
1000 if (*line == '\0' || *line == '#') continue;
1001
1002 char* equal = strchr(line, '=');
1003 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001004 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001005 }
1006
1007 // trim whitespace between key and '='
1008 char* key_end = equal-1;
1009 while (key_end > line && isspace(*key_end)) --key_end;
1010 key_end[1] = '\0';
1011
1012 // not the key we're looking for
1013 if (strcmp(key, line) != 0) continue;
1014
1015 // skip whitespace after the '=' to the start of the value
1016 char* val_start = equal+1;
1017 while(*val_start && isspace(*val_start)) ++val_start;
1018
1019 // trim trailing whitespace
1020 char* val_end = val_start + strlen(val_start)-1;
1021 while (val_end > val_start && isspace(*val_end)) --val_end;
1022 val_end[1] = '\0';
1023
1024 result = strdup(val_start);
1025 break;
1026
1027 } while ((line = strtok(NULL, "\n")));
1028
1029 if (result == NULL) result = strdup("");
1030
1031 done:
1032 free(filename);
1033 free(key);
1034 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001035 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001036}
1037
Doug Zongker179b2d92011-04-12 15:49:04 -07001038// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001039Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001040 char* result = NULL;
1041
Doug Zongker179b2d92011-04-12 15:49:04 -07001042 Value* partition_value;
1043 Value* contents;
1044 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001045 return NULL;
1046 }
1047
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001048 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001049 if (partition_value->type != VAL_STRING) {
1050 ErrorAbort(state, "partition argument to %s must be string", name);
1051 goto done;
1052 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001053 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001054 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001055 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001056 goto done;
1057 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001058 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001059 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001060 goto done;
1061 }
1062
1063 mtd_scan_partitions();
Tao Bao485b6372015-06-23 23:23:33 -07001064 const MtdPartition* mtd;
1065 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001067 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 result = strdup("");
1069 goto done;
1070 }
1071
Tao Bao485b6372015-06-23 23:23:33 -07001072 MtdWriteContext* ctx;
1073 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001074 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001075 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076 name, partition);
1077 result = strdup("");
1078 goto done;
1079 }
1080
1081 bool success;
1082
Doug Zongker179b2d92011-04-12 15:49:04 -07001083 if (contents->type == VAL_STRING) {
1084 // we're given a filename as the contents
1085 char* filename = contents->data;
1086 FILE* f = fopen(filename, "rb");
1087 if (f == NULL) {
Tao Bao485b6372015-06-23 23:23:33 -07001088 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001089 result = strdup("");
1090 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001092
1093 success = true;
Tao Bao485b6372015-06-23 23:23:33 -07001094 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001095 int read;
1096 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1097 int wrote = mtd_write_data(ctx, buffer, read);
1098 success = success && (wrote == read);
1099 }
1100 free(buffer);
1101 fclose(f);
1102 } else {
1103 // we're given a blob as the contents
1104 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1105 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001106 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001107 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001108 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001109 partition, strerror(errno));
1110 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001111
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001112 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001113 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001114 }
1115 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001116 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001117 }
1118
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 printf("%s %s partition\n",
1120 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001121
1122 result = success ? partition : strdup("");
1123
1124done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001125 if (result != partition) FreeValue(partition_value);
1126 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001127 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001128}
1129
Doug Zongker8edb00c2009-06-11 17:21:44 -07001130// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001131Value* ApplyPatchSpaceFn(const char* name, State* state,
1132 int argc, Expr* argv[]) {
1133 char* bytes_str;
1134 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1135 return NULL;
1136 }
1137
1138 char* endptr;
1139 size_t bytes = strtol(bytes_str, &endptr, 10);
1140 if (bytes == 0 && endptr == bytes_str) {
Tao Bao485b6372015-06-23 23:23:33 -07001141 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001142 free(bytes_str);
1143 return NULL;
1144 }
1145
1146 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1147}
1148
Doug Zongker52b40362014-02-10 15:30:30 -08001149// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1150
Doug Zongker512536a2010-02-17 16:11:44 -08001151Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001152 if (argc < 6 || (argc % 2) == 1) {
1153 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1154 "even number, got %d",
1155 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001156 }
1157
Doug Zongkerc4351c72010-02-22 14:46:32 -08001158 char* source_filename;
1159 char* target_filename;
1160 char* target_sha1;
1161 char* target_size_str;
1162 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1163 &target_sha1, &target_size_str) < 0) {
1164 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001165 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001166
Doug Zongkerc4351c72010-02-22 14:46:32 -08001167 char* endptr;
1168 size_t target_size = strtol(target_size_str, &endptr, 10);
1169 if (target_size == 0 && endptr == target_size_str) {
1170 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1171 name, target_size_str);
1172 free(source_filename);
1173 free(target_filename);
1174 free(target_sha1);
1175 free(target_size_str);
1176 return NULL;
1177 }
1178
1179 int patchcount = (argc-4) / 2;
1180 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001181
1182 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001183 for (i = 0; i < patchcount; ++i) {
1184 if (patches[i*2]->type != VAL_STRING) {
1185 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1186 break;
1187 }
1188 if (patches[i*2+1]->type != VAL_BLOB) {
1189 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1190 break;
1191 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001192 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193 if (i != patchcount) {
1194 for (i = 0; i < patchcount*2; ++i) {
1195 FreeValue(patches[i]);
1196 }
1197 free(patches);
1198 return NULL;
1199 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001200
Tao Bao485b6372015-06-23 23:23:33 -07001201 char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -08001202 for (i = 0; i < patchcount; ++i) {
1203 patch_sha_str[i] = patches[i*2]->data;
1204 patches[i*2]->data = NULL;
1205 FreeValue(patches[i*2]);
1206 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001207 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001208
1209 int result = applypatch(source_filename, target_filename,
1210 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001211 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001212
1213 for (i = 0; i < patchcount; ++i) {
1214 FreeValue(patches[i]);
1215 }
1216 free(patch_sha_str);
1217 free(patches);
1218
1219 return StringValue(strdup(result == 0 ? "t" : ""));
1220}
1221
1222// apply_patch_check(file, [sha1_1, ...])
1223Value* ApplyPatchCheckFn(const char* name, State* state,
1224 int argc, Expr* argv[]) {
1225 if (argc < 1) {
1226 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1227 name, argc);
1228 }
1229
1230 char* filename;
1231 if (ReadArgs(state, argv, 1, &filename) < 0) {
1232 return NULL;
1233 }
1234
1235 int patchcount = argc-1;
1236 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1237
1238 int result = applypatch_check(filename, patchcount, sha1s);
1239
1240 int i;
1241 for (i = 0; i < patchcount; ++i) {
1242 free(sha1s[i]);
1243 }
1244 free(sha1s);
1245
1246 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001247}
1248
Doug Zongker512536a2010-02-17 16:11:44 -08001249Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001250 char** args = ReadVarArgs(state, argc, argv);
1251 if (args == NULL) {
1252 return NULL;
1253 }
1254
1255 int size = 0;
1256 int i;
1257 for (i = 0; i < argc; ++i) {
1258 size += strlen(args[i]);
1259 }
Tao Bao485b6372015-06-23 23:23:33 -07001260 char* buffer = reinterpret_cast<char*>(malloc(size+1));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001261 size = 0;
1262 for (i = 0; i < argc; ++i) {
1263 strcpy(buffer+size, args[i]);
1264 size += strlen(args[i]);
1265 free(args[i]);
1266 }
1267 free(args);
1268 buffer[size] = '\0';
Michael Runged4a63422014-10-22 19:48:41 -07001269 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001270 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001271}
1272
Doug Zongkerd0181b82011-10-19 10:51:12 -07001273Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1274 if (argc != 0) {
1275 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1276 }
1277 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1278 return StringValue(strdup("t"));
1279}
1280
Doug Zongker512536a2010-02-17 16:11:44 -08001281Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001282 if (argc < 1) {
1283 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1284 }
1285 char** args = ReadVarArgs(state, argc, argv);
1286 if (args == NULL) {
1287 return NULL;
1288 }
1289
Tao Bao485b6372015-06-23 23:23:33 -07001290 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001291 memcpy(args2, args, sizeof(char*) * argc);
1292 args2[argc] = NULL;
1293
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001294 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001295
1296 pid_t child = fork();
1297 if (child == 0) {
1298 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001299 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001300 _exit(1);
1301 }
1302 int status;
1303 waitpid(child, &status, 0);
1304 if (WIFEXITED(status)) {
1305 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001306 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001307 WEXITSTATUS(status));
1308 }
1309 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001310 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001311 WTERMSIG(status));
1312 }
1313
1314 int i;
1315 for (i = 0; i < argc; ++i) {
1316 free(args[i]);
1317 }
1318 free(args);
1319 free(args2);
1320
1321 char buffer[20];
1322 sprintf(buffer, "%d", status);
1323
Doug Zongker512536a2010-02-17 16:11:44 -08001324 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001325}
1326
Doug Zongker512536a2010-02-17 16:11:44 -08001327// sha1_check(data)
1328// to return the sha1 of the data (given in the format returned by
1329// read_file).
1330//
1331// sha1_check(data, sha1_hex, [sha1_hex, ...])
1332// returns the sha1 of the file if it matches any of the hex
1333// strings passed, or "" if it does not equal any of them.
1334//
1335Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1336 if (argc < 1) {
1337 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1338 }
1339
1340 Value** args = ReadValueVarArgs(state, argc, argv);
1341 if (args == NULL) {
1342 return NULL;
1343 }
1344
1345 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001346 return StringValue(strdup(""));
1347 }
1348 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001349 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001350 FreeValue(args[0]);
1351
1352 if (argc == 1) {
1353 return StringValue(PrintSha1(digest));
1354 }
1355
1356 int i;
Tao Bao485b6372015-06-23 23:23:33 -07001357 uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -08001358 for (i = 1; i < argc; ++i) {
1359 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001360 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001361 name, i);
1362 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1363 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001364 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1365 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001366 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1367 break;
1368 }
1369 FreeValue(args[i]);
1370 }
1371 if (i >= argc) {
1372 // Didn't match any of the hex strings; return false.
1373 return StringValue(strdup(""));
1374 }
1375 // Found a match; free all the remaining arguments and return the
1376 // matched one.
1377 int j;
1378 for (j = i+1; j < argc; ++j) {
1379 FreeValue(args[j]);
1380 }
1381 return args[i];
1382}
1383
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001384// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001385// is actually a FileContents*).
1386Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1387 if (argc != 1) {
1388 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1389 }
1390 char* filename;
1391 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1392
Tao Bao485b6372015-06-23 23:23:33 -07001393 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001394 v->type = VAL_BLOB;
1395
1396 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001397 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001398 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001399 v->size = -1;
1400 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001401 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001402 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001403 }
1404
1405 v->size = fc.size;
1406 v->data = (char*)fc.data;
1407
1408 free(filename);
1409 return v;
1410}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001411
Doug Zongkerc87bab12013-11-25 13:53:25 -08001412// Immediately reboot the device. Recovery is not finished normally,
1413// so if you reboot into recovery it will re-start applying the
1414// current package (because nothing has cleared the copy of the
1415// arguments stored in the BCB).
1416//
1417// The argument is the partition name passed to the android reboot
1418// property. It can be "recovery" to boot from the recovery
1419// partition, or "" (empty string) to boot from the regular boot
1420// partition.
1421Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1422 if (argc != 2) {
1423 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1424 }
1425
1426 char* filename;
1427 char* property;
1428 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1429
1430 char buffer[80];
1431
1432 // zero out the 'command' field of the bootloader message.
1433 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1434 FILE* f = fopen(filename, "r+b");
1435 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1436 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1437 fclose(f);
1438 free(filename);
1439
1440 strcpy(buffer, "reboot,");
1441 if (property != NULL) {
1442 strncat(buffer, property, sizeof(buffer)-10);
1443 }
1444
1445 property_set(ANDROID_RB_PROPERTY, buffer);
1446
1447 sleep(5);
1448 free(property);
1449 ErrorAbort(state, "%s() failed to reboot", name);
1450 return NULL;
1451}
1452
1453// Store a string value somewhere that future invocations of recovery
1454// can access it. This value is called the "stage" and can be used to
1455// drive packages that need to do reboots in the middle of
1456// installation and keep track of where they are in the multi-stage
1457// install.
1458//
1459// The first argument is the block device for the misc partition
1460// ("/misc" in the fstab), which is where this value is stored. The
1461// second argument is the string to store; it should not exceed 31
1462// bytes.
1463Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1464 if (argc != 2) {
1465 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1466 }
1467
1468 char* filename;
1469 char* stagestr;
1470 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1471
1472 // Store this value in the misc partition, immediately after the
1473 // bootloader message that the main recovery uses to save its
1474 // arguments in case of the device restarting midway through
1475 // package installation.
1476 FILE* f = fopen(filename, "r+b");
1477 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1478 int to_write = strlen(stagestr)+1;
1479 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1480 if (to_write > max_size) {
1481 to_write = max_size;
1482 stagestr[max_size-1] = 0;
1483 }
1484 fwrite(stagestr, to_write, 1, f);
1485 fclose(f);
1486
1487 free(stagestr);
1488 return StringValue(filename);
1489}
1490
1491// Return the value most recently saved with SetStageFn. The argument
1492// is the block device for the misc partition.
1493Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001494 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001495 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1496 }
1497
1498 char* filename;
1499 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1500
1501 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1502 FILE* f = fopen(filename, "rb");
1503 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1504 fread(buffer, sizeof(buffer), 1, f);
1505 fclose(f);
1506 buffer[sizeof(buffer)-1] = '\0';
1507
1508 return StringValue(strdup(buffer));
1509}
1510
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001511Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1512 if (argc != 2) {
1513 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1514 }
1515
1516 char* filename;
1517 char* len_str;
1518 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1519
1520 size_t len = strtoull(len_str, NULL, 0);
1521 int fd = open(filename, O_WRONLY, 0644);
1522 int success = wipe_block_device(fd, len);
1523
1524 free(filename);
1525 free(len_str);
1526
1527 close(fd);
1528
1529 return StringValue(strdup(success ? "t" : ""));
1530}
1531
Doug Zongkerc704e062014-05-23 08:40:35 -07001532Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1533 if (argc != 0) {
1534 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1535 }
1536 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1537 fprintf(ui->cmd_pipe, "enable_reboot\n");
1538 return StringValue(strdup("t"));
1539}
1540
Michael Rungeacf47db2014-11-21 00:12:28 -08001541Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1542 if (argc == 0) {
1543 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1544 }
1545
1546 char** args = ReadVarArgs(state, argc, argv);
1547 if (args == NULL) {
1548 return ErrorAbort(state, "%s() could not read args", name);
1549 }
1550
Tao Bao485b6372015-06-23 23:23:33 -07001551 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001552 // Tune2fs expects the program name as its args[0]
1553 args2[0] = strdup(name);
Tao Bao485b6372015-06-23 23:23:33 -07001554 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001555 args2[i + 1] = args[i];
1556 }
1557 int result = tune2fs_main(argc + 1, args2);
Tao Bao485b6372015-06-23 23:23:33 -07001558 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001559 free(args[i]);
1560 }
1561 free(args);
1562
1563 free(args2[0]);
1564 free(args2);
1565 if (result != 0) {
1566 return ErrorAbort(state, "%s() returned error code %d", name, result);
1567 }
1568 return StringValue(strdup("t"));
1569}
1570
Doug Zongker9931f7f2009-06-10 14:11:53 -07001571void RegisterInstallFunctions() {
1572 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001573 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001574 RegisterFunction("unmount", UnmountFn);
1575 RegisterFunction("format", FormatFn);
1576 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001577 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001578 RegisterFunction("delete", DeleteFn);
1579 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001580 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1581 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001582 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001583
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001584 // Usage:
1585 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1586 // Example:
1587 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1588 RegisterFunction("set_metadata", SetMetadataFn);
1589
1590 // Usage:
1591 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1592 // Example:
1593 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1594 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1595
Doug Zongker8edb00c2009-06-11 17:21:44 -07001596 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001597 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001598 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001599
1600 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001601 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1602 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001603
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001604 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001605
Doug Zongker512536a2010-02-17 16:11:44 -08001606 RegisterFunction("read_file", ReadFileFn);
1607 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001608 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001609
Doug Zongkerd0181b82011-10-19 10:51:12 -07001610 RegisterFunction("wipe_cache", WipeCacheFn);
1611
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001612 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001613
1614 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001615
1616 RegisterFunction("reboot_now", RebootNowFn);
1617 RegisterFunction("get_stage", GetStageFn);
1618 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001619
1620 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001621 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001622}