blob: 64a363e98613d61e4d3bf95cd91af6c64a40f4ae [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 Lawrence0a423d92015-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{
Emmanuel Berthier30cc3d72015-04-27 13:50:09 +020060 char filename_val[PROP_VALUE_MAX];
Tom Cherrycce7e932015-05-12 13:54:41 -070061 if (expand_props(filename_val, filename, sizeof(filename_val)) == -1) {
Emmanuel Berthier30cc3d72015-04-27 13:50:09 +020062 ERROR("insmod: cannot expand '%s'\n", filename);
63 return -EINVAL;
64 }
65
Tom Cherrycce7e932015-05-12 13:54:41 -070066 std::string module;
Emmanuel Berthier30cc3d72015-04-27 13:50:09 +020067 if (!read_file(filename_val, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080069 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Elliott Hughesf682b472015-02-06 12:19:48 -080071 // TODO: use finit_module for >= 3.8 kernels.
72 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073}
74
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static int __ifupdown(const char *interface, int up)
76{
77 struct ifreq ifr;
78 int s, ret;
79
80 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
81
82 s = socket(AF_INET, SOCK_DGRAM, 0);
83 if (s < 0)
84 return -1;
85
86 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
87 if (ret < 0) {
88 goto done;
89 }
90
91 if (up)
92 ifr.ifr_flags |= IFF_UP;
93 else
94 ifr.ifr_flags &= ~IFF_UP;
95
96 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080097
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098done:
99 close(s);
100 return ret;
101}
102
103static void service_start_if_not_disabled(struct service *svc)
104{
105 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700106 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700107 } else {
108 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 }
110}
111
112int do_class_start(int nargs, char **args)
113{
114 /* Starting a class does not start services
115 * which are explicitly disabled. They must
116 * be started individually.
117 */
118 service_for_each_class(args[1], service_start_if_not_disabled);
119 return 0;
120}
121
122int do_class_stop(int nargs, char **args)
123{
124 service_for_each_class(args[1], service_stop);
125 return 0;
126}
127
Ken Sumrall752923c2010-12-03 16:33:31 -0800128int do_class_reset(int nargs, char **args)
129{
130 service_for_each_class(args[1], service_reset);
131 return 0;
132}
133
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134int do_domainname(int nargs, char **args)
135{
136 return write_file("/proc/sys/kernel/domainname", args[1]);
137}
138
JP Abgrall3beec7e2014-05-02 21:14:29 -0700139int do_enable(int nargs, char **args)
140{
141 struct service *svc;
142 svc = service_find_by_name(args[1]);
143 if (svc) {
144 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
145 if (svc->flags & SVC_DISABLED_START) {
146 service_start(svc, NULL);
147 }
148 } else {
149 return -1;
150 }
151 return 0;
152}
153
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800154int do_exec(int nargs, char** args) {
155 service* svc = make_exec_oneshot_service(nargs, args);
156 if (svc == NULL) {
157 return -1;
158 }
159 service_start(svc, NULL);
160 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161}
162
163int do_export(int nargs, char **args)
164{
James Morrissey381341f2014-05-16 11:36:36 +0100165 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166}
167
168int do_hostname(int nargs, char **args)
169{
170 return write_file("/proc/sys/kernel/hostname", args[1]);
171}
172
173int do_ifup(int nargs, char **args)
174{
175 return __ifupdown(args[1], 1);
176}
177
The Android Open Source Project35237d12008-12-17 18:08:08 -0800178
179static int do_insmod_inner(int nargs, char **args, int opt_len)
180{
181 char options[opt_len + 1];
182 int i;
183
184 options[0] = '\0';
185 if (nargs > 2) {
186 strcpy(options, args[2]);
187 for (i = 3; i < nargs; ++i) {
188 strcat(options, " ");
189 strcat(options, args[i]);
190 }
191 }
192
193 return insmod(args[1], options);
194}
195
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196int do_insmod(int nargs, char **args)
197{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800198 int i;
199 int size = 0;
200
201 if (nargs > 2) {
202 for (i = 2; i < nargs; ++i)
203 size += strlen(args[i]) + 1;
204 }
205
206 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207}
208
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209int do_mkdir(int nargs, char **args)
210{
211 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700212 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213
214 /* mkdir <path> [mode] [owner] [group] */
215
216 if (nargs >= 3) {
217 mode = strtoul(args[2], 0, 8);
218 }
219
Stephen Smalleye096e362012-06-11 13:37:39 -0400220 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700221 /* chmod in case the directory already exists */
222 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800223 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700224 }
225 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 return -errno;
227 }
228
229 if (nargs >= 4) {
230 uid_t uid = decode_uid(args[3]);
231 gid_t gid = -1;
232
233 if (nargs == 5) {
234 gid = decode_uid(args[4]);
235 }
236
Nick Kralevichbc609542015-01-31 21:39:46 -0800237 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238 return -errno;
239 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700240
241 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
242 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800243 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700244 if (ret == -1) {
245 return -errno;
246 }
247 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248 }
249
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000250 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251}
252
253static struct {
254 const char *name;
255 unsigned flag;
256} mount_flags[] = {
257 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200258 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 { "nosuid", MS_NOSUID },
260 { "nodev", MS_NODEV },
261 { "nodiratime", MS_NODIRATIME },
262 { "ro", MS_RDONLY },
263 { "rw", 0 },
264 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700265 { "bind", MS_BIND },
266 { "rec", MS_REC },
267 { "unbindable", MS_UNBINDABLE },
268 { "private", MS_PRIVATE },
269 { "slave", MS_SLAVE },
270 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271 { "defaults", 0 },
272 { 0, 0 },
273};
274
Ken Sumrall752923c2010-12-03 16:33:31 -0800275#define DATA_MNT_POINT "/data"
276
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277/* mount <type> <device> <path> <flags ...> <options> */
278int do_mount(int nargs, char **args)
279{
280 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000281 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 char *options = NULL;
283 unsigned flags = 0;
284 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700285 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286
287 for (n = 4; n < nargs; n++) {
288 for (i = 0; mount_flags[i].name; i++) {
289 if (!strcmp(args[n], mount_flags[i].name)) {
290 flags |= mount_flags[i].flag;
291 break;
292 }
293 }
294
Colin Crosscd0f1732010-04-19 17:10:24 -0700295 if (!mount_flags[i].name) {
296 if (!strcmp(args[n], "wait"))
297 wait = 1;
298 /* if our last argument isn't a flag, wolf it up as an option string */
299 else if (n + 1 == nargs)
300 options = args[n];
301 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 }
303
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000304 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000306 target = args[3];
307
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 if (!strncmp(source, "mtd@", 4)) {
309 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000310 if (n < 0) {
311 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000313
Yabin Cuie2d63af2015-02-17 19:27:51 -0800314 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000315
Colin Crosscd0f1732010-04-19 17:10:24 -0700316 if (wait)
317 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000318 if (mount(tmp, target, system, flags, options) < 0) {
319 return -1;
320 }
321
Ken Sumralldd4d7862011-02-17 18:09:47 -0800322 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000323 } else if (!strncmp(source, "loop@", 5)) {
324 int mode, loop, fd;
325 struct loop_info info;
326
327 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800328 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000329 if (fd < 0) {
330 return -1;
331 }
332
333 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800334 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800335 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000336 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100337 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000338 return -1;
339 }
340
341 /* if it is a blank loop device */
342 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
343 /* if it becomes our loop device */
344 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
345 close(fd);
346
347 if (mount(tmp, target, system, flags, options) < 0) {
348 ioctl(loop, LOOP_CLR_FD, 0);
349 close(loop);
350 return -1;
351 }
352
353 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800354 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000355 }
356 }
357
358 close(loop);
359 }
360
361 close(fd);
362 ERROR("out of loopback devices");
363 return -1;
364 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700365 if (wait)
366 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700368 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369 }
370
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800372
373exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800374 return 0;
375
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376}
377
JP Abgrallcee20682014-07-02 14:26:54 -0700378static int wipe_data_via_recovery()
379{
380 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800381 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700382 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700383 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
384 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700385 close(fd);
386 } else {
387 ERROR("could not open /cache/recovery/command\n");
388 return -1;
389 }
390 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
391 while (1) { pause(); } // never reached
392}
393
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000394/*
JP Abgrallcee20682014-07-02 14:26:54 -0700395 * This function might request a reboot, in which case it will
396 * not return.
397 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700398int do_mount_all(int nargs, char **args)
399{
400 pid_t pid;
401 int ret = -1;
402 int child_ret = -1;
403 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800404 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700405
406 if (nargs != 2) {
407 return -1;
408 }
409
410 /*
411 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
412 * do the call in the child to provide protection to the main init
413 * process if anything goes wrong (crash or memory leak), and wait for
414 * the child to finish in the parent.
415 */
416 pid = fork();
417 if (pid > 0) {
418 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700419 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
420 if (wp_ret < 0) {
421 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700422 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700423 }
424
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700425 if (WIFEXITED(status)) {
426 ret = WEXITSTATUS(status);
427 } else {
428 ret = -1;
429 }
430 } else if (pid == 0) {
431 /* child, call fs_mgr_mount_all() */
432 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800433 fstab = fs_mgr_read_fstab(args[1]);
434 child_ret = fs_mgr_mount_all(fstab);
435 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700436 if (child_ret == -1) {
437 ERROR("fs_mgr_mount_all returned an error\n");
438 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700439 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700440 } else {
441 /* fork failed, return an error */
442 return -1;
443 }
444
JP Abgrallf22b7452014-07-02 13:16:04 -0700445 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800446 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700447 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448 property_set("ro.crypto.state", "encrypted");
Paul Lawrence0a423d92015-04-28 22:07:10 +0000449 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800450 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700451 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700452 property_set("ro.crypto.state", "unencrypted");
453 /* If fs_mgr determined this is an unencrypted device, then trigger
454 * that action.
455 */
456 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700457 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
458 /* Setup a wipe via recovery, and reboot into recovery */
459 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
460 ret = wipe_data_via_recovery();
461 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000462 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000463 if (e4crypt_install_keyring()) {
464 return -1;
465 }
466 property_set("ro.crypto.state", "encrypted");
Paul Lawrence0a423d92015-04-28 22:07:10 +0000467 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000468
469 // Although encrypted, we have device key, so we do not need to
470 // do anything different from the nonencrypted case.
471 action_for_each_trigger("nonencrypted", action_add_queue_tail);
472 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
473 if (e4crypt_install_keyring()) {
474 return -1;
475 }
476 property_set("ro.crypto.state", "encrypted");
Paul Lawrence0a423d92015-04-28 22:07:10 +0000477 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000478 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700479 } else if (ret > 0) {
480 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700481 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700482 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700483
484 return ret;
485}
486
Ken Sumralla76baaa2013-07-09 18:42:09 -0700487int do_swapon_all(int nargs, char **args)
488{
489 struct fstab *fstab;
490 int ret;
491
492 fstab = fs_mgr_read_fstab(args[1]);
493 ret = fs_mgr_swapon_all(fstab);
494 fs_mgr_free_fstab(fstab);
495
496 return ret;
497}
498
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700499int do_setprop(int nargs, char **args)
500{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700501 const char *name = args[1];
502 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800503 char prop_val[PROP_VALUE_MAX];
504 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700505
Dima Zavin84bf9af2011-12-20 13:44:41 -0800506 ret = expand_props(prop_val, value, sizeof(prop_val));
507 if (ret) {
508 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
509 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700510 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800511 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700512 return 0;
513}
514
515int do_setrlimit(int nargs, char **args)
516{
517 struct rlimit limit;
518 int resource;
519 resource = atoi(args[1]);
520 limit.rlim_cur = atoi(args[2]);
521 limit.rlim_max = atoi(args[3]);
522 return setrlimit(resource, &limit);
523}
524
525int do_start(int nargs, char **args)
526{
527 struct service *svc;
528 svc = service_find_by_name(args[1]);
529 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700530 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700531 }
532 return 0;
533}
534
535int do_stop(int nargs, char **args)
536{
537 struct service *svc;
538 svc = service_find_by_name(args[1]);
539 if (svc) {
540 service_stop(svc);
541 }
542 return 0;
543}
544
545int do_restart(int nargs, char **args)
546{
547 struct service *svc;
548 svc = service_find_by_name(args[1]);
549 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500550 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700551 }
552 return 0;
553}
554
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700555int do_powerctl(int nargs, char **args)
556{
557 char command[PROP_VALUE_MAX];
558 int res;
559 int len = 0;
560 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800561 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700562
563 res = expand_props(command, args[1], sizeof(command));
564 if (res) {
565 ERROR("powerctl: cannot expand '%s'\n", args[1]);
566 return -EINVAL;
567 }
568
569 if (strncmp(command, "shutdown", 8) == 0) {
570 cmd = ANDROID_RB_POWEROFF;
571 len = 8;
572 } else if (strncmp(command, "reboot", 6) == 0) {
573 cmd = ANDROID_RB_RESTART2;
574 len = 6;
575 } else {
576 ERROR("powerctl: unrecognized command '%s'\n", command);
577 return -EINVAL;
578 }
579
580 if (command[len] == ',') {
581 reboot_target = &command[len + 1];
582 } else if (command[len] == '\0') {
583 reboot_target = "";
584 } else {
585 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
586 return -EINVAL;
587 }
588
589 return android_reboot(cmd, 0, reboot_target);
590}
591
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592int do_trigger(int nargs, char **args)
593{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000594 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 return 0;
596}
597
598int do_symlink(int nargs, char **args)
599{
600 return symlink(args[1], args[2]);
601}
602
Ken Sumrall203bad52011-01-18 17:37:41 -0800603int do_rm(int nargs, char **args)
604{
605 return unlink(args[1]);
606}
607
608int do_rmdir(int nargs, char **args)
609{
610 return rmdir(args[1]);
611}
612
The Android Open Source Project35237d12008-12-17 18:08:08 -0800613int do_sysclktz(int nargs, char **args)
614{
615 struct timezone tz;
616
617 if (nargs != 2)
618 return -1;
619
620 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800621 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800622 if (settimeofday(NULL, &tz))
623 return -1;
624 return 0;
625}
626
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000627int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700628 int mode = -1;
629 int rc = fs_mgr_load_verity_state(&mode);
630 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
631 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000632 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700633 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000634}
635
Sami Tolvanen45474232015-03-30 11:38:38 +0100636static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
637 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
638 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000639}
640
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700641int do_verity_update_state(int nargs, char** args) {
642 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000643}
644
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700645int do_write(int nargs, char **args)
646{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700647 const char *path = args[1];
648 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700649
Johan Redestig7e952f42014-12-05 00:36:41 +0100650 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800651 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800652 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
653 return -EINVAL;
654 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800655 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656}
657
San Mehat7c44fe52009-08-26 16:39:25 -0700658int do_copy(int nargs, char **args)
659{
660 char *buffer = NULL;
661 int rc = 0;
662 int fd1 = -1, fd2 = -1;
663 struct stat info;
664 int brtw, brtr;
665 char *p;
666
667 if (nargs != 3)
668 return -1;
669
Elliott Hughes24627902015-02-04 10:25:09 -0800670 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700671 return -1;
672
Nick Kralevich45a884f2015-02-02 14:37:22 -0800673 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700674 goto out_err;
675
Nick Kralevich45a884f2015-02-02 14:37:22 -0800676 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700677 goto out_err;
678
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800679 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700680 goto out_err;
681
682 p = buffer;
683 brtr = info.st_size;
684 while(brtr) {
685 rc = read(fd1, p, brtr);
686 if (rc < 0)
687 goto out_err;
688 if (rc == 0)
689 break;
690 p += rc;
691 brtr -= rc;
692 }
693
694 p = buffer;
695 brtw = info.st_size;
696 while(brtw) {
697 rc = write(fd2, p, brtw);
698 if (rc < 0)
699 goto out_err;
700 if (rc == 0)
701 break;
702 p += rc;
703 brtw -= rc;
704 }
705
706 rc = 0;
707 goto out;
708out_err:
709 rc = -1;
710out:
711 if (buffer)
712 free(buffer);
713 if (fd1 >= 0)
714 close(fd1);
715 if (fd2 >= 0)
716 close(fd2);
717 return rc;
718}
719
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720int do_chown(int nargs, char **args) {
721 /* GID is optional. */
722 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800723 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700724 return -errno;
725 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800726 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727 return -errno;
728 } else {
729 return -1;
730 }
731 return 0;
732}
733
734static mode_t get_mode(const char *s) {
735 mode_t mode = 0;
736 while (*s) {
737 if (*s >= '0' && *s <= '7') {
738 mode = (mode<<3) | (*s-'0');
739 } else {
740 return -1;
741 }
742 s++;
743 }
744 return mode;
745}
746
747int do_chmod(int nargs, char **args) {
748 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800749 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750 return -errno;
751 }
752 return 0;
753}
754
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500755int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500756 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400757 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500758
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500759 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400760 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400761 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500762 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400763 return ret;
764}
765
766int do_restorecon_recursive(int nargs, char **args) {
767 int i;
768 int ret = 0;
769
770 for (i = 1; i < nargs; i++) {
771 if (restorecon_recursive(args[i]) < 0)
772 ret = -errno;
773 }
774 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500775}
776
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700778 int log_level;
779 char log_level_str[PROP_VALUE_MAX] = "";
780 if (nargs != 2) {
781 ERROR("loglevel: missing argument\n");
782 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700784
785 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
786 ERROR("loglevel: cannot expand '%s'\n", args[1]);
787 return -EINVAL;
788 }
789 log_level = atoi(log_level_str);
790 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
791 ERROR("loglevel: invalid log level'%d'\n", log_level);
792 return -EINVAL;
793 }
794 klog_set_level(log_level);
795 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796}
797
Ken Sumrallc5c51032011-03-08 17:01:29 -0800798int do_load_persist_props(int nargs, char **args) {
799 if (nargs == 1) {
800 load_persist_props();
801 return 0;
802 }
803 return -1;
804}
805
Paul Lawrence948410a2015-07-01 14:40:56 -0700806int do_load_system_props(int nargs, char **args) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700807 if (nargs == 1) {
Paul Lawrence948410a2015-07-01 14:40:56 -0700808 load_system_props();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700809 return 0;
810 }
811 return -1;
812}
813
Colin Crosscd0f1732010-04-19 17:10:24 -0700814int do_wait(int nargs, char **args)
815{
816 if (nargs == 2) {
817 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800818 } else if (nargs == 3) {
819 return wait_for_file(args[1], atoi(args[2]));
820 } else
821 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700822}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000823
Paul Lawrence0a423d92015-04-28 22:07:10 +0000824/*
825 * Callback to make a directory from the ext4 code
826 */
827static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000828{
Paul Lawrence0a423d92015-04-28 22:07:10 +0000829 if (make_dir(dir, 0700) && errno != EEXIST) {
830 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000831 }
832
Paul Lawrence0a423d92015-04-28 22:07:10 +0000833 return 0;
834}
835
836int do_installkey(int nargs, char **args)
837{
838 if (nargs != 2) {
839 return -1;
840 }
841
842 char prop_value[PROP_VALUE_MAX] = {0};
843 property_get("ro.crypto.type", prop_value);
844 if (strcmp(prop_value, "file")) {
845 return 0;
846 }
847
848 return e4crypt_create_device_key(args[1],
849 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000850}