blob: 8d47da44560062434c412adb50d6b4dce9da5d03 [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;
Nan Liu12df1e12015-07-21 19:44:07 +0800471 char fstabfile[PROP_VALUE_MAX];
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472
473 if (nargs != 2) {
474 return -1;
475 }
476
Nan Liu12df1e12015-07-21 19:44:07 +0800477 if (expand_props(fstabfile, args[1], sizeof(fstabfile)) == -1) {
478 ERROR("mount_all: cannot expand '%s' \n", args[1]);
479 return -EINVAL;
480 }
481
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700482 /*
483 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
484 * do the call in the child to provide protection to the main init
485 * process if anything goes wrong (crash or memory leak), and wait for
486 * the child to finish in the parent.
487 */
488 pid = fork();
489 if (pid > 0) {
490 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700491 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
492 if (wp_ret < 0) {
493 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700494 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700495 }
496
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700497 if (WIFEXITED(status)) {
498 ret = WEXITSTATUS(status);
499 } else {
500 ret = -1;
501 }
502 } else if (pid == 0) {
503 /* child, call fs_mgr_mount_all() */
504 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800505 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800506 child_ret = fs_mgr_mount_all(fstab);
507 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508 if (child_ret == -1) {
509 ERROR("fs_mgr_mount_all returned an error\n");
510 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700511 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700512 } else {
513 /* fork failed, return an error */
514 return -1;
515 }
516
JP Abgrallf22b7452014-07-02 13:16:04 -0700517 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800518 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700519 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700520 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000521 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800522 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700523 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700524 property_set("ro.crypto.state", "unencrypted");
525 /* If fs_mgr determined this is an unencrypted device, then trigger
526 * that action.
527 */
528 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700529 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
530 /* Setup a wipe via recovery, and reboot into recovery */
531 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
532 ret = wipe_data_via_recovery();
533 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000534 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000535 if (e4crypt_install_keyring()) {
536 return -1;
537 }
538 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000539 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000540
541 // Although encrypted, we have device key, so we do not need to
542 // do anything different from the nonencrypted case.
543 action_for_each_trigger("nonencrypted", action_add_queue_tail);
544 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
545 if (e4crypt_install_keyring()) {
546 return -1;
547 }
548 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000549 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000550 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700551 } else if (ret > 0) {
552 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700553 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700554 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700555
556 return ret;
557}
558
Ken Sumralla76baaa2013-07-09 18:42:09 -0700559int do_swapon_all(int nargs, char **args)
560{
561 struct fstab *fstab;
562 int ret;
563
564 fstab = fs_mgr_read_fstab(args[1]);
565 ret = fs_mgr_swapon_all(fstab);
566 fs_mgr_free_fstab(fstab);
567
568 return ret;
569}
570
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571int do_setprop(int nargs, char **args)
572{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700573 const char *name = args[1];
574 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800575 char prop_val[PROP_VALUE_MAX];
576 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700577
Dima Zavin84bf9af2011-12-20 13:44:41 -0800578 ret = expand_props(prop_val, value, sizeof(prop_val));
579 if (ret) {
580 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
581 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700582 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800583 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700584 return 0;
585}
586
587int do_setrlimit(int nargs, char **args)
588{
589 struct rlimit limit;
590 int resource;
591 resource = atoi(args[1]);
592 limit.rlim_cur = atoi(args[2]);
593 limit.rlim_max = atoi(args[3]);
594 return setrlimit(resource, &limit);
595}
596
597int do_start(int nargs, char **args)
598{
599 struct service *svc;
600 svc = service_find_by_name(args[1]);
601 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700602 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603 }
604 return 0;
605}
606
607int do_stop(int nargs, char **args)
608{
609 struct service *svc;
610 svc = service_find_by_name(args[1]);
611 if (svc) {
612 service_stop(svc);
613 }
614 return 0;
615}
616
617int do_restart(int nargs, char **args)
618{
619 struct service *svc;
620 svc = service_find_by_name(args[1]);
621 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500622 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623 }
624 return 0;
625}
626
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700627int do_powerctl(int nargs, char **args)
628{
629 char command[PROP_VALUE_MAX];
630 int res;
631 int len = 0;
632 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800633 const char *reboot_target;
Yusuke Sato0df08272015-07-08 14:57:07 -0700634 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700635
636 res = expand_props(command, args[1], sizeof(command));
637 if (res) {
638 ERROR("powerctl: cannot expand '%s'\n", args[1]);
639 return -EINVAL;
640 }
641
642 if (strncmp(command, "shutdown", 8) == 0) {
643 cmd = ANDROID_RB_POWEROFF;
644 len = 8;
Yusuke Sato0df08272015-07-08 14:57:07 -0700645 callback_on_ro_remount = unmount_and_fsck;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646 } else if (strncmp(command, "reboot", 6) == 0) {
647 cmd = ANDROID_RB_RESTART2;
648 len = 6;
649 } else {
650 ERROR("powerctl: unrecognized command '%s'\n", command);
651 return -EINVAL;
652 }
653
654 if (command[len] == ',') {
655 reboot_target = &command[len + 1];
656 } else if (command[len] == '\0') {
657 reboot_target = "";
658 } else {
659 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
660 return -EINVAL;
661 }
662
Yusuke Sato0df08272015-07-08 14:57:07 -0700663 return android_reboot_with_callback(cmd, 0, reboot_target,
664 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700665}
666
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700667int do_trigger(int nargs, char **args)
668{
Mark Salyzyndd0e3162015-05-27 10:46:54 -0700669 char prop_val[PROP_VALUE_MAX];
670 int res = expand_props(prop_val, args[1], sizeof(prop_val));
671 if (res) {
672 ERROR("trigger: cannot expand '%s'\n", args[1]);
673 return -EINVAL;
674 }
675 action_for_each_trigger(prop_val, action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700676 return 0;
677}
678
679int do_symlink(int nargs, char **args)
680{
681 return symlink(args[1], args[2]);
682}
683
Ken Sumrall203bad52011-01-18 17:37:41 -0800684int do_rm(int nargs, char **args)
685{
686 return unlink(args[1]);
687}
688
689int do_rmdir(int nargs, char **args)
690{
691 return rmdir(args[1]);
692}
693
The Android Open Source Project35237d12008-12-17 18:08:08 -0800694int do_sysclktz(int nargs, char **args)
695{
696 struct timezone tz;
697
698 if (nargs != 2)
699 return -1;
700
701 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800702 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800703 if (settimeofday(NULL, &tz))
704 return -1;
705 return 0;
706}
707
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000708int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700709 int mode = -1;
710 int rc = fs_mgr_load_verity_state(&mode);
711 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
712 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000713 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700714 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000715}
716
Sami Tolvanen45474232015-03-30 11:38:38 +0100717static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
718 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
719 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000720}
721
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700722int do_verity_update_state(int nargs, char** args) {
723 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000724}
725
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726int do_write(int nargs, char **args)
727{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700728 const char *path = args[1];
729 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700730
Johan Redestig7e952f42014-12-05 00:36:41 +0100731 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800732 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800733 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
734 return -EINVAL;
735 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800736 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700737}
738
San Mehat7c44fe52009-08-26 16:39:25 -0700739int do_copy(int nargs, char **args)
740{
741 char *buffer = NULL;
742 int rc = 0;
743 int fd1 = -1, fd2 = -1;
744 struct stat info;
745 int brtw, brtr;
746 char *p;
747
748 if (nargs != 3)
749 return -1;
750
Elliott Hughes24627902015-02-04 10:25:09 -0800751 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700752 return -1;
753
Nick Kralevich45a884f2015-02-02 14:37:22 -0800754 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700755 goto out_err;
756
Nick Kralevich45a884f2015-02-02 14:37:22 -0800757 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700758 goto out_err;
759
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800760 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700761 goto out_err;
762
763 p = buffer;
764 brtr = info.st_size;
765 while(brtr) {
766 rc = read(fd1, p, brtr);
767 if (rc < 0)
768 goto out_err;
769 if (rc == 0)
770 break;
771 p += rc;
772 brtr -= rc;
773 }
774
775 p = buffer;
776 brtw = info.st_size;
777 while(brtw) {
778 rc = write(fd2, p, brtw);
779 if (rc < 0)
780 goto out_err;
781 if (rc == 0)
782 break;
783 p += rc;
784 brtw -= rc;
785 }
786
787 rc = 0;
788 goto out;
789out_err:
790 rc = -1;
791out:
792 if (buffer)
793 free(buffer);
794 if (fd1 >= 0)
795 close(fd1);
796 if (fd2 >= 0)
797 close(fd2);
798 return rc;
799}
800
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700801int do_chown(int nargs, char **args) {
802 /* GID is optional. */
803 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800804 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805 return -errno;
806 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800807 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808 return -errno;
809 } else {
810 return -1;
811 }
812 return 0;
813}
814
815static mode_t get_mode(const char *s) {
816 mode_t mode = 0;
817 while (*s) {
818 if (*s >= '0' && *s <= '7') {
819 mode = (mode<<3) | (*s-'0');
820 } else {
821 return -1;
822 }
823 s++;
824 }
825 return mode;
826}
827
828int do_chmod(int nargs, char **args) {
829 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800830 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 return -errno;
832 }
833 return 0;
834}
835
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500836int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500837 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400838 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500839
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500840 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400841 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400842 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500843 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400844 return ret;
845}
846
847int do_restorecon_recursive(int nargs, char **args) {
848 int i;
849 int ret = 0;
850
851 for (i = 1; i < nargs; i++) {
852 if (restorecon_recursive(args[i]) < 0)
853 ret = -errno;
854 }
855 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500856}
857
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700859 int log_level;
860 char log_level_str[PROP_VALUE_MAX] = "";
861 if (nargs != 2) {
862 ERROR("loglevel: missing argument\n");
863 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700865
866 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
867 ERROR("loglevel: cannot expand '%s'\n", args[1]);
868 return -EINVAL;
869 }
870 log_level = atoi(log_level_str);
871 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
872 ERROR("loglevel: invalid log level'%d'\n", log_level);
873 return -EINVAL;
874 }
875 klog_set_level(log_level);
876 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700877}
878
Ken Sumrallc5c51032011-03-08 17:01:29 -0800879int do_load_persist_props(int nargs, char **args) {
880 if (nargs == 1) {
881 load_persist_props();
882 return 0;
883 }
884 return -1;
885}
886
Riley Andrewse4b7b292014-06-16 15:06:21 -0700887int do_load_all_props(int nargs, char **args) {
888 if (nargs == 1) {
889 load_all_props();
890 return 0;
891 }
892 return -1;
893}
894
Colin Crosscd0f1732010-04-19 17:10:24 -0700895int do_wait(int nargs, char **args)
896{
897 if (nargs == 2) {
898 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800899 } else if (nargs == 3) {
900 return wait_for_file(args[1], atoi(args[2]));
901 } else
902 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700903}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000904
Paul Lawrence806d10b2015-04-28 22:07:10 +0000905/*
906 * Callback to make a directory from the ext4 code
907 */
908static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000909{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000910 if (make_dir(dir, 0700) && errno != EEXIST) {
911 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000912 }
913
Paul Lawrence806d10b2015-04-28 22:07:10 +0000914 return 0;
915}
916
917int do_installkey(int nargs, char **args)
918{
919 if (nargs != 2) {
920 return -1;
921 }
922
923 char prop_value[PROP_VALUE_MAX] = {0};
924 property_get("ro.crypto.type", prop_value);
925 if (strcmp(prop_value, "file")) {
926 return 0;
927 }
928
929 return e4crypt_create_device_key(args[1],
930 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000931}