blob: 9d5b8a830d2d399a4f9b586e5e1522a5f21cdb09 [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 Lawrenceb8c9d272015-03-26 15:49:42 +000032#include <ext4_crypt.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;
61 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080063 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Elliott Hughesf682b472015-02-06 12:19:48 -080065 // TODO: use finit_module for >= 3.8 kernels.
66 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067}
68
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069static int __ifupdown(const char *interface, int up)
70{
71 struct ifreq ifr;
72 int s, ret;
73
74 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
75
76 s = socket(AF_INET, SOCK_DGRAM, 0);
77 if (s < 0)
78 return -1;
79
80 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
81 if (ret < 0) {
82 goto done;
83 }
84
85 if (up)
86 ifr.ifr_flags |= IFF_UP;
87 else
88 ifr.ifr_flags &= ~IFF_UP;
89
90 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080091
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092done:
93 close(s);
94 return ret;
95}
96
97static void service_start_if_not_disabled(struct service *svc)
98{
99 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700100 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700101 } else {
102 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103 }
104}
105
106int do_class_start(int nargs, char **args)
107{
108 /* Starting a class does not start services
109 * which are explicitly disabled. They must
110 * be started individually.
111 */
112 service_for_each_class(args[1], service_start_if_not_disabled);
113 return 0;
114}
115
116int do_class_stop(int nargs, char **args)
117{
118 service_for_each_class(args[1], service_stop);
119 return 0;
120}
121
Ken Sumrall752923c2010-12-03 16:33:31 -0800122int do_class_reset(int nargs, char **args)
123{
124 service_for_each_class(args[1], service_reset);
125 return 0;
126}
127
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128int do_domainname(int nargs, char **args)
129{
130 return write_file("/proc/sys/kernel/domainname", args[1]);
131}
132
JP Abgrall3beec7e2014-05-02 21:14:29 -0700133int do_enable(int nargs, char **args)
134{
135 struct service *svc;
136 svc = service_find_by_name(args[1]);
137 if (svc) {
138 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
139 if (svc->flags & SVC_DISABLED_START) {
140 service_start(svc, NULL);
141 }
142 } else {
143 return -1;
144 }
145 return 0;
146}
147
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800148int do_exec(int nargs, char** args) {
149 service* svc = make_exec_oneshot_service(nargs, args);
150 if (svc == NULL) {
151 return -1;
152 }
153 service_start(svc, NULL);
154 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155}
156
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800157// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700158int do_execonce(int nargs, char **args)
159{
160 pid_t child;
161 int child_status = 0;
162 static int already_done;
163
164 if (already_done) {
165 return -1;
166 }
167 already_done = 1;
168 if (!(child = fork())) {
169 /*
170 * Child process.
171 */
172 zap_stdio();
173 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800174 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700175
176 memset(exec_args, 0, sizeof(exec_args));
177 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800178 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700179 ARRAY_SIZE(exec_args) - 1);
180 _exit(1);
181 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800182 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700183 exec_args[i - 1] = args[i];
184
185 if (execv(exec_args[0], exec_args) == -1) {
186 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
187 _exit(1);
188 }
189 ERROR("Returned from execv()!");
190 _exit(1);
191 }
192
193 /*
194 * Parent process.
195 */
196 if (child == -1) {
197 ERROR("Fork failed\n");
198 return -1;
199 }
200
201 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
202 ERROR("waitpid(): failed (%s)\n", strerror(errno));
203 return -1;
204 }
205
206 if (WIFSIGNALED(child_status)) {
207 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
208 return -1;
209 } else if (WIFEXITED(child_status)) {
210 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
211 return WEXITSTATUS(child_status);
212 }
213
214 ERROR("Abnormal child process exit\n");
215
216 return -1;
217}
218
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219int do_export(int nargs, char **args)
220{
James Morrissey381341f2014-05-16 11:36:36 +0100221 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
224int do_hostname(int nargs, char **args)
225{
226 return write_file("/proc/sys/kernel/hostname", args[1]);
227}
228
229int do_ifup(int nargs, char **args)
230{
231 return __ifupdown(args[1], 1);
232}
233
The Android Open Source Project35237d12008-12-17 18:08:08 -0800234
235static int do_insmod_inner(int nargs, char **args, int opt_len)
236{
237 char options[opt_len + 1];
238 int i;
239
240 options[0] = '\0';
241 if (nargs > 2) {
242 strcpy(options, args[2]);
243 for (i = 3; i < nargs; ++i) {
244 strcat(options, " ");
245 strcat(options, args[i]);
246 }
247 }
248
249 return insmod(args[1], options);
250}
251
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252int do_insmod(int nargs, char **args)
253{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800254 int i;
255 int size = 0;
256
257 if (nargs > 2) {
258 for (i = 2; i < nargs; ++i)
259 size += strlen(args[i]) + 1;
260 }
261
262 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263}
264
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265int do_mkdir(int nargs, char **args)
266{
267 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700268 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269
270 /* mkdir <path> [mode] [owner] [group] */
271
272 if (nargs >= 3) {
273 mode = strtoul(args[2], 0, 8);
274 }
275
Stephen Smalleye096e362012-06-11 13:37:39 -0400276 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700277 /* chmod in case the directory already exists */
278 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800279 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700280 }
281 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 return -errno;
283 }
284
285 if (nargs >= 4) {
286 uid_t uid = decode_uid(args[3]);
287 gid_t gid = -1;
288
289 if (nargs == 5) {
290 gid = decode_uid(args[4]);
291 }
292
Nick Kralevichbc609542015-01-31 21:39:46 -0800293 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 return -errno;
295 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700296
297 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
298 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800299 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700300 if (ret == -1) {
301 return -errno;
302 }
303 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 }
305
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000306 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307}
308
309static struct {
310 const char *name;
311 unsigned flag;
312} mount_flags[] = {
313 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200314 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 { "nosuid", MS_NOSUID },
316 { "nodev", MS_NODEV },
317 { "nodiratime", MS_NODIRATIME },
318 { "ro", MS_RDONLY },
319 { "rw", 0 },
320 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700321 { "bind", MS_BIND },
322 { "rec", MS_REC },
323 { "unbindable", MS_UNBINDABLE },
324 { "private", MS_PRIVATE },
325 { "slave", MS_SLAVE },
326 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 { "defaults", 0 },
328 { 0, 0 },
329};
330
Ken Sumrall752923c2010-12-03 16:33:31 -0800331#define DATA_MNT_POINT "/data"
332
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333/* mount <type> <device> <path> <flags ...> <options> */
334int do_mount(int nargs, char **args)
335{
336 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000337 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 char *options = NULL;
339 unsigned flags = 0;
340 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700341 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342
343 for (n = 4; n < nargs; n++) {
344 for (i = 0; mount_flags[i].name; i++) {
345 if (!strcmp(args[n], mount_flags[i].name)) {
346 flags |= mount_flags[i].flag;
347 break;
348 }
349 }
350
Colin Crosscd0f1732010-04-19 17:10:24 -0700351 if (!mount_flags[i].name) {
352 if (!strcmp(args[n], "wait"))
353 wait = 1;
354 /* if our last argument isn't a flag, wolf it up as an option string */
355 else if (n + 1 == nargs)
356 options = args[n];
357 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 }
359
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000360 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 target = args[3];
363
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364 if (!strncmp(source, "mtd@", 4)) {
365 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 if (n < 0) {
367 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369
Yabin Cuie2d63af2015-02-17 19:27:51 -0800370 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000371
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 if (wait)
373 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000374 if (mount(tmp, target, system, flags, options) < 0) {
375 return -1;
376 }
377
Ken Sumralldd4d7862011-02-17 18:09:47 -0800378 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000379 } else if (!strncmp(source, "loop@", 5)) {
380 int mode, loop, fd;
381 struct loop_info info;
382
383 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800384 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000385 if (fd < 0) {
386 return -1;
387 }
388
389 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800390 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800391 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100393 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000394 return -1;
395 }
396
397 /* if it is a blank loop device */
398 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
399 /* if it becomes our loop device */
400 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
401 close(fd);
402
403 if (mount(tmp, target, system, flags, options) < 0) {
404 ioctl(loop, LOOP_CLR_FD, 0);
405 close(loop);
406 return -1;
407 }
408
409 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800410 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000411 }
412 }
413
414 close(loop);
415 }
416
417 close(fd);
418 ERROR("out of loopback devices");
419 return -1;
420 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700421 if (wait)
422 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700424 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000425 }
426
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800428
429exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800430 return 0;
431
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432}
433
JP Abgrallcee20682014-07-02 14:26:54 -0700434static int wipe_data_via_recovery()
435{
436 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800437 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700438 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700439 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
440 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700441 close(fd);
442 } else {
443 ERROR("could not open /cache/recovery/command\n");
444 return -1;
445 }
446 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
447 while (1) { pause(); } // never reached
448}
449
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000450/*
451 * Callback to make a directory from the ext4 code
452 */
453static int do_mount_alls_make_dir(const char* dir)
454{
455 if (make_dir(dir, 0700) && errno != EEXIST) {
456 return -1;
457 }
458
459 return 0;
460}
JP Abgrallcee20682014-07-02 14:26:54 -0700461
462/*
463 * This function might request a reboot, in which case it will
464 * not return.
465 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700466int do_mount_all(int nargs, char **args)
467{
468 pid_t pid;
469 int ret = -1;
470 int child_ret = -1;
471 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800472 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700473
474 if (nargs != 2) {
475 return -1;
476 }
477
478 /*
479 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
480 * do the call in the child to provide protection to the main init
481 * process if anything goes wrong (crash or memory leak), and wait for
482 * the child to finish in the parent.
483 */
484 pid = fork();
485 if (pid > 0) {
486 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700487 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
488 if (wp_ret < 0) {
489 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700490 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700491 }
492
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700493 if (WIFEXITED(status)) {
494 ret = WEXITSTATUS(status);
495 } else {
496 ret = -1;
497 }
498 } else if (pid == 0) {
499 /* child, call fs_mgr_mount_all() */
500 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800501 fstab = fs_mgr_read_fstab(args[1]);
502 child_ret = fs_mgr_mount_all(fstab);
503 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 if (child_ret == -1) {
505 ERROR("fs_mgr_mount_all returned an error\n");
506 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700507 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508 } else {
509 /* fork failed, return an error */
510 return -1;
511 }
512
JP Abgrallf22b7452014-07-02 13:16:04 -0700513 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800514 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700515 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700516 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800517 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700518 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 property_set("ro.crypto.state", "unencrypted");
520 /* If fs_mgr determined this is an unencrypted device, then trigger
521 * that action.
522 */
523 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700524 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
525 /* Setup a wipe via recovery, and reboot into recovery */
526 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
527 ret = wipe_data_via_recovery();
528 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000529 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
530 // We have to create the key files here. Only init can call make_dir,
531 // and we can't do it from fs_mgr as then fs_mgr would depend on
532 // make_dir creating a circular dependency.
533 fstab = fs_mgr_read_fstab(args[1]);
534 for (int i = 0; i < fstab->num_entries; ++i) {
535 if (fs_mgr_is_file_encrypted(&fstab->recs[i])) {
536 if (e4crypt_create_device_key(fstab->recs[i].mount_point,
537 do_mount_alls_make_dir)) {
538 ERROR("Could not create device key on %s"
539 " - continue unencrypted\n",
540 fstab->recs[i].mount_point);
541 }
542 }
543 }
544 fs_mgr_free_fstab(fstab);
545
546 if (e4crypt_install_keyring()) {
547 return -1;
548 }
549 property_set("ro.crypto.state", "encrypted");
550
551 // Although encrypted, we have device key, so we do not need to
552 // do anything different from the nonencrypted case.
553 action_for_each_trigger("nonencrypted", action_add_queue_tail);
554 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
555 if (e4crypt_install_keyring()) {
556 return -1;
557 }
558 property_set("ro.crypto.state", "encrypted");
559 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700560 } else if (ret > 0) {
561 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700562 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700563 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700564
565 return ret;
566}
567
Ken Sumralla76baaa2013-07-09 18:42:09 -0700568int do_swapon_all(int nargs, char **args)
569{
570 struct fstab *fstab;
571 int ret;
572
573 fstab = fs_mgr_read_fstab(args[1]);
574 ret = fs_mgr_swapon_all(fstab);
575 fs_mgr_free_fstab(fstab);
576
577 return ret;
578}
579
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500580int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500581 if (is_selinux_enabled() <= 0)
582 return 0;
583 if (setcon(args[1]) < 0) {
584 return -errno;
585 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500586 return 0;
587}
588
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589int do_setprop(int nargs, char **args)
590{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700591 const char *name = args[1];
592 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800593 char prop_val[PROP_VALUE_MAX];
594 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700595
Dima Zavin84bf9af2011-12-20 13:44:41 -0800596 ret = expand_props(prop_val, value, sizeof(prop_val));
597 if (ret) {
598 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
599 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700600 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800601 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 return 0;
603}
604
605int do_setrlimit(int nargs, char **args)
606{
607 struct rlimit limit;
608 int resource;
609 resource = atoi(args[1]);
610 limit.rlim_cur = atoi(args[2]);
611 limit.rlim_max = atoi(args[3]);
612 return setrlimit(resource, &limit);
613}
614
615int do_start(int nargs, char **args)
616{
617 struct service *svc;
618 svc = service_find_by_name(args[1]);
619 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700620 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700621 }
622 return 0;
623}
624
625int do_stop(int nargs, char **args)
626{
627 struct service *svc;
628 svc = service_find_by_name(args[1]);
629 if (svc) {
630 service_stop(svc);
631 }
632 return 0;
633}
634
635int do_restart(int nargs, char **args)
636{
637 struct service *svc;
638 svc = service_find_by_name(args[1]);
639 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500640 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641 }
642 return 0;
643}
644
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700645int do_powerctl(int nargs, char **args)
646{
647 char command[PROP_VALUE_MAX];
648 int res;
649 int len = 0;
650 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800651 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700652
653 res = expand_props(command, args[1], sizeof(command));
654 if (res) {
655 ERROR("powerctl: cannot expand '%s'\n", args[1]);
656 return -EINVAL;
657 }
658
659 if (strncmp(command, "shutdown", 8) == 0) {
660 cmd = ANDROID_RB_POWEROFF;
661 len = 8;
662 } else if (strncmp(command, "reboot", 6) == 0) {
663 cmd = ANDROID_RB_RESTART2;
664 len = 6;
665 } else {
666 ERROR("powerctl: unrecognized command '%s'\n", command);
667 return -EINVAL;
668 }
669
670 if (command[len] == ',') {
671 reboot_target = &command[len + 1];
672 } else if (command[len] == '\0') {
673 reboot_target = "";
674 } else {
675 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
676 return -EINVAL;
677 }
678
679 return android_reboot(cmd, 0, reboot_target);
680}
681
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682int do_trigger(int nargs, char **args)
683{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000684 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700685 return 0;
686}
687
688int do_symlink(int nargs, char **args)
689{
690 return symlink(args[1], args[2]);
691}
692
Ken Sumrall203bad52011-01-18 17:37:41 -0800693int do_rm(int nargs, char **args)
694{
695 return unlink(args[1]);
696}
697
698int do_rmdir(int nargs, char **args)
699{
700 return rmdir(args[1]);
701}
702
The Android Open Source Project35237d12008-12-17 18:08:08 -0800703int do_sysclktz(int nargs, char **args)
704{
705 struct timezone tz;
706
707 if (nargs != 2)
708 return -1;
709
710 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800711 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800712 if (settimeofday(NULL, &tz))
713 return -1;
714 return 0;
715}
716
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000717int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700718 int mode = -1;
719 int rc = fs_mgr_load_verity_state(&mode);
720 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
721 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000722 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700723 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000724}
725
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700726static void verity_update_property(fstab_rec *fstab, const char *mount_point, int status) {
727 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(), "1");
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000728}
729
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700730int do_verity_update_state(int nargs, char** args) {
731 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000732}
733
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700734int do_write(int nargs, char **args)
735{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700736 const char *path = args[1];
737 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700738
Johan Redestig7e952f42014-12-05 00:36:41 +0100739 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800740 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800741 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
742 return -EINVAL;
743 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800744 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700745}
746
San Mehat7c44fe52009-08-26 16:39:25 -0700747int do_copy(int nargs, char **args)
748{
749 char *buffer = NULL;
750 int rc = 0;
751 int fd1 = -1, fd2 = -1;
752 struct stat info;
753 int brtw, brtr;
754 char *p;
755
756 if (nargs != 3)
757 return -1;
758
Elliott Hughes24627902015-02-04 10:25:09 -0800759 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700760 return -1;
761
Nick Kralevich45a884f2015-02-02 14:37:22 -0800762 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700763 goto out_err;
764
Nick Kralevich45a884f2015-02-02 14:37:22 -0800765 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700766 goto out_err;
767
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800768 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700769 goto out_err;
770
771 p = buffer;
772 brtr = info.st_size;
773 while(brtr) {
774 rc = read(fd1, p, brtr);
775 if (rc < 0)
776 goto out_err;
777 if (rc == 0)
778 break;
779 p += rc;
780 brtr -= rc;
781 }
782
783 p = buffer;
784 brtw = info.st_size;
785 while(brtw) {
786 rc = write(fd2, p, brtw);
787 if (rc < 0)
788 goto out_err;
789 if (rc == 0)
790 break;
791 p += rc;
792 brtw -= rc;
793 }
794
795 rc = 0;
796 goto out;
797out_err:
798 rc = -1;
799out:
800 if (buffer)
801 free(buffer);
802 if (fd1 >= 0)
803 close(fd1);
804 if (fd2 >= 0)
805 close(fd2);
806 return rc;
807}
808
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809int do_chown(int nargs, char **args) {
810 /* GID is optional. */
811 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800812 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 return -errno;
814 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800815 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816 return -errno;
817 } else {
818 return -1;
819 }
820 return 0;
821}
822
823static mode_t get_mode(const char *s) {
824 mode_t mode = 0;
825 while (*s) {
826 if (*s >= '0' && *s <= '7') {
827 mode = (mode<<3) | (*s-'0');
828 } else {
829 return -1;
830 }
831 s++;
832 }
833 return mode;
834}
835
836int do_chmod(int nargs, char **args) {
837 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800838 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700839 return -errno;
840 }
841 return 0;
842}
843
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500844int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500845 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400846 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500847
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500848 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400849 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400850 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500851 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400852 return ret;
853}
854
855int do_restorecon_recursive(int nargs, char **args) {
856 int i;
857 int ret = 0;
858
859 for (i = 1; i < nargs; i++) {
860 if (restorecon_recursive(args[i]) < 0)
861 ret = -errno;
862 }
863 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500864}
865
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700867 int log_level;
868 char log_level_str[PROP_VALUE_MAX] = "";
869 if (nargs != 2) {
870 ERROR("loglevel: missing argument\n");
871 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700873
874 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
875 ERROR("loglevel: cannot expand '%s'\n", args[1]);
876 return -EINVAL;
877 }
878 log_level = atoi(log_level_str);
879 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
880 ERROR("loglevel: invalid log level'%d'\n", log_level);
881 return -EINVAL;
882 }
883 klog_set_level(log_level);
884 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885}
886
Ken Sumrallc5c51032011-03-08 17:01:29 -0800887int do_load_persist_props(int nargs, char **args) {
888 if (nargs == 1) {
889 load_persist_props();
890 return 0;
891 }
892 return -1;
893}
894
Riley Andrewse4b7b292014-06-16 15:06:21 -0700895int do_load_all_props(int nargs, char **args) {
896 if (nargs == 1) {
897 load_all_props();
898 return 0;
899 }
900 return -1;
901}
902
Colin Crosscd0f1732010-04-19 17:10:24 -0700903int do_wait(int nargs, char **args)
904{
905 if (nargs == 2) {
906 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800907 } else if (nargs == 3) {
908 return wait_for_file(args[1], atoi(args[2]));
909 } else
910 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700911}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000912
913int do_installkey(int nargs, char **args)
914{
915 if (nargs == 2) {
916 return e4crypt_install_key(args[1]);
917 }
918
919 return -1;
920}