blob: 3bbaf8336894e534a2b52e89fde890c276a87cf6 [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
157int do_export(int nargs, char **args)
158{
James Morrissey381341f2014-05-16 11:36:36 +0100159 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160}
161
162int do_hostname(int nargs, char **args)
163{
164 return write_file("/proc/sys/kernel/hostname", args[1]);
165}
166
167int do_ifup(int nargs, char **args)
168{
169 return __ifupdown(args[1], 1);
170}
171
The Android Open Source Project35237d12008-12-17 18:08:08 -0800172
173static int do_insmod_inner(int nargs, char **args, int opt_len)
174{
175 char options[opt_len + 1];
176 int i;
177
178 options[0] = '\0';
179 if (nargs > 2) {
180 strcpy(options, args[2]);
181 for (i = 3; i < nargs; ++i) {
182 strcat(options, " ");
183 strcat(options, args[i]);
184 }
185 }
186
187 return insmod(args[1], options);
188}
189
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190int do_insmod(int nargs, char **args)
191{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800192 int i;
193 int size = 0;
194
195 if (nargs > 2) {
196 for (i = 2; i < nargs; ++i)
197 size += strlen(args[i]) + 1;
198 }
199
200 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201}
202
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203int do_mkdir(int nargs, char **args)
204{
205 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700206 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
208 /* mkdir <path> [mode] [owner] [group] */
209
210 if (nargs >= 3) {
211 mode = strtoul(args[2], 0, 8);
212 }
213
Stephen Smalleye096e362012-06-11 13:37:39 -0400214 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700215 /* chmod in case the directory already exists */
216 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800217 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700218 }
219 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 return -errno;
221 }
222
223 if (nargs >= 4) {
224 uid_t uid = decode_uid(args[3]);
225 gid_t gid = -1;
226
227 if (nargs == 5) {
228 gid = decode_uid(args[4]);
229 }
230
Nick Kralevichbc609542015-01-31 21:39:46 -0800231 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 return -errno;
233 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700234
235 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
236 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800237 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700238 if (ret == -1) {
239 return -errno;
240 }
241 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242 }
243
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000244 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245}
246
247static struct {
248 const char *name;
249 unsigned flag;
250} mount_flags[] = {
251 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200252 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700253 { "nosuid", MS_NOSUID },
254 { "nodev", MS_NODEV },
255 { "nodiratime", MS_NODIRATIME },
256 { "ro", MS_RDONLY },
257 { "rw", 0 },
258 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700259 { "bind", MS_BIND },
260 { "rec", MS_REC },
261 { "unbindable", MS_UNBINDABLE },
262 { "private", MS_PRIVATE },
263 { "slave", MS_SLAVE },
264 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265 { "defaults", 0 },
266 { 0, 0 },
267};
268
Ken Sumrall752923c2010-12-03 16:33:31 -0800269#define DATA_MNT_POINT "/data"
270
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271/* mount <type> <device> <path> <flags ...> <options> */
272int do_mount(int nargs, char **args)
273{
274 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000275 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 char *options = NULL;
277 unsigned flags = 0;
278 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700279 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280
281 for (n = 4; n < nargs; n++) {
282 for (i = 0; mount_flags[i].name; i++) {
283 if (!strcmp(args[n], mount_flags[i].name)) {
284 flags |= mount_flags[i].flag;
285 break;
286 }
287 }
288
Colin Crosscd0f1732010-04-19 17:10:24 -0700289 if (!mount_flags[i].name) {
290 if (!strcmp(args[n], "wait"))
291 wait = 1;
292 /* if our last argument isn't a flag, wolf it up as an option string */
293 else if (n + 1 == nargs)
294 options = args[n];
295 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296 }
297
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000298 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000300 target = args[3];
301
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 if (!strncmp(source, "mtd@", 4)) {
303 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000304 if (n < 0) {
305 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000307
Yabin Cuie2d63af2015-02-17 19:27:51 -0800308 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000309
Colin Crosscd0f1732010-04-19 17:10:24 -0700310 if (wait)
311 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000312 if (mount(tmp, target, system, flags, options) < 0) {
313 return -1;
314 }
315
Ken Sumralldd4d7862011-02-17 18:09:47 -0800316 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000317 } else if (!strncmp(source, "loop@", 5)) {
318 int mode, loop, fd;
319 struct loop_info info;
320
321 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800322 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000323 if (fd < 0) {
324 return -1;
325 }
326
327 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800328 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800329 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000330 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100331 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000332 return -1;
333 }
334
335 /* if it is a blank loop device */
336 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
337 /* if it becomes our loop device */
338 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
339 close(fd);
340
341 if (mount(tmp, target, system, flags, options) < 0) {
342 ioctl(loop, LOOP_CLR_FD, 0);
343 close(loop);
344 return -1;
345 }
346
347 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800348 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000349 }
350 }
351
352 close(loop);
353 }
354
355 close(fd);
356 ERROR("out of loopback devices");
357 return -1;
358 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700359 if (wait)
360 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700362 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000363 }
364
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800366
367exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800368 return 0;
369
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370}
371
JP Abgrallcee20682014-07-02 14:26:54 -0700372static int wipe_data_via_recovery()
373{
374 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800375 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700376 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700377 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
378 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700379 close(fd);
380 } else {
381 ERROR("could not open /cache/recovery/command\n");
382 return -1;
383 }
384 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
385 while (1) { pause(); } // never reached
386}
387
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000388/*
389 * Callback to make a directory from the ext4 code
390 */
391static int do_mount_alls_make_dir(const char* dir)
392{
393 if (make_dir(dir, 0700) && errno != EEXIST) {
394 return -1;
395 }
396
397 return 0;
398}
JP Abgrallcee20682014-07-02 14:26:54 -0700399
400/*
401 * This function might request a reboot, in which case it will
402 * not return.
403 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700404int do_mount_all(int nargs, char **args)
405{
406 pid_t pid;
407 int ret = -1;
408 int child_ret = -1;
409 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800410 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700411
412 if (nargs != 2) {
413 return -1;
414 }
415
416 /*
417 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
418 * do the call in the child to provide protection to the main init
419 * process if anything goes wrong (crash or memory leak), and wait for
420 * the child to finish in the parent.
421 */
422 pid = fork();
423 if (pid > 0) {
424 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700425 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
426 if (wp_ret < 0) {
427 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700428 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700429 }
430
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700431 if (WIFEXITED(status)) {
432 ret = WEXITSTATUS(status);
433 } else {
434 ret = -1;
435 }
436 } else if (pid == 0) {
437 /* child, call fs_mgr_mount_all() */
438 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800439 fstab = fs_mgr_read_fstab(args[1]);
440 child_ret = fs_mgr_mount_all(fstab);
441 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700442 if (child_ret == -1) {
443 ERROR("fs_mgr_mount_all returned an error\n");
444 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700445 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700446 } else {
447 /* fork failed, return an error */
448 return -1;
449 }
450
JP Abgrallf22b7452014-07-02 13:16:04 -0700451 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800452 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700453 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800455 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700456 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700457 property_set("ro.crypto.state", "unencrypted");
458 /* If fs_mgr determined this is an unencrypted device, then trigger
459 * that action.
460 */
461 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700462 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
463 /* Setup a wipe via recovery, and reboot into recovery */
464 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
465 ret = wipe_data_via_recovery();
466 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000467 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
468 // We have to create the key files here. Only init can call make_dir,
469 // and we can't do it from fs_mgr as then fs_mgr would depend on
470 // make_dir creating a circular dependency.
471 fstab = fs_mgr_read_fstab(args[1]);
472 for (int i = 0; i < fstab->num_entries; ++i) {
473 if (fs_mgr_is_file_encrypted(&fstab->recs[i])) {
474 if (e4crypt_create_device_key(fstab->recs[i].mount_point,
475 do_mount_alls_make_dir)) {
476 ERROR("Could not create device key on %s"
477 " - continue unencrypted\n",
478 fstab->recs[i].mount_point);
479 }
480 }
481 }
482 fs_mgr_free_fstab(fstab);
483
484 if (e4crypt_install_keyring()) {
485 return -1;
486 }
487 property_set("ro.crypto.state", "encrypted");
488
489 // Although encrypted, we have device key, so we do not need to
490 // do anything different from the nonencrypted case.
491 action_for_each_trigger("nonencrypted", action_add_queue_tail);
492 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
493 if (e4crypt_install_keyring()) {
494 return -1;
495 }
496 property_set("ro.crypto.state", "encrypted");
497 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700498 } else if (ret > 0) {
499 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700501 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700502
503 return ret;
504}
505
Ken Sumralla76baaa2013-07-09 18:42:09 -0700506int do_swapon_all(int nargs, char **args)
507{
508 struct fstab *fstab;
509 int ret;
510
511 fstab = fs_mgr_read_fstab(args[1]);
512 ret = fs_mgr_swapon_all(fstab);
513 fs_mgr_free_fstab(fstab);
514
515 return ret;
516}
517
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500518int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500519 if (is_selinux_enabled() <= 0)
520 return 0;
521 if (setcon(args[1]) < 0) {
522 return -errno;
523 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500524 return 0;
525}
526
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527int do_setprop(int nargs, char **args)
528{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700529 const char *name = args[1];
530 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800531 char prop_val[PROP_VALUE_MAX];
532 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700533
Dima Zavin84bf9af2011-12-20 13:44:41 -0800534 ret = expand_props(prop_val, value, sizeof(prop_val));
535 if (ret) {
536 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
537 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700538 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800539 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 return 0;
541}
542
543int do_setrlimit(int nargs, char **args)
544{
545 struct rlimit limit;
546 int resource;
547 resource = atoi(args[1]);
548 limit.rlim_cur = atoi(args[2]);
549 limit.rlim_max = atoi(args[3]);
550 return setrlimit(resource, &limit);
551}
552
553int do_start(int nargs, char **args)
554{
555 struct service *svc;
556 svc = service_find_by_name(args[1]);
557 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700558 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 }
560 return 0;
561}
562
563int do_stop(int nargs, char **args)
564{
565 struct service *svc;
566 svc = service_find_by_name(args[1]);
567 if (svc) {
568 service_stop(svc);
569 }
570 return 0;
571}
572
573int do_restart(int nargs, char **args)
574{
575 struct service *svc;
576 svc = service_find_by_name(args[1]);
577 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500578 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 }
580 return 0;
581}
582
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700583int do_powerctl(int nargs, char **args)
584{
585 char command[PROP_VALUE_MAX];
586 int res;
587 int len = 0;
588 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800589 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700590
591 res = expand_props(command, args[1], sizeof(command));
592 if (res) {
593 ERROR("powerctl: cannot expand '%s'\n", args[1]);
594 return -EINVAL;
595 }
596
597 if (strncmp(command, "shutdown", 8) == 0) {
598 cmd = ANDROID_RB_POWEROFF;
599 len = 8;
600 } else if (strncmp(command, "reboot", 6) == 0) {
601 cmd = ANDROID_RB_RESTART2;
602 len = 6;
603 } else {
604 ERROR("powerctl: unrecognized command '%s'\n", command);
605 return -EINVAL;
606 }
607
608 if (command[len] == ',') {
609 reboot_target = &command[len + 1];
610 } else if (command[len] == '\0') {
611 reboot_target = "";
612 } else {
613 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
614 return -EINVAL;
615 }
616
617 return android_reboot(cmd, 0, reboot_target);
618}
619
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620int do_trigger(int nargs, char **args)
621{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000622 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623 return 0;
624}
625
626int do_symlink(int nargs, char **args)
627{
628 return symlink(args[1], args[2]);
629}
630
Ken Sumrall203bad52011-01-18 17:37:41 -0800631int do_rm(int nargs, char **args)
632{
633 return unlink(args[1]);
634}
635
636int do_rmdir(int nargs, char **args)
637{
638 return rmdir(args[1]);
639}
640
The Android Open Source Project35237d12008-12-17 18:08:08 -0800641int do_sysclktz(int nargs, char **args)
642{
643 struct timezone tz;
644
645 if (nargs != 2)
646 return -1;
647
648 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800649 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800650 if (settimeofday(NULL, &tz))
651 return -1;
652 return 0;
653}
654
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000655int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700656 int mode = -1;
657 int rc = fs_mgr_load_verity_state(&mode);
658 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
659 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000660 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700661 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000662}
663
Sami Tolvanen45474232015-03-30 11:38:38 +0100664static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
665 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
666 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000667}
668
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700669int do_verity_update_state(int nargs, char** args) {
670 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000671}
672
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700673int do_write(int nargs, char **args)
674{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700675 const char *path = args[1];
676 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700677
Johan Redestig7e952f42014-12-05 00:36:41 +0100678 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800679 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800680 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
681 return -EINVAL;
682 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800683 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700684}
685
San Mehat7c44fe52009-08-26 16:39:25 -0700686int do_copy(int nargs, char **args)
687{
688 char *buffer = NULL;
689 int rc = 0;
690 int fd1 = -1, fd2 = -1;
691 struct stat info;
692 int brtw, brtr;
693 char *p;
694
695 if (nargs != 3)
696 return -1;
697
Elliott Hughes24627902015-02-04 10:25:09 -0800698 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700699 return -1;
700
Nick Kralevich45a884f2015-02-02 14:37:22 -0800701 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700702 goto out_err;
703
Nick Kralevich45a884f2015-02-02 14:37:22 -0800704 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700705 goto out_err;
706
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800707 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700708 goto out_err;
709
710 p = buffer;
711 brtr = info.st_size;
712 while(brtr) {
713 rc = read(fd1, p, brtr);
714 if (rc < 0)
715 goto out_err;
716 if (rc == 0)
717 break;
718 p += rc;
719 brtr -= rc;
720 }
721
722 p = buffer;
723 brtw = info.st_size;
724 while(brtw) {
725 rc = write(fd2, p, brtw);
726 if (rc < 0)
727 goto out_err;
728 if (rc == 0)
729 break;
730 p += rc;
731 brtw -= rc;
732 }
733
734 rc = 0;
735 goto out;
736out_err:
737 rc = -1;
738out:
739 if (buffer)
740 free(buffer);
741 if (fd1 >= 0)
742 close(fd1);
743 if (fd2 >= 0)
744 close(fd2);
745 return rc;
746}
747
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748int do_chown(int nargs, char **args) {
749 /* GID is optional. */
750 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800751 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752 return -errno;
753 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800754 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700755 return -errno;
756 } else {
757 return -1;
758 }
759 return 0;
760}
761
762static mode_t get_mode(const char *s) {
763 mode_t mode = 0;
764 while (*s) {
765 if (*s >= '0' && *s <= '7') {
766 mode = (mode<<3) | (*s-'0');
767 } else {
768 return -1;
769 }
770 s++;
771 }
772 return mode;
773}
774
775int do_chmod(int nargs, char **args) {
776 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800777 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 return -errno;
779 }
780 return 0;
781}
782
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500783int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500784 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400785 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500786
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500787 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400788 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400789 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500790 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400791 return ret;
792}
793
794int do_restorecon_recursive(int nargs, char **args) {
795 int i;
796 int ret = 0;
797
798 for (i = 1; i < nargs; i++) {
799 if (restorecon_recursive(args[i]) < 0)
800 ret = -errno;
801 }
802 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803}
804
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700806 int log_level;
807 char log_level_str[PROP_VALUE_MAX] = "";
808 if (nargs != 2) {
809 ERROR("loglevel: missing argument\n");
810 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700812
813 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
814 ERROR("loglevel: cannot expand '%s'\n", args[1]);
815 return -EINVAL;
816 }
817 log_level = atoi(log_level_str);
818 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
819 ERROR("loglevel: invalid log level'%d'\n", log_level);
820 return -EINVAL;
821 }
822 klog_set_level(log_level);
823 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824}
825
Ken Sumrallc5c51032011-03-08 17:01:29 -0800826int do_load_persist_props(int nargs, char **args) {
827 if (nargs == 1) {
828 load_persist_props();
829 return 0;
830 }
831 return -1;
832}
833
Riley Andrewse4b7b292014-06-16 15:06:21 -0700834int do_load_all_props(int nargs, char **args) {
835 if (nargs == 1) {
836 load_all_props();
837 return 0;
838 }
839 return -1;
840}
841
Colin Crosscd0f1732010-04-19 17:10:24 -0700842int do_wait(int nargs, char **args)
843{
844 if (nargs == 2) {
845 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800846 } else if (nargs == 3) {
847 return wait_for_file(args[1], atoi(args[2]));
848 } else
849 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700850}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000851
852int do_installkey(int nargs, char **args)
853{
854 if (nargs == 2) {
855 return e4crypt_install_key(args[1]);
856 }
857
858 return -1;
859}