blob: fb1aa7c8b668719bbeb0c4b3e0d3f48c6f24203a [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
107int do_class_start(int nargs, char **args)
108{
109 /* Starting a class does not start services
110 * which are explicitly disabled. They must
111 * be started individually.
112 */
113 service_for_each_class(args[1], service_start_if_not_disabled);
114 return 0;
115}
116
117int do_class_stop(int nargs, char **args)
118{
119 service_for_each_class(args[1], service_stop);
120 return 0;
121}
122
Ken Sumrall752923c2010-12-03 16:33:31 -0800123int do_class_reset(int nargs, char **args)
124{
125 service_for_each_class(args[1], service_reset);
126 return 0;
127}
128
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129int do_domainname(int nargs, char **args)
130{
131 return write_file("/proc/sys/kernel/domainname", args[1]);
132}
133
JP Abgrall3beec7e2014-05-02 21:14:29 -0700134int do_enable(int nargs, char **args)
135{
136 struct service *svc;
137 svc = service_find_by_name(args[1]);
138 if (svc) {
139 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
140 if (svc->flags & SVC_DISABLED_START) {
141 service_start(svc, NULL);
142 }
143 } else {
144 return -1;
145 }
146 return 0;
147}
148
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800149int do_exec(int nargs, char** args) {
150 service* svc = make_exec_oneshot_service(nargs, args);
151 if (svc == NULL) {
152 return -1;
153 }
154 service_start(svc, NULL);
155 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156}
157
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800158// TODO: remove execonce when exec is available.
San Mehat429721c2014-09-23 07:48:47 -0700159int do_execonce(int nargs, char **args)
160{
161 pid_t child;
162 int child_status = 0;
163 static int already_done;
164
165 if (already_done) {
166 return -1;
167 }
168 already_done = 1;
169 if (!(child = fork())) {
170 /*
171 * Child process.
172 */
173 zap_stdio();
174 char *exec_args[100];
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800175 size_t num_process_args = nargs;
San Mehat429721c2014-09-23 07:48:47 -0700176
177 memset(exec_args, 0, sizeof(exec_args));
178 if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800179 ERROR("exec called with %zu args, limit is %zu", num_process_args,
San Mehat429721c2014-09-23 07:48:47 -0700180 ARRAY_SIZE(exec_args) - 1);
181 _exit(1);
182 }
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800183 for (size_t i = 1; i < num_process_args; i++)
San Mehat429721c2014-09-23 07:48:47 -0700184 exec_args[i - 1] = args[i];
185
186 if (execv(exec_args[0], exec_args) == -1) {
187 ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
188 _exit(1);
189 }
190 ERROR("Returned from execv()!");
191 _exit(1);
192 }
193
194 /*
195 * Parent process.
196 */
197 if (child == -1) {
198 ERROR("Fork failed\n");
199 return -1;
200 }
201
202 if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
203 ERROR("waitpid(): failed (%s)\n", strerror(errno));
204 return -1;
205 }
206
207 if (WIFSIGNALED(child_status)) {
208 INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
209 return -1;
210 } else if (WIFEXITED(child_status)) {
211 INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
212 return WEXITSTATUS(child_status);
213 }
214
215 ERROR("Abnormal child process exit\n");
216
217 return -1;
218}
219
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220int do_export(int nargs, char **args)
221{
James Morrissey381341f2014-05-16 11:36:36 +0100222 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223}
224
225int do_hostname(int nargs, char **args)
226{
227 return write_file("/proc/sys/kernel/hostname", args[1]);
228}
229
230int do_ifup(int nargs, char **args)
231{
232 return __ifupdown(args[1], 1);
233}
234
The Android Open Source Project35237d12008-12-17 18:08:08 -0800235
236static int do_insmod_inner(int nargs, char **args, int opt_len)
237{
238 char options[opt_len + 1];
239 int i;
240
241 options[0] = '\0';
242 if (nargs > 2) {
243 strcpy(options, args[2]);
244 for (i = 3; i < nargs; ++i) {
245 strcat(options, " ");
246 strcat(options, args[i]);
247 }
248 }
249
250 return insmod(args[1], options);
251}
252
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700253int do_insmod(int nargs, char **args)
254{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800255 int i;
256 int size = 0;
257
258 if (nargs > 2) {
259 for (i = 2; i < nargs; ++i)
260 size += strlen(args[i]) + 1;
261 }
262
263 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264}
265
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266int do_mkdir(int nargs, char **args)
267{
268 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700269 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270
271 /* mkdir <path> [mode] [owner] [group] */
272
273 if (nargs >= 3) {
274 mode = strtoul(args[2], 0, 8);
275 }
276
Stephen Smalleye096e362012-06-11 13:37:39 -0400277 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700278 /* chmod in case the directory already exists */
279 if (ret == -1 && errno == EEXIST) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800280 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700281 }
282 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283 return -errno;
284 }
285
286 if (nargs >= 4) {
287 uid_t uid = decode_uid(args[3]);
288 gid_t gid = -1;
289
290 if (nargs == 5) {
291 gid = decode_uid(args[4]);
292 }
293
Nick Kralevichbc609542015-01-31 21:39:46 -0800294 if (lchown(args[1], uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 return -errno;
296 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700297
298 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
299 if (mode & (S_ISUID | S_ISGID)) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800300 ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700301 if (ret == -1) {
302 return -errno;
303 }
304 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 }
306
307 return 0;
308}
309
310static struct {
311 const char *name;
312 unsigned flag;
313} mount_flags[] = {
314 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200315 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 { "nosuid", MS_NOSUID },
317 { "nodev", MS_NODEV },
318 { "nodiratime", MS_NODIRATIME },
319 { "ro", MS_RDONLY },
320 { "rw", 0 },
321 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700322 { "bind", MS_BIND },
323 { "rec", MS_REC },
324 { "unbindable", MS_UNBINDABLE },
325 { "private", MS_PRIVATE },
326 { "slave", MS_SLAVE },
327 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 { "defaults", 0 },
329 { 0, 0 },
330};
331
Ken Sumrall752923c2010-12-03 16:33:31 -0800332#define DATA_MNT_POINT "/data"
333
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334/* mount <type> <device> <path> <flags ...> <options> */
335int do_mount(int nargs, char **args)
336{
337 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000338 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700339 char *options = NULL;
340 unsigned flags = 0;
341 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700342 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343
344 for (n = 4; n < nargs; n++) {
345 for (i = 0; mount_flags[i].name; i++) {
346 if (!strcmp(args[n], mount_flags[i].name)) {
347 flags |= mount_flags[i].flag;
348 break;
349 }
350 }
351
Colin Crosscd0f1732010-04-19 17:10:24 -0700352 if (!mount_flags[i].name) {
353 if (!strcmp(args[n], "wait"))
354 wait = 1;
355 /* if our last argument isn't a flag, wolf it up as an option string */
356 else if (n + 1 == nargs)
357 options = args[n];
358 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 }
360
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000363 target = args[3];
364
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 if (!strncmp(source, "mtd@", 4)) {
366 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 if (n < 0) {
368 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370
Yabin Cuie2d63af2015-02-17 19:27:51 -0800371 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000372
Colin Crosscd0f1732010-04-19 17:10:24 -0700373 if (wait)
374 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000375 if (mount(tmp, target, system, flags, options) < 0) {
376 return -1;
377 }
378
Ken Sumralldd4d7862011-02-17 18:09:47 -0800379 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000380 } else if (!strncmp(source, "loop@", 5)) {
381 int mode, loop, fd;
382 struct loop_info info;
383
384 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800385 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000386 if (fd < 0) {
387 return -1;
388 }
389
390 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800391 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800392 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100394 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 return -1;
396 }
397
398 /* if it is a blank loop device */
399 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
400 /* if it becomes our loop device */
401 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
402 close(fd);
403
404 if (mount(tmp, target, system, flags, options) < 0) {
405 ioctl(loop, LOOP_CLR_FD, 0);
406 close(loop);
407 return -1;
408 }
409
410 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800411 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412 }
413 }
414
415 close(loop);
416 }
417
418 close(fd);
419 ERROR("out of loopback devices");
420 return -1;
421 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700422 if (wait)
423 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000424 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700425 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000426 }
427
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800429
430exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800431 return 0;
432
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433}
434
JP Abgrallcee20682014-07-02 14:26:54 -0700435static int wipe_data_via_recovery()
436{
437 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800438 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700439 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700440 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
441 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700442 close(fd);
443 } else {
444 ERROR("could not open /cache/recovery/command\n");
445 return -1;
446 }
447 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
448 while (1) { pause(); } // never reached
449}
450
451
452/*
453 * This function might request a reboot, in which case it will
454 * not return.
455 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700456int do_mount_all(int nargs, char **args)
457{
458 pid_t pid;
459 int ret = -1;
460 int child_ret = -1;
461 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800462 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700463
464 if (nargs != 2) {
465 return -1;
466 }
467
468 /*
469 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
470 * do the call in the child to provide protection to the main init
471 * process if anything goes wrong (crash or memory leak), and wait for
472 * the child to finish in the parent.
473 */
474 pid = fork();
475 if (pid > 0) {
476 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700477 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
478 if (wp_ret < 0) {
479 /* Unexpected error code. We will continue anyway. */
480 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
481 }
482
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700483 if (WIFEXITED(status)) {
484 ret = WEXITSTATUS(status);
485 } else {
486 ret = -1;
487 }
488 } else if (pid == 0) {
489 /* child, call fs_mgr_mount_all() */
490 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800491 fstab = fs_mgr_read_fstab(args[1]);
492 child_ret = fs_mgr_mount_all(fstab);
493 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700494 if (child_ret == -1) {
495 ERROR("fs_mgr_mount_all returned an error\n");
496 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700497 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700498 } else {
499 /* fork failed, return an error */
500 return -1;
501 }
502
JP Abgrallf22b7452014-07-02 13:16:04 -0700503 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800504 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700505 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800507 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700508 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700509 property_set("ro.crypto.state", "unencrypted");
510 /* If fs_mgr determined this is an unencrypted device, then trigger
511 * that action.
512 */
513 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700514 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
515 /* Setup a wipe via recovery, and reboot into recovery */
516 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
517 ret = wipe_data_via_recovery();
518 /* If reboot worked, there is no return. */
519 } else if (ret > 0) {
520 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700522 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700523
524 return ret;
525}
526
Ken Sumralla76baaa2013-07-09 18:42:09 -0700527int do_swapon_all(int nargs, char **args)
528{
529 struct fstab *fstab;
530 int ret;
531
532 fstab = fs_mgr_read_fstab(args[1]);
533 ret = fs_mgr_swapon_all(fstab);
534 fs_mgr_free_fstab(fstab);
535
536 return ret;
537}
538
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500539int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500540 if (is_selinux_enabled() <= 0)
541 return 0;
542 if (setcon(args[1]) < 0) {
543 return -errno;
544 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500545 return 0;
546}
547
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548int do_setprop(int nargs, char **args)
549{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700550 const char *name = args[1];
551 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800552 char prop_val[PROP_VALUE_MAX];
553 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700554
Dima Zavin84bf9af2011-12-20 13:44:41 -0800555 ret = expand_props(prop_val, value, sizeof(prop_val));
556 if (ret) {
557 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
558 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700559 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800560 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700561 return 0;
562}
563
564int do_setrlimit(int nargs, char **args)
565{
566 struct rlimit limit;
567 int resource;
568 resource = atoi(args[1]);
569 limit.rlim_cur = atoi(args[2]);
570 limit.rlim_max = atoi(args[3]);
571 return setrlimit(resource, &limit);
572}
573
574int do_start(int nargs, char **args)
575{
576 struct service *svc;
577 svc = service_find_by_name(args[1]);
578 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700579 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700580 }
581 return 0;
582}
583
584int do_stop(int nargs, char **args)
585{
586 struct service *svc;
587 svc = service_find_by_name(args[1]);
588 if (svc) {
589 service_stop(svc);
590 }
591 return 0;
592}
593
594int do_restart(int nargs, char **args)
595{
596 struct service *svc;
597 svc = service_find_by_name(args[1]);
598 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500599 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 }
601 return 0;
602}
603
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700604int do_powerctl(int nargs, char **args)
605{
606 char command[PROP_VALUE_MAX];
607 int res;
608 int len = 0;
609 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800610 const char *reboot_target;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700611
612 res = expand_props(command, args[1], sizeof(command));
613 if (res) {
614 ERROR("powerctl: cannot expand '%s'\n", args[1]);
615 return -EINVAL;
616 }
617
618 if (strncmp(command, "shutdown", 8) == 0) {
619 cmd = ANDROID_RB_POWEROFF;
620 len = 8;
621 } else if (strncmp(command, "reboot", 6) == 0) {
622 cmd = ANDROID_RB_RESTART2;
623 len = 6;
624 } else {
625 ERROR("powerctl: unrecognized command '%s'\n", command);
626 return -EINVAL;
627 }
628
629 if (command[len] == ',') {
630 reboot_target = &command[len + 1];
631 } else if (command[len] == '\0') {
632 reboot_target = "";
633 } else {
634 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
635 return -EINVAL;
636 }
637
638 return android_reboot(cmd, 0, reboot_target);
639}
640
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641int do_trigger(int nargs, char **args)
642{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000643 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700644 return 0;
645}
646
647int do_symlink(int nargs, char **args)
648{
649 return symlink(args[1], args[2]);
650}
651
Ken Sumrall203bad52011-01-18 17:37:41 -0800652int do_rm(int nargs, char **args)
653{
654 return unlink(args[1]);
655}
656
657int do_rmdir(int nargs, char **args)
658{
659 return rmdir(args[1]);
660}
661
The Android Open Source Project35237d12008-12-17 18:08:08 -0800662int do_sysclktz(int nargs, char **args)
663{
664 struct timezone tz;
665
666 if (nargs != 2)
667 return -1;
668
669 memset(&tz, 0, sizeof(tz));
Elliott Hughes24627902015-02-04 10:25:09 -0800670 tz.tz_minuteswest = atoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800671 if (settimeofday(NULL, &tz))
672 return -1;
673 return 0;
674}
675
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000676int do_verity_load_state(int nargs, char **args) {
677 if (nargs == 1) {
678 int mode = -1;
679 int rc = fs_mgr_load_verity_state(&mode);
680
681 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
682 action_for_each_trigger("verity-logging", action_add_queue_tail);
683 }
684
685 return rc;
686 }
687 return -1;
688}
689
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700690int do_write(int nargs, char **args)
691{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700692 const char *path = args[1];
693 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700694
Johan Redestig7e952f42014-12-05 00:36:41 +0100695 char expanded_value[256];
Elliott Hughesf682b472015-02-06 12:19:48 -0800696 if (expand_props(expanded_value, value, sizeof(expanded_value))) {
Dima Zavin84bf9af2011-12-20 13:44:41 -0800697 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
698 return -EINVAL;
699 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800700 return write_file(path, expanded_value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701}
702
San Mehat7c44fe52009-08-26 16:39:25 -0700703int do_copy(int nargs, char **args)
704{
705 char *buffer = NULL;
706 int rc = 0;
707 int fd1 = -1, fd2 = -1;
708 struct stat info;
709 int brtw, brtr;
710 char *p;
711
712 if (nargs != 3)
713 return -1;
714
Elliott Hughes24627902015-02-04 10:25:09 -0800715 if (stat(args[1], &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700716 return -1;
717
Nick Kralevich45a884f2015-02-02 14:37:22 -0800718 if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700719 goto out_err;
720
Nick Kralevich45a884f2015-02-02 14:37:22 -0800721 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700722 goto out_err;
723
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800724 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700725 goto out_err;
726
727 p = buffer;
728 brtr = info.st_size;
729 while(brtr) {
730 rc = read(fd1, p, brtr);
731 if (rc < 0)
732 goto out_err;
733 if (rc == 0)
734 break;
735 p += rc;
736 brtr -= rc;
737 }
738
739 p = buffer;
740 brtw = info.st_size;
741 while(brtw) {
742 rc = write(fd2, p, brtw);
743 if (rc < 0)
744 goto out_err;
745 if (rc == 0)
746 break;
747 p += rc;
748 brtw -= rc;
749 }
750
751 rc = 0;
752 goto out;
753out_err:
754 rc = -1;
755out:
756 if (buffer)
757 free(buffer);
758 if (fd1 >= 0)
759 close(fd1);
760 if (fd2 >= 0)
761 close(fd2);
762 return rc;
763}
764
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765int do_chown(int nargs, char **args) {
766 /* GID is optional. */
767 if (nargs == 3) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800768 if (lchown(args[2], decode_uid(args[1]), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 return -errno;
770 } else if (nargs == 4) {
Nick Kralevichbc609542015-01-31 21:39:46 -0800771 if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700772 return -errno;
773 } else {
774 return -1;
775 }
776 return 0;
777}
778
779static mode_t get_mode(const char *s) {
780 mode_t mode = 0;
781 while (*s) {
782 if (*s >= '0' && *s <= '7') {
783 mode = (mode<<3) | (*s-'0');
784 } else {
785 return -1;
786 }
787 s++;
788 }
789 return mode;
790}
791
792int do_chmod(int nargs, char **args) {
793 mode_t mode = get_mode(args[1]);
Nick Kralevichbc609542015-01-31 21:39:46 -0800794 if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795 return -errno;
796 }
797 return 0;
798}
799
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500801 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400802 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500804 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400805 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400806 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400808 return ret;
809}
810
811int do_restorecon_recursive(int nargs, char **args) {
812 int i;
813 int ret = 0;
814
815 for (i = 1; i < nargs; i++) {
816 if (restorecon_recursive(args[i]) < 0)
817 ret = -errno;
818 }
819 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500820}
821
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700823 int log_level;
824 char log_level_str[PROP_VALUE_MAX] = "";
825 if (nargs != 2) {
826 ERROR("loglevel: missing argument\n");
827 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700829
830 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
831 ERROR("loglevel: cannot expand '%s'\n", args[1]);
832 return -EINVAL;
833 }
834 log_level = atoi(log_level_str);
835 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
836 ERROR("loglevel: invalid log level'%d'\n", log_level);
837 return -EINVAL;
838 }
839 klog_set_level(log_level);
840 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700841}
842
Ken Sumrallc5c51032011-03-08 17:01:29 -0800843int do_load_persist_props(int nargs, char **args) {
844 if (nargs == 1) {
845 load_persist_props();
846 return 0;
847 }
848 return -1;
849}
850
Riley Andrewse4b7b292014-06-16 15:06:21 -0700851int do_load_all_props(int nargs, char **args) {
852 if (nargs == 1) {
853 load_all_props();
854 return 0;
855 }
856 return -1;
857}
858
Colin Crosscd0f1732010-04-19 17:10:24 -0700859int do_wait(int nargs, char **args)
860{
861 if (nargs == 2) {
862 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800863 } else if (nargs == 3) {
864 return wait_for_file(args[1], atoi(args[2]));
865 } else
866 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700867}