blob: 4bc3b875e62fb5ba84481b87feff1df9d3295000 [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
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046#include "action.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "init.h"
48#include "keywords.h"
49#include "property_service.h"
50#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070051#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070052#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Nick Kralevichbc609542015-01-31 21:39:46 -080055#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070056#define UNMOUNT_CHECK_MS 5000
57#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080058
James Morrissey381341f2014-05-16 11:36:36 +010059int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Elliott Hughesf3cf4382015-02-03 17:12:07 -080061// System call provided by bionic but not in any header file.
62extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
The Android Open Source Project35237d12008-12-17 18:08:08 -080064static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065{
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070066 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070067 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080069 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Elliott Hughesf682b472015-02-06 12:19:48 -080071 // TODO: use finit_module for >= 3.8 kernels.
72 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073}
74
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static int __ifupdown(const char *interface, int up)
76{
77 struct ifreq ifr;
78 int s, ret;
79
80 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
81
82 s = socket(AF_INET, SOCK_DGRAM, 0);
83 if (s < 0)
84 return -1;
85
86 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
87 if (ret < 0) {
88 goto done;
89 }
90
91 if (up)
92 ifr.ifr_flags |= IFF_UP;
93 else
94 ifr.ifr_flags &= ~IFF_UP;
95
96 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080097
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098done:
99 close(s);
100 return ret;
101}
102
103static void service_start_if_not_disabled(struct service *svc)
104{
105 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700106 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700107 } else {
108 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 }
110}
111
Yusuke Sato0df08272015-07-08 14:57:07 -0700112static void unmount_and_fsck(const struct mntent *entry)
113{
114 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
115 return;
116
117 /* First, lazily unmount the directory. This unmount request finishes when
118 * all processes that open a file or directory in |entry->mnt_dir| exit.
119 */
120 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
121
122 /* Next, kill all processes except init, kthreadd, and kthreadd's
123 * children to finish the lazy unmount. Killing all processes here is okay
124 * because this callback function is only called right before reboot().
125 * It might be cleaner to selectively kill processes that actually use
126 * |entry->mnt_dir| rather than killing all, probably by reusing a function
127 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
128 * not allow init to scan /proc/<pid> files which the utility function
129 * heavily relies on. The policy does not allow the process to execute
130 * killall/pkill binaries either. Note that some processes might
131 * automatically restart after kill(), but that is not really a problem
132 * because |entry->mnt_dir| is no longer visible to such new processes.
133 */
134 service_for_each(service_stop);
135 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
136
137 int count = 0;
138 while (count++ < UNMOUNT_CHECK_TIMES) {
139 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
140 if (fd >= 0) {
141 /* |entry->mnt_dir| has sucessfully been unmounted. */
142 close(fd);
143 break;
144 } else if (errno == EBUSY) {
145 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
146 * while then retry.
147 */
148 TEMP_FAILURE_RETRY(
149 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
150 continue;
151 } else {
152 /* Cannot open the device. Give up. */
153 return;
154 }
155 }
156
157 int st;
158 if (!strcmp(entry->mnt_type, "f2fs")) {
159 const char *f2fs_argv[] = {
160 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
161 };
162 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
163 &st, true, LOG_KLOG, true, NULL);
164 } else if (!strcmp(entry->mnt_type, "ext4")) {
165 const char *ext4_argv[] = {
166 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
167 };
168 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
169 &st, true, LOG_KLOG, true, NULL);
170 }
171}
172
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173int do_class_start(int nargs, char **args)
174{
175 /* Starting a class does not start services
176 * which are explicitly disabled. They must
177 * be started individually.
178 */
179 service_for_each_class(args[1], service_start_if_not_disabled);
180 return 0;
181}
182
183int do_class_stop(int nargs, char **args)
184{
185 service_for_each_class(args[1], service_stop);
186 return 0;
187}
188
Ken Sumrall752923c2010-12-03 16:33:31 -0800189int do_class_reset(int nargs, char **args)
190{
191 service_for_each_class(args[1], service_reset);
192 return 0;
193}
194
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195int do_domainname(int nargs, char **args)
196{
197 return write_file("/proc/sys/kernel/domainname", args[1]);
198}
199
JP Abgrall3beec7e2014-05-02 21:14:29 -0700200int do_enable(int nargs, char **args)
201{
202 struct service *svc;
203 svc = service_find_by_name(args[1]);
204 if (svc) {
205 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
206 if (svc->flags & SVC_DISABLED_START) {
207 service_start(svc, NULL);
208 }
209 } else {
210 return -1;
211 }
212 return 0;
213}
214
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800215int do_exec(int nargs, char** args) {
216 service* svc = make_exec_oneshot_service(nargs, args);
217 if (svc == NULL) {
218 return -1;
219 }
220 service_start(svc, NULL);
221 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
224int do_export(int nargs, char **args)
225{
James Morrissey381341f2014-05-16 11:36:36 +0100226 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
229int do_hostname(int nargs, char **args)
230{
231 return write_file("/proc/sys/kernel/hostname", args[1]);
232}
233
234int do_ifup(int nargs, char **args)
235{
236 return __ifupdown(args[1], 1);
237}
238
The Android Open Source Project35237d12008-12-17 18:08:08 -0800239
240static int do_insmod_inner(int nargs, char **args, int opt_len)
241{
242 char options[opt_len + 1];
243 int i;
244
245 options[0] = '\0';
246 if (nargs > 2) {
247 strcpy(options, args[2]);
248 for (i = 3; i < nargs; ++i) {
249 strcat(options, " ");
250 strcat(options, args[i]);
251 }
252 }
253
254 return insmod(args[1], options);
255}
256
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257int do_insmod(int nargs, char **args)
258{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800259 int i;
260 int size = 0;
261
262 if (nargs > 2) {
263 for (i = 2; i < nargs; ++i)
264 size += strlen(args[i]) + 1;
265 }
266
267 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268}
269
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270int do_mkdir(int nargs, char **args)
271{
272 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700273 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274
275 /* mkdir <path> [mode] [owner] [group] */
276
277 if (nargs >= 3) {
278 mode = strtoul(args[2], 0, 8);
279 }
280
Stephen Smalleye096e362012-06-11 13:37:39 -0400281 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700282 /* chmod in case the directory already exists */
283 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800284 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700285 }
286 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287 return -errno;
288 }
289
290 if (nargs >= 4) {
291 uid_t uid = decode_uid(args[3]);
292 gid_t gid = -1;
293
294 if (nargs == 5) {
295 gid = decode_uid(args[4]);
296 }
297
Nick Kralevichbc609542015-01-31 21:39:46 -0800298 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 return -errno;
300 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700301
302 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
303 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800304 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700305 if (ret == -1) {
306 return -errno;
307 }
308 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 }
310
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000311 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312}
313
314static struct {
315 const char *name;
316 unsigned flag;
317} mount_flags[] = {
318 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200319 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 { "nosuid", MS_NOSUID },
321 { "nodev", MS_NODEV },
322 { "nodiratime", MS_NODIRATIME },
323 { "ro", MS_RDONLY },
324 { "rw", 0 },
325 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700326 { "bind", MS_BIND },
327 { "rec", MS_REC },
328 { "unbindable", MS_UNBINDABLE },
329 { "private", MS_PRIVATE },
330 { "slave", MS_SLAVE },
331 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332 { "defaults", 0 },
333 { 0, 0 },
334};
335
Ken Sumrall752923c2010-12-03 16:33:31 -0800336#define DATA_MNT_POINT "/data"
337
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338/* mount <type> <device> <path> <flags ...> <options> */
339int do_mount(int nargs, char **args)
340{
341 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000342 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343 char *options = NULL;
344 unsigned flags = 0;
345 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700346 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347
348 for (n = 4; n < nargs; n++) {
349 for (i = 0; mount_flags[i].name; i++) {
350 if (!strcmp(args[n], mount_flags[i].name)) {
351 flags |= mount_flags[i].flag;
352 break;
353 }
354 }
355
Colin Crosscd0f1732010-04-19 17:10:24 -0700356 if (!mount_flags[i].name) {
357 if (!strcmp(args[n], "wait"))
358 wait = 1;
359 /* if our last argument isn't a flag, wolf it up as an option string */
360 else if (n + 1 == nargs)
361 options = args[n];
362 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 }
364
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000365 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 target = args[3];
368
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369 if (!strncmp(source, "mtd@", 4)) {
370 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000371 if (n < 0) {
372 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000374
Yabin Cuie2d63af2015-02-17 19:27:51 -0800375 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000376
Colin Crosscd0f1732010-04-19 17:10:24 -0700377 if (wait)
378 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000379 if (mount(tmp, target, system, flags, options) < 0) {
380 return -1;
381 }
382
Ken Sumralldd4d7862011-02-17 18:09:47 -0800383 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 } else if (!strncmp(source, "loop@", 5)) {
385 int mode, loop, fd;
386 struct loop_info info;
387
388 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800389 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000390 if (fd < 0) {
391 return -1;
392 }
393
394 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800395 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800396 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000397 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100398 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000399 return -1;
400 }
401
402 /* if it is a blank loop device */
403 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
404 /* if it becomes our loop device */
405 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
406 close(fd);
407
408 if (mount(tmp, target, system, flags, options) < 0) {
409 ioctl(loop, LOOP_CLR_FD, 0);
410 close(loop);
411 return -1;
412 }
413
414 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800415 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000416 }
417 }
418
419 close(loop);
420 }
421
422 close(fd);
423 ERROR("out of loopback devices");
424 return -1;
425 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700426 if (wait)
427 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000428 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700429 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 }
431
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800433
434exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800435 return 0;
436
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437}
438
JP Abgrallcee20682014-07-02 14:26:54 -0700439static int wipe_data_via_recovery()
440{
441 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800442 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700443 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700444 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
445 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700446 close(fd);
447 } else {
448 ERROR("could not open /cache/recovery/command\n");
449 return -1;
450 }
451 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
452 while (1) { pause(); } // never reached
453}
454
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000455/*
JP Abgrallcee20682014-07-02 14:26:54 -0700456 * This function might request a reboot, in which case it will
457 * not return.
458 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700459int do_mount_all(int nargs, char **args)
460{
461 pid_t pid;
462 int ret = -1;
463 int child_ret = -1;
464 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800465 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700466
467 if (nargs != 2) {
468 return -1;
469 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700470 const char* fstabfile = args[1];
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700471 /*
472 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
473 * do the call in the child to provide protection to the main init
474 * process if anything goes wrong (crash or memory leak), and wait for
475 * the child to finish in the parent.
476 */
477 pid = fork();
478 if (pid > 0) {
479 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700480 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
481 if (wp_ret < 0) {
482 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700483 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700484 }
485
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486 if (WIFEXITED(status)) {
487 ret = WEXITSTATUS(status);
488 } else {
489 ret = -1;
490 }
491 } else if (pid == 0) {
492 /* child, call fs_mgr_mount_all() */
493 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800494 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800495 child_ret = fs_mgr_mount_all(fstab);
496 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700497 if (child_ret == -1) {
498 ERROR("fs_mgr_mount_all returned an error\n");
499 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700500 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501 } else {
502 /* fork failed, return an error */
503 return -1;
504 }
505
JP Abgrallf22b7452014-07-02 13:16:04 -0700506 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800507 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700508 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700509 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000510 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800511 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700512 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700513 property_set("ro.crypto.state", "unencrypted");
514 /* If fs_mgr determined this is an unencrypted device, then trigger
515 * that action.
516 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700517 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700518 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
519 /* Setup a wipe via recovery, and reboot into recovery */
520 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
521 ret = wipe_data_via_recovery();
522 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000523 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000524 if (e4crypt_install_keyring()) {
525 return -1;
526 }
527 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000528 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000529
530 // Although encrypted, we have device key, so we do not need to
531 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700532 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000533 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
534 if (e4crypt_install_keyring()) {
535 return -1;
536 }
537 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000538 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000539 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700540 } else if (ret > 0) {
541 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700542 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700543 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700544
545 return ret;
546}
547
Ken Sumralla76baaa2013-07-09 18:42:09 -0700548int do_swapon_all(int nargs, char **args)
549{
550 struct fstab *fstab;
551 int ret;
552
553 fstab = fs_mgr_read_fstab(args[1]);
554 ret = fs_mgr_swapon_all(fstab);
555 fs_mgr_free_fstab(fstab);
556
557 return ret;
558}
559
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700560int do_setprop(int nargs, char **args)
561{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700562 const char *name = args[1];
563 const char *value = args[2];
Yabin Cui00ede7d2015-07-24 13:26:04 -0700564 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565 return 0;
566}
567
568int do_setrlimit(int nargs, char **args)
569{
570 struct rlimit limit;
571 int resource;
572 resource = atoi(args[1]);
573 limit.rlim_cur = atoi(args[2]);
574 limit.rlim_max = atoi(args[3]);
575 return setrlimit(resource, &limit);
576}
577
578int do_start(int nargs, char **args)
579{
580 struct service *svc;
581 svc = service_find_by_name(args[1]);
582 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700583 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700584 }
585 return 0;
586}
587
588int do_stop(int nargs, char **args)
589{
590 struct service *svc;
591 svc = service_find_by_name(args[1]);
592 if (svc) {
593 service_stop(svc);
594 }
595 return 0;
596}
597
598int do_restart(int nargs, char **args)
599{
600 struct service *svc;
601 svc = service_find_by_name(args[1]);
602 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500603 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700604 }
605 return 0;
606}
607
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700608int do_powerctl(int nargs, char **args)
609{
Yabin Cui00ede7d2015-07-24 13:26:04 -0700610 const char* command = args[1];
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700611 int len = 0;
612 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800613 const char *reboot_target;
Yusuke Sato0df08272015-07-08 14:57:07 -0700614 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700615
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700616 if (strncmp(command, "shutdown", 8) == 0) {
617 cmd = ANDROID_RB_POWEROFF;
618 len = 8;
Yusuke Sato0df08272015-07-08 14:57:07 -0700619 callback_on_ro_remount = unmount_and_fsck;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700620 } else if (strncmp(command, "reboot", 6) == 0) {
621 cmd = ANDROID_RB_RESTART2;
622 len = 6;
623 } else {
624 ERROR("powerctl: unrecognized command '%s'\n", command);
625 return -EINVAL;
626 }
627
628 if (command[len] == ',') {
629 reboot_target = &command[len + 1];
630 } else if (command[len] == '\0') {
631 reboot_target = "";
632 } else {
633 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
634 return -EINVAL;
635 }
636
Yusuke Sato0df08272015-07-08 14:57:07 -0700637 return android_reboot_with_callback(cmd, 0, reboot_target,
638 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700639}
640
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641int do_trigger(int nargs, char **args)
642{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700643 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700644 return 0;
645}
646
647int do_symlink(int nargs, char **args)
648{
649 return symlink(args[1], args[2]);
650}
651
Ken Sumrall203bad52011-01-18 17:37:41 -0800652int do_rm(int nargs, char **args)
653{
654 return unlink(args[1]);
655}
656
657int do_rmdir(int nargs, char **args)
658{
659 return rmdir(args[1]);
660}
661
The Android Open Source Project35237d12008-12-17 18:08:08 -0800662int do_sysclktz(int nargs, char **args)
663{
664 struct timezone tz;
665
666 if (nargs != 2)
667 return -1;
668
669 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800670 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800671 if (settimeofday(NULL, &tz))
672 return -1;
673 return 0;
674}
675
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000676int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700677 int mode = -1;
678 int rc = fs_mgr_load_verity_state(&mode);
679 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700680 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000681 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700682 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000683}
684
Sami Tolvanen45474232015-03-30 11:38:38 +0100685static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
686 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
687 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000688}
689
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700690int do_verity_update_state(int nargs, char** args) {
691 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000692}
693
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700694int do_write(int nargs, char **args)
695{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700696 const char *path = args[1];
697 const char *value = args[2];
Yabin Cui00ede7d2015-07-24 13:26:04 -0700698 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700699}
700
San Mehat7c44fe52009-08-26 16:39:25 -0700701int do_copy(int nargs, char **args)
702{
703 char *buffer = NULL;
704 int rc = 0;
705 int fd1 = -1, fd2 = -1;
706 struct stat info;
707 int brtw, brtr;
708 char *p;
709
710 if (nargs != 3)
711 return -1;
712
Elliott Hughes24627902015-02-04 10:25:09 -0800713 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700714 return -1;
715
Nick Kralevich45a884f2015-02-02 14:37:22 -0800716 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700717 goto out_err;
718
Nick Kralevich45a884f2015-02-02 14:37:22 -0800719 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700720 goto out_err;
721
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800722 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700723 goto out_err;
724
725 p = buffer;
726 brtr = info.st_size;
727 while(brtr) {
728 rc = read(fd1, p, brtr);
729 if (rc < 0)
730 goto out_err;
731 if (rc == 0)
732 break;
733 p += rc;
734 brtr -= rc;
735 }
736
737 p = buffer;
738 brtw = info.st_size;
739 while(brtw) {
740 rc = write(fd2, p, brtw);
741 if (rc < 0)
742 goto out_err;
743 if (rc == 0)
744 break;
745 p += rc;
746 brtw -= rc;
747 }
748
749 rc = 0;
750 goto out;
751out_err:
752 rc = -1;
753out:
754 if (buffer)
755 free(buffer);
756 if (fd1 >= 0)
757 close(fd1);
758 if (fd2 >= 0)
759 close(fd2);
760 return rc;
761}
762
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763int do_chown(int nargs, char **args) {
764 /* GID is optional. */
765 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800766 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767 return -errno;
768 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800769 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770 return -errno;
771 } else {
772 return -1;
773 }
774 return 0;
775}
776
777static mode_t get_mode(const char *s) {
778 mode_t mode = 0;
779 while (*s) {
780 if (*s >= '0' && *s <= '7') {
781 mode = (mode<<3) | (*s-'0');
782 } else {
783 return -1;
784 }
785 s++;
786 }
787 return mode;
788}
789
790int do_chmod(int nargs, char **args) {
791 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800792 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700793 return -errno;
794 }
795 return 0;
796}
797
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500798int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500799 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400800 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500801
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500802 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400803 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400804 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500805 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400806 return ret;
807}
808
809int do_restorecon_recursive(int nargs, char **args) {
810 int i;
811 int ret = 0;
812
813 for (i = 1; i < nargs; i++) {
814 if (restorecon_recursive(args[i]) < 0)
815 ret = -errno;
816 }
817 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500818}
819
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700821 if (nargs != 2) {
822 ERROR("loglevel: missing argument\n");
823 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700825
Yabin Cui00ede7d2015-07-24 13:26:04 -0700826 int log_level = atoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700827 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
828 ERROR("loglevel: invalid log level'%d'\n", log_level);
829 return -EINVAL;
830 }
831 klog_set_level(log_level);
832 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833}
834
Ken Sumrallc5c51032011-03-08 17:01:29 -0800835int do_load_persist_props(int nargs, char **args) {
836 if (nargs == 1) {
837 load_persist_props();
838 return 0;
839 }
840 return -1;
841}
842
Riley Andrewse4b7b292014-06-16 15:06:21 -0700843int do_load_all_props(int nargs, char **args) {
844 if (nargs == 1) {
845 load_all_props();
846 return 0;
847 }
848 return -1;
849}
850
Colin Crosscd0f1732010-04-19 17:10:24 -0700851int do_wait(int nargs, char **args)
852{
853 if (nargs == 2) {
854 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800855 } else if (nargs == 3) {
856 return wait_for_file(args[1], atoi(args[2]));
857 } else
858 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700859}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000860
Paul Lawrence806d10b2015-04-28 22:07:10 +0000861/*
862 * Callback to make a directory from the ext4 code
863 */
864static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000865{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000866 if (make_dir(dir, 0700) && errno != EEXIST) {
867 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000868 }
869
Paul Lawrence806d10b2015-04-28 22:07:10 +0000870 return 0;
871}
872
873int do_installkey(int nargs, char **args)
874{
875 if (nargs != 2) {
876 return -1;
877 }
878
Yabin Cui74edcea2015-07-24 10:11:05 -0700879 std::string prop_value = property_get("ro.crypto.type");
880 if (prop_value != "file") {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000881 return 0;
882 }
883
884 return e4crypt_create_device_key(args[1],
885 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000886}