blob: d5d3faff62489a3f9f9d97dcc4722fffeee869fb [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>
Yusuke Sato0df08272015-07-08 14:57:07 -070019#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070020#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070021#include <signal.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070024#include <string.h>
25#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <sys/mount.h>
27#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080028#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070029#include <sys/types.h>
30#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070031#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070032#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000033#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000034#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Elliott Hughesdb3f2672015-03-20 09:45:18 -070039#include <fs_mgr.h>
40#include <base/stringprintf.h>
41#include <cutils/partition_utils.h>
42#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070043#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <private/android_filesystem_config.h>
45
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include "init.h"
47#include "keywords.h"
48#include "property_service.h"
49#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070050#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070051#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
Nick Kralevichbc609542015-01-31 21:39:46 -080054#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070055#define UNMOUNT_CHECK_MS 5000
56#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080057
James Morrissey381341f2014-05-16 11:36:36 +010058int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
Elliott Hughesf3cf4382015-02-03 17:12:07 -080060// System call provided by bionic but not in any header file.
61extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
The Android Open Source Project35237d12008-12-17 18:08:08 -080063static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064{
Emmanuel Berthierac412302015-04-27 13:50:09 +020065 char filename_val[PROP_VALUE_MAX];
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070066 if (expand_props(filename_val, filename, sizeof(filename_val)) == -1) {
Emmanuel Berthierac412302015-04-27 13:50:09 +020067 ERROR("insmod: cannot expand '%s'\n", filename);
68 return -EINVAL;
69 }
70
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070071 std::string module;
Emmanuel Berthierac412302015-04-27 13:50:09 +020072 if (!read_file(filename_val, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080074 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075
Elliott Hughesf682b472015-02-06 12:19:48 -080076 // TODO: use finit_module for >= 3.8 kernels.
77 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078}
79
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080static int __ifupdown(const char *interface, int up)
81{
82 struct ifreq ifr;
83 int s, ret;
84
85 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
86
87 s = socket(AF_INET, SOCK_DGRAM, 0);
88 if (s < 0)
89 return -1;
90
91 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
92 if (ret < 0) {
93 goto done;
94 }
95
96 if (up)
97 ifr.ifr_flags |= IFF_UP;
98 else
99 ifr.ifr_flags &= ~IFF_UP;
100
101 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800102
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103done:
104 close(s);
105 return ret;
106}
107
108static void service_start_if_not_disabled(struct service *svc)
109{
110 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700111 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700112 } else {
113 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 }
115}
116
Yusuke Sato0df08272015-07-08 14:57:07 -0700117static void unmount_and_fsck(const struct mntent *entry)
118{
119 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
120 return;
121
122 /* First, lazily unmount the directory. This unmount request finishes when
123 * all processes that open a file or directory in |entry->mnt_dir| exit.
124 */
125 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
126
127 /* Next, kill all processes except init, kthreadd, and kthreadd's
128 * children to finish the lazy unmount. Killing all processes here is okay
129 * because this callback function is only called right before reboot().
130 * It might be cleaner to selectively kill processes that actually use
131 * |entry->mnt_dir| rather than killing all, probably by reusing a function
132 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
133 * not allow init to scan /proc/<pid> files which the utility function
134 * heavily relies on. The policy does not allow the process to execute
135 * killall/pkill binaries either. Note that some processes might
136 * automatically restart after kill(), but that is not really a problem
137 * because |entry->mnt_dir| is no longer visible to such new processes.
138 */
139 service_for_each(service_stop);
140 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
141
142 int count = 0;
143 while (count++ < UNMOUNT_CHECK_TIMES) {
144 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
145 if (fd >= 0) {
146 /* |entry->mnt_dir| has sucessfully been unmounted. */
147 close(fd);
148 break;
149 } else if (errno == EBUSY) {
150 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
151 * while then retry.
152 */
153 TEMP_FAILURE_RETRY(
154 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
155 continue;
156 } else {
157 /* Cannot open the device. Give up. */
158 return;
159 }
160 }
161
162 int st;
163 if (!strcmp(entry->mnt_type, "f2fs")) {
164 const char *f2fs_argv[] = {
165 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
166 };
167 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
168 &st, true, LOG_KLOG, true, NULL);
169 } else if (!strcmp(entry->mnt_type, "ext4")) {
170 const char *ext4_argv[] = {
171 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
172 };
173 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
174 &st, true, LOG_KLOG, true, NULL);
175 }
176}
177
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178int do_class_start(int nargs, char **args)
179{
180 /* Starting a class does not start services
181 * which are explicitly disabled. They must
182 * be started individually.
183 */
184 service_for_each_class(args[1], service_start_if_not_disabled);
185 return 0;
186}
187
188int do_class_stop(int nargs, char **args)
189{
190 service_for_each_class(args[1], service_stop);
191 return 0;
192}
193
Ken Sumrall752923c2010-12-03 16:33:31 -0800194int do_class_reset(int nargs, char **args)
195{
196 service_for_each_class(args[1], service_reset);
197 return 0;
198}
199
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200int do_domainname(int nargs, char **args)
201{
202 return write_file("/proc/sys/kernel/domainname", args[1]);
203}
204
JP Abgrall3beec7e2014-05-02 21:14:29 -0700205int do_enable(int nargs, char **args)
206{
207 struct service *svc;
208 svc = service_find_by_name(args[1]);
209 if (svc) {
210 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
211 if (svc->flags & SVC_DISABLED_START) {
212 service_start(svc, NULL);
213 }
214 } else {
215 return -1;
216 }
217 return 0;
218}
219
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800220int do_exec(int nargs, char** args) {
221 service* svc = make_exec_oneshot_service(nargs, args);
222 if (svc == NULL) {
223 return -1;
224 }
225 service_start(svc, NULL);
226 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
229int do_export(int nargs, char **args)
230{
James Morrissey381341f2014-05-16 11:36:36 +0100231 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232}
233
234int do_hostname(int nargs, char **args)
235{
236 return write_file("/proc/sys/kernel/hostname", args[1]);
237}
238
239int do_ifup(int nargs, char **args)
240{
241 return __ifupdown(args[1], 1);
242}
243
The Android Open Source Project35237d12008-12-17 18:08:08 -0800244
245static int do_insmod_inner(int nargs, char **args, int opt_len)
246{
247 char options[opt_len + 1];
248 int i;
249
250 options[0] = '\0';
251 if (nargs > 2) {
252 strcpy(options, args[2]);
253 for (i = 3; i < nargs; ++i) {
254 strcat(options, " ");
255 strcat(options, args[i]);
256 }
257 }
258
259 return insmod(args[1], options);
260}
261
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262int do_insmod(int nargs, char **args)
263{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800264 int i;
265 int size = 0;
266
267 if (nargs > 2) {
268 for (i = 2; i < nargs; ++i)
269 size += strlen(args[i]) + 1;
270 }
271
272 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273}
274
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275int do_mkdir(int nargs, char **args)
276{
277 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700278 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279
280 /* mkdir <path> [mode] [owner] [group] */
281
282 if (nargs >= 3) {
283 mode = strtoul(args[2], 0, 8);
284 }
285
Stephen Smalleye096e362012-06-11 13:37:39 -0400286 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700287 /* chmod in case the directory already exists */
288 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800289 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700290 }
291 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292 return -errno;
293 }
294
295 if (nargs >= 4) {
296 uid_t uid = decode_uid(args[3]);
297 gid_t gid = -1;
298
299 if (nargs == 5) {
300 gid = decode_uid(args[4]);
301 }
302
Nick Kralevichbc609542015-01-31 21:39:46 -0800303 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 return -errno;
305 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700306
307 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
308 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800309 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700310 if (ret == -1) {
311 return -errno;
312 }
313 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 }
315
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000316 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317}
318
319static struct {
320 const char *name;
321 unsigned flag;
322} mount_flags[] = {
323 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200324 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 { "nosuid", MS_NOSUID },
326 { "nodev", MS_NODEV },
327 { "nodiratime", MS_NODIRATIME },
328 { "ro", MS_RDONLY },
329 { "rw", 0 },
330 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700331 { "bind", MS_BIND },
332 { "rec", MS_REC },
333 { "unbindable", MS_UNBINDABLE },
334 { "private", MS_PRIVATE },
335 { "slave", MS_SLAVE },
336 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 { "defaults", 0 },
338 { 0, 0 },
339};
340
Ken Sumrall752923c2010-12-03 16:33:31 -0800341#define DATA_MNT_POINT "/data"
342
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343/* mount <type> <device> <path> <flags ...> <options> */
344int do_mount(int nargs, char **args)
345{
346 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000347 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 char *options = NULL;
349 unsigned flags = 0;
350 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700351 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352
353 for (n = 4; n < nargs; n++) {
354 for (i = 0; mount_flags[i].name; i++) {
355 if (!strcmp(args[n], mount_flags[i].name)) {
356 flags |= mount_flags[i].flag;
357 break;
358 }
359 }
360
Colin Crosscd0f1732010-04-19 17:10:24 -0700361 if (!mount_flags[i].name) {
362 if (!strcmp(args[n], "wait"))
363 wait = 1;
364 /* if our last argument isn't a flag, wolf it up as an option string */
365 else if (n + 1 == nargs)
366 options = args[n];
367 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368 }
369
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000372 target = args[3];
373
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374 if (!strncmp(source, "mtd@", 4)) {
375 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000376 if (n < 0) {
377 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000379
Yabin Cuie2d63af2015-02-17 19:27:51 -0800380 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000381
Colin Crosscd0f1732010-04-19 17:10:24 -0700382 if (wait)
383 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 if (mount(tmp, target, system, flags, options) < 0) {
385 return -1;
386 }
387
Ken Sumralldd4d7862011-02-17 18:09:47 -0800388 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389 } else if (!strncmp(source, "loop@", 5)) {
390 int mode, loop, fd;
391 struct loop_info info;
392
393 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800394 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 if (fd < 0) {
396 return -1;
397 }
398
399 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800400 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800401 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000402 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100403 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000404 return -1;
405 }
406
407 /* if it is a blank loop device */
408 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
409 /* if it becomes our loop device */
410 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
411 close(fd);
412
413 if (mount(tmp, target, system, flags, options) < 0) {
414 ioctl(loop, LOOP_CLR_FD, 0);
415 close(loop);
416 return -1;
417 }
418
419 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800420 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000421 }
422 }
423
424 close(loop);
425 }
426
427 close(fd);
428 ERROR("out of loopback devices");
429 return -1;
430 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700431 if (wait)
432 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000433 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700434 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000435 }
436
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800438
439exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800440 return 0;
441
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442}
443
JP Abgrallcee20682014-07-02 14:26:54 -0700444static int wipe_data_via_recovery()
445{
446 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800447 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700448 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700449 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
450 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700451 close(fd);
452 } else {
453 ERROR("could not open /cache/recovery/command\n");
454 return -1;
455 }
456 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
457 while (1) { pause(); } // never reached
458}
459
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000460/*
JP Abgrallcee20682014-07-02 14:26:54 -0700461 * This function might request a reboot, in which case it will
462 * not return.
463 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700464int do_mount_all(int nargs, char **args)
465{
466 pid_t pid;
467 int ret = -1;
468 int child_ret = -1;
469 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800470 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700471
472 if (nargs != 2) {
473 return -1;
474 }
475
476 /*
477 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
478 * do the call in the child to provide protection to the main init
479 * process if anything goes wrong (crash or memory leak), and wait for
480 * the child to finish in the parent.
481 */
482 pid = fork();
483 if (pid > 0) {
484 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700485 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
486 if (wp_ret < 0) {
487 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700488 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700489 }
490
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700491 if (WIFEXITED(status)) {
492 ret = WEXITSTATUS(status);
493 } else {
494 ret = -1;
495 }
496 } else if (pid == 0) {
497 /* child, call fs_mgr_mount_all() */
498 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800499 fstab = fs_mgr_read_fstab(args[1]);
500 child_ret = fs_mgr_mount_all(fstab);
501 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700502 if (child_ret == -1) {
503 ERROR("fs_mgr_mount_all returned an error\n");
504 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700505 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 } else {
507 /* fork failed, return an error */
508 return -1;
509 }
510
JP Abgrallf22b7452014-07-02 13:16:04 -0700511 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800512 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700513 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700514 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000515 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800516 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700517 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700518 property_set("ro.crypto.state", "unencrypted");
519 /* If fs_mgr determined this is an unencrypted device, then trigger
520 * that action.
521 */
522 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700523 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
524 /* Setup a wipe via recovery, and reboot into recovery */
525 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
526 ret = wipe_data_via_recovery();
527 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000528 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000529 if (e4crypt_install_keyring()) {
530 return -1;
531 }
532 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000533 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000534
535 // Although encrypted, we have device key, so we do not need to
536 // do anything different from the nonencrypted case.
537 action_for_each_trigger("nonencrypted", action_add_queue_tail);
538 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
539 if (e4crypt_install_keyring()) {
540 return -1;
541 }
542 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000543 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000544 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700545 } else if (ret > 0) {
546 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700547 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700548 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700549
550 return ret;
551}
552
Ken Sumralla76baaa2013-07-09 18:42:09 -0700553int do_swapon_all(int nargs, char **args)
554{
555 struct fstab *fstab;
556 int ret;
557
558 fstab = fs_mgr_read_fstab(args[1]);
559 ret = fs_mgr_swapon_all(fstab);
560 fs_mgr_free_fstab(fstab);
561
562 return ret;
563}
564
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565int do_setprop(int nargs, char **args)
566{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700567 const char *name = args[1];
568 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800569 char prop_val[PROP_VALUE_MAX];
570 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700571
Dima Zavin84bf9af2011-12-20 13:44:41 -0800572 ret = expand_props(prop_val, value, sizeof(prop_val));
573 if (ret) {
574 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
575 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700576 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800577 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 return 0;
579}
580
581int do_setrlimit(int nargs, char **args)
582{
583 struct rlimit limit;
584 int resource;
585 resource = atoi(args[1]);
586 limit.rlim_cur = atoi(args[2]);
587 limit.rlim_max = atoi(args[3]);
588 return setrlimit(resource, &limit);
589}
590
591int do_start(int nargs, char **args)
592{
593 struct service *svc;
594 svc = service_find_by_name(args[1]);
595 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700596 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 }
598 return 0;
599}
600
601int do_stop(int nargs, char **args)
602{
603 struct service *svc;
604 svc = service_find_by_name(args[1]);
605 if (svc) {
606 service_stop(svc);
607 }
608 return 0;
609}
610
611int do_restart(int nargs, char **args)
612{
613 struct service *svc;
614 svc = service_find_by_name(args[1]);
615 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500616 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 }
618 return 0;
619}
620
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700621int do_powerctl(int nargs, char **args)
622{
623 char command[PROP_VALUE_MAX];
624 int res;
625 int len = 0;
626 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800627 const char *reboot_target;
Yusuke Sato0df08272015-07-08 14:57:07 -0700628 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700629
630 res = expand_props(command, args[1], sizeof(command));
631 if (res) {
632 ERROR("powerctl: cannot expand '%s'\n", args[1]);
633 return -EINVAL;
634 }
635
636 if (strncmp(command, "shutdown", 8) == 0) {
637 cmd = ANDROID_RB_POWEROFF;
638 len = 8;
Yusuke Sato0df08272015-07-08 14:57:07 -0700639 callback_on_ro_remount = unmount_and_fsck;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700640 } else if (strncmp(command, "reboot", 6) == 0) {
641 cmd = ANDROID_RB_RESTART2;
642 len = 6;
643 } else {
644 ERROR("powerctl: unrecognized command '%s'\n", command);
645 return -EINVAL;
646 }
647
648 if (command[len] == ',') {
649 reboot_target = &command[len + 1];
650 } else if (command[len] == '\0') {
651 reboot_target = "";
652 } else {
653 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
654 return -EINVAL;
655 }
656
Yusuke Sato0df08272015-07-08 14:57:07 -0700657 return android_reboot_with_callback(cmd, 0, reboot_target,
658 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700659}
660
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661int do_trigger(int nargs, char **args)
662{
Mark Salyzyndd0e3162015-05-27 10:46:54 -0700663 char prop_val[PROP_VALUE_MAX];
664 int res = expand_props(prop_val, args[1], sizeof(prop_val));
665 if (res) {
666 ERROR("trigger: cannot expand '%s'\n", args[1]);
667 return -EINVAL;
668 }
669 action_for_each_trigger(prop_val, action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700670 return 0;
671}
672
673int do_symlink(int nargs, char **args)
674{
675 return symlink(args[1], args[2]);
676}
677
Ken Sumrall203bad52011-01-18 17:37:41 -0800678int do_rm(int nargs, char **args)
679{
680 return unlink(args[1]);
681}
682
683int do_rmdir(int nargs, char **args)
684{
685 return rmdir(args[1]);
686}
687
The Android Open Source Project35237d12008-12-17 18:08:08 -0800688int do_sysclktz(int nargs, char **args)
689{
690 struct timezone tz;
691
692 if (nargs != 2)
693 return -1;
694
695 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800696 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800697 if (settimeofday(NULL, &tz))
698 return -1;
699 return 0;
700}
701
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000702int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700703 int mode = -1;
704 int rc = fs_mgr_load_verity_state(&mode);
705 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
706 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000707 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700708 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000709}
710
Sami Tolvanen45474232015-03-30 11:38:38 +0100711static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
712 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
713 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000714}
715
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700716int do_verity_update_state(int nargs, char** args) {
717 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000718}
719
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720int do_write(int nargs, char **args)
721{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700722 const char *path = args[1];
723 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700724
Johan Redestig7e952f42014-12-05 00:36:41 +0100725 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800726 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800727 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
728 return -EINVAL;
729 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800730 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731}
732
San Mehat7c44fe52009-08-26 16:39:25 -0700733int do_copy(int nargs, char **args)
734{
735 char *buffer = NULL;
736 int rc = 0;
737 int fd1 = -1, fd2 = -1;
738 struct stat info;
739 int brtw, brtr;
740 char *p;
741
742 if (nargs != 3)
743 return -1;
744
Elliott Hughes24627902015-02-04 10:25:09 -0800745 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700746 return -1;
747
Nick Kralevich45a884f2015-02-02 14:37:22 -0800748 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700749 goto out_err;
750
Nick Kralevich45a884f2015-02-02 14:37:22 -0800751 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700752 goto out_err;
753
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800754 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700755 goto out_err;
756
757 p = buffer;
758 brtr = info.st_size;
759 while(brtr) {
760 rc = read(fd1, p, brtr);
761 if (rc < 0)
762 goto out_err;
763 if (rc == 0)
764 break;
765 p += rc;
766 brtr -= rc;
767 }
768
769 p = buffer;
770 brtw = info.st_size;
771 while(brtw) {
772 rc = write(fd2, p, brtw);
773 if (rc < 0)
774 goto out_err;
775 if (rc == 0)
776 break;
777 p += rc;
778 brtw -= rc;
779 }
780
781 rc = 0;
782 goto out;
783out_err:
784 rc = -1;
785out:
786 if (buffer)
787 free(buffer);
788 if (fd1 >= 0)
789 close(fd1);
790 if (fd2 >= 0)
791 close(fd2);
792 return rc;
793}
794
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795int do_chown(int nargs, char **args) {
796 /* GID is optional. */
797 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800798 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799 return -errno;
800 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800801 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802 return -errno;
803 } else {
804 return -1;
805 }
806 return 0;
807}
808
809static mode_t get_mode(const char *s) {
810 mode_t mode = 0;
811 while (*s) {
812 if (*s >= '0' && *s <= '7') {
813 mode = (mode<<3) | (*s-'0');
814 } else {
815 return -1;
816 }
817 s++;
818 }
819 return mode;
820}
821
822int do_chmod(int nargs, char **args) {
823 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800824 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 return -errno;
826 }
827 return 0;
828}
829
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500830int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500831 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400832 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500833
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500834 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400835 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400836 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500837 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400838 return ret;
839}
840
841int do_restorecon_recursive(int nargs, char **args) {
842 int i;
843 int ret = 0;
844
845 for (i = 1; i < nargs; i++) {
846 if (restorecon_recursive(args[i]) < 0)
847 ret = -errno;
848 }
849 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500850}
851
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700853 int log_level;
854 char log_level_str[PROP_VALUE_MAX] = "";
855 if (nargs != 2) {
856 ERROR("loglevel: missing argument\n");
857 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700859
860 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
861 ERROR("loglevel: cannot expand '%s'\n", args[1]);
862 return -EINVAL;
863 }
864 log_level = atoi(log_level_str);
865 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
866 ERROR("loglevel: invalid log level'%d'\n", log_level);
867 return -EINVAL;
868 }
869 klog_set_level(log_level);
870 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700871}
872
Ken Sumrallc5c51032011-03-08 17:01:29 -0800873int do_load_persist_props(int nargs, char **args) {
874 if (nargs == 1) {
875 load_persist_props();
876 return 0;
877 }
878 return -1;
879}
880
Riley Andrewse4b7b292014-06-16 15:06:21 -0700881int do_load_all_props(int nargs, char **args) {
882 if (nargs == 1) {
883 load_all_props();
884 return 0;
885 }
886 return -1;
887}
888
Colin Crosscd0f1732010-04-19 17:10:24 -0700889int do_wait(int nargs, char **args)
890{
891 if (nargs == 2) {
892 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800893 } else if (nargs == 3) {
894 return wait_for_file(args[1], atoi(args[2]));
895 } else
896 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700897}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000898
Paul Lawrence806d10b2015-04-28 22:07:10 +0000899/*
900 * Callback to make a directory from the ext4 code
901 */
902static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000903{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000904 if (make_dir(dir, 0700) && errno != EEXIST) {
905 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000906 }
907
Paul Lawrence806d10b2015-04-28 22:07:10 +0000908 return 0;
909}
910
911int do_installkey(int nargs, char **args)
912{
913 if (nargs != 2) {
914 return -1;
915 }
916
917 char prop_value[PROP_VALUE_MAX] = {0};
918 property_get("ro.crypto.type", prop_value);
919 if (strcmp(prop_value, "file")) {
920 return 0;
921 }
922
923 return e4crypt_create_device_key(args[1],
924 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000925}