blob: 87e56bc4ef45ef0737453d140fce7f330afe9047 [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{
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070065 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070066 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080068 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
Elliott Hughesf682b472015-02-06 12:19:48 -080070 // TODO: use finit_module for >= 3.8 kernels.
71 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072}
73
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074static int __ifupdown(const char *interface, int up)
75{
76 struct ifreq ifr;
77 int s, ret;
78
79 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
80
81 s = socket(AF_INET, SOCK_DGRAM, 0);
82 if (s < 0)
83 return -1;
84
85 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
86 if (ret < 0) {
87 goto done;
88 }
89
90 if (up)
91 ifr.ifr_flags |= IFF_UP;
92 else
93 ifr.ifr_flags &= ~IFF_UP;
94
95 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080096
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097done:
98 close(s);
99 return ret;
100}
101
102static void service_start_if_not_disabled(struct service *svc)
103{
104 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700105 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700106 } else {
107 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108 }
109}
110
Yusuke Sato0df08272015-07-08 14:57:07 -0700111static void unmount_and_fsck(const struct mntent *entry)
112{
113 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
114 return;
115
116 /* First, lazily unmount the directory. This unmount request finishes when
117 * all processes that open a file or directory in |entry->mnt_dir| exit.
118 */
119 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
120
121 /* Next, kill all processes except init, kthreadd, and kthreadd's
122 * children to finish the lazy unmount. Killing all processes here is okay
123 * because this callback function is only called right before reboot().
124 * It might be cleaner to selectively kill processes that actually use
125 * |entry->mnt_dir| rather than killing all, probably by reusing a function
126 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
127 * not allow init to scan /proc/<pid> files which the utility function
128 * heavily relies on. The policy does not allow the process to execute
129 * killall/pkill binaries either. Note that some processes might
130 * automatically restart after kill(), but that is not really a problem
131 * because |entry->mnt_dir| is no longer visible to such new processes.
132 */
133 service_for_each(service_stop);
134 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
135
136 int count = 0;
137 while (count++ < UNMOUNT_CHECK_TIMES) {
138 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
139 if (fd >= 0) {
140 /* |entry->mnt_dir| has sucessfully been unmounted. */
141 close(fd);
142 break;
143 } else if (errno == EBUSY) {
144 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
145 * while then retry.
146 */
147 TEMP_FAILURE_RETRY(
148 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
149 continue;
150 } else {
151 /* Cannot open the device. Give up. */
152 return;
153 }
154 }
155
156 int st;
157 if (!strcmp(entry->mnt_type, "f2fs")) {
158 const char *f2fs_argv[] = {
159 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
160 };
161 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
162 &st, true, LOG_KLOG, true, NULL);
163 } else if (!strcmp(entry->mnt_type, "ext4")) {
164 const char *ext4_argv[] = {
165 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
166 };
167 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
168 &st, true, LOG_KLOG, true, NULL);
169 }
170}
171
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172int do_class_start(int nargs, char **args)
173{
174 /* Starting a class does not start services
175 * which are explicitly disabled. They must
176 * be started individually.
177 */
178 service_for_each_class(args[1], service_start_if_not_disabled);
179 return 0;
180}
181
182int do_class_stop(int nargs, char **args)
183{
184 service_for_each_class(args[1], service_stop);
185 return 0;
186}
187
Ken Sumrall752923c2010-12-03 16:33:31 -0800188int do_class_reset(int nargs, char **args)
189{
190 service_for_each_class(args[1], service_reset);
191 return 0;
192}
193
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194int do_domainname(int nargs, char **args)
195{
196 return write_file("/proc/sys/kernel/domainname", args[1]);
197}
198
JP Abgrall3beec7e2014-05-02 21:14:29 -0700199int do_enable(int nargs, char **args)
200{
201 struct service *svc;
202 svc = service_find_by_name(args[1]);
203 if (svc) {
204 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
205 if (svc->flags & SVC_DISABLED_START) {
206 service_start(svc, NULL);
207 }
208 } else {
209 return -1;
210 }
211 return 0;
212}
213
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800214int do_exec(int nargs, char** args) {
215 service* svc = make_exec_oneshot_service(nargs, args);
216 if (svc == NULL) {
217 return -1;
218 }
219 service_start(svc, NULL);
220 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221}
222
223int do_export(int nargs, char **args)
224{
James Morrissey381341f2014-05-16 11:36:36 +0100225 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226}
227
228int do_hostname(int nargs, char **args)
229{
230 return write_file("/proc/sys/kernel/hostname", args[1]);
231}
232
233int do_ifup(int nargs, char **args)
234{
235 return __ifupdown(args[1], 1);
236}
237
The Android Open Source Project35237d12008-12-17 18:08:08 -0800238
239static int do_insmod_inner(int nargs, char **args, int opt_len)
240{
241 char options[opt_len + 1];
242 int i;
243
244 options[0] = '\0';
245 if (nargs > 2) {
246 strcpy(options, args[2]);
247 for (i = 3; i < nargs; ++i) {
248 strcat(options, " ");
249 strcat(options, args[i]);
250 }
251 }
252
253 return insmod(args[1], options);
254}
255
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256int do_insmod(int nargs, char **args)
257{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800258 int i;
259 int size = 0;
260
261 if (nargs > 2) {
262 for (i = 2; i < nargs; ++i)
263 size += strlen(args[i]) + 1;
264 }
265
266 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267}
268
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269int do_mkdir(int nargs, char **args)
270{
271 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700272 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273
274 /* mkdir <path> [mode] [owner] [group] */
275
276 if (nargs >= 3) {
277 mode = strtoul(args[2], 0, 8);
278 }
279
Stephen Smalleye096e362012-06-11 13:37:39 -0400280 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700281 /* chmod in case the directory already exists */
282 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800283 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700284 }
285 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 return -errno;
287 }
288
289 if (nargs >= 4) {
290 uid_t uid = decode_uid(args[3]);
291 gid_t gid = -1;
292
293 if (nargs == 5) {
294 gid = decode_uid(args[4]);
295 }
296
Nick Kralevichbc609542015-01-31 21:39:46 -0800297 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 return -errno;
299 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700300
301 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
302 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800303 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700304 if (ret == -1) {
305 return -errno;
306 }
307 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 }
309
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000310 return e4crypt_set_directory_policy(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311}
312
313static struct {
314 const char *name;
315 unsigned flag;
316} mount_flags[] = {
317 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200318 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 { "nosuid", MS_NOSUID },
320 { "nodev", MS_NODEV },
321 { "nodiratime", MS_NODIRATIME },
322 { "ro", MS_RDONLY },
323 { "rw", 0 },
324 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700325 { "bind", MS_BIND },
326 { "rec", MS_REC },
327 { "unbindable", MS_UNBINDABLE },
328 { "private", MS_PRIVATE },
329 { "slave", MS_SLAVE },
330 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700331 { "defaults", 0 },
332 { 0, 0 },
333};
334
Ken Sumrall752923c2010-12-03 16:33:31 -0800335#define DATA_MNT_POINT "/data"
336
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337/* mount <type> <device> <path> <flags ...> <options> */
338int do_mount(int nargs, char **args)
339{
340 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000341 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 char *options = NULL;
343 unsigned flags = 0;
344 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700345 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346
347 for (n = 4; n < nargs; n++) {
348 for (i = 0; mount_flags[i].name; i++) {
349 if (!strcmp(args[n], mount_flags[i].name)) {
350 flags |= mount_flags[i].flag;
351 break;
352 }
353 }
354
Colin Crosscd0f1732010-04-19 17:10:24 -0700355 if (!mount_flags[i].name) {
356 if (!strcmp(args[n], "wait"))
357 wait = 1;
358 /* if our last argument isn't a flag, wolf it up as an option string */
359 else if (n + 1 == nargs)
360 options = args[n];
361 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 }
363
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 target = args[3];
367
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368 if (!strncmp(source, "mtd@", 4)) {
369 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370 if (n < 0) {
371 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373
Yabin Cuie2d63af2015-02-17 19:27:51 -0800374 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000375
Colin Crosscd0f1732010-04-19 17:10:24 -0700376 if (wait)
377 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000378 if (mount(tmp, target, system, flags, options) < 0) {
379 return -1;
380 }
381
Ken Sumralldd4d7862011-02-17 18:09:47 -0800382 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000383 } else if (!strncmp(source, "loop@", 5)) {
384 int mode, loop, fd;
385 struct loop_info info;
386
387 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800388 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389 if (fd < 0) {
390 return -1;
391 }
392
393 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800394 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800395 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000396 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100397 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398 return -1;
399 }
400
401 /* if it is a blank loop device */
402 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
403 /* if it becomes our loop device */
404 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
405 close(fd);
406
407 if (mount(tmp, target, system, flags, options) < 0) {
408 ioctl(loop, LOOP_CLR_FD, 0);
409 close(loop);
410 return -1;
411 }
412
413 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800414 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415 }
416 }
417
418 close(loop);
419 }
420
421 close(fd);
422 ERROR("out of loopback devices");
423 return -1;
424 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700425 if (wait)
426 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000427 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700428 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000429 }
430
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800432
433exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800434 return 0;
435
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700436}
437
JP Abgrallcee20682014-07-02 14:26:54 -0700438static int wipe_data_via_recovery()
439{
440 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800441 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700442 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700443 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
444 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700445 close(fd);
446 } else {
447 ERROR("could not open /cache/recovery/command\n");
448 return -1;
449 }
450 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
451 while (1) { pause(); } // never reached
452}
453
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000454/*
JP Abgrallcee20682014-07-02 14:26:54 -0700455 * This function might request a reboot, in which case it will
456 * not return.
457 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700458int do_mount_all(int nargs, char **args)
459{
460 pid_t pid;
461 int ret = -1;
462 int child_ret = -1;
463 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800464 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700465
466 if (nargs != 2) {
467 return -1;
468 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700469 const char* fstabfile = args[1];
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700470 /*
471 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
472 * do the call in the child to provide protection to the main init
473 * process if anything goes wrong (crash or memory leak), and wait for
474 * the child to finish in the parent.
475 */
476 pid = fork();
477 if (pid > 0) {
478 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700479 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
480 if (wp_ret < 0) {
481 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700482 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700483 }
484
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700485 if (WIFEXITED(status)) {
486 ret = WEXITSTATUS(status);
487 } else {
488 ret = -1;
489 }
490 } else if (pid == 0) {
491 /* child, call fs_mgr_mount_all() */
492 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800493 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800494 child_ret = fs_mgr_mount_all(fstab);
495 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700496 if (child_ret == -1) {
497 ERROR("fs_mgr_mount_all returned an error\n");
498 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700499 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500 } else {
501 /* fork failed, return an error */
502 return -1;
503 }
504
JP Abgrallf22b7452014-07-02 13:16:04 -0700505 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800506 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700507 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000509 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800510 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700511 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700512 property_set("ro.crypto.state", "unencrypted");
513 /* If fs_mgr determined this is an unencrypted device, then trigger
514 * that action.
515 */
516 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700517 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
518 /* Setup a wipe via recovery, and reboot into recovery */
519 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
520 ret = wipe_data_via_recovery();
521 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000522 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000523 if (e4crypt_install_keyring()) {
524 return -1;
525 }
526 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000527 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000528
529 // Although encrypted, we have device key, so we do not need to
530 // do anything different from the nonencrypted case.
531 action_for_each_trigger("nonencrypted", action_add_queue_tail);
532 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
533 if (e4crypt_install_keyring()) {
534 return -1;
535 }
536 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000537 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000538 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700539 } else if (ret > 0) {
540 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700541 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700542 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700543
544 return ret;
545}
546
Ken Sumralla76baaa2013-07-09 18:42:09 -0700547int do_swapon_all(int nargs, char **args)
548{
549 struct fstab *fstab;
550 int ret;
551
552 fstab = fs_mgr_read_fstab(args[1]);
553 ret = fs_mgr_swapon_all(fstab);
554 fs_mgr_free_fstab(fstab);
555
556 return ret;
557}
558
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559int do_setprop(int nargs, char **args)
560{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700561 const char *name = args[1];
562 const char *value = args[2];
Yabin Cui00ede7d2015-07-24 13:26:04 -0700563 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700564 return 0;
565}
566
567int do_setrlimit(int nargs, char **args)
568{
569 struct rlimit limit;
570 int resource;
571 resource = atoi(args[1]);
572 limit.rlim_cur = atoi(args[2]);
573 limit.rlim_max = atoi(args[3]);
574 return setrlimit(resource, &limit);
575}
576
577int do_start(int nargs, char **args)
578{
579 struct service *svc;
580 svc = service_find_by_name(args[1]);
581 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700582 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583 }
584 return 0;
585}
586
587int do_stop(int nargs, char **args)
588{
589 struct service *svc;
590 svc = service_find_by_name(args[1]);
591 if (svc) {
592 service_stop(svc);
593 }
594 return 0;
595}
596
597int do_restart(int nargs, char **args)
598{
599 struct service *svc;
600 svc = service_find_by_name(args[1]);
601 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500602 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603 }
604 return 0;
605}
606
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700607int do_powerctl(int nargs, char **args)
608{
Yabin Cui00ede7d2015-07-24 13:26:04 -0700609 const char* command = args[1];
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700610 int len = 0;
611 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800612 const char *reboot_target;
Yusuke Sato0df08272015-07-08 14:57:07 -0700613 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700614
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700615 if (strncmp(command, "shutdown", 8) == 0) {
616 cmd = ANDROID_RB_POWEROFF;
617 len = 8;
Yusuke Sato0df08272015-07-08 14:57:07 -0700618 callback_on_ro_remount = unmount_and_fsck;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700619 } else if (strncmp(command, "reboot", 6) == 0) {
620 cmd = ANDROID_RB_RESTART2;
621 len = 6;
622 } else {
623 ERROR("powerctl: unrecognized command '%s'\n", command);
624 return -EINVAL;
625 }
626
627 if (command[len] == ',') {
628 reboot_target = &command[len + 1];
629 } else if (command[len] == '\0') {
630 reboot_target = "";
631 } else {
632 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
633 return -EINVAL;
634 }
635
Yusuke Sato0df08272015-07-08 14:57:07 -0700636 return android_reboot_with_callback(cmd, 0, reboot_target,
637 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700638}
639
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640int do_trigger(int nargs, char **args)
641{
Yabin Cui00ede7d2015-07-24 13:26:04 -0700642 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700643 return 0;
644}
645
646int do_symlink(int nargs, char **args)
647{
648 return symlink(args[1], args[2]);
649}
650
Ken Sumrall203bad52011-01-18 17:37:41 -0800651int do_rm(int nargs, char **args)
652{
653 return unlink(args[1]);
654}
655
656int do_rmdir(int nargs, char **args)
657{
658 return rmdir(args[1]);
659}
660
The Android Open Source Project35237d12008-12-17 18:08:08 -0800661int do_sysclktz(int nargs, char **args)
662{
663 struct timezone tz;
664
665 if (nargs != 2)
666 return -1;
667
668 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800669 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800670 if (settimeofday(NULL, &tz))
671 return -1;
672 return 0;
673}
674
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000675int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700676 int mode = -1;
677 int rc = fs_mgr_load_verity_state(&mode);
678 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
679 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000680 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700681 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000682}
683
Sami Tolvanen45474232015-03-30 11:38:38 +0100684static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
685 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
686 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000687}
688
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700689int do_verity_update_state(int nargs, char** args) {
690 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000691}
692
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700693int do_write(int nargs, char **args)
694{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700695 const char *path = args[1];
696 const char *value = args[2];
Yabin Cui00ede7d2015-07-24 13:26:04 -0700697 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700698}
699
San Mehat7c44fe52009-08-26 16:39:25 -0700700int do_copy(int nargs, char **args)
701{
702 char *buffer = NULL;
703 int rc = 0;
704 int fd1 = -1, fd2 = -1;
705 struct stat info;
706 int brtw, brtr;
707 char *p;
708
709 if (nargs != 3)
710 return -1;
711
Elliott Hughes24627902015-02-04 10:25:09 -0800712 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700713 return -1;
714
Nick Kralevich45a884f2015-02-02 14:37:22 -0800715 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700716 goto out_err;
717
Nick Kralevich45a884f2015-02-02 14:37:22 -0800718 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700719 goto out_err;
720
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800721 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700722 goto out_err;
723
724 p = buffer;
725 brtr = info.st_size;
726 while(brtr) {
727 rc = read(fd1, p, brtr);
728 if (rc < 0)
729 goto out_err;
730 if (rc == 0)
731 break;
732 p += rc;
733 brtr -= rc;
734 }
735
736 p = buffer;
737 brtw = info.st_size;
738 while(brtw) {
739 rc = write(fd2, p, brtw);
740 if (rc < 0)
741 goto out_err;
742 if (rc == 0)
743 break;
744 p += rc;
745 brtw -= rc;
746 }
747
748 rc = 0;
749 goto out;
750out_err:
751 rc = -1;
752out:
753 if (buffer)
754 free(buffer);
755 if (fd1 >= 0)
756 close(fd1);
757 if (fd2 >= 0)
758 close(fd2);
759 return rc;
760}
761
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700762int do_chown(int nargs, char **args) {
763 /* GID is optional. */
764 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800765 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766 return -errno;
767 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800768 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 return -errno;
770 } else {
771 return -1;
772 }
773 return 0;
774}
775
776static mode_t get_mode(const char *s) {
777 mode_t mode = 0;
778 while (*s) {
779 if (*s >= '0' && *s <= '7') {
780 mode = (mode<<3) | (*s-'0');
781 } else {
782 return -1;
783 }
784 s++;
785 }
786 return mode;
787}
788
789int do_chmod(int nargs, char **args) {
790 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800791 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792 return -errno;
793 }
794 return 0;
795}
796
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500797int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500798 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400799 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500801 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400802 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400803 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500804 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400805 return ret;
806}
807
808int do_restorecon_recursive(int nargs, char **args) {
809 int i;
810 int ret = 0;
811
812 for (i = 1; i < nargs; i++) {
813 if (restorecon_recursive(args[i]) < 0)
814 ret = -errno;
815 }
816 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500817}
818
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700820 if (nargs != 2) {
821 ERROR("loglevel: missing argument\n");
822 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700824
Yabin Cui00ede7d2015-07-24 13:26:04 -0700825 int log_level = atoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700826 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
827 ERROR("loglevel: invalid log level'%d'\n", log_level);
828 return -EINVAL;
829 }
830 klog_set_level(log_level);
831 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832}
833
Ken Sumrallc5c51032011-03-08 17:01:29 -0800834int do_load_persist_props(int nargs, char **args) {
835 if (nargs == 1) {
836 load_persist_props();
837 return 0;
838 }
839 return -1;
840}
841
Paul Lawrence948410a2015-07-01 14:40:56 -0700842int do_load_system_props(int nargs, char **args) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700843 if (nargs == 1) {
Paul Lawrence948410a2015-07-01 14:40:56 -0700844 load_system_props();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700845 return 0;
846 }
847 return -1;
848}
849
Colin Crosscd0f1732010-04-19 17:10:24 -0700850int do_wait(int nargs, char **args)
851{
852 if (nargs == 2) {
853 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800854 } else if (nargs == 3) {
855 return wait_for_file(args[1], atoi(args[2]));
856 } else
857 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700858}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000859
Paul Lawrence806d10b2015-04-28 22:07:10 +0000860/*
861 * Callback to make a directory from the ext4 code
862 */
863static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000864{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000865 if (make_dir(dir, 0700) && errno != EEXIST) {
866 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000867 }
868
Paul Lawrence806d10b2015-04-28 22:07:10 +0000869 return 0;
870}
871
Paul Crowley749af8c2015-05-28 17:35:06 +0100872static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700873 std::string value = property_get("ro.crypto.type");
874 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100875}
876
Paul Lawrence806d10b2015-04-28 22:07:10 +0000877int do_installkey(int nargs, char **args)
878{
879 if (nargs != 2) {
880 return -1;
881 }
Paul Crowley749af8c2015-05-28 17:35:06 +0100882 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000883 return 0;
884 }
Paul Lawrence806d10b2015-04-28 22:07:10 +0000885 return e4crypt_create_device_key(args[1],
886 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000887}
Paul Crowley749af8c2015-05-28 17:35:06 +0100888
889int do_setusercryptopolicies(int nargs, char **args)
890{
891 if (nargs != 2) {
892 return -1;
893 }
894 if (!is_file_crypto()) {
895 return 0;
896 }
897 return e4crypt_set_user_crypto_policies(args[1]);
898}