blob: 543f89bfd500911d3a41a2b5f0a19978f902f0b0 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdio.h>
23#include <linux/kd.h>
24#include <errno.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <linux/if.h>
28#include <arpa/inet.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080032#include <sys/time.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070033#include <sys/wait.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000034#include <linux/loop.h>
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -070035#include <cutils/partition_utils.h>
Nick Kralevichca8e66a2013-04-18 12:20:02 -070036#include <cutils/android_reboot.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070037#include <fs_mgr.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042#include "init.h"
43#include "keywords.h"
44#include "property_service.h"
45#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070046#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
50#include <private/android_filesystem_config.h>
51
Nick Kralevichbc609542015-01-31 21:39:46 -080052#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
53
James Morrissey381341f2014-05-16 11:36:36 +010054int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
Elliott Hughesf3cf4382015-02-03 17:12:07 -080056// System call provided by bionic but not in any header file.
57extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
The Android Open Source Project35237d12008-12-17 18:08:08 -080059static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060{
Elliott Hughesf682b472015-02-06 12:19:48 -080061 std::string module;
62 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080064 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Elliott Hughesf682b472015-02-06 12:19:48 -080066 // TODO: use finit_module for >= 3.8 kernels.
67 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068}
69
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070static int __ifupdown(const char *interface, int up)
71{
72 struct ifreq ifr;
73 int s, ret;
74
75 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
76
77 s = socket(AF_INET, SOCK_DGRAM, 0);
78 if (s < 0)
79 return -1;
80
81 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
82 if (ret < 0) {
83 goto done;
84 }
85
86 if (up)
87 ifr.ifr_flags |= IFF_UP;
88 else
89 ifr.ifr_flags &= ~IFF_UP;
90
91 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080092
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093done:
94 close(s);
95 return ret;
96}
97
98static void service_start_if_not_disabled(struct service *svc)
99{
100 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700101 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700102 } else {
103 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 }
105}
106
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000107int do_chroot(int nargs, char **args)
108{
109 chroot(args[1]);
110 return 0;
111}
112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113int do_class_start(int nargs, char **args)
114{
115 /* Starting a class does not start services
116 * which are explicitly disabled. They must
117 * be started individually.
118 */
119 service_for_each_class(args[1], service_start_if_not_disabled);
120 return 0;
121}
122
123int do_class_stop(int nargs, char **args)
124{
125 service_for_each_class(args[1], service_stop);
126 return 0;
127}
128
Ken Sumrall752923c2010-12-03 16:33:31 -0800129int do_class_reset(int nargs, char **args)
130{
131 service_for_each_class(args[1], service_reset);
132 return 0;
133}
134
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135int do_domainname(int nargs, char **args)
136{
137 return write_file("/proc/sys/kernel/domainname", args[1]);
138}
139
JP Abgrall3beec7e2014-05-02 21:14:29 -0700140int do_enable(int nargs, char **args)
141{
142 struct service *svc;
143 svc = service_find_by_name(args[1]);
144 if (svc) {
145 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
146 if (svc->flags & SVC_DISABLED_START) {
147 service_start(svc, NULL);
148 }
149 } else {
150 return -1;
151 }
152 return 0;
153}
154
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800155int do_exec(int nargs, char** args) {
156 service* svc = make_exec_oneshot_service(nargs, args);
157 if (svc == NULL) {
158 return -1;
159 }
160 service_start(svc, NULL);
161 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162}
163
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800164// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700165int do_execonce(int nargs, char **args)
166{
167 pid_t child;
168 int child_status = 0;
169 static int already_done;
170
171 if (already_done) {
172 return -1;
173 }
174 already_done = 1;
175 if (!(child = fork())) {
176 /*
177 * Child process.
178 */
179 zap_stdio();
180 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800181 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700182
183 memset(exec_args, 0, sizeof(exec_args));
184 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800185 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700186 ARRAY_SIZE(exec_args) - 1);
187 _exit(1);
188 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800189 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700190 exec_args[i - 1] = args[i];
191
192 if (execv(exec_args[0], exec_args) == -1) {
193 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
194 _exit(1);
195 }
196 ERROR("Returned from execv()!");
197 _exit(1);
198 }
199
200 /*
201 * Parent process.
202 */
203 if (child == -1) {
204 ERROR("Fork failed\n");
205 return -1;
206 }
207
208 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
209 ERROR("waitpid(): failed (%s)\n", strerror(errno));
210 return -1;
211 }
212
213 if (WIFSIGNALED(child_status)) {
214 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
215 return -1;
216 } else if (WIFEXITED(child_status)) {
217 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
218 return WEXITSTATUS(child_status);
219 }
220
221 ERROR("Abnormal child process exit\n");
222
223 return -1;
224}
225
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226int do_export(int nargs, char **args)
227{
James Morrissey381341f2014-05-16 11:36:36 +0100228 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
231int do_hostname(int nargs, char **args)
232{
233 return write_file("/proc/sys/kernel/hostname", args[1]);
234}
235
236int do_ifup(int nargs, char **args)
237{
238 return __ifupdown(args[1], 1);
239}
240
The Android Open Source Project35237d12008-12-17 18:08:08 -0800241
242static int do_insmod_inner(int nargs, char **args, int opt_len)
243{
244 char options[opt_len + 1];
245 int i;
246
247 options[0] = '\0';
248 if (nargs > 2) {
249 strcpy(options, args[2]);
250 for (i = 3; i < nargs; ++i) {
251 strcat(options, " ");
252 strcat(options, args[i]);
253 }
254 }
255
256 return insmod(args[1], options);
257}
258
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259int do_insmod(int nargs, char **args)
260{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800261 int i;
262 int size = 0;
263
264 if (nargs > 2) {
265 for (i = 2; i < nargs; ++i)
266 size += strlen(args[i]) + 1;
267 }
268
269 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270}
271
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272int do_mkdir(int nargs, char **args)
273{
274 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700275 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276
277 /* mkdir <path> [mode] [owner] [group] */
278
279 if (nargs >= 3) {
280 mode = strtoul(args[2], 0, 8);
281 }
282
Stephen Smalleye096e362012-06-11 13:37:39 -0400283 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700284 /* chmod in case the directory already exists */
285 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800286 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700287 }
288 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289 return -errno;
290 }
291
292 if (nargs >= 4) {
293 uid_t uid = decode_uid(args[3]);
294 gid_t gid = -1;
295
296 if (nargs == 5) {
297 gid = decode_uid(args[4]);
298 }
299
Nick Kralevichbc609542015-01-31 21:39:46 -0800300 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 return -errno;
302 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700303
304 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
305 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800306 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700307 if (ret == -1) {
308 return -errno;
309 }
310 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
312
313 return 0;
314}
315
316static struct {
317 const char *name;
318 unsigned flag;
319} mount_flags[] = {
320 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200321 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322 { "nosuid", MS_NOSUID },
323 { "nodev", MS_NODEV },
324 { "nodiratime", MS_NODIRATIME },
325 { "ro", MS_RDONLY },
326 { "rw", 0 },
327 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700328 { "bind", MS_BIND },
329 { "rec", MS_REC },
330 { "unbindable", MS_UNBINDABLE },
331 { "private", MS_PRIVATE },
332 { "slave", MS_SLAVE },
333 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334 { "defaults", 0 },
335 { 0, 0 },
336};
337
Ken Sumrall752923c2010-12-03 16:33:31 -0800338#define DATA_MNT_POINT "/data"
339
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340/* mount <type> <device> <path> <flags ...> <options> */
341int do_mount(int nargs, char **args)
342{
343 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000344 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345 char *options = NULL;
346 unsigned flags = 0;
347 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700348 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349
350 for (n = 4; n < nargs; n++) {
351 for (i = 0; mount_flags[i].name; i++) {
352 if (!strcmp(args[n], mount_flags[i].name)) {
353 flags |= mount_flags[i].flag;
354 break;
355 }
356 }
357
Colin Crosscd0f1732010-04-19 17:10:24 -0700358 if (!mount_flags[i].name) {
359 if (!strcmp(args[n], "wait"))
360 wait = 1;
361 /* if our last argument isn't a flag, wolf it up as an option string */
362 else if (n + 1 == nargs)
363 options = args[n];
364 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 }
366
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369 target = args[3];
370
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 if (!strncmp(source, "mtd@", 4)) {
372 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 if (n < 0) {
374 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000376
Yabin Cuie2d63af2015-02-17 19:27:51 -0800377 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000378
Colin Crosscd0f1732010-04-19 17:10:24 -0700379 if (wait)
380 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000381 if (mount(tmp, target, system, flags, options) < 0) {
382 return -1;
383 }
384
Ken Sumralldd4d7862011-02-17 18:09:47 -0800385 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000386 } else if (!strncmp(source, "loop@", 5)) {
387 int mode, loop, fd;
388 struct loop_info info;
389
390 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800391 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392 if (fd < 0) {
393 return -1;
394 }
395
396 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800397 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800398 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000399 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100400 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401 return -1;
402 }
403
404 /* if it is a blank loop device */
405 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
406 /* if it becomes our loop device */
407 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
408 close(fd);
409
410 if (mount(tmp, target, system, flags, options) < 0) {
411 ioctl(loop, LOOP_CLR_FD, 0);
412 close(loop);
413 return -1;
414 }
415
416 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800417 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 }
419 }
420
421 close(loop);
422 }
423
424 close(fd);
425 ERROR("out of loopback devices");
426 return -1;
427 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700428 if (wait)
429 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700431 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000432 }
433
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700434 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800435
436exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800437 return 0;
438
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439}
440
JP Abgrallcee20682014-07-02 14:26:54 -0700441static int wipe_data_via_recovery()
442{
443 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800444 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700445 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700446 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
447 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700448 close(fd);
449 } else {
450 ERROR("could not open /cache/recovery/command\n");
451 return -1;
452 }
453 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
454 while (1) { pause(); } // never reached
455}
456
457
458/*
459 * This function might request a reboot, in which case it will
460 * not return.
461 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700462int do_mount_all(int nargs, char **args)
463{
464 pid_t pid;
465 int ret = -1;
466 int child_ret = -1;
467 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800468 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700469
470 if (nargs != 2) {
471 return -1;
472 }
473
474 /*
475 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
476 * do the call in the child to provide protection to the main init
477 * process if anything goes wrong (crash or memory leak), and wait for
478 * the child to finish in the parent.
479 */
480 pid = fork();
481 if (pid > 0) {
482 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700483 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
484 if (wp_ret < 0) {
485 /* Unexpected error code. We will continue anyway. */
486 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
487 }
488
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489 if (WIFEXITED(status)) {
490 ret = WEXITSTATUS(status);
491 } else {
492 ret = -1;
493 }
494 } else if (pid == 0) {
495 /* child, call fs_mgr_mount_all() */
496 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800497 fstab = fs_mgr_read_fstab(args[1]);
498 child_ret = fs_mgr_mount_all(fstab);
499 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500 if (child_ret == -1) {
501 ERROR("fs_mgr_mount_all returned an error\n");
502 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700503 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 } else {
505 /* fork failed, return an error */
506 return -1;
507 }
508
JP Abgrallf22b7452014-07-02 13:16:04 -0700509 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800510 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700511 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700512 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800513 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700514 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515 property_set("ro.crypto.state", "unencrypted");
516 /* If fs_mgr determined this is an unencrypted device, then trigger
517 * that action.
518 */
519 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700520 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
521 /* Setup a wipe via recovery, and reboot into recovery */
522 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
523 ret = wipe_data_via_recovery();
524 /* If reboot worked, there is no return. */
525 } else if (ret > 0) {
526 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700527 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700528 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700529
530 return ret;
531}
532
Ken Sumralla76baaa2013-07-09 18:42:09 -0700533int do_swapon_all(int nargs, char **args)
534{
535 struct fstab *fstab;
536 int ret;
537
538 fstab = fs_mgr_read_fstab(args[1]);
539 ret = fs_mgr_swapon_all(fstab);
540 fs_mgr_free_fstab(fstab);
541
542 return ret;
543}
544
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500545int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500546 if (is_selinux_enabled() <= 0)
547 return 0;
548 if (setcon(args[1]) < 0) {
549 return -errno;
550 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500551 return 0;
552}
553
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554int do_setprop(int nargs, char **args)
555{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700556 const char *name = args[1];
557 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800558 char prop_val[PROP_VALUE_MAX];
559 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700560
Dima Zavin84bf9af2011-12-20 13:44:41 -0800561 ret = expand_props(prop_val, value, sizeof(prop_val));
562 if (ret) {
563 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
564 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700565 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800566 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567 return 0;
568}
569
570int do_setrlimit(int nargs, char **args)
571{
572 struct rlimit limit;
573 int resource;
574 resource = atoi(args[1]);
575 limit.rlim_cur = atoi(args[2]);
576 limit.rlim_max = atoi(args[3]);
577 return setrlimit(resource, &limit);
578}
579
580int do_start(int nargs, char **args)
581{
582 struct service *svc;
583 svc = service_find_by_name(args[1]);
584 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700585 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700586 }
587 return 0;
588}
589
590int do_stop(int nargs, char **args)
591{
592 struct service *svc;
593 svc = service_find_by_name(args[1]);
594 if (svc) {
595 service_stop(svc);
596 }
597 return 0;
598}
599
600int do_restart(int nargs, char **args)
601{
602 struct service *svc;
603 svc = service_find_by_name(args[1]);
604 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500605 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 }
607 return 0;
608}
609
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700610int do_powerctl(int nargs, char **args)
611{
612 char command[PROP_VALUE_MAX];
613 int res;
614 int len = 0;
615 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800616 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700617
618 res = expand_props(command, args[1], sizeof(command));
619 if (res) {
620 ERROR("powerctl: cannot expand '%s'\n", args[1]);
621 return -EINVAL;
622 }
623
624 if (strncmp(command, "shutdown", 8) == 0) {
625 cmd = ANDROID_RB_POWEROFF;
626 len = 8;
627 } else if (strncmp(command, "reboot", 6) == 0) {
628 cmd = ANDROID_RB_RESTART2;
629 len = 6;
630 } else {
631 ERROR("powerctl: unrecognized command '%s'\n", command);
632 return -EINVAL;
633 }
634
635 if (command[len] == ',') {
636 reboot_target = &command[len + 1];
637 } else if (command[len] == '\0') {
638 reboot_target = "";
639 } else {
640 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
641 return -EINVAL;
642 }
643
644 return android_reboot(cmd, 0, reboot_target);
645}
646
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700647int do_trigger(int nargs, char **args)
648{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000649 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700650 return 0;
651}
652
653int do_symlink(int nargs, char **args)
654{
655 return symlink(args[1], args[2]);
656}
657
Ken Sumrall203bad52011-01-18 17:37:41 -0800658int do_rm(int nargs, char **args)
659{
660 return unlink(args[1]);
661}
662
663int do_rmdir(int nargs, char **args)
664{
665 return rmdir(args[1]);
666}
667
The Android Open Source Project35237d12008-12-17 18:08:08 -0800668int do_sysclktz(int nargs, char **args)
669{
670 struct timezone tz;
671
672 if (nargs != 2)
673 return -1;
674
675 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800676 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800677 if (settimeofday(NULL, &tz))
678 return -1;
679 return 0;
680}
681
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000682int do_verity_load_state(int nargs, char **args) {
683 if (nargs == 1) {
684 int mode = -1;
685 int rc = fs_mgr_load_verity_state(&mode);
686
687 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
688 action_for_each_trigger("verity-logging", action_add_queue_tail);
689 }
690
691 return rc;
692 }
693 return -1;
694}
695
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700696int do_write(int nargs, char **args)
697{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700698 const char *path = args[1];
699 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700700
Johan Redestig7e952f42014-12-05 00:36:41 +0100701 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800702 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800703 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
704 return -EINVAL;
705 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800706 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700707}
708
San Mehat7c44fe52009-08-26 16:39:25 -0700709int do_copy(int nargs, char **args)
710{
711 char *buffer = NULL;
712 int rc = 0;
713 int fd1 = -1, fd2 = -1;
714 struct stat info;
715 int brtw, brtr;
716 char *p;
717
718 if (nargs != 3)
719 return -1;
720
Elliott Hughes24627902015-02-04 10:25:09 -0800721 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700722 return -1;
723
Nick Kralevich45a884f2015-02-02 14:37:22 -0800724 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700725 goto out_err;
726
Nick Kralevich45a884f2015-02-02 14:37:22 -0800727 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700728 goto out_err;
729
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800730 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700731 goto out_err;
732
733 p = buffer;
734 brtr = info.st_size;
735 while(brtr) {
736 rc = read(fd1, p, brtr);
737 if (rc < 0)
738 goto out_err;
739 if (rc == 0)
740 break;
741 p += rc;
742 brtr -= rc;
743 }
744
745 p = buffer;
746 brtw = info.st_size;
747 while(brtw) {
748 rc = write(fd2, p, brtw);
749 if (rc < 0)
750 goto out_err;
751 if (rc == 0)
752 break;
753 p += rc;
754 brtw -= rc;
755 }
756
757 rc = 0;
758 goto out;
759out_err:
760 rc = -1;
761out:
762 if (buffer)
763 free(buffer);
764 if (fd1 >= 0)
765 close(fd1);
766 if (fd2 >= 0)
767 close(fd2);
768 return rc;
769}
770
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771int do_chown(int nargs, char **args) {
772 /* GID is optional. */
773 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800774 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775 return -errno;
776 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800777 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 return -errno;
779 } else {
780 return -1;
781 }
782 return 0;
783}
784
785static mode_t get_mode(const char *s) {
786 mode_t mode = 0;
787 while (*s) {
788 if (*s >= '0' && *s <= '7') {
789 mode = (mode<<3) | (*s-'0');
790 } else {
791 return -1;
792 }
793 s++;
794 }
795 return mode;
796}
797
798int do_chmod(int nargs, char **args) {
799 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800800 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700801 return -errno;
802 }
803 return 0;
804}
805
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500806int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400808 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500809
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500810 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400811 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400812 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400814 return ret;
815}
816
817int do_restorecon_recursive(int nargs, char **args) {
818 int i;
819 int ret = 0;
820
821 for (i = 1; i < nargs; i++) {
822 if (restorecon_recursive(args[i]) < 0)
823 ret = -errno;
824 }
825 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500826}
827
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700829 int log_level;
830 char log_level_str[PROP_VALUE_MAX] = "";
831 if (nargs != 2) {
832 ERROR("loglevel: missing argument\n");
833 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700835
836 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
837 ERROR("loglevel: cannot expand '%s'\n", args[1]);
838 return -EINVAL;
839 }
840 log_level = atoi(log_level_str);
841 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
842 ERROR("loglevel: invalid log level'%d'\n", log_level);
843 return -EINVAL;
844 }
845 klog_set_level(log_level);
846 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847}
848
Ken Sumrallc5c51032011-03-08 17:01:29 -0800849int do_load_persist_props(int nargs, char **args) {
850 if (nargs == 1) {
851 load_persist_props();
852 return 0;
853 }
854 return -1;
855}
856
Riley Andrewse4b7b292014-06-16 15:06:21 -0700857int do_load_all_props(int nargs, char **args) {
858 if (nargs == 1) {
859 load_all_props();
860 return 0;
861 }
862 return -1;
863}
864
Colin Crosscd0f1732010-04-19 17:10:24 -0700865int do_wait(int nargs, char **args)
866{
867 if (nargs == 2) {
868 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800869 } else if (nargs == 3) {
870 return wait_for_file(args[1], atoi(args[2]));
871 } else
872 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700873}