blob: b4204d8ddc31351a066ecc0ab3e2c2cb4026b4f5 [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
70static int setkey(struct kbentry *kbe)
71{
72 int fd, ret;
73
Nick Kralevich45a884f2015-02-02 14:37:22 -080074 fd = open("/dev/tty0", O_RDWR | O_SYNC | O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 if (fd < 0)
76 return -1;
77
78 ret = ioctl(fd, KDSKBENT, kbe);
79
80 close(fd);
81 return ret;
82}
83
84static int __ifupdown(const char *interface, int up)
85{
86 struct ifreq ifr;
87 int s, ret;
88
89 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
90
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0)
93 return -1;
94
95 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
96 if (ret < 0) {
97 goto done;
98 }
99
100 if (up)
101 ifr.ifr_flags |= IFF_UP;
102 else
103 ifr.ifr_flags &= ~IFF_UP;
104
105 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107done:
108 close(s);
109 return ret;
110}
111
112static void service_start_if_not_disabled(struct service *svc)
113{
114 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700115 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700116 } else {
117 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 }
119}
120
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000121int do_chdir(int nargs, char **args)
122{
123 chdir(args[1]);
124 return 0;
125}
126
127int do_chroot(int nargs, char **args)
128{
129 chroot(args[1]);
130 return 0;
131}
132
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133int do_class_start(int nargs, char **args)
134{
135 /* Starting a class does not start services
136 * which are explicitly disabled. They must
137 * be started individually.
138 */
139 service_for_each_class(args[1], service_start_if_not_disabled);
140 return 0;
141}
142
143int do_class_stop(int nargs, char **args)
144{
145 service_for_each_class(args[1], service_stop);
146 return 0;
147}
148
Ken Sumrall752923c2010-12-03 16:33:31 -0800149int do_class_reset(int nargs, char **args)
150{
151 service_for_each_class(args[1], service_reset);
152 return 0;
153}
154
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155int do_domainname(int nargs, char **args)
156{
157 return write_file("/proc/sys/kernel/domainname", args[1]);
158}
159
JP Abgrall3beec7e2014-05-02 21:14:29 -0700160int do_enable(int nargs, char **args)
161{
162 struct service *svc;
163 svc = service_find_by_name(args[1]);
164 if (svc) {
165 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
166 if (svc->flags & SVC_DISABLED_START) {
167 service_start(svc, NULL);
168 }
169 } else {
170 return -1;
171 }
172 return 0;
173}
174
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175int do_exec(int nargs, char **args)
176{
177 return -1;
178}
179
San Mehat429721c2014-09-23 07:48:47 -0700180int do_execonce(int nargs, char **args)
181{
182 pid_t child;
183 int child_status = 0;
184 static int already_done;
185
186 if (already_done) {
187 return -1;
188 }
189 already_done = 1;
190 if (!(child = fork())) {
191 /*
192 * Child process.
193 */
194 zap_stdio();
195 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800196 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700197
198 memset(exec_args, 0, sizeof(exec_args));
199 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800200 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700201 ARRAY_SIZE(exec_args) - 1);
202 _exit(1);
203 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800204 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700205 exec_args[i - 1] = args[i];
206
207 if (execv(exec_args[0], exec_args) == -1) {
208 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
209 _exit(1);
210 }
211 ERROR("Returned from execv()!");
212 _exit(1);
213 }
214
215 /*
216 * Parent process.
217 */
218 if (child == -1) {
219 ERROR("Fork failed\n");
220 return -1;
221 }
222
223 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
224 ERROR("waitpid(): failed (%s)\n", strerror(errno));
225 return -1;
226 }
227
228 if (WIFSIGNALED(child_status)) {
229 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
230 return -1;
231 } else if (WIFEXITED(child_status)) {
232 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
233 return WEXITSTATUS(child_status);
234 }
235
236 ERROR("Abnormal child process exit\n");
237
238 return -1;
239}
240
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241int do_export(int nargs, char **args)
242{
James Morrissey381341f2014-05-16 11:36:36 +0100243 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244}
245
246int do_hostname(int nargs, char **args)
247{
248 return write_file("/proc/sys/kernel/hostname", args[1]);
249}
250
251int do_ifup(int nargs, char **args)
252{
253 return __ifupdown(args[1], 1);
254}
255
The Android Open Source Project35237d12008-12-17 18:08:08 -0800256
257static int do_insmod_inner(int nargs, char **args, int opt_len)
258{
259 char options[opt_len + 1];
260 int i;
261
262 options[0] = '\0';
263 if (nargs > 2) {
264 strcpy(options, args[2]);
265 for (i = 3; i < nargs; ++i) {
266 strcat(options, " ");
267 strcat(options, args[i]);
268 }
269 }
270
271 return insmod(args[1], options);
272}
273
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274int do_insmod(int nargs, char **args)
275{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800276 int i;
277 int size = 0;
278
279 if (nargs > 2) {
280 for (i = 2; i < nargs; ++i)
281 size += strlen(args[i]) + 1;
282 }
283
284 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285}
286
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287int do_mkdir(int nargs, char **args)
288{
289 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700290 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700291
292 /* mkdir <path> [mode] [owner] [group] */
293
294 if (nargs >= 3) {
295 mode = strtoul(args[2], 0, 8);
296 }
297
Stephen Smalleye096e362012-06-11 13:37:39 -0400298 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700299 /* chmod in case the directory already exists */
300 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800301 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700302 }
303 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 return -errno;
305 }
306
307 if (nargs >= 4) {
308 uid_t uid = decode_uid(args[3]);
309 gid_t gid = -1;
310
311 if (nargs == 5) {
312 gid = decode_uid(args[4]);
313 }
314
Nick Kralevichbc609542015-01-31 21:39:46 -0800315 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 return -errno;
317 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700318
319 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
320 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800321 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700322 if (ret == -1) {
323 return -errno;
324 }
325 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 }
327
328 return 0;
329}
330
331static struct {
332 const char *name;
333 unsigned flag;
334} mount_flags[] = {
335 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200336 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 { "nosuid", MS_NOSUID },
338 { "nodev", MS_NODEV },
339 { "nodiratime", MS_NODIRATIME },
340 { "ro", MS_RDONLY },
341 { "rw", 0 },
342 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700343 { "bind", MS_BIND },
344 { "rec", MS_REC },
345 { "unbindable", MS_UNBINDABLE },
346 { "private", MS_PRIVATE },
347 { "slave", MS_SLAVE },
348 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349 { "defaults", 0 },
350 { 0, 0 },
351};
352
Ken Sumrall752923c2010-12-03 16:33:31 -0800353#define DATA_MNT_POINT "/data"
354
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355/* mount <type> <device> <path> <flags ...> <options> */
356int do_mount(int nargs, char **args)
357{
358 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 char *options = NULL;
361 unsigned flags = 0;
362 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700363 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364
365 for (n = 4; n < nargs; n++) {
366 for (i = 0; mount_flags[i].name; i++) {
367 if (!strcmp(args[n], mount_flags[i].name)) {
368 flags |= mount_flags[i].flag;
369 break;
370 }
371 }
372
Colin Crosscd0f1732010-04-19 17:10:24 -0700373 if (!mount_flags[i].name) {
374 if (!strcmp(args[n], "wait"))
375 wait = 1;
376 /* if our last argument isn't a flag, wolf it up as an option string */
377 else if (n + 1 == nargs)
378 options = args[n];
379 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380 }
381
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 target = args[3];
385
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386 if (!strncmp(source, "mtd@", 4)) {
387 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000388 if (n < 0) {
389 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391
Yabin Cuie2d63af2015-02-17 19:27:51 -0800392 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393
Colin Crosscd0f1732010-04-19 17:10:24 -0700394 if (wait)
395 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000396 if (mount(tmp, target, system, flags, options) < 0) {
397 return -1;
398 }
399
Ken Sumralldd4d7862011-02-17 18:09:47 -0800400 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401 } else if (!strncmp(source, "loop@", 5)) {
402 int mode, loop, fd;
403 struct loop_info info;
404
405 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800406 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407 if (fd < 0) {
408 return -1;
409 }
410
411 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800412 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800413 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000414 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100415 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000416 return -1;
417 }
418
419 /* if it is a blank loop device */
420 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
421 /* if it becomes our loop device */
422 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
423 close(fd);
424
425 if (mount(tmp, target, system, flags, options) < 0) {
426 ioctl(loop, LOOP_CLR_FD, 0);
427 close(loop);
428 return -1;
429 }
430
431 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800432 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000433 }
434 }
435
436 close(loop);
437 }
438
439 close(fd);
440 ERROR("out of loopback devices");
441 return -1;
442 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700443 if (wait)
444 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000445 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700446 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000447 }
448
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700449 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800450
451exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800452 return 0;
453
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454}
455
JP Abgrallcee20682014-07-02 14:26:54 -0700456static int wipe_data_via_recovery()
457{
458 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800459 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700460 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700461 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
462 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700463 close(fd);
464 } else {
465 ERROR("could not open /cache/recovery/command\n");
466 return -1;
467 }
468 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
469 while (1) { pause(); } // never reached
470}
471
472
473/*
474 * This function might request a reboot, in which case it will
475 * not return.
476 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700477int do_mount_all(int nargs, char **args)
478{
479 pid_t pid;
480 int ret = -1;
481 int child_ret = -1;
482 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800483 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700484
485 if (nargs != 2) {
486 return -1;
487 }
488
489 /*
490 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
491 * do the call in the child to provide protection to the main init
492 * process if anything goes wrong (crash or memory leak), and wait for
493 * the child to finish in the parent.
494 */
495 pid = fork();
496 if (pid > 0) {
497 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700498 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
499 if (wp_ret < 0) {
500 /* Unexpected error code. We will continue anyway. */
501 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
502 }
503
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 if (WIFEXITED(status)) {
505 ret = WEXITSTATUS(status);
506 } else {
507 ret = -1;
508 }
509 } else if (pid == 0) {
510 /* child, call fs_mgr_mount_all() */
511 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800512 fstab = fs_mgr_read_fstab(args[1]);
513 child_ret = fs_mgr_mount_all(fstab);
514 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515 if (child_ret == -1) {
516 ERROR("fs_mgr_mount_all returned an error\n");
517 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700518 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 } else {
520 /* fork failed, return an error */
521 return -1;
522 }
523
JP Abgrallf22b7452014-07-02 13:16:04 -0700524 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800525 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700526 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700527 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800528 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700529 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700530 property_set("ro.crypto.state", "unencrypted");
531 /* If fs_mgr determined this is an unencrypted device, then trigger
532 * that action.
533 */
534 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700535 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
536 /* Setup a wipe via recovery, and reboot into recovery */
537 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
538 ret = wipe_data_via_recovery();
539 /* If reboot worked, there is no return. */
540 } else if (ret > 0) {
541 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700542 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700543 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700544
545 return ret;
546}
547
Ken Sumralla76baaa2013-07-09 18:42:09 -0700548int do_swapon_all(int nargs, char **args)
549{
550 struct fstab *fstab;
551 int ret;
552
553 fstab = fs_mgr_read_fstab(args[1]);
554 ret = fs_mgr_swapon_all(fstab);
555 fs_mgr_free_fstab(fstab);
556
557 return ret;
558}
559
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500560int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500561 if (is_selinux_enabled() <= 0)
562 return 0;
563 if (setcon(args[1]) < 0) {
564 return -errno;
565 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500566 return 0;
567}
568
569int do_setenforce(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500570 if (is_selinux_enabled() <= 0)
571 return 0;
572 if (security_setenforce(atoi(args[1])) < 0) {
573 return -errno;
574 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500575 return 0;
576}
577
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578int do_setkey(int nargs, char **args)
579{
580 struct kbentry kbe;
581 kbe.kb_table = strtoul(args[1], 0, 0);
582 kbe.kb_index = strtoul(args[2], 0, 0);
583 kbe.kb_value = strtoul(args[3], 0, 0);
584 return setkey(&kbe);
585}
586
587int do_setprop(int nargs, char **args)
588{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700589 const char *name = args[1];
590 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800591 char prop_val[PROP_VALUE_MAX];
592 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700593
Dima Zavin84bf9af2011-12-20 13:44:41 -0800594 ret = expand_props(prop_val, value, sizeof(prop_val));
595 if (ret) {
596 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
597 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700598 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800599 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 return 0;
601}
602
603int do_setrlimit(int nargs, char **args)
604{
605 struct rlimit limit;
606 int resource;
607 resource = atoi(args[1]);
608 limit.rlim_cur = atoi(args[2]);
609 limit.rlim_max = atoi(args[3]);
610 return setrlimit(resource, &limit);
611}
612
613int do_start(int nargs, char **args)
614{
615 struct service *svc;
616 svc = service_find_by_name(args[1]);
617 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700618 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 }
620 return 0;
621}
622
623int do_stop(int nargs, char **args)
624{
625 struct service *svc;
626 svc = service_find_by_name(args[1]);
627 if (svc) {
628 service_stop(svc);
629 }
630 return 0;
631}
632
633int do_restart(int nargs, char **args)
634{
635 struct service *svc;
636 svc = service_find_by_name(args[1]);
637 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500638 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700639 }
640 return 0;
641}
642
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700643int do_powerctl(int nargs, char **args)
644{
645 char command[PROP_VALUE_MAX];
646 int res;
647 int len = 0;
648 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800649 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700650
651 res = expand_props(command, args[1], sizeof(command));
652 if (res) {
653 ERROR("powerctl: cannot expand '%s'\n", args[1]);
654 return -EINVAL;
655 }
656
657 if (strncmp(command, "shutdown", 8) == 0) {
658 cmd = ANDROID_RB_POWEROFF;
659 len = 8;
660 } else if (strncmp(command, "reboot", 6) == 0) {
661 cmd = ANDROID_RB_RESTART2;
662 len = 6;
663 } else {
664 ERROR("powerctl: unrecognized command '%s'\n", command);
665 return -EINVAL;
666 }
667
668 if (command[len] == ',') {
669 reboot_target = &command[len + 1];
670 } else if (command[len] == '\0') {
671 reboot_target = "";
672 } else {
673 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
674 return -EINVAL;
675 }
676
677 return android_reboot(cmd, 0, reboot_target);
678}
679
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700680int do_trigger(int nargs, char **args)
681{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000682 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700683 return 0;
684}
685
686int do_symlink(int nargs, char **args)
687{
688 return symlink(args[1], args[2]);
689}
690
Ken Sumrall203bad52011-01-18 17:37:41 -0800691int do_rm(int nargs, char **args)
692{
693 return unlink(args[1]);
694}
695
696int do_rmdir(int nargs, char **args)
697{
698 return rmdir(args[1]);
699}
700
The Android Open Source Project35237d12008-12-17 18:08:08 -0800701int do_sysclktz(int nargs, char **args)
702{
703 struct timezone tz;
704
705 if (nargs != 2)
706 return -1;
707
708 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800709 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800710 if (settimeofday(NULL, &tz))
711 return -1;
712 return 0;
713}
714
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000715int do_verity_load_state(int nargs, char **args) {
716 if (nargs == 1) {
717 int mode = -1;
718 int rc = fs_mgr_load_verity_state(&mode);
719
720 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
721 action_for_each_trigger("verity-logging", action_add_queue_tail);
722 }
723
724 return rc;
725 }
726 return -1;
727}
728
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729int do_write(int nargs, char **args)
730{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700731 const char *path = args[1];
732 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700733
Johan Redestig7e952f42014-12-05 00:36:41 +0100734 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800735 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800736 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
737 return -EINVAL;
738 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800739 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700740}
741
San Mehat7c44fe52009-08-26 16:39:25 -0700742int do_copy(int nargs, char **args)
743{
744 char *buffer = NULL;
745 int rc = 0;
746 int fd1 = -1, fd2 = -1;
747 struct stat info;
748 int brtw, brtr;
749 char *p;
750
751 if (nargs != 3)
752 return -1;
753
Elliott Hughes24627902015-02-04 10:25:09 -0800754 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700755 return -1;
756
Nick Kralevich45a884f2015-02-02 14:37:22 -0800757 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700758 goto out_err;
759
Nick Kralevich45a884f2015-02-02 14:37:22 -0800760 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700761 goto out_err;
762
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800763 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700764 goto out_err;
765
766 p = buffer;
767 brtr = info.st_size;
768 while(brtr) {
769 rc = read(fd1, p, brtr);
770 if (rc < 0)
771 goto out_err;
772 if (rc == 0)
773 break;
774 p += rc;
775 brtr -= rc;
776 }
777
778 p = buffer;
779 brtw = info.st_size;
780 while(brtw) {
781 rc = write(fd2, p, brtw);
782 if (rc < 0)
783 goto out_err;
784 if (rc == 0)
785 break;
786 p += rc;
787 brtw -= rc;
788 }
789
790 rc = 0;
791 goto out;
792out_err:
793 rc = -1;
794out:
795 if (buffer)
796 free(buffer);
797 if (fd1 >= 0)
798 close(fd1);
799 if (fd2 >= 0)
800 close(fd2);
801 return rc;
802}
803
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804int do_chown(int nargs, char **args) {
805 /* GID is optional. */
806 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800807 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808 return -errno;
809 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800810 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 return -errno;
812 } else {
813 return -1;
814 }
815 return 0;
816}
817
818static mode_t get_mode(const char *s) {
819 mode_t mode = 0;
820 while (*s) {
821 if (*s >= '0' && *s <= '7') {
822 mode = (mode<<3) | (*s-'0');
823 } else {
824 return -1;
825 }
826 s++;
827 }
828 return mode;
829}
830
831int do_chmod(int nargs, char **args) {
832 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800833 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834 return -errno;
835 }
836 return 0;
837}
838
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500839int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500840 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400841 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500842
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500843 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400844 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400845 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500846 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400847 return ret;
848}
849
850int do_restorecon_recursive(int nargs, char **args) {
851 int i;
852 int ret = 0;
853
854 for (i = 1; i < nargs; i++) {
855 if (restorecon_recursive(args[i]) < 0)
856 ret = -errno;
857 }
858 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500859}
860
861int do_setsebool(int nargs, char **args) {
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500862 const char *name = args[1];
863 const char *value = args[2];
864 SELboolean b;
865 int ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500866
867 if (is_selinux_enabled() <= 0)
868 return 0;
869
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500870 b.name = name;
871 if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
872 b.value = 1;
873 else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
874 b.value = 0;
875 else {
876 ERROR("setsebool: invalid value %s\n", value);
877 return -EINVAL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500878 }
879
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500880 if (security_set_boolean_list(1, &b, 0) < 0) {
881 ret = -errno;
882 ERROR("setsebool: could not set %s to %s\n", name, value);
883 return ret;
884 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700885
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500886 return 0;
887}
888
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700890 int log_level;
891 char log_level_str[PROP_VALUE_MAX] = "";
892 if (nargs != 2) {
893 ERROR("loglevel: missing argument\n");
894 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700896
897 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
898 ERROR("loglevel: cannot expand '%s'\n", args[1]);
899 return -EINVAL;
900 }
901 log_level = atoi(log_level_str);
902 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
903 ERROR("loglevel: invalid log level'%d'\n", log_level);
904 return -EINVAL;
905 }
906 klog_set_level(log_level);
907 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908}
909
Ken Sumrallc5c51032011-03-08 17:01:29 -0800910int do_load_persist_props(int nargs, char **args) {
911 if (nargs == 1) {
912 load_persist_props();
913 return 0;
914 }
915 return -1;
916}
917
Riley Andrewse4b7b292014-06-16 15:06:21 -0700918int do_load_all_props(int nargs, char **args) {
919 if (nargs == 1) {
920 load_all_props();
921 return 0;
922 }
923 return -1;
924}
925
Colin Crosscd0f1732010-04-19 17:10:24 -0700926int do_wait(int nargs, char **args)
927{
928 if (nargs == 2) {
929 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800930 } else if (nargs == 3) {
931 return wait_for_file(args[1], atoi(args[2]));
932 } else
933 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700934}