blob: 5d2a517c6781e22ee86726719374e0ec661f10dd [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>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070032#include <sys/wait.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000033#include <linux/loop.h>
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -070034#include <cutils/partition_utils.h>
Nick Kralevichca8e66a2013-04-18 12:20:02 -070035#include <cutils/android_reboot.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070036#include <fs_mgr.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041#include "init.h"
42#include "keywords.h"
43#include "property_service.h"
44#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070045#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070046#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070047#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
49#include <private/android_filesystem_config.h>
50
James Morrissey381341f2014-05-16 11:36:36 +010051int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052
53extern int init_module(void *, unsigned long, const char *);
54
55static int write_file(const char *path, const char *value)
56{
57 int fd, ret, len;
58
Nick Kralevich5535b052013-09-17 14:43:12 -070059 fd = open(path, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
61 if (fd < 0)
Mike Chan008abac2009-06-29 20:30:55 -070062 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
64 len = strlen(value);
65
66 do {
67 ret = write(fd, value, len);
68 } while (ret < 0 && errno == EINTR);
69
70 close(fd);
71 if (ret < 0) {
Mike Chan008abac2009-06-29 20:30:55 -070072 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 } else {
74 return 0;
75 }
76}
77
Benoit Goby58903012012-03-28 18:15:56 -070078static int _open(const char *path)
79{
80 int fd;
81
82 fd = open(path, O_RDONLY | O_NOFOLLOW);
83 if (fd < 0)
84 fd = open(path, O_WRONLY | O_NOFOLLOW);
85
86 return fd;
87}
88
Geremy Condra9ed1fe72012-03-20 12:49:55 -070089static int _chown(const char *path, unsigned int uid, unsigned int gid)
90{
91 int fd;
92 int ret;
93
Benoit Goby58903012012-03-28 18:15:56 -070094 fd = _open(path);
Geremy Condra9ed1fe72012-03-20 12:49:55 -070095 if (fd < 0) {
96 return -1;
97 }
98
99 ret = fchown(fd, uid, gid);
100 if (ret < 0) {
101 int errno_copy = errno;
102 close(fd);
103 errno = errno_copy;
104 return -1;
105 }
106
107 close(fd);
108
109 return 0;
110}
111
112static int _chmod(const char *path, mode_t mode)
113{
114 int fd;
115 int ret;
116
Benoit Goby58903012012-03-28 18:15:56 -0700117 fd = _open(path);
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700118 if (fd < 0) {
119 return -1;
120 }
121
122 ret = fchmod(fd, mode);
123 if (ret < 0) {
124 int errno_copy = errno;
125 close(fd);
126 errno = errno_copy;
127 return -1;
128 }
129
130 close(fd);
131
132 return 0;
133}
134
The Android Open Source Project35237d12008-12-17 18:08:08 -0800135static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136{
137 void *module;
138 unsigned size;
139 int ret;
140
141 module = read_file(filename, &size);
142 if (!module)
143 return -1;
144
The Android Open Source Project35237d12008-12-17 18:08:08 -0800145 ret = init_module(module, size, options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146
147 free(module);
148
149 return ret;
150}
151
152static int setkey(struct kbentry *kbe)
153{
154 int fd, ret;
155
156 fd = open("/dev/tty0", O_RDWR | O_SYNC);
157 if (fd < 0)
158 return -1;
159
160 ret = ioctl(fd, KDSKBENT, kbe);
161
162 close(fd);
163 return ret;
164}
165
166static int __ifupdown(const char *interface, int up)
167{
168 struct ifreq ifr;
169 int s, ret;
170
171 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
172
173 s = socket(AF_INET, SOCK_DGRAM, 0);
174 if (s < 0)
175 return -1;
176
177 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
178 if (ret < 0) {
179 goto done;
180 }
181
182 if (up)
183 ifr.ifr_flags |= IFF_UP;
184 else
185 ifr.ifr_flags &= ~IFF_UP;
186
187 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
188
189done:
190 close(s);
191 return ret;
192}
193
194static void service_start_if_not_disabled(struct service *svc)
195{
196 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700197 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700198 } else {
199 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200 }
201}
202
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000203int do_chdir(int nargs, char **args)
204{
205 chdir(args[1]);
206 return 0;
207}
208
209int do_chroot(int nargs, char **args)
210{
211 chroot(args[1]);
212 return 0;
213}
214
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215int do_class_start(int nargs, char **args)
216{
217 /* Starting a class does not start services
218 * which are explicitly disabled. They must
219 * be started individually.
220 */
221 service_for_each_class(args[1], service_start_if_not_disabled);
222 return 0;
223}
224
225int do_class_stop(int nargs, char **args)
226{
227 service_for_each_class(args[1], service_stop);
228 return 0;
229}
230
Ken Sumrall752923c2010-12-03 16:33:31 -0800231int do_class_reset(int nargs, char **args)
232{
233 service_for_each_class(args[1], service_reset);
234 return 0;
235}
236
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237int do_domainname(int nargs, char **args)
238{
239 return write_file("/proc/sys/kernel/domainname", args[1]);
240}
241
JP Abgrall3beec7e2014-05-02 21:14:29 -0700242int do_enable(int nargs, char **args)
243{
244 struct service *svc;
245 svc = service_find_by_name(args[1]);
246 if (svc) {
247 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
248 if (svc->flags & SVC_DISABLED_START) {
249 service_start(svc, NULL);
250 }
251 } else {
252 return -1;
253 }
254 return 0;
255}
256
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257int do_exec(int nargs, char **args)
258{
259 return -1;
260}
261
262int do_export(int nargs, char **args)
263{
James Morrissey381341f2014-05-16 11:36:36 +0100264 return add_environment(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265}
266
267int do_hostname(int nargs, char **args)
268{
269 return write_file("/proc/sys/kernel/hostname", args[1]);
270}
271
272int do_ifup(int nargs, char **args)
273{
274 return __ifupdown(args[1], 1);
275}
276
The Android Open Source Project35237d12008-12-17 18:08:08 -0800277
278static int do_insmod_inner(int nargs, char **args, int opt_len)
279{
280 char options[opt_len + 1];
281 int i;
282
283 options[0] = '\0';
284 if (nargs > 2) {
285 strcpy(options, args[2]);
286 for (i = 3; i < nargs; ++i) {
287 strcat(options, " ");
288 strcat(options, args[i]);
289 }
290 }
291
292 return insmod(args[1], options);
293}
294
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295int do_insmod(int nargs, char **args)
296{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800297 int i;
298 int size = 0;
299
300 if (nargs > 2) {
301 for (i = 2; i < nargs; ++i)
302 size += strlen(args[i]) + 1;
303 }
304
305 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306}
307
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308int do_mkdir(int nargs, char **args)
309{
310 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700311 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312
313 /* mkdir <path> [mode] [owner] [group] */
314
315 if (nargs >= 3) {
316 mode = strtoul(args[2], 0, 8);
317 }
318
Stephen Smalleye096e362012-06-11 13:37:39 -0400319 ret = make_dir(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700320 /* chmod in case the directory already exists */
321 if (ret == -1 && errno == EEXIST) {
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700322 ret = _chmod(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700323 }
324 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 return -errno;
326 }
327
328 if (nargs >= 4) {
329 uid_t uid = decode_uid(args[3]);
330 gid_t gid = -1;
331
332 if (nargs == 5) {
333 gid = decode_uid(args[4]);
334 }
335
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700336 if (_chown(args[1], uid, gid) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 return -errno;
338 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700339
340 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
341 if (mode & (S_ISUID | S_ISGID)) {
342 ret = _chmod(args[1], mode);
343 if (ret == -1) {
344 return -errno;
345 }
346 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347 }
348
349 return 0;
350}
351
352static struct {
353 const char *name;
354 unsigned flag;
355} mount_flags[] = {
356 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200357 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 { "nosuid", MS_NOSUID },
359 { "nodev", MS_NODEV },
360 { "nodiratime", MS_NODIRATIME },
361 { "ro", MS_RDONLY },
362 { "rw", 0 },
363 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700364 { "bind", MS_BIND },
365 { "rec", MS_REC },
366 { "unbindable", MS_UNBINDABLE },
367 { "private", MS_PRIVATE },
368 { "slave", MS_SLAVE },
369 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370 { "defaults", 0 },
371 { 0, 0 },
372};
373
Ken Sumrall752923c2010-12-03 16:33:31 -0800374#define DATA_MNT_POINT "/data"
375
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376/* mount <type> <device> <path> <flags ...> <options> */
377int do_mount(int nargs, char **args)
378{
379 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000380 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381 char *options = NULL;
382 unsigned flags = 0;
383 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700384 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385
386 for (n = 4; n < nargs; n++) {
387 for (i = 0; mount_flags[i].name; i++) {
388 if (!strcmp(args[n], mount_flags[i].name)) {
389 flags |= mount_flags[i].flag;
390 break;
391 }
392 }
393
Colin Crosscd0f1732010-04-19 17:10:24 -0700394 if (!mount_flags[i].name) {
395 if (!strcmp(args[n], "wait"))
396 wait = 1;
397 /* if our last argument isn't a flag, wolf it up as an option string */
398 else if (n + 1 == nargs)
399 options = args[n];
400 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 }
402
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000403 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 target = args[3];
406
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 if (!strncmp(source, "mtd@", 4)) {
408 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000409 if (n < 0) {
410 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412
413 sprintf(tmp, "/dev/block/mtdblock%d", n);
414
Colin Crosscd0f1732010-04-19 17:10:24 -0700415 if (wait)
416 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000417 if (mount(tmp, target, system, flags, options) < 0) {
418 return -1;
419 }
420
Ken Sumralldd4d7862011-02-17 18:09:47 -0800421 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000422 } else if (!strncmp(source, "loop@", 5)) {
423 int mode, loop, fd;
424 struct loop_info info;
425
426 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
427 fd = open(source + 5, mode);
428 if (fd < 0) {
429 return -1;
430 }
431
432 for (n = 0; ; n++) {
433 sprintf(tmp, "/dev/block/loop%d", n);
434 loop = open(tmp, mode);
435 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100436 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000437 return -1;
438 }
439
440 /* if it is a blank loop device */
441 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
442 /* if it becomes our loop device */
443 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
444 close(fd);
445
446 if (mount(tmp, target, system, flags, options) < 0) {
447 ioctl(loop, LOOP_CLR_FD, 0);
448 close(loop);
449 return -1;
450 }
451
452 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800453 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000454 }
455 }
456
457 close(loop);
458 }
459
460 close(fd);
461 ERROR("out of loopback devices");
462 return -1;
463 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700464 if (wait)
465 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000466 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700467 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000468 }
469
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800471
472exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800473 return 0;
474
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475}
476
JP Abgrallcee20682014-07-02 14:26:54 -0700477static int wipe_data_via_recovery()
478{
479 mkdir("/cache/recovery", 0700);
480 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
481 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700482 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
483 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700484 close(fd);
485 } else {
486 ERROR("could not open /cache/recovery/command\n");
487 return -1;
488 }
489 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
490 while (1) { pause(); } // never reached
491}
492
493
494/*
495 * This function might request a reboot, in which case it will
496 * not return.
497 */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700498int do_mount_all(int nargs, char **args)
499{
500 pid_t pid;
501 int ret = -1;
502 int child_ret = -1;
503 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800504 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505
506 if (nargs != 2) {
507 return -1;
508 }
509
510 /*
511 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
512 * do the call in the child to provide protection to the main init
513 * process if anything goes wrong (crash or memory leak), and wait for
514 * the child to finish in the parent.
515 */
516 pid = fork();
517 if (pid > 0) {
518 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700519 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
520 if (wp_ret < 0) {
521 /* Unexpected error code. We will continue anyway. */
522 NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
523 }
524
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700525 if (WIFEXITED(status)) {
526 ret = WEXITSTATUS(status);
527 } else {
528 ret = -1;
529 }
530 } else if (pid == 0) {
531 /* child, call fs_mgr_mount_all() */
532 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800533 fstab = fs_mgr_read_fstab(args[1]);
534 child_ret = fs_mgr_mount_all(fstab);
535 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700536 if (child_ret == -1) {
537 ERROR("fs_mgr_mount_all returned an error\n");
538 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700539 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700540 } else {
541 /* fork failed, return an error */
542 return -1;
543 }
544
JP Abgrallf22b7452014-07-02 13:16:04 -0700545 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800546 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700547 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700548 property_set("ro.crypto.state", "encrypted");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800549 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700550 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700551 property_set("ro.crypto.state", "unencrypted");
552 /* If fs_mgr determined this is an unencrypted device, then trigger
553 * that action.
554 */
555 action_for_each_trigger("nonencrypted", action_add_queue_tail);
JP Abgrallcee20682014-07-02 14:26:54 -0700556 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
557 /* Setup a wipe via recovery, and reboot into recovery */
558 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
559 ret = wipe_data_via_recovery();
560 /* If reboot worked, there is no return. */
561 } else if (ret > 0) {
562 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700563 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700564 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700565
566 return ret;
567}
568
Ken Sumralla76baaa2013-07-09 18:42:09 -0700569int do_swapon_all(int nargs, char **args)
570{
571 struct fstab *fstab;
572 int ret;
573
574 fstab = fs_mgr_read_fstab(args[1]);
575 ret = fs_mgr_swapon_all(fstab);
576 fs_mgr_free_fstab(fstab);
577
578 return ret;
579}
580
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500581int do_setcon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500582 if (is_selinux_enabled() <= 0)
583 return 0;
584 if (setcon(args[1]) < 0) {
585 return -errno;
586 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500587 return 0;
588}
589
590int do_setenforce(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500591 if (is_selinux_enabled() <= 0)
592 return 0;
593 if (security_setenforce(atoi(args[1])) < 0) {
594 return -errno;
595 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500596 return 0;
597}
598
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599int do_setkey(int nargs, char **args)
600{
601 struct kbentry kbe;
602 kbe.kb_table = strtoul(args[1], 0, 0);
603 kbe.kb_index = strtoul(args[2], 0, 0);
604 kbe.kb_value = strtoul(args[3], 0, 0);
605 return setkey(&kbe);
606}
607
608int do_setprop(int nargs, char **args)
609{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700610 const char *name = args[1];
611 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800612 char prop_val[PROP_VALUE_MAX];
613 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700614
Dima Zavin84bf9af2011-12-20 13:44:41 -0800615 ret = expand_props(prop_val, value, sizeof(prop_val));
616 if (ret) {
617 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
618 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700619 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800620 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700621 return 0;
622}
623
624int do_setrlimit(int nargs, char **args)
625{
626 struct rlimit limit;
627 int resource;
628 resource = atoi(args[1]);
629 limit.rlim_cur = atoi(args[2]);
630 limit.rlim_max = atoi(args[3]);
631 return setrlimit(resource, &limit);
632}
633
634int do_start(int nargs, char **args)
635{
636 struct service *svc;
637 svc = service_find_by_name(args[1]);
638 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700639 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640 }
641 return 0;
642}
643
644int do_stop(int nargs, char **args)
645{
646 struct service *svc;
647 svc = service_find_by_name(args[1]);
648 if (svc) {
649 service_stop(svc);
650 }
651 return 0;
652}
653
654int do_restart(int nargs, char **args)
655{
656 struct service *svc;
657 svc = service_find_by_name(args[1]);
658 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500659 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700660 }
661 return 0;
662}
663
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700664int do_powerctl(int nargs, char **args)
665{
666 char command[PROP_VALUE_MAX];
667 int res;
668 int len = 0;
669 int cmd = 0;
670 char *reboot_target;
671
672 res = expand_props(command, args[1], sizeof(command));
673 if (res) {
674 ERROR("powerctl: cannot expand '%s'\n", args[1]);
675 return -EINVAL;
676 }
677
678 if (strncmp(command, "shutdown", 8) == 0) {
679 cmd = ANDROID_RB_POWEROFF;
680 len = 8;
681 } else if (strncmp(command, "reboot", 6) == 0) {
682 cmd = ANDROID_RB_RESTART2;
683 len = 6;
684 } else {
685 ERROR("powerctl: unrecognized command '%s'\n", command);
686 return -EINVAL;
687 }
688
689 if (command[len] == ',') {
690 reboot_target = &command[len + 1];
691 } else if (command[len] == '\0') {
692 reboot_target = "";
693 } else {
694 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
695 return -EINVAL;
696 }
697
698 return android_reboot(cmd, 0, reboot_target);
699}
700
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701int do_trigger(int nargs, char **args)
702{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000703 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 return 0;
705}
706
707int do_symlink(int nargs, char **args)
708{
709 return symlink(args[1], args[2]);
710}
711
Ken Sumrall203bad52011-01-18 17:37:41 -0800712int do_rm(int nargs, char **args)
713{
714 return unlink(args[1]);
715}
716
717int do_rmdir(int nargs, char **args)
718{
719 return rmdir(args[1]);
720}
721
The Android Open Source Project35237d12008-12-17 18:08:08 -0800722int do_sysclktz(int nargs, char **args)
723{
724 struct timezone tz;
725
726 if (nargs != 2)
727 return -1;
728
729 memset(&tz, 0, sizeof(tz));
730 tz.tz_minuteswest = atoi(args[1]);
731 if (settimeofday(NULL, &tz))
732 return -1;
733 return 0;
734}
735
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736int do_write(int nargs, char **args)
737{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700738 const char *path = args[1];
739 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800740 char prop_val[PROP_VALUE_MAX];
741 int ret;
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700742
Dima Zavin84bf9af2011-12-20 13:44:41 -0800743 ret = expand_props(prop_val, value, sizeof(prop_val));
744 if (ret) {
745 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
746 return -EINVAL;
747 }
748 return write_file(path, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700749}
750
San Mehat7c44fe52009-08-26 16:39:25 -0700751int do_copy(int nargs, char **args)
752{
753 char *buffer = NULL;
754 int rc = 0;
755 int fd1 = -1, fd2 = -1;
756 struct stat info;
757 int brtw, brtr;
758 char *p;
759
760 if (nargs != 3)
761 return -1;
762
763 if (stat(args[1], &info) < 0)
764 return -1;
765
766 if ((fd1 = open(args[1], O_RDONLY)) < 0)
767 goto out_err;
768
Tom Zhu4833d9f2009-09-28 19:53:12 -0500769 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700770 goto out_err;
771
772 if (!(buffer = malloc(info.st_size)))
773 goto out_err;
774
775 p = buffer;
776 brtr = info.st_size;
777 while(brtr) {
778 rc = read(fd1, p, brtr);
779 if (rc < 0)
780 goto out_err;
781 if (rc == 0)
782 break;
783 p += rc;
784 brtr -= rc;
785 }
786
787 p = buffer;
788 brtw = info.st_size;
789 while(brtw) {
790 rc = write(fd2, p, brtw);
791 if (rc < 0)
792 goto out_err;
793 if (rc == 0)
794 break;
795 p += rc;
796 brtw -= rc;
797 }
798
799 rc = 0;
800 goto out;
801out_err:
802 rc = -1;
803out:
804 if (buffer)
805 free(buffer);
806 if (fd1 >= 0)
807 close(fd1);
808 if (fd2 >= 0)
809 close(fd2);
810 return rc;
811}
812
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813int do_chown(int nargs, char **args) {
814 /* GID is optional. */
815 if (nargs == 3) {
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700816 if (_chown(args[2], decode_uid(args[1]), -1) < 0)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 return -errno;
818 } else if (nargs == 4) {
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700819 if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820 return -errno;
821 } else {
822 return -1;
823 }
824 return 0;
825}
826
827static mode_t get_mode(const char *s) {
828 mode_t mode = 0;
829 while (*s) {
830 if (*s >= '0' && *s <= '7') {
831 mode = (mode<<3) | (*s-'0');
832 } else {
833 return -1;
834 }
835 s++;
836 }
837 return mode;
838}
839
840int do_chmod(int nargs, char **args) {
841 mode_t mode = get_mode(args[1]);
Geremy Condra9ed1fe72012-03-20 12:49:55 -0700842 if (_chmod(args[2], mode) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843 return -errno;
844 }
845 return 0;
846}
847
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500848int do_restorecon(int nargs, char **args) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500849 int i;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400850 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500851
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500852 for (i = 1; i < nargs; i++) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400853 if (restorecon(args[i]) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400854 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500855 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400856 return ret;
857}
858
859int do_restorecon_recursive(int nargs, char **args) {
860 int i;
861 int ret = 0;
862
863 for (i = 1; i < nargs; i++) {
864 if (restorecon_recursive(args[i]) < 0)
865 ret = -errno;
866 }
867 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500868}
869
870int do_setsebool(int nargs, char **args) {
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500871 const char *name = args[1];
872 const char *value = args[2];
873 SELboolean b;
874 int ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500875
876 if (is_selinux_enabled() <= 0)
877 return 0;
878
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500879 b.name = name;
880 if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
881 b.value = 1;
882 else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
883 b.value = 0;
884 else {
885 ERROR("setsebool: invalid value %s\n", value);
886 return -EINVAL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500887 }
888
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500889 if (security_set_boolean_list(1, &b, 0) < 0) {
890 ret = -errno;
891 ERROR("setsebool: could not set %s to %s\n", name, value);
892 return ret;
893 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700894
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500895 return 0;
896}
897
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700898int do_loglevel(int nargs, char **args) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700899 int log_level;
900 char log_level_str[PROP_VALUE_MAX] = "";
901 if (nargs != 2) {
902 ERROR("loglevel: missing argument\n");
903 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700905
906 if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
907 ERROR("loglevel: cannot expand '%s'\n", args[1]);
908 return -EINVAL;
909 }
910 log_level = atoi(log_level_str);
911 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
912 ERROR("loglevel: invalid log level'%d'\n", log_level);
913 return -EINVAL;
914 }
915 klog_set_level(log_level);
916 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700917}
918
Ken Sumrallc5c51032011-03-08 17:01:29 -0800919int do_load_persist_props(int nargs, char **args) {
920 if (nargs == 1) {
921 load_persist_props();
922 return 0;
923 }
924 return -1;
925}
926
Riley Andrewse4b7b292014-06-16 15:06:21 -0700927int do_load_all_props(int nargs, char **args) {
928 if (nargs == 1) {
929 load_all_props();
930 return 0;
931 }
932 return -1;
933}
934
Colin Crosscd0f1732010-04-19 17:10:24 -0700935int do_wait(int nargs, char **args)
936{
937 if (nargs == 2) {
938 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800939 } else if (nargs == 3) {
940 return wait_for_file(args[1], atoi(args[2]));
941 } else
942 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700943}