blob: 86c9c2e0761d594e010b4fa1f11d02c933681ff5 [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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574int do_setkey(int nargs, char **args)
575{
576 struct kbentry kbe;
577 kbe.kb_table = strtoul(args[1], 0, 0);
578 kbe.kb_index = strtoul(args[2], 0, 0);
579 kbe.kb_value = strtoul(args[3], 0, 0);
580 return setkey(&kbe);
581}
582
583int do_setprop(int nargs, char **args)
584{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700585 const char *name = args[1];
586 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800587 char prop_val[PROP_VALUE_MAX];
588 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700589
Dima Zavin84bf9af2011-12-20 13:44:41 -0800590 ret = expand_props(prop_val, value, sizeof(prop_val));
591 if (ret) {
592 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
593 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700594 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800595 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596 return 0;
597}
598
599int do_setrlimit(int nargs, char **args)
600{
601 struct rlimit limit;
602 int resource;
603 resource = atoi(args[1]);
604 limit.rlim_cur = atoi(args[2]);
605 limit.rlim_max = atoi(args[3]);
606 return setrlimit(resource, &limit);
607}
608
609int do_start(int nargs, char **args)
610{
611 struct service *svc;
612 svc = service_find_by_name(args[1]);
613 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700614 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700615 }
616 return 0;
617}
618
619int do_stop(int nargs, char **args)
620{
621 struct service *svc;
622 svc = service_find_by_name(args[1]);
623 if (svc) {
624 service_stop(svc);
625 }
626 return 0;
627}
628
629int do_restart(int nargs, char **args)
630{
631 struct service *svc;
632 svc = service_find_by_name(args[1]);
633 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500634 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700635 }
636 return 0;
637}
638
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700639int do_powerctl(int nargs, char **args)
640{
641 char command[PROP_VALUE_MAX];
642 int res;
643 int len = 0;
644 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800645 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646
647 res = expand_props(command, args[1], sizeof(command));
648 if (res) {
649 ERROR("powerctl: cannot expand '%s'\n", args[1]);
650 return -EINVAL;
651 }
652
653 if (strncmp(command, "shutdown", 8) == 0) {
654 cmd = ANDROID_RB_POWEROFF;
655 len = 8;
656 } else if (strncmp(command, "reboot", 6) == 0) {
657 cmd = ANDROID_RB_RESTART2;
658 len = 6;
659 } else {
660 ERROR("powerctl: unrecognized command '%s'\n", command);
661 return -EINVAL;
662 }
663
664 if (command[len] == ',') {
665 reboot_target = &command[len + 1];
666 } else if (command[len] == '\0') {
667 reboot_target = "";
668 } else {
669 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
670 return -EINVAL;
671 }
672
673 return android_reboot(cmd, 0, reboot_target);
674}
675
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700676int do_trigger(int nargs, char **args)
677{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000678 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700679 return 0;
680}
681
682int do_symlink(int nargs, char **args)
683{
684 return symlink(args[1], args[2]);
685}
686
Ken Sumrall203bad52011-01-18 17:37:41 -0800687int do_rm(int nargs, char **args)
688{
689 return unlink(args[1]);
690}
691
692int do_rmdir(int nargs, char **args)
693{
694 return rmdir(args[1]);
695}
696
The Android Open Source Project35237d12008-12-17 18:08:08 -0800697int do_sysclktz(int nargs, char **args)
698{
699 struct timezone tz;
700
701 if (nargs != 2)
702 return -1;
703
704 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800705 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800706 if (settimeofday(NULL, &tz))
707 return -1;
708 return 0;
709}
710
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000711int do_verity_load_state(int nargs, char **args) {
712 if (nargs == 1) {
713 int mode = -1;
714 int rc = fs_mgr_load_verity_state(&mode);
715
716 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
717 action_for_each_trigger("verity-logging", action_add_queue_tail);
718 }
719
720 return rc;
721 }
722 return -1;
723}
724
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700725int do_write(int nargs, char **args)
726{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700727 const char *path = args[1];
728 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700729
Johan Redestig7e952f42014-12-05 00:36:41 +0100730 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800731 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800732 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
733 return -EINVAL;
734 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800735 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736}
737
San Mehat7c44fe52009-08-26 16:39:25 -0700738int do_copy(int nargs, char **args)
739{
740 char *buffer = NULL;
741 int rc = 0;
742 int fd1 = -1, fd2 = -1;
743 struct stat info;
744 int brtw, brtr;
745 char *p;
746
747 if (nargs != 3)
748 return -1;
749
Elliott Hughes24627902015-02-04 10:25:09 -0800750 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700751 return -1;
752
Nick Kralevich45a884f2015-02-02 14:37:22 -0800753 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700754 goto out_err;
755
Nick Kralevich45a884f2015-02-02 14:37:22 -0800756 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700757 goto out_err;
758
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800759 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700760 goto out_err;
761
762 p = buffer;
763 brtr = info.st_size;
764 while(brtr) {
765 rc = read(fd1, p, brtr);
766 if (rc < 0)
767 goto out_err;
768 if (rc == 0)
769 break;
770 p += rc;
771 brtr -= rc;
772 }
773
774 p = buffer;
775 brtw = info.st_size;
776 while(brtw) {
777 rc = write(fd2, p, brtw);
778 if (rc < 0)
779 goto out_err;
780 if (rc == 0)
781 break;
782 p += rc;
783 brtw -= rc;
784 }
785
786 rc = 0;
787 goto out;
788out_err:
789 rc = -1;
790out:
791 if (buffer)
792 free(buffer);
793 if (fd1 >= 0)
794 close(fd1);
795 if (fd2 >= 0)
796 close(fd2);
797 return rc;
798}
799
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800int do_chown(int nargs, char **args) {
801 /* GID is optional. */
802 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800803 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 return -errno;
805 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800806 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 return -errno;
808 } else {
809 return -1;
810 }
811 return 0;
812}
813
814static mode_t get_mode(const char *s) {
815 mode_t mode = 0;
816 while (*s) {
817 if (*s >= '0' && *s <= '7') {
818 mode = (mode<<3) | (*s-'0');
819 } else {
820 return -1;
821 }
822 s++;
823 }
824 return mode;
825}
826
827int do_chmod(int nargs, char **args) {
828 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800829 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 return -errno;
831 }
832 return 0;
833}
834
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500835int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500836 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400837 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500838
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500839 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400840 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400841 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500842 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400843 return ret;
844}
845
846int do_restorecon_recursive(int nargs, char **args) {
847 int i;
848 int ret = 0;
849
850 for (i = 1; i < nargs; i++) {
851 if (restorecon_recursive(args[i]) < 0)
852 ret = -errno;
853 }
854 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500855}
856
857int do_setsebool(int nargs, char **args) {
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500858 const char *name = args[1];
859 const char *value = args[2];
860 SELboolean b;
861 int ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500862
863 if (is_selinux_enabled() <= 0)
864 return 0;
865
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500866 b.name = name;
867 if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
868 b.value = 1;
869 else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
870 b.value = 0;
871 else {
872 ERROR("setsebool: invalid value %s\n", value);
873 return -EINVAL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500874 }
875
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500876 if (security_set_boolean_list(1, &b, 0) < 0) {
877 ret = -errno;
878 ERROR("setsebool: could not set %s to %s\n", name, value);
879 return ret;
880 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700881
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500882 return 0;
883}
884
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700886 int log_level;
887 char log_level_str[PROP_VALUE_MAX] = "";
888 if (nargs != 2) {
889 ERROR("loglevel: missing argument\n");
890 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700892
893 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
894 ERROR("loglevel: cannot expand '%s'\n", args[1]);
895 return -EINVAL;
896 }
897 log_level = atoi(log_level_str);
898 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
899 ERROR("loglevel: invalid log level'%d'\n", log_level);
900 return -EINVAL;
901 }
902 klog_set_level(log_level);
903 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904}
905
Ken Sumrallc5c51032011-03-08 17:01:29 -0800906int do_load_persist_props(int nargs, char **args) {
907 if (nargs == 1) {
908 load_persist_props();
909 return 0;
910 }
911 return -1;
912}
913
Riley Andrewse4b7b292014-06-16 15:06:21 -0700914int do_load_all_props(int nargs, char **args) {
915 if (nargs == 1) {
916 load_all_props();
917 return 0;
918 }
919 return -1;
920}
921
Colin Crosscd0f1732010-04-19 17:10:24 -0700922int do_wait(int nargs, char **args)
923{
924 if (nargs == 2) {
925 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800926 } else if (nargs == 3) {
927 return wait_for_file(args[1], atoi(args[2]));
928 } else
929 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700930}