blob: aa5c649aaafda9a4b3533f09ac2db8cd19d8da2b [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070017#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070018#include <fcntl.h>
19#include <net/if.h>
20#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070021#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <string.h>
23#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <sys/mount.h>
25#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080026#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070027#include <sys/types.h>
28#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070029#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070030#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000031#include <linux/loop.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070032
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033#include <selinux/selinux.h>
34#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035
Elliott Hughesdb3f2672015-03-20 09:45:18 -070036#include <fs_mgr.h>
37#include <base/stringprintf.h>
38#include <cutils/partition_utils.h>
39#include <cutils/android_reboot.h>
40#include <private/android_filesystem_config.h>
41
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042#include "init.h"
43#include "keywords.h"
44#include "property_service.h"
45#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070046#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
Nick Kralevichbc609542015-01-31 21:39:46 -080050#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
51
James Morrissey381341f2014-05-16 11:36:36 +010052int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
Elliott Hughesf3cf4382015-02-03 17:12:07 -080054// System call provided by bionic but not in any header file.
55extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056
The Android Open Source Project35237d12008-12-17 18:08:08 -080057static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058{
Elliott Hughesf682b472015-02-06 12:19:48 -080059 std::string module;
60 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080062 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
Elliott Hughesf682b472015-02-06 12:19:48 -080064 // TODO: use finit_module for >= 3.8 kernels.
65 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066}
67
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068static int __ifupdown(const char *interface, int up)
69{
70 struct ifreq ifr;
71 int s, ret;
72
73 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
74
75 s = socket(AF_INET, SOCK_DGRAM, 0);
76 if (s < 0)
77 return -1;
78
79 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
80 if (ret < 0) {
81 goto done;
82 }
83
84 if (up)
85 ifr.ifr_flags |= IFF_UP;
86 else
87 ifr.ifr_flags &= ~IFF_UP;
88
89 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080090
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091done:
92 close(s);
93 return ret;
94}
95
96static void service_start_if_not_disabled(struct service *svc)
97{
98 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -070099 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700100 } else {
101 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 }
103}
104
105int do_class_start(int nargs, char **args)
106{
107 /* Starting a class does not start services
108 * which are explicitly disabled. They must
109 * be started individually.
110 */
111 service_for_each_class(args[1], service_start_if_not_disabled);
112 return 0;
113}
114
115int do_class_stop(int nargs, char **args)
116{
117 service_for_each_class(args[1], service_stop);
118 return 0;
119}
120
Ken Sumrall752923c2010-12-03 16:33:31 -0800121int do_class_reset(int nargs, char **args)
122{
123 service_for_each_class(args[1], service_reset);
124 return 0;
125}
126
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127int do_domainname(int nargs, char **args)
128{
129 return write_file("/proc/sys/kernel/domainname", args[1]);
130}
131
JP Abgrall3beec7e2014-05-02 21:14:29 -0700132int do_enable(int nargs, char **args)
133{
134 struct service *svc;
135 svc = service_find_by_name(args[1]);
136 if (svc) {
137 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
138 if (svc->flags & SVC_DISABLED_START) {
139 service_start(svc, NULL);
140 }
141 } else {
142 return -1;
143 }
144 return 0;
145}
146
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800147int do_exec(int nargs, char** args) {
148 service* svc = make_exec_oneshot_service(nargs, args);
149 if (svc == NULL) {
150 return -1;
151 }
152 service_start(svc, NULL);
153 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154}
155
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800156// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700157int do_execonce(int nargs, char **args)
158{
159 pid_t child;
160 int child_status = 0;
161 static int already_done;
162
163 if (already_done) {
164 return -1;
165 }
166 already_done = 1;
167 if (!(child = fork())) {
168 /*
169 * Child process.
170 */
171 zap_stdio();
172 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800173 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700174
175 memset(exec_args, 0, sizeof(exec_args));
176 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800177 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700178 ARRAY_SIZE(exec_args) - 1);
179 _exit(1);
180 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800181 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700182 exec_args[i - 1] = args[i];
183
184 if (execv(exec_args[0], exec_args) == -1) {
185 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
186 _exit(1);
187 }
188 ERROR("Returned from execv()!");
189 _exit(1);
190 }
191
192 /*
193 * Parent process.
194 */
195 if (child == -1) {
196 ERROR("Fork failed\n");
197 return -1;
198 }
199
200 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
201 ERROR("waitpid(): failed (%s)\n", strerror(errno));
202 return -1;
203 }
204
205 if (WIFSIGNALED(child_status)) {
206 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
207 return -1;
208 } else if (WIFEXITED(child_status)) {
209 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
210 return WEXITSTATUS(child_status);
211 }
212
213 ERROR("Abnormal child process exit\n");
214
215 return -1;
216}
217
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218int do_export(int nargs, char **args)
219{
James Morrissey381341f2014-05-16 11:36:36 +0100220 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221}
222
223int do_hostname(int nargs, char **args)
224{
225 return write_file("/proc/sys/kernel/hostname", args[1]);
226}
227
228int do_ifup(int nargs, char **args)
229{
230 return __ifupdown(args[1], 1);
231}
232
The Android Open Source Project35237d12008-12-17 18:08:08 -0800233
234static int do_insmod_inner(int nargs, char **args, int opt_len)
235{
236 char options[opt_len + 1];
237 int i;
238
239 options[0] = '\0';
240 if (nargs > 2) {
241 strcpy(options, args[2]);
242 for (i = 3; i < nargs; ++i) {
243 strcat(options, " ");
244 strcat(options, args[i]);
245 }
246 }
247
248 return insmod(args[1], options);
249}
250
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251int do_insmod(int nargs, char **args)
252{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800253 int i;
254 int size = 0;
255
256 if (nargs > 2) {
257 for (i = 2; i < nargs; ++i)
258 size += strlen(args[i]) + 1;
259 }
260
261 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262}
263
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264int do_mkdir(int nargs, char **args)
265{
266 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700267 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268
269 /* mkdir <path> [mode] [owner] [group] */
270
271 if (nargs >= 3) {
272 mode = strtoul(args[2], 0, 8);
273 }
274
Stephen Smalleye096e362012-06-11 13:37:39 -0400275 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700276 /* chmod in case the directory already exists */
277 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800278 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700279 }
280 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 return -errno;
282 }
283
284 if (nargs >= 4) {
285 uid_t uid = decode_uid(args[3]);
286 gid_t gid = -1;
287
288 if (nargs == 5) {
289 gid = decode_uid(args[4]);
290 }
291
Nick Kralevichbc609542015-01-31 21:39:46 -0800292 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 return -errno;
294 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700295
296 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
297 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800298 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700299 if (ret == -1) {
300 return -errno;
301 }
302 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 }
304
305 return 0;
306}
307
308static struct {
309 const char *name;
310 unsigned flag;
311} mount_flags[] = {
312 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200313 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 { "nosuid", MS_NOSUID },
315 { "nodev", MS_NODEV },
316 { "nodiratime", MS_NODIRATIME },
317 { "ro", MS_RDONLY },
318 { "rw", 0 },
319 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700320 { "bind", MS_BIND },
321 { "rec", MS_REC },
322 { "unbindable", MS_UNBINDABLE },
323 { "private", MS_PRIVATE },
324 { "slave", MS_SLAVE },
325 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 { "defaults", 0 },
327 { 0, 0 },
328};
329
Ken Sumrall752923c2010-12-03 16:33:31 -0800330#define DATA_MNT_POINT "/data"
331
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332/* mount <type> <device> <path> <flags ...> <options> */
333int do_mount(int nargs, char **args)
334{
335 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000336 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 char *options = NULL;
338 unsigned flags = 0;
339 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700340 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700341
342 for (n = 4; n < nargs; n++) {
343 for (i = 0; mount_flags[i].name; i++) {
344 if (!strcmp(args[n], mount_flags[i].name)) {
345 flags |= mount_flags[i].flag;
346 break;
347 }
348 }
349
Colin Crosscd0f1732010-04-19 17:10:24 -0700350 if (!mount_flags[i].name) {
351 if (!strcmp(args[n], "wait"))
352 wait = 1;
353 /* if our last argument isn't a flag, wolf it up as an option string */
354 else if (n + 1 == nargs)
355 options = args[n];
356 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 }
358
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361 target = args[3];
362
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 if (!strncmp(source, "mtd@", 4)) {
364 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000365 if (n < 0) {
366 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000368
Yabin Cuie2d63af2015-02-17 19:27:51 -0800369 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370
Colin Crosscd0f1732010-04-19 17:10:24 -0700371 if (wait)
372 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 if (mount(tmp, target, system, flags, options) < 0) {
374 return -1;
375 }
376
Ken Sumralldd4d7862011-02-17 18:09:47 -0800377 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000378 } else if (!strncmp(source, "loop@", 5)) {
379 int mode, loop, fd;
380 struct loop_info info;
381
382 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800383 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 if (fd < 0) {
385 return -1;
386 }
387
388 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800389 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800390 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100392 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 return -1;
394 }
395
396 /* if it is a blank loop device */
397 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
398 /* if it becomes our loop device */
399 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
400 close(fd);
401
402 if (mount(tmp, target, system, flags, options) < 0) {
403 ioctl(loop, LOOP_CLR_FD, 0);
404 close(loop);
405 return -1;
406 }
407
408 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800409 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 }
411 }
412
413 close(loop);
414 }
415
416 close(fd);
417 ERROR("out of loopback devices");
418 return -1;
419 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700420 if (wait)
421 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000422 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700423 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000424 }
425
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700426 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800427
428exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800429 return 0;
430
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431}
432
JP Abgrallcee20682014-07-02 14:26:54 -0700433static int wipe_data_via_recovery()
434{
435 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800436 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700437 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700438 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
439 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700440 close(fd);
441 } else {
442 ERROR("could not open /cache/recovery/command\n");
443 return -1;
444 }
445 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
446 while (1) { pause(); } // never reached
447}
448
449
450/*
451 * This function might request a reboot, in which case it will
452 * not return.
453 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454int do_mount_all(int nargs, char **args)
455{
456 pid_t pid;
457 int ret = -1;
458 int child_ret = -1;
459 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800460 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700461
462 if (nargs != 2) {
463 return -1;
464 }
465
466 /*
467 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
468 * do the call in the child to provide protection to the main init
469 * process if anything goes wrong (crash or memory leak), and wait for
470 * the child to finish in the parent.
471 */
472 pid = fork();
473 if (pid > 0) {
474 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700475 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
476 if (wp_ret < 0) {
477 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700478 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700479 }
480
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700481 if (WIFEXITED(status)) {
482 ret = WEXITSTATUS(status);
483 } else {
484 ret = -1;
485 }
486 } else if (pid == 0) {
487 /* child, call fs_mgr_mount_all() */
488 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800489 fstab = fs_mgr_read_fstab(args[1]);
490 child_ret = fs_mgr_mount_all(fstab);
491 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700492 if (child_ret == -1) {
493 ERROR("fs_mgr_mount_all returned an error\n");
494 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700495 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700496 } else {
497 /* fork failed, return an error */
498 return -1;
499 }
500
JP Abgrallf22b7452014-07-02 13:16:04 -0700501 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800502 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700503 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800505 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700506 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700507 property_set("ro.crypto.state", "unencrypted");
508 /* If fs_mgr determined this is an unencrypted device, then trigger
509 * that action.
510 */
511 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700512 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
513 /* Setup a wipe via recovery, and reboot into recovery */
514 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
515 ret = wipe_data_via_recovery();
516 /* If reboot worked, there is no return. */
517 } else if (ret > 0) {
518 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700520 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521
522 return ret;
523}
524
Ken Sumralla76baaa2013-07-09 18:42:09 -0700525int do_swapon_all(int nargs, char **args)
526{
527 struct fstab *fstab;
528 int ret;
529
530 fstab = fs_mgr_read_fstab(args[1]);
531 ret = fs_mgr_swapon_all(fstab);
532 fs_mgr_free_fstab(fstab);
533
534 return ret;
535}
536
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500537int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500538 if (is_selinux_enabled() <= 0)
539 return 0;
540 if (setcon(args[1]) < 0) {
541 return -errno;
542 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500543 return 0;
544}
545
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546int do_setprop(int nargs, char **args)
547{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700548 const char *name = args[1];
549 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800550 char prop_val[PROP_VALUE_MAX];
551 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700552
Dima Zavin84bf9af2011-12-20 13:44:41 -0800553 ret = expand_props(prop_val, value, sizeof(prop_val));
554 if (ret) {
555 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
556 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700557 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800558 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 return 0;
560}
561
562int do_setrlimit(int nargs, char **args)
563{
564 struct rlimit limit;
565 int resource;
566 resource = atoi(args[1]);
567 limit.rlim_cur = atoi(args[2]);
568 limit.rlim_max = atoi(args[3]);
569 return setrlimit(resource, &limit);
570}
571
572int do_start(int nargs, char **args)
573{
574 struct service *svc;
575 svc = service_find_by_name(args[1]);
576 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700577 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 }
579 return 0;
580}
581
582int do_stop(int nargs, char **args)
583{
584 struct service *svc;
585 svc = service_find_by_name(args[1]);
586 if (svc) {
587 service_stop(svc);
588 }
589 return 0;
590}
591
592int do_restart(int nargs, char **args)
593{
594 struct service *svc;
595 svc = service_find_by_name(args[1]);
596 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500597 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 }
599 return 0;
600}
601
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700602int do_powerctl(int nargs, char **args)
603{
604 char command[PROP_VALUE_MAX];
605 int res;
606 int len = 0;
607 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800608 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700609
610 res = expand_props(command, args[1], sizeof(command));
611 if (res) {
612 ERROR("powerctl: cannot expand '%s'\n", args[1]);
613 return -EINVAL;
614 }
615
616 if (strncmp(command, "shutdown", 8) == 0) {
617 cmd = ANDROID_RB_POWEROFF;
618 len = 8;
619 } 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
636 return android_reboot(cmd, 0, reboot_target);
637}
638
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700639int do_trigger(int nargs, char **args)
640{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000641 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700642 return 0;
643}
644
645int do_symlink(int nargs, char **args)
646{
647 return symlink(args[1], args[2]);
648}
649
Ken Sumrall203bad52011-01-18 17:37:41 -0800650int do_rm(int nargs, char **args)
651{
652 return unlink(args[1]);
653}
654
655int do_rmdir(int nargs, char **args)
656{
657 return rmdir(args[1]);
658}
659
The Android Open Source Project35237d12008-12-17 18:08:08 -0800660int do_sysclktz(int nargs, char **args)
661{
662 struct timezone tz;
663
664 if (nargs != 2)
665 return -1;
666
667 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800668 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800669 if (settimeofday(NULL, &tz))
670 return -1;
671 return 0;
672}
673
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000674int do_verity_load_state(int nargs, char **args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700675 int mode = -1;
676 int rc = fs_mgr_load_verity_state(&mode);
677 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
678 action_for_each_trigger("verity-logging", action_add_queue_tail);
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000679 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700680 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000681}
682
Sami Tolvanen45474232015-03-30 11:38:38 +0100683static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
684 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
685 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000686}
687
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700688int do_verity_update_state(int nargs, char** args) {
689 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000690}
691
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700692int do_write(int nargs, char **args)
693{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700694 const char *path = args[1];
695 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700696
Johan Redestig7e952f42014-12-05 00:36:41 +0100697 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800698 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800699 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
700 return -EINVAL;
701 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800702 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700703}
704
San Mehat7c44fe52009-08-26 16:39:25 -0700705int do_copy(int nargs, char **args)
706{
707 char *buffer = NULL;
708 int rc = 0;
709 int fd1 = -1, fd2 = -1;
710 struct stat info;
711 int brtw, brtr;
712 char *p;
713
714 if (nargs != 3)
715 return -1;
716
Elliott Hughes24627902015-02-04 10:25:09 -0800717 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700718 return -1;
719
Nick Kralevich45a884f2015-02-02 14:37:22 -0800720 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700721 goto out_err;
722
Nick Kralevich45a884f2015-02-02 14:37:22 -0800723 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700724 goto out_err;
725
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800726 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700727 goto out_err;
728
729 p = buffer;
730 brtr = info.st_size;
731 while(brtr) {
732 rc = read(fd1, p, brtr);
733 if (rc < 0)
734 goto out_err;
735 if (rc == 0)
736 break;
737 p += rc;
738 brtr -= rc;
739 }
740
741 p = buffer;
742 brtw = info.st_size;
743 while(brtw) {
744 rc = write(fd2, p, brtw);
745 if (rc < 0)
746 goto out_err;
747 if (rc == 0)
748 break;
749 p += rc;
750 brtw -= rc;
751 }
752
753 rc = 0;
754 goto out;
755out_err:
756 rc = -1;
757out:
758 if (buffer)
759 free(buffer);
760 if (fd1 >= 0)
761 close(fd1);
762 if (fd2 >= 0)
763 close(fd2);
764 return rc;
765}
766
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767int do_chown(int nargs, char **args) {
768 /* GID is optional. */
769 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800770 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771 return -errno;
772 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800773 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774 return -errno;
775 } else {
776 return -1;
777 }
778 return 0;
779}
780
781static mode_t get_mode(const char *s) {
782 mode_t mode = 0;
783 while (*s) {
784 if (*s >= '0' && *s <= '7') {
785 mode = (mode<<3) | (*s-'0');
786 } else {
787 return -1;
788 }
789 s++;
790 }
791 return mode;
792}
793
794int do_chmod(int nargs, char **args) {
795 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800796 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700797 return -errno;
798 }
799 return 0;
800}
801
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500802int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400804 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500805
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500806 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400807 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400808 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500809 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400810 return ret;
811}
812
813int do_restorecon_recursive(int nargs, char **args) {
814 int i;
815 int ret = 0;
816
817 for (i = 1; i < nargs; i++) {
818 if (restorecon_recursive(args[i]) < 0)
819 ret = -errno;
820 }
821 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500822}
823
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700825 int log_level;
826 char log_level_str[PROP_VALUE_MAX] = "";
827 if (nargs != 2) {
828 ERROR("loglevel: missing argument\n");
829 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700831
832 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
833 ERROR("loglevel: cannot expand '%s'\n", args[1]);
834 return -EINVAL;
835 }
836 log_level = atoi(log_level_str);
837 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
838 ERROR("loglevel: invalid log level'%d'\n", log_level);
839 return -EINVAL;
840 }
841 klog_set_level(log_level);
842 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843}
844
Ken Sumrallc5c51032011-03-08 17:01:29 -0800845int do_load_persist_props(int nargs, char **args) {
846 if (nargs == 1) {
847 load_persist_props();
848 return 0;
849 }
850 return -1;
851}
852
Riley Andrewse4b7b292014-06-16 15:06:21 -0700853int do_load_all_props(int nargs, char **args) {
854 if (nargs == 1) {
855 load_all_props();
856 return 0;
857 }
858 return -1;
859}
860
Colin Crosscd0f1732010-04-19 17:10:24 -0700861int do_wait(int nargs, char **args)
862{
863 if (nargs == 2) {
864 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800865 } else if (nargs == 3) {
866 return wait_for_file(args[1], atoi(args[2]));
867 } else
868 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700869}