blob: 03b143d042c097ad1a5abe70dc9db11881781982 [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
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800175int do_exec(int nargs, char** args) {
176 service* svc = make_exec_oneshot_service(nargs, args);
177 if (svc == NULL) {
178 return -1;
179 }
180 service_start(svc, NULL);
181 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800184// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700185int do_execonce(int nargs, char **args)
186{
187 pid_t child;
188 int child_status = 0;
189 static int already_done;
190
191 if (already_done) {
192 return -1;
193 }
194 already_done = 1;
195 if (!(child = fork())) {
196 /*
197 * Child process.
198 */
199 zap_stdio();
200 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800201 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700202
203 memset(exec_args, 0, sizeof(exec_args));
204 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800205 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700206 ARRAY_SIZE(exec_args) - 1);
207 _exit(1);
208 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800209 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700210 exec_args[i - 1] = args[i];
211
212 if (execv(exec_args[0], exec_args) == -1) {
213 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
214 _exit(1);
215 }
216 ERROR("Returned from execv()!");
217 _exit(1);
218 }
219
220 /*
221 * Parent process.
222 */
223 if (child == -1) {
224 ERROR("Fork failed\n");
225 return -1;
226 }
227
228 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
229 ERROR("waitpid(): failed (%s)\n", strerror(errno));
230 return -1;
231 }
232
233 if (WIFSIGNALED(child_status)) {
234 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
235 return -1;
236 } else if (WIFEXITED(child_status)) {
237 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
238 return WEXITSTATUS(child_status);
239 }
240
241 ERROR("Abnormal child process exit\n");
242
243 return -1;
244}
245
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700246int do_export(int nargs, char **args)
247{
James Morrissey381341f2014-05-16 11:36:36 +0100248 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249}
250
251int do_hostname(int nargs, char **args)
252{
253 return write_file("/proc/sys/kernel/hostname", args[1]);
254}
255
256int do_ifup(int nargs, char **args)
257{
258 return __ifupdown(args[1], 1);
259}
260
The Android Open Source Project35237d12008-12-17 18:08:08 -0800261
262static int do_insmod_inner(int nargs, char **args, int opt_len)
263{
264 char options[opt_len + 1];
265 int i;
266
267 options[0] = '\0';
268 if (nargs > 2) {
269 strcpy(options, args[2]);
270 for (i = 3; i < nargs; ++i) {
271 strcat(options, " ");
272 strcat(options, args[i]);
273 }
274 }
275
276 return insmod(args[1], options);
277}
278
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279int do_insmod(int nargs, char **args)
280{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800281 int i;
282 int size = 0;
283
284 if (nargs > 2) {
285 for (i = 2; i < nargs; ++i)
286 size += strlen(args[i]) + 1;
287 }
288
289 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290}
291
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292int do_mkdir(int nargs, char **args)
293{
294 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700295 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296
297 /* mkdir <path> [mode] [owner] [group] */
298
299 if (nargs >= 3) {
300 mode = strtoul(args[2], 0, 8);
301 }
302
Stephen Smalleye096e362012-06-11 13:37:39 -0400303 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700304 /* chmod in case the directory already exists */
305 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800306 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700307 }
308 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 return -errno;
310 }
311
312 if (nargs >= 4) {
313 uid_t uid = decode_uid(args[3]);
314 gid_t gid = -1;
315
316 if (nargs == 5) {
317 gid = decode_uid(args[4]);
318 }
319
Nick Kralevichbc609542015-01-31 21:39:46 -0800320 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 return -errno;
322 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700323
324 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
325 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800326 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700327 if (ret == -1) {
328 return -errno;
329 }
330 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700331 }
332
333 return 0;
334}
335
336static struct {
337 const char *name;
338 unsigned flag;
339} mount_flags[] = {
340 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200341 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 { "nosuid", MS_NOSUID },
343 { "nodev", MS_NODEV },
344 { "nodiratime", MS_NODIRATIME },
345 { "ro", MS_RDONLY },
346 { "rw", 0 },
347 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700348 { "bind", MS_BIND },
349 { "rec", MS_REC },
350 { "unbindable", MS_UNBINDABLE },
351 { "private", MS_PRIVATE },
352 { "slave", MS_SLAVE },
353 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 { "defaults", 0 },
355 { 0, 0 },
356};
357
Ken Sumrall752923c2010-12-03 16:33:31 -0800358#define DATA_MNT_POINT "/data"
359
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360/* mount <type> <device> <path> <flags ...> <options> */
361int do_mount(int nargs, char **args)
362{
363 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 char *options = NULL;
366 unsigned flags = 0;
367 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700368 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369
370 for (n = 4; n < nargs; n++) {
371 for (i = 0; mount_flags[i].name; i++) {
372 if (!strcmp(args[n], mount_flags[i].name)) {
373 flags |= mount_flags[i].flag;
374 break;
375 }
376 }
377
Colin Crosscd0f1732010-04-19 17:10:24 -0700378 if (!mount_flags[i].name) {
379 if (!strcmp(args[n], "wait"))
380 wait = 1;
381 /* if our last argument isn't a flag, wolf it up as an option string */
382 else if (n + 1 == nargs)
383 options = args[n];
384 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 }
386
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389 target = args[3];
390
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700391 if (!strncmp(source, "mtd@", 4)) {
392 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 if (n < 0) {
394 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000396
Yabin Cuie2d63af2015-02-17 19:27:51 -0800397 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398
Colin Crosscd0f1732010-04-19 17:10:24 -0700399 if (wait)
400 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401 if (mount(tmp, target, system, flags, options) < 0) {
402 return -1;
403 }
404
Ken Sumralldd4d7862011-02-17 18:09:47 -0800405 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000406 } else if (!strncmp(source, "loop@", 5)) {
407 int mode, loop, fd;
408 struct loop_info info;
409
410 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800411 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412 if (fd < 0) {
413 return -1;
414 }
415
416 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800417 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800418 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000419 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100420 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000421 return -1;
422 }
423
424 /* if it is a blank loop device */
425 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
426 /* if it becomes our loop device */
427 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
428 close(fd);
429
430 if (mount(tmp, target, system, flags, options) < 0) {
431 ioctl(loop, LOOP_CLR_FD, 0);
432 close(loop);
433 return -1;
434 }
435
436 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800437 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000438 }
439 }
440
441 close(loop);
442 }
443
444 close(fd);
445 ERROR("out of loopback devices");
446 return -1;
447 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700448 if (wait)
449 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000450 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700451 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000452 }
453
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800455
456exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800457 return 0;
458
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459}
460
JP Abgrallcee20682014-07-02 14:26:54 -0700461static int wipe_data_via_recovery()
462{
463 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800464 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700465 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700466 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
467 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700468 close(fd);
469 } else {
470 ERROR("could not open /cache/recovery/command\n");
471 return -1;
472 }
473 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
474 while (1) { pause(); } // never reached
475}
476
477
478/*
479 * This function might request a reboot, in which case it will
480 * not return.
481 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700482int do_mount_all(int nargs, char **args)
483{
484 pid_t pid;
485 int ret = -1;
486 int child_ret = -1;
487 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800488 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489
490 if (nargs != 2) {
491 return -1;
492 }
493
494 /*
495 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
496 * do the call in the child to provide protection to the main init
497 * process if anything goes wrong (crash or memory leak), and wait for
498 * the child to finish in the parent.
499 */
500 pid = fork();
501 if (pid > 0) {
502 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700503 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
504 if (wp_ret < 0) {
505 /* Unexpected error code. We will continue anyway. */
506 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
507 }
508
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700509 if (WIFEXITED(status)) {
510 ret = WEXITSTATUS(status);
511 } else {
512 ret = -1;
513 }
514 } else if (pid == 0) {
515 /* child, call fs_mgr_mount_all() */
516 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800517 fstab = fs_mgr_read_fstab(args[1]);
518 child_ret = fs_mgr_mount_all(fstab);
519 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700520 if (child_ret == -1) {
521 ERROR("fs_mgr_mount_all returned an error\n");
522 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700523 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700524 } else {
525 /* fork failed, return an error */
526 return -1;
527 }
528
JP Abgrallf22b7452014-07-02 13:16:04 -0700529 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800530 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700531 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700532 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800533 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700534 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700535 property_set("ro.crypto.state", "unencrypted");
536 /* If fs_mgr determined this is an unencrypted device, then trigger
537 * that action.
538 */
539 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700540 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
541 /* Setup a wipe via recovery, and reboot into recovery */
542 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
543 ret = wipe_data_via_recovery();
544 /* If reboot worked, there is no return. */
545 } else if (ret > 0) {
546 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700547 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700548 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700549
550 return ret;
551}
552
Ken Sumralla76baaa2013-07-09 18:42:09 -0700553int do_swapon_all(int nargs, char **args)
554{
555 struct fstab *fstab;
556 int ret;
557
558 fstab = fs_mgr_read_fstab(args[1]);
559 ret = fs_mgr_swapon_all(fstab);
560 fs_mgr_free_fstab(fstab);
561
562 return ret;
563}
564
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500565int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500566 if (is_selinux_enabled() <= 0)
567 return 0;
568 if (setcon(args[1]) < 0) {
569 return -errno;
570 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500571 return 0;
572}
573
574int do_setenforce(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500575 if (is_selinux_enabled() <= 0)
576 return 0;
577 if (security_setenforce(atoi(args[1])) < 0) {
578 return -errno;
579 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500580 return 0;
581}
582
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583int do_setkey(int nargs, char **args)
584{
585 struct kbentry kbe;
586 kbe.kb_table = strtoul(args[1], 0, 0);
587 kbe.kb_index = strtoul(args[2], 0, 0);
588 kbe.kb_value = strtoul(args[3], 0, 0);
589 return setkey(&kbe);
590}
591
592int do_setprop(int nargs, char **args)
593{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700594 const char *name = args[1];
595 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800596 char prop_val[PROP_VALUE_MAX];
597 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700598
Dima Zavin84bf9af2011-12-20 13:44:41 -0800599 ret = expand_props(prop_val, value, sizeof(prop_val));
600 if (ret) {
601 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
602 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700603 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800604 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700605 return 0;
606}
607
608int do_setrlimit(int nargs, char **args)
609{
610 struct rlimit limit;
611 int resource;
612 resource = atoi(args[1]);
613 limit.rlim_cur = atoi(args[2]);
614 limit.rlim_max = atoi(args[3]);
615 return setrlimit(resource, &limit);
616}
617
618int do_start(int nargs, char **args)
619{
620 struct service *svc;
621 svc = service_find_by_name(args[1]);
622 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700623 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700624 }
625 return 0;
626}
627
628int do_stop(int nargs, char **args)
629{
630 struct service *svc;
631 svc = service_find_by_name(args[1]);
632 if (svc) {
633 service_stop(svc);
634 }
635 return 0;
636}
637
638int do_restart(int nargs, char **args)
639{
640 struct service *svc;
641 svc = service_find_by_name(args[1]);
642 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500643 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700644 }
645 return 0;
646}
647
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700648int do_powerctl(int nargs, char **args)
649{
650 char command[PROP_VALUE_MAX];
651 int res;
652 int len = 0;
653 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800654 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700655
656 res = expand_props(command, args[1], sizeof(command));
657 if (res) {
658 ERROR("powerctl: cannot expand '%s'\n", args[1]);
659 return -EINVAL;
660 }
661
662 if (strncmp(command, "shutdown", 8) == 0) {
663 cmd = ANDROID_RB_POWEROFF;
664 len = 8;
665 } else if (strncmp(command, "reboot", 6) == 0) {
666 cmd = ANDROID_RB_RESTART2;
667 len = 6;
668 } else {
669 ERROR("powerctl: unrecognized command '%s'\n", command);
670 return -EINVAL;
671 }
672
673 if (command[len] == ',') {
674 reboot_target = &command[len + 1];
675 } else if (command[len] == '\0') {
676 reboot_target = "";
677 } else {
678 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
679 return -EINVAL;
680 }
681
682 return android_reboot(cmd, 0, reboot_target);
683}
684
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700685int do_trigger(int nargs, char **args)
686{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000687 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700688 return 0;
689}
690
691int do_symlink(int nargs, char **args)
692{
693 return symlink(args[1], args[2]);
694}
695
Ken Sumrall203bad52011-01-18 17:37:41 -0800696int do_rm(int nargs, char **args)
697{
698 return unlink(args[1]);
699}
700
701int do_rmdir(int nargs, char **args)
702{
703 return rmdir(args[1]);
704}
705
The Android Open Source Project35237d12008-12-17 18:08:08 -0800706int do_sysclktz(int nargs, char **args)
707{
708 struct timezone tz;
709
710 if (nargs != 2)
711 return -1;
712
713 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800714 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800715 if (settimeofday(NULL, &tz))
716 return -1;
717 return 0;
718}
719
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720int do_write(int nargs, char **args)
721{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700722 const char *path = args[1];
723 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700724
Johan Redestig7e952f42014-12-05 00:36:41 +0100725 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800726 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800727 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
728 return -EINVAL;
729 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800730 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731}
732
San Mehat7c44fe52009-08-26 16:39:25 -0700733int do_copy(int nargs, char **args)
734{
735 char *buffer = NULL;
736 int rc = 0;
737 int fd1 = -1, fd2 = -1;
738 struct stat info;
739 int brtw, brtr;
740 char *p;
741
742 if (nargs != 3)
743 return -1;
744
Elliott Hughes24627902015-02-04 10:25:09 -0800745 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700746 return -1;
747
Nick Kralevich45a884f2015-02-02 14:37:22 -0800748 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700749 goto out_err;
750
Nick Kralevich45a884f2015-02-02 14:37:22 -0800751 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700752 goto out_err;
753
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800754 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700755 goto out_err;
756
757 p = buffer;
758 brtr = info.st_size;
759 while(brtr) {
760 rc = read(fd1, p, brtr);
761 if (rc < 0)
762 goto out_err;
763 if (rc == 0)
764 break;
765 p += rc;
766 brtr -= rc;
767 }
768
769 p = buffer;
770 brtw = info.st_size;
771 while(brtw) {
772 rc = write(fd2, p, brtw);
773 if (rc < 0)
774 goto out_err;
775 if (rc == 0)
776 break;
777 p += rc;
778 brtw -= rc;
779 }
780
781 rc = 0;
782 goto out;
783out_err:
784 rc = -1;
785out:
786 if (buffer)
787 free(buffer);
788 if (fd1 >= 0)
789 close(fd1);
790 if (fd2 >= 0)
791 close(fd2);
792 return rc;
793}
794
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795int do_chown(int nargs, char **args) {
796 /* GID is optional. */
797 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800798 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799 return -errno;
800 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800801 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802 return -errno;
803 } else {
804 return -1;
805 }
806 return 0;
807}
808
809static mode_t get_mode(const char *s) {
810 mode_t mode = 0;
811 while (*s) {
812 if (*s >= '0' && *s <= '7') {
813 mode = (mode<<3) | (*s-'0');
814 } else {
815 return -1;
816 }
817 s++;
818 }
819 return mode;
820}
821
822int do_chmod(int nargs, char **args) {
823 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800824 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 return -errno;
826 }
827 return 0;
828}
829
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500830int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500831 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400832 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500833
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500834 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400835 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400836 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500837 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400838 return ret;
839}
840
841int do_restorecon_recursive(int nargs, char **args) {
842 int i;
843 int ret = 0;
844
845 for (i = 1; i < nargs; i++) {
846 if (restorecon_recursive(args[i]) < 0)
847 ret = -errno;
848 }
849 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500850}
851
852int do_setsebool(int nargs, char **args) {
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500853 const char *name = args[1];
854 const char *value = args[2];
855 SELboolean b;
856 int ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500857
858 if (is_selinux_enabled() <= 0)
859 return 0;
860
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500861 b.name = name;
862 if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
863 b.value = 1;
864 else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
865 b.value = 0;
866 else {
867 ERROR("setsebool: invalid value %s\n", value);
868 return -EINVAL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500869 }
870
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500871 if (security_set_boolean_list(1, &b, 0) < 0) {
872 ret = -errno;
873 ERROR("setsebool: could not set %s to %s\n", name, value);
874 return ret;
875 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700876
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500877 return 0;
878}
879
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700881 int log_level;
882 char log_level_str[PROP_VALUE_MAX] = "";
883 if (nargs != 2) {
884 ERROR("loglevel: missing argument\n");
885 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700887
888 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
889 ERROR("loglevel: cannot expand '%s'\n", args[1]);
890 return -EINVAL;
891 }
892 log_level = atoi(log_level_str);
893 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
894 ERROR("loglevel: invalid log level'%d'\n", log_level);
895 return -EINVAL;
896 }
897 klog_set_level(log_level);
898 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899}
900
Ken Sumrallc5c51032011-03-08 17:01:29 -0800901int do_load_persist_props(int nargs, char **args) {
902 if (nargs == 1) {
903 load_persist_props();
904 return 0;
905 }
906 return -1;
907}
908
Riley Andrewse4b7b292014-06-16 15:06:21 -0700909int do_load_all_props(int nargs, char **args) {
910 if (nargs == 1) {
911 load_all_props();
912 return 0;
913 }
914 return -1;
915}
916
Colin Crosscd0f1732010-04-19 17:10:24 -0700917int do_wait(int nargs, char **args)
918{
919 if (nargs == 2) {
920 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800921 } else if (nargs == 3) {
922 return wait_for_file(args[1], atoi(args[2]));
923 } else
924 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700925}