blob: 88d61650948fdecdeb42e175b692bb84b0e2b7fa [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070017#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070018#include <fcntl.h>
19#include <net/if.h>
20#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070021#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <string.h>
23#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <sys/mount.h>
25#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080026#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070027#include <sys/types.h>
28#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070029#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070030#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000031#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000032#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070033
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034#include <selinux/selinux.h>
35#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <fs_mgr.h>
38#include <base/stringprintf.h>
39#include <cutils/partition_utils.h>
40#include <cutils/android_reboot.h>
41#include <private/android_filesystem_config.h>
42
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include "init.h"
44#include "keywords.h"
45#include "property_service.h"
46#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070047#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070048#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070049#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050
Nick Kralevichbc609542015-01-31 21:39:46 -080051#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
52
James Morrissey381341f2014-05-16 11:36:36 +010053int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Elliott Hughesf3cf4382015-02-03 17:12:07 -080055// System call provided by bionic but not in any header file.
56extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
The Android Open Source Project35237d12008-12-17 18:08:08 -080058static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059{
Elliott Hughesf682b472015-02-06 12:19:48 -080060 std::string module;
Emmanuel Berthierac412302015-04-27 13:50:09 +020061 char filename_val[PROP_VALUE_MAX];
62 int ret;
63
64 ret = expand_props(filename_val, filename, sizeof(filename_val));
65 if (ret) {
66 ERROR("insmod: cannot expand '%s'\n", filename);
67 return -EINVAL;
68 }
69
70 if (!read_file(filename_val, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080072 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073
Elliott Hughesf682b472015-02-06 12:19:48 -080074 // TODO: use finit_module for >= 3.8 kernels.
75 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076}
77
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078static int __ifupdown(const char *interface, int up)
79{
80 struct ifreq ifr;
81 int s, ret;
82
83 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
84
85 s = socket(AF_INET, SOCK_DGRAM, 0);
86 if (s < 0)
87 return -1;
88
89 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
90 if (ret < 0) {
91 goto done;
92 }
93
94 if (up)
95 ifr.ifr_flags |= IFF_UP;
96 else
97 ifr.ifr_flags &= ~IFF_UP;
98
99 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800100
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101done:
102 close(s);
103 return ret;
104}
105
106static void service_start_if_not_disabled(struct service *svc)
107{
108 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700109 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700110 } else {
111 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 }
113}
114
115int do_class_start(int nargs, char **args)
116{
117 /* Starting a class does not start services
118 * which are explicitly disabled. They must
119 * be started individually.
120 */
121 service_for_each_class(args[1], service_start_if_not_disabled);
122 return 0;
123}
124
125int do_class_stop(int nargs, char **args)
126{
127 service_for_each_class(args[1], service_stop);
128 return 0;
129}
130
Ken Sumrall752923c2010-12-03 16:33:31 -0800131int do_class_reset(int nargs, char **args)
132{
133 service_for_each_class(args[1], service_reset);
134 return 0;
135}
136
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137int do_domainname(int nargs, char **args)
138{
139 return write_file("/proc/sys/kernel/domainname", args[1]);
140}
141
JP Abgrall3beec7e2014-05-02 21:14:29 -0700142int do_enable(int nargs, char **args)
143{
144 struct service *svc;
145 svc = service_find_by_name(args[1]);
146 if (svc) {
147 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
148 if (svc->flags & SVC_DISABLED_START) {
149 service_start(svc, NULL);
150 }
151 } else {
152 return -1;
153 }
154 return 0;
155}
156
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800157int do_exec(int nargs, char** args) {
158 service* svc = make_exec_oneshot_service(nargs, args);
159 if (svc == NULL) {
160 return -1;
161 }
162 service_start(svc, NULL);
163 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164}
165
166int do_export(int nargs, char **args)
167{
James Morrissey381341f2014-05-16 11:36:36 +0100168 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169}
170
171int do_hostname(int nargs, char **args)
172{
173 return write_file("/proc/sys/kernel/hostname", args[1]);
174}
175
176int do_ifup(int nargs, char **args)
177{
178 return __ifupdown(args[1], 1);
179}
180
The Android Open Source Project35237d12008-12-17 18:08:08 -0800181
182static int do_insmod_inner(int nargs, char **args, int opt_len)
183{
184 char options[opt_len + 1];
185 int i;
186
187 options[0] = '\0';
188 if (nargs > 2) {
189 strcpy(options, args[2]);
190 for (i = 3; i < nargs; ++i) {
191 strcat(options, " ");
192 strcat(options, args[i]);
193 }
194 }
195
196 return insmod(args[1], options);
197}
198
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199int do_insmod(int nargs, char **args)
200{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800201 int i;
202 int size = 0;
203
204 if (nargs > 2) {
205 for (i = 2; i < nargs; ++i)
206 size += strlen(args[i]) + 1;
207 }
208
209 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210}
211
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212int do_mkdir(int nargs, char **args)
213{
214 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700215 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216
217 /* mkdir <path> [mode] [owner] [group] */
218
219 if (nargs >= 3) {
220 mode = strtoul(args[2], 0, 8);
221 }
222
Stephen Smalleye096e362012-06-11 13:37:39 -0400223 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700224 /* chmod in case the directory already exists */
225 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800226 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700227 }
228 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229 return -errno;
230 }
231
232 if (nargs >= 4) {
233 uid_t uid = decode_uid(args[3]);
234 gid_t gid = -1;
235
236 if (nargs == 5) {
237 gid = decode_uid(args[4]);
238 }
239
Nick Kralevichbc609542015-01-31 21:39:46 -0800240 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241 return -errno;
242 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700243
244 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
245 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800246 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700247 if (ret == -1) {
248 return -errno;
249 }
250 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251 }
252
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000253 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254}
255
256static struct {
257 const char *name;
258 unsigned flag;
259} mount_flags[] = {
260 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200261 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262 { "nosuid", MS_NOSUID },
263 { "nodev", MS_NODEV },
264 { "nodiratime", MS_NODIRATIME },
265 { "ro", MS_RDONLY },
266 { "rw", 0 },
267 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700268 { "bind", MS_BIND },
269 { "rec", MS_REC },
270 { "unbindable", MS_UNBINDABLE },
271 { "private", MS_PRIVATE },
272 { "slave", MS_SLAVE },
273 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274 { "defaults", 0 },
275 { 0, 0 },
276};
277
Ken Sumrall752923c2010-12-03 16:33:31 -0800278#define DATA_MNT_POINT "/data"
279
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280/* mount <type> <device> <path> <flags ...> <options> */
281int do_mount(int nargs, char **args)
282{
283 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000284 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 char *options = NULL;
286 unsigned flags = 0;
287 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700288 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289
290 for (n = 4; n < nargs; n++) {
291 for (i = 0; mount_flags[i].name; i++) {
292 if (!strcmp(args[n], mount_flags[i].name)) {
293 flags |= mount_flags[i].flag;
294 break;
295 }
296 }
297
Colin Crosscd0f1732010-04-19 17:10:24 -0700298 if (!mount_flags[i].name) {
299 if (!strcmp(args[n], "wait"))
300 wait = 1;
301 /* if our last argument isn't a flag, wolf it up as an option string */
302 else if (n + 1 == nargs)
303 options = args[n];
304 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 }
306
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000307 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000309 target = args[3];
310
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 if (!strncmp(source, "mtd@", 4)) {
312 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000313 if (n < 0) {
314 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000316
Yabin Cuie2d63af2015-02-17 19:27:51 -0800317 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000318
Colin Crosscd0f1732010-04-19 17:10:24 -0700319 if (wait)
320 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000321 if (mount(tmp, target, system, flags, options) < 0) {
322 return -1;
323 }
324
Ken Sumralldd4d7862011-02-17 18:09:47 -0800325 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000326 } else if (!strncmp(source, "loop@", 5)) {
327 int mode, loop, fd;
328 struct loop_info info;
329
330 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800331 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000332 if (fd < 0) {
333 return -1;
334 }
335
336 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800337 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800338 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000339 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100340 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000341 return -1;
342 }
343
344 /* if it is a blank loop device */
345 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
346 /* if it becomes our loop device */
347 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
348 close(fd);
349
350 if (mount(tmp, target, system, flags, options) < 0) {
351 ioctl(loop, LOOP_CLR_FD, 0);
352 close(loop);
353 return -1;
354 }
355
356 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800357 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000358 }
359 }
360
361 close(loop);
362 }
363
364 close(fd);
365 ERROR("out of loopback devices");
366 return -1;
367 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700368 if (wait)
369 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700371 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000372 }
373
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800375
376exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800377 return 0;
378
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379}
380
JP Abgrallcee20682014-07-02 14:26:54 -0700381static int wipe_data_via_recovery()
382{
383 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800384 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700385 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700386 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
387 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700388 close(fd);
389 } else {
390 ERROR("could not open /cache/recovery/command\n");
391 return -1;
392 }
393 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
394 while (1) { pause(); } // never reached
395}
396
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000397/*
JP Abgrallcee20682014-07-02 14:26:54 -0700398 * This function might request a reboot, in which case it will
399 * not return.
400 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700401int do_mount_all(int nargs, char **args)
402{
403 pid_t pid;
404 int ret = -1;
405 int child_ret = -1;
406 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800407 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700408
409 if (nargs != 2) {
410 return -1;
411 }
412
413 /*
414 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
415 * do the call in the child to provide protection to the main init
416 * process if anything goes wrong (crash or memory leak), and wait for
417 * the child to finish in the parent.
418 */
419 pid = fork();
420 if (pid > 0) {
421 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700422 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
423 if (wp_ret < 0) {
424 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700425 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700426 }
427
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700428 if (WIFEXITED(status)) {
429 ret = WEXITSTATUS(status);
430 } else {
431 ret = -1;
432 }
433 } else if (pid == 0) {
434 /* child, call fs_mgr_mount_all() */
435 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800436 fstab = fs_mgr_read_fstab(args[1]);
437 child_ret = fs_mgr_mount_all(fstab);
438 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700439 if (child_ret == -1) {
440 ERROR("fs_mgr_mount_all returned an error\n");
441 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700442 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700443 } else {
444 /* fork failed, return an error */
445 return -1;
446 }
447
JP Abgrallf22b7452014-07-02 13:16:04 -0700448 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800449 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700450 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700451 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000452 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800453 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700454 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700455 property_set("ro.crypto.state", "unencrypted");
456 /* If fs_mgr determined this is an unencrypted device, then trigger
457 * that action.
458 */
459 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700460 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
461 /* Setup a wipe via recovery, and reboot into recovery */
462 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
463 ret = wipe_data_via_recovery();
464 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000465 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000466 if (e4crypt_install_keyring()) {
467 return -1;
468 }
469 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000470 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000471
472 // Although encrypted, we have device key, so we do not need to
473 // do anything different from the nonencrypted case.
474 action_for_each_trigger("nonencrypted", action_add_queue_tail);
475 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
476 if (e4crypt_install_keyring()) {
477 return -1;
478 }
479 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000480 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000481 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700482 } else if (ret > 0) {
483 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700484 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700485 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486
487 return ret;
488}
489
Ken Sumralla76baaa2013-07-09 18:42:09 -0700490int do_swapon_all(int nargs, char **args)
491{
492 struct fstab *fstab;
493 int ret;
494
495 fstab = fs_mgr_read_fstab(args[1]);
496 ret = fs_mgr_swapon_all(fstab);
497 fs_mgr_free_fstab(fstab);
498
499 return ret;
500}
501
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700502int do_setprop(int nargs, char **args)
503{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700504 const char *name = args[1];
505 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800506 char prop_val[PROP_VALUE_MAX];
507 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700508
Dima Zavin84bf9af2011-12-20 13:44:41 -0800509 ret = expand_props(prop_val, value, sizeof(prop_val));
510 if (ret) {
511 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
512 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700513 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800514 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700515 return 0;
516}
517
518int do_setrlimit(int nargs, char **args)
519{
520 struct rlimit limit;
521 int resource;
522 resource = atoi(args[1]);
523 limit.rlim_cur = atoi(args[2]);
524 limit.rlim_max = atoi(args[3]);
525 return setrlimit(resource, &limit);
526}
527
528int do_start(int nargs, char **args)
529{
530 struct service *svc;
531 svc = service_find_by_name(args[1]);
532 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700533 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700534 }
535 return 0;
536}
537
538int do_stop(int nargs, char **args)
539{
540 struct service *svc;
541 svc = service_find_by_name(args[1]);
542 if (svc) {
543 service_stop(svc);
544 }
545 return 0;
546}
547
548int do_restart(int nargs, char **args)
549{
550 struct service *svc;
551 svc = service_find_by_name(args[1]);
552 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500553 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554 }
555 return 0;
556}
557
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700558int do_powerctl(int nargs, char **args)
559{
560 char command[PROP_VALUE_MAX];
561 int res;
562 int len = 0;
563 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800564 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700565
566 res = expand_props(command, args[1], sizeof(command));
567 if (res) {
568 ERROR("powerctl: cannot expand '%s'\n", args[1]);
569 return -EINVAL;
570 }
571
572 if (strncmp(command, "shutdown", 8) == 0) {
573 cmd = ANDROID_RB_POWEROFF;
574 len = 8;
575 } else if (strncmp(command, "reboot", 6) == 0) {
576 cmd = ANDROID_RB_RESTART2;
577 len = 6;
578 } else {
579 ERROR("powerctl: unrecognized command '%s'\n", command);
580 return -EINVAL;
581 }
582
583 if (command[len] == ',') {
584 reboot_target = &command[len + 1];
585 } else if (command[len] == '\0') {
586 reboot_target = "";
587 } else {
588 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
589 return -EINVAL;
590 }
591
592 return android_reboot(cmd, 0, reboot_target);
593}
594
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595int do_trigger(int nargs, char **args)
596{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000597 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 return 0;
599}
600
601int do_symlink(int nargs, char **args)
602{
603 return symlink(args[1], args[2]);
604}
605
Ken Sumrall203bad52011-01-18 17:37:41 -0800606int do_rm(int nargs, char **args)
607{
608 return unlink(args[1]);
609}
610
611int do_rmdir(int nargs, char **args)
612{
613 return rmdir(args[1]);
614}
615
The Android Open Source Project35237d12008-12-17 18:08:08 -0800616int do_sysclktz(int nargs, char **args)
617{
618 struct timezone tz;
619
620 if (nargs != 2)
621 return -1;
622
623 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800624 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800625 if (settimeofday(NULL, &tz))
626 return -1;
627 return 0;
628}
629
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000630int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700631 int mode = -1;
632 int rc = fs_mgr_load_verity_state(&mode);
633 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
634 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000635 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700636 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000637}
638
Sami Tolvanen45474232015-03-30 11:38:38 +0100639static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
640 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
641 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000642}
643
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700644int do_verity_update_state(int nargs, char** args) {
645 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000646}
647
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700648int do_write(int nargs, char **args)
649{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700650 const char *path = args[1];
651 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700652
Johan Redestig7e952f42014-12-05 00:36:41 +0100653 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800654 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800655 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
656 return -EINVAL;
657 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800658 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700659}
660
San Mehat7c44fe52009-08-26 16:39:25 -0700661int do_copy(int nargs, char **args)
662{
663 char *buffer = NULL;
664 int rc = 0;
665 int fd1 = -1, fd2 = -1;
666 struct stat info;
667 int brtw, brtr;
668 char *p;
669
670 if (nargs != 3)
671 return -1;
672
Elliott Hughes24627902015-02-04 10:25:09 -0800673 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700674 return -1;
675
Nick Kralevich45a884f2015-02-02 14:37:22 -0800676 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700677 goto out_err;
678
Nick Kralevich45a884f2015-02-02 14:37:22 -0800679 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700680 goto out_err;
681
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800682 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700683 goto out_err;
684
685 p = buffer;
686 brtr = info.st_size;
687 while(brtr) {
688 rc = read(fd1, p, brtr);
689 if (rc < 0)
690 goto out_err;
691 if (rc == 0)
692 break;
693 p += rc;
694 brtr -= rc;
695 }
696
697 p = buffer;
698 brtw = info.st_size;
699 while(brtw) {
700 rc = write(fd2, p, brtw);
701 if (rc < 0)
702 goto out_err;
703 if (rc == 0)
704 break;
705 p += rc;
706 brtw -= rc;
707 }
708
709 rc = 0;
710 goto out;
711out_err:
712 rc = -1;
713out:
714 if (buffer)
715 free(buffer);
716 if (fd1 >= 0)
717 close(fd1);
718 if (fd2 >= 0)
719 close(fd2);
720 return rc;
721}
722
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723int do_chown(int nargs, char **args) {
724 /* GID is optional. */
725 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800726 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727 return -errno;
728 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800729 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730 return -errno;
731 } else {
732 return -1;
733 }
734 return 0;
735}
736
737static mode_t get_mode(const char *s) {
738 mode_t mode = 0;
739 while (*s) {
740 if (*s >= '0' && *s <= '7') {
741 mode = (mode<<3) | (*s-'0');
742 } else {
743 return -1;
744 }
745 s++;
746 }
747 return mode;
748}
749
750int do_chmod(int nargs, char **args) {
751 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800752 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753 return -errno;
754 }
755 return 0;
756}
757
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500758int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500759 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400760 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500762 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400763 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400764 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500765 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400766 return ret;
767}
768
769int do_restorecon_recursive(int nargs, char **args) {
770 int i;
771 int ret = 0;
772
773 for (i = 1; i < nargs; i++) {
774 if (restorecon_recursive(args[i]) < 0)
775 ret = -errno;
776 }
777 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500778}
779
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700781 int log_level;
782 char log_level_str[PROP_VALUE_MAX] = "";
783 if (nargs != 2) {
784 ERROR("loglevel: missing argument\n");
785 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700787
788 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
789 ERROR("loglevel: cannot expand '%s'\n", args[1]);
790 return -EINVAL;
791 }
792 log_level = atoi(log_level_str);
793 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
794 ERROR("loglevel: invalid log level'%d'\n", log_level);
795 return -EINVAL;
796 }
797 klog_set_level(log_level);
798 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799}
800
Ken Sumrallc5c51032011-03-08 17:01:29 -0800801int do_load_persist_props(int nargs, char **args) {
802 if (nargs == 1) {
803 load_persist_props();
804 return 0;
805 }
806 return -1;
807}
808
Riley Andrewse4b7b292014-06-16 15:06:21 -0700809int do_load_all_props(int nargs, char **args) {
810 if (nargs == 1) {
811 load_all_props();
812 return 0;
813 }
814 return -1;
815}
816
Colin Crosscd0f1732010-04-19 17:10:24 -0700817int do_wait(int nargs, char **args)
818{
819 if (nargs == 2) {
820 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800821 } else if (nargs == 3) {
822 return wait_for_file(args[1], atoi(args[2]));
823 } else
824 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700825}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000826
Paul Lawrence806d10b2015-04-28 22:07:10 +0000827/*
828 * Callback to make a directory from the ext4 code
829 */
830static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000831{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000832 if (make_dir(dir, 0700) && errno != EEXIST) {
833 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000834 }
835
Paul Lawrence806d10b2015-04-28 22:07:10 +0000836 return 0;
837}
838
839int do_installkey(int nargs, char **args)
840{
841 if (nargs != 2) {
842 return -1;
843 }
844
845 char prop_value[PROP_VALUE_MAX] = {0};
846 property_get("ro.crypto.type", prop_value);
847 if (strcmp(prop_value, "file")) {
848 return 0;
849 }
850
851 return e4crypt_create_device_key(args[1],
852 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000853}