blob: 5d1ad296e3c0c04815efe3d8112697912b0a1bba [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
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdio.h>
23#include <linux/kd.h>
24#include <errno.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <linux/if.h>
28#include <arpa/inet.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080032#include <sys/time.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070033#include <sys/wait.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000034#include <linux/loop.h>
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -070035#include <cutils/partition_utils.h>
Nick Kralevichca8e66a2013-04-18 12:20:02 -070036#include <cutils/android_reboot.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070037#include <fs_mgr.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
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
50#include <private/android_filesystem_config.h>
51
Nick Kralevichbc609542015-01-31 21:39:46 -080052#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
53
James Morrissey381341f2014-05-16 11:36:36 +010054int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
Elliott Hughesf3cf4382015-02-03 17:12:07 -080056// System call provided by bionic but not in any header file.
57extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
The Android Open Source Project35237d12008-12-17 18:08:08 -080059static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060{
Elliott Hughesf682b472015-02-06 12:19:48 -080061 std::string module;
62 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080064 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Elliott Hughesf682b472015-02-06 12:19:48 -080066 // TODO: use finit_module for >= 3.8 kernels.
67 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068}
69
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070static int __ifupdown(const char *interface, int up)
71{
72 struct ifreq ifr;
73 int s, ret;
74
75 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
76
77 s = socket(AF_INET, SOCK_DGRAM, 0);
78 if (s < 0)
79 return -1;
80
81 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
82 if (ret < 0) {
83 goto done;
84 }
85
86 if (up)
87 ifr.ifr_flags |= IFF_UP;
88 else
89 ifr.ifr_flags &= ~IFF_UP;
90
91 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080092
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093done:
94 close(s);
95 return ret;
96}
97
98static void service_start_if_not_disabled(struct service *svc)
99{
100 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700101 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700102 } else {
103 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 }
105}
106
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000107int do_chdir(int nargs, char **args)
108{
109 chdir(args[1]);
110 return 0;
111}
112
113int do_chroot(int nargs, char **args)
114{
115 chroot(args[1]);
116 return 0;
117}
118
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700119int do_class_start(int nargs, char **args)
120{
121 /* Starting a class does not start services
122 * which are explicitly disabled. They must
123 * be started individually.
124 */
125 service_for_each_class(args[1], service_start_if_not_disabled);
126 return 0;
127}
128
129int do_class_stop(int nargs, char **args)
130{
131 service_for_each_class(args[1], service_stop);
132 return 0;
133}
134
Ken Sumrall752923c2010-12-03 16:33:31 -0800135int do_class_reset(int nargs, char **args)
136{
137 service_for_each_class(args[1], service_reset);
138 return 0;
139}
140
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141int do_domainname(int nargs, char **args)
142{
143 return write_file("/proc/sys/kernel/domainname", args[1]);
144}
145
JP Abgrall3beec7e2014-05-02 21:14:29 -0700146int do_enable(int nargs, char **args)
147{
148 struct service *svc;
149 svc = service_find_by_name(args[1]);
150 if (svc) {
151 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
152 if (svc->flags & SVC_DISABLED_START) {
153 service_start(svc, NULL);
154 }
155 } else {
156 return -1;
157 }
158 return 0;
159}
160
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800161int do_exec(int nargs, char** args) {
162 service* svc = make_exec_oneshot_service(nargs, args);
163 if (svc == NULL) {
164 return -1;
165 }
166 service_start(svc, NULL);
167 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168}
169
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800170// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700171int do_execonce(int nargs, char **args)
172{
173 pid_t child;
174 int child_status = 0;
175 static int already_done;
176
177 if (already_done) {
178 return -1;
179 }
180 already_done = 1;
181 if (!(child = fork())) {
182 /*
183 * Child process.
184 */
185 zap_stdio();
186 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800187 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700188
189 memset(exec_args, 0, sizeof(exec_args));
190 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800191 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700192 ARRAY_SIZE(exec_args) - 1);
193 _exit(1);
194 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800195 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700196 exec_args[i - 1] = args[i];
197
198 if (execv(exec_args[0], exec_args) == -1) {
199 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
200 _exit(1);
201 }
202 ERROR("Returned from execv()!");
203 _exit(1);
204 }
205
206 /*
207 * Parent process.
208 */
209 if (child == -1) {
210 ERROR("Fork failed\n");
211 return -1;
212 }
213
214 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
215 ERROR("waitpid(): failed (%s)\n", strerror(errno));
216 return -1;
217 }
218
219 if (WIFSIGNALED(child_status)) {
220 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
221 return -1;
222 } else if (WIFEXITED(child_status)) {
223 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
224 return WEXITSTATUS(child_status);
225 }
226
227 ERROR("Abnormal child process exit\n");
228
229 return -1;
230}
231
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232int do_export(int nargs, char **args)
233{
James Morrissey381341f2014-05-16 11:36:36 +0100234 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235}
236
237int do_hostname(int nargs, char **args)
238{
239 return write_file("/proc/sys/kernel/hostname", args[1]);
240}
241
242int do_ifup(int nargs, char **args)
243{
244 return __ifupdown(args[1], 1);
245}
246
The Android Open Source Project35237d12008-12-17 18:08:08 -0800247
248static int do_insmod_inner(int nargs, char **args, int opt_len)
249{
250 char options[opt_len + 1];
251 int i;
252
253 options[0] = '\0';
254 if (nargs > 2) {
255 strcpy(options, args[2]);
256 for (i = 3; i < nargs; ++i) {
257 strcat(options, " ");
258 strcat(options, args[i]);
259 }
260 }
261
262 return insmod(args[1], options);
263}
264
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265int do_insmod(int nargs, char **args)
266{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800267 int i;
268 int size = 0;
269
270 if (nargs > 2) {
271 for (i = 2; i < nargs; ++i)
272 size += strlen(args[i]) + 1;
273 }
274
275 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276}
277
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700278int do_mkdir(int nargs, char **args)
279{
280 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700281 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282
283 /* mkdir <path> [mode] [owner] [group] */
284
285 if (nargs >= 3) {
286 mode = strtoul(args[2], 0, 8);
287 }
288
Stephen Smalleye096e362012-06-11 13:37:39 -0400289 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700290 /* chmod in case the directory already exists */
291 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800292 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700293 }
294 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 return -errno;
296 }
297
298 if (nargs >= 4) {
299 uid_t uid = decode_uid(args[3]);
300 gid_t gid = -1;
301
302 if (nargs == 5) {
303 gid = decode_uid(args[4]);
304 }
305
Nick Kralevichbc609542015-01-31 21:39:46 -0800306 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 return -errno;
308 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700309
310 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
311 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800312 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700313 if (ret == -1) {
314 return -errno;
315 }
316 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 }
318
319 return 0;
320}
321
322static struct {
323 const char *name;
324 unsigned flag;
325} mount_flags[] = {
326 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200327 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 { "nosuid", MS_NOSUID },
329 { "nodev", MS_NODEV },
330 { "nodiratime", MS_NODIRATIME },
331 { "ro", MS_RDONLY },
332 { "rw", 0 },
333 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700334 { "bind", MS_BIND },
335 { "rec", MS_REC },
336 { "unbindable", MS_UNBINDABLE },
337 { "private", MS_PRIVATE },
338 { "slave", MS_SLAVE },
339 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340 { "defaults", 0 },
341 { 0, 0 },
342};
343
Ken Sumrall752923c2010-12-03 16:33:31 -0800344#define DATA_MNT_POINT "/data"
345
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346/* mount <type> <device> <path> <flags ...> <options> */
347int do_mount(int nargs, char **args)
348{
349 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000350 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 char *options = NULL;
352 unsigned flags = 0;
353 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700354 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355
356 for (n = 4; n < nargs; n++) {
357 for (i = 0; mount_flags[i].name; i++) {
358 if (!strcmp(args[n], mount_flags[i].name)) {
359 flags |= mount_flags[i].flag;
360 break;
361 }
362 }
363
Colin Crosscd0f1732010-04-19 17:10:24 -0700364 if (!mount_flags[i].name) {
365 if (!strcmp(args[n], "wait"))
366 wait = 1;
367 /* if our last argument isn't a flag, wolf it up as an option string */
368 else if (n + 1 == nargs)
369 options = args[n];
370 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 }
372
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000375 target = args[3];
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 if (!strncmp(source, "mtd@", 4)) {
378 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000379 if (n < 0) {
380 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382
Yabin Cuie2d63af2015-02-17 19:27:51 -0800383 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384
Colin Crosscd0f1732010-04-19 17:10:24 -0700385 if (wait)
386 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387 if (mount(tmp, target, system, flags, options) < 0) {
388 return -1;
389 }
390
Ken Sumralldd4d7862011-02-17 18:09:47 -0800391 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392 } else if (!strncmp(source, "loop@", 5)) {
393 int mode, loop, fd;
394 struct loop_info info;
395
396 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800397 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398 if (fd < 0) {
399 return -1;
400 }
401
402 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800403 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800404 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100406 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407 return -1;
408 }
409
410 /* if it is a blank loop device */
411 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
412 /* if it becomes our loop device */
413 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
414 close(fd);
415
416 if (mount(tmp, target, system, flags, options) < 0) {
417 ioctl(loop, LOOP_CLR_FD, 0);
418 close(loop);
419 return -1;
420 }
421
422 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800423 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000424 }
425 }
426
427 close(loop);
428 }
429
430 close(fd);
431 ERROR("out of loopback devices");
432 return -1;
433 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700434 if (wait)
435 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000436 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700437 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000438 }
439
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800441
442exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800443 return 0;
444
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445}
446
JP Abgrallcee20682014-07-02 14:26:54 -0700447static int wipe_data_via_recovery()
448{
449 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800450 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700451 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700452 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
453 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700454 close(fd);
455 } else {
456 ERROR("could not open /cache/recovery/command\n");
457 return -1;
458 }
459 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
460 while (1) { pause(); } // never reached
461}
462
463
464/*
465 * This function might request a reboot, in which case it will
466 * not return.
467 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700468int do_mount_all(int nargs, char **args)
469{
470 pid_t pid;
471 int ret = -1;
472 int child_ret = -1;
473 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800474 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700475
476 if (nargs != 2) {
477 return -1;
478 }
479
480 /*
481 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
482 * do the call in the child to provide protection to the main init
483 * process if anything goes wrong (crash or memory leak), and wait for
484 * the child to finish in the parent.
485 */
486 pid = fork();
487 if (pid > 0) {
488 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700489 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
490 if (wp_ret < 0) {
491 /* Unexpected error code. We will continue anyway. */
492 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
493 }
494
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700495 if (WIFEXITED(status)) {
496 ret = WEXITSTATUS(status);
497 } else {
498 ret = -1;
499 }
500 } else if (pid == 0) {
501 /* child, call fs_mgr_mount_all() */
502 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800503 fstab = fs_mgr_read_fstab(args[1]);
504 child_ret = fs_mgr_mount_all(fstab);
505 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 if (child_ret == -1) {
507 ERROR("fs_mgr_mount_all returned an error\n");
508 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700509 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700510 } else {
511 /* fork failed, return an error */
512 return -1;
513 }
514
JP Abgrallf22b7452014-07-02 13:16:04 -0700515 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800516 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700517 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700518 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800519 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700520 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 property_set("ro.crypto.state", "unencrypted");
522 /* If fs_mgr determined this is an unencrypted device, then trigger
523 * that action.
524 */
525 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700526 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
527 /* Setup a wipe via recovery, and reboot into recovery */
528 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
529 ret = wipe_data_via_recovery();
530 /* If reboot worked, there is no return. */
531 } else if (ret > 0) {
532 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700533 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700534 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700535
536 return ret;
537}
538
Ken Sumralla76baaa2013-07-09 18:42:09 -0700539int do_swapon_all(int nargs, char **args)
540{
541 struct fstab *fstab;
542 int ret;
543
544 fstab = fs_mgr_read_fstab(args[1]);
545 ret = fs_mgr_swapon_all(fstab);
546 fs_mgr_free_fstab(fstab);
547
548 return ret;
549}
550
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500551int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500552 if (is_selinux_enabled() <= 0)
553 return 0;
554 if (setcon(args[1]) < 0) {
555 return -errno;
556 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500557 return 0;
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];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800564 char prop_val[PROP_VALUE_MAX];
565 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700566
Dima Zavin84bf9af2011-12-20 13:44:41 -0800567 ret = expand_props(prop_val, value, sizeof(prop_val));
568 if (ret) {
569 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
570 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700571 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800572 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700573 return 0;
574}
575
576int do_setrlimit(int nargs, char **args)
577{
578 struct rlimit limit;
579 int resource;
580 resource = atoi(args[1]);
581 limit.rlim_cur = atoi(args[2]);
582 limit.rlim_max = atoi(args[3]);
583 return setrlimit(resource, &limit);
584}
585
586int do_start(int nargs, char **args)
587{
588 struct service *svc;
589 svc = service_find_by_name(args[1]);
590 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700591 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 }
593 return 0;
594}
595
596int do_stop(int nargs, char **args)
597{
598 struct service *svc;
599 svc = service_find_by_name(args[1]);
600 if (svc) {
601 service_stop(svc);
602 }
603 return 0;
604}
605
606int do_restart(int nargs, char **args)
607{
608 struct service *svc;
609 svc = service_find_by_name(args[1]);
610 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500611 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700612 }
613 return 0;
614}
615
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700616int do_powerctl(int nargs, char **args)
617{
618 char command[PROP_VALUE_MAX];
619 int res;
620 int len = 0;
621 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800622 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700623
624 res = expand_props(command, args[1], sizeof(command));
625 if (res) {
626 ERROR("powerctl: cannot expand '%s'\n", args[1]);
627 return -EINVAL;
628 }
629
630 if (strncmp(command, "shutdown", 8) == 0) {
631 cmd = ANDROID_RB_POWEROFF;
632 len = 8;
633 } else if (strncmp(command, "reboot", 6) == 0) {
634 cmd = ANDROID_RB_RESTART2;
635 len = 6;
636 } else {
637 ERROR("powerctl: unrecognized command '%s'\n", command);
638 return -EINVAL;
639 }
640
641 if (command[len] == ',') {
642 reboot_target = &command[len + 1];
643 } else if (command[len] == '\0') {
644 reboot_target = "";
645 } else {
646 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
647 return -EINVAL;
648 }
649
650 return android_reboot(cmd, 0, reboot_target);
651}
652
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700653int do_trigger(int nargs, char **args)
654{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000655 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656 return 0;
657}
658
659int do_symlink(int nargs, char **args)
660{
661 return symlink(args[1], args[2]);
662}
663
Ken Sumrall203bad52011-01-18 17:37:41 -0800664int do_rm(int nargs, char **args)
665{
666 return unlink(args[1]);
667}
668
669int do_rmdir(int nargs, char **args)
670{
671 return rmdir(args[1]);
672}
673
The Android Open Source Project35237d12008-12-17 18:08:08 -0800674int do_sysclktz(int nargs, char **args)
675{
676 struct timezone tz;
677
678 if (nargs != 2)
679 return -1;
680
681 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800682 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800683 if (settimeofday(NULL, &tz))
684 return -1;
685 return 0;
686}
687
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000688int do_verity_load_state(int nargs, char **args) {
689 if (nargs == 1) {
690 int mode = -1;
691 int rc = fs_mgr_load_verity_state(&mode);
692
693 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
694 action_for_each_trigger("verity-logging", action_add_queue_tail);
695 }
696
697 return rc;
698 }
699 return -1;
700}
701
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700702int do_write(int nargs, char **args)
703{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700704 const char *path = args[1];
705 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700706
Johan Redestig7e952f42014-12-05 00:36:41 +0100707 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800708 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800709 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
710 return -EINVAL;
711 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800712 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700713}
714
San Mehat7c44fe52009-08-26 16:39:25 -0700715int do_copy(int nargs, char **args)
716{
717 char *buffer = NULL;
718 int rc = 0;
719 int fd1 = -1, fd2 = -1;
720 struct stat info;
721 int brtw, brtr;
722 char *p;
723
724 if (nargs != 3)
725 return -1;
726
Elliott Hughes24627902015-02-04 10:25:09 -0800727 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700728 return -1;
729
Nick Kralevich45a884f2015-02-02 14:37:22 -0800730 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700731 goto out_err;
732
Nick Kralevich45a884f2015-02-02 14:37:22 -0800733 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700734 goto out_err;
735
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800736 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700737 goto out_err;
738
739 p = buffer;
740 brtr = info.st_size;
741 while(brtr) {
742 rc = read(fd1, p, brtr);
743 if (rc < 0)
744 goto out_err;
745 if (rc == 0)
746 break;
747 p += rc;
748 brtr -= rc;
749 }
750
751 p = buffer;
752 brtw = info.st_size;
753 while(brtw) {
754 rc = write(fd2, p, brtw);
755 if (rc < 0)
756 goto out_err;
757 if (rc == 0)
758 break;
759 p += rc;
760 brtw -= rc;
761 }
762
763 rc = 0;
764 goto out;
765out_err:
766 rc = -1;
767out:
768 if (buffer)
769 free(buffer);
770 if (fd1 >= 0)
771 close(fd1);
772 if (fd2 >= 0)
773 close(fd2);
774 return rc;
775}
776
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777int do_chown(int nargs, char **args) {
778 /* GID is optional. */
779 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800780 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781 return -errno;
782 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800783 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 return -errno;
785 } else {
786 return -1;
787 }
788 return 0;
789}
790
791static mode_t get_mode(const char *s) {
792 mode_t mode = 0;
793 while (*s) {
794 if (*s >= '0' && *s <= '7') {
795 mode = (mode<<3) | (*s-'0');
796 } else {
797 return -1;
798 }
799 s++;
800 }
801 return mode;
802}
803
804int do_chmod(int nargs, char **args) {
805 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800806 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 return -errno;
808 }
809 return 0;
810}
811
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500812int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400814 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500815
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500816 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400817 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400818 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500819 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400820 return ret;
821}
822
823int do_restorecon_recursive(int nargs, char **args) {
824 int i;
825 int ret = 0;
826
827 for (i = 1; i < nargs; i++) {
828 if (restorecon_recursive(args[i]) < 0)
829 ret = -errno;
830 }
831 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500832}
833
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700835 int log_level;
836 char log_level_str[PROP_VALUE_MAX] = "";
837 if (nargs != 2) {
838 ERROR("loglevel: missing argument\n");
839 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700840 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700841
842 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
843 ERROR("loglevel: cannot expand '%s'\n", args[1]);
844 return -EINVAL;
845 }
846 log_level = atoi(log_level_str);
847 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
848 ERROR("loglevel: invalid log level'%d'\n", log_level);
849 return -EINVAL;
850 }
851 klog_set_level(log_level);
852 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853}
854
Ken Sumrallc5c51032011-03-08 17:01:29 -0800855int do_load_persist_props(int nargs, char **args) {
856 if (nargs == 1) {
857 load_persist_props();
858 return 0;
859 }
860 return -1;
861}
862
Riley Andrewse4b7b292014-06-16 15:06:21 -0700863int do_load_all_props(int nargs, char **args) {
864 if (nargs == 1) {
865 load_all_props();
866 return 0;
867 }
868 return -1;
869}
870
Colin Crosscd0f1732010-04-19 17:10:24 -0700871int do_wait(int nargs, char **args)
872{
873 if (nargs == 2) {
874 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800875 } else if (nargs == 3) {
876 return wait_for_file(args[1], atoi(args[2]));
877 } else
878 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700879}