blob: ca31c50e1ec18c0928afb85e8a81d08156471c75 [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{
Emmanuel Berthierac412302015-04-27 13:50:09 +020060 char filename_val[PROP_VALUE_MAX];
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070061 if (expand_props(filename_val, filename, sizeof(filename_val)) == -1) {
Emmanuel Berthierac412302015-04-27 13:50:09 +020062 ERROR("insmod: cannot expand '%s'\n", filename);
63 return -EINVAL;
64 }
65
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070066 std::string module;
Emmanuel Berthierac412302015-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 Lawrence806d10b2015-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 Lawrence806d10b2015-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 Lawrence806d10b2015-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{
Mark Salyzyndd0e3162015-05-27 10:46:54 -0700594 char prop_val[PROP_VALUE_MAX];
595 int res = expand_props(prop_val, args[1], sizeof(prop_val));
596 if (res) {
597 ERROR("trigger: cannot expand '%s'\n", args[1]);
598 return -EINVAL;
599 }
600 action_for_each_trigger(prop_val, action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 return 0;
602}
603
604int do_symlink(int nargs, char **args)
605{
606 return symlink(args[1], args[2]);
607}
608
Ken Sumrall203bad52011-01-18 17:37:41 -0800609int do_rm(int nargs, char **args)
610{
611 return unlink(args[1]);
612}
613
614int do_rmdir(int nargs, char **args)
615{
616 return rmdir(args[1]);
617}
618
The Android Open Source Project35237d12008-12-17 18:08:08 -0800619int do_sysclktz(int nargs, char **args)
620{
621 struct timezone tz;
622
623 if (nargs != 2)
624 return -1;
625
626 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800627 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800628 if (settimeofday(NULL, &tz))
629 return -1;
630 return 0;
631}
632
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000633int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700634 int mode = -1;
635 int rc = fs_mgr_load_verity_state(&mode);
636 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
637 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000638 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700639 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000640}
641
Sami Tolvanen45474232015-03-30 11:38:38 +0100642static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
643 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
644 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000645}
646
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700647int do_verity_update_state(int nargs, char** args) {
648 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000649}
650
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700651int do_write(int nargs, char **args)
652{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700653 const char *path = args[1];
654 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700655
Johan Redestig7e952f42014-12-05 00:36:41 +0100656 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800657 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800658 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
659 return -EINVAL;
660 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800661 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700662}
663
San Mehat7c44fe52009-08-26 16:39:25 -0700664int do_copy(int nargs, char **args)
665{
666 char *buffer = NULL;
667 int rc = 0;
668 int fd1 = -1, fd2 = -1;
669 struct stat info;
670 int brtw, brtr;
671 char *p;
672
673 if (nargs != 3)
674 return -1;
675
Elliott Hughes24627902015-02-04 10:25:09 -0800676 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700677 return -1;
678
Nick Kralevich45a884f2015-02-02 14:37:22 -0800679 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700680 goto out_err;
681
Nick Kralevich45a884f2015-02-02 14:37:22 -0800682 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700683 goto out_err;
684
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800685 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700686 goto out_err;
687
688 p = buffer;
689 brtr = info.st_size;
690 while(brtr) {
691 rc = read(fd1, p, brtr);
692 if (rc < 0)
693 goto out_err;
694 if (rc == 0)
695 break;
696 p += rc;
697 brtr -= rc;
698 }
699
700 p = buffer;
701 brtw = info.st_size;
702 while(brtw) {
703 rc = write(fd2, p, brtw);
704 if (rc < 0)
705 goto out_err;
706 if (rc == 0)
707 break;
708 p += rc;
709 brtw -= rc;
710 }
711
712 rc = 0;
713 goto out;
714out_err:
715 rc = -1;
716out:
717 if (buffer)
718 free(buffer);
719 if (fd1 >= 0)
720 close(fd1);
721 if (fd2 >= 0)
722 close(fd2);
723 return rc;
724}
725
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726int do_chown(int nargs, char **args) {
727 /* GID is optional. */
728 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800729 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730 return -errno;
731 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800732 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700733 return -errno;
734 } else {
735 return -1;
736 }
737 return 0;
738}
739
740static mode_t get_mode(const char *s) {
741 mode_t mode = 0;
742 while (*s) {
743 if (*s >= '0' && *s <= '7') {
744 mode = (mode<<3) | (*s-'0');
745 } else {
746 return -1;
747 }
748 s++;
749 }
750 return mode;
751}
752
753int do_chmod(int nargs, char **args) {
754 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800755 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756 return -errno;
757 }
758 return 0;
759}
760
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500762 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400763 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500764
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500765 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400766 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400767 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500768 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400769 return ret;
770}
771
772int do_restorecon_recursive(int nargs, char **args) {
773 int i;
774 int ret = 0;
775
776 for (i = 1; i < nargs; i++) {
777 if (restorecon_recursive(args[i]) < 0)
778 ret = -errno;
779 }
780 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500781}
782
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700784 int log_level;
785 char log_level_str[PROP_VALUE_MAX] = "";
786 if (nargs != 2) {
787 ERROR("loglevel: missing argument\n");
788 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700790
791 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
792 ERROR("loglevel: cannot expand '%s'\n", args[1]);
793 return -EINVAL;
794 }
795 log_level = atoi(log_level_str);
796 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
797 ERROR("loglevel: invalid log level'%d'\n", log_level);
798 return -EINVAL;
799 }
800 klog_set_level(log_level);
801 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802}
803
Ken Sumrallc5c51032011-03-08 17:01:29 -0800804int do_load_persist_props(int nargs, char **args) {
805 if (nargs == 1) {
806 load_persist_props();
807 return 0;
808 }
809 return -1;
810}
811
Riley Andrewse4b7b292014-06-16 15:06:21 -0700812int do_load_all_props(int nargs, char **args) {
813 if (nargs == 1) {
814 load_all_props();
815 return 0;
816 }
817 return -1;
818}
819
Colin Crosscd0f1732010-04-19 17:10:24 -0700820int do_wait(int nargs, char **args)
821{
822 if (nargs == 2) {
823 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800824 } else if (nargs == 3) {
825 return wait_for_file(args[1], atoi(args[2]));
826 } else
827 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700828}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000829
Paul Lawrence806d10b2015-04-28 22:07:10 +0000830/*
831 * Callback to make a directory from the ext4 code
832 */
833static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000834{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000835 if (make_dir(dir, 0700) && errno != EEXIST) {
836 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000837 }
838
Paul Lawrence806d10b2015-04-28 22:07:10 +0000839 return 0;
840}
841
842int do_installkey(int nargs, char **args)
843{
844 if (nargs != 2) {
845 return -1;
846 }
847
848 char prop_value[PROP_VALUE_MAX] = {0};
849 property_get("ro.crypto.type", prop_value);
850 if (strcmp(prop_value, "file")) {
851 return 0;
852 }
853
854 return e4crypt_create_device_key(args[1],
855 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000856}