blob: 41fc03e12360d9ccd89701149ba3130ec6329c64 [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>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000032#include <linux/loop.h>
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -070033#include <cutils/partition_utils.h>
Dima Zavin84bf9af2011-12-20 13:44:41 -080034#include <sys/system_properties.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#ifdef HAVE_SELINUX
37#include <selinux/selinux.h>
38#include <selinux/label.h>
39#endif
40
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
51void add_environment(const char *name, const char *value);
52
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
59 fd = open(path, O_WRONLY|O_CREAT, 0622);
60
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
Geremy Condra42a93492012-03-20 12:49:55 -070078static int _chown(const char *path, unsigned int uid, unsigned int gid)
79{
80 int fd;
81 int ret;
82
83 fd = open(path, O_RDONLY | O_NOFOLLOW);
84 if (fd < 0) {
85 return -1;
86 }
87
88 ret = fchown(fd, uid, gid);
89 if (ret < 0) {
90 int errno_copy = errno;
91 close(fd);
92 errno = errno_copy;
93 return -1;
94 }
95
96 close(fd);
97
98 return 0;
99}
100
101static int _chmod(const char *path, mode_t mode)
102{
103 int fd;
104 int ret;
105
106 fd = open(path, O_RDONLY | O_NOFOLLOW);
107 if (fd < 0) {
108 return -1;
109 }
110
111 ret = fchmod(fd, mode);
112 if (ret < 0) {
113 int errno_copy = errno;
114 close(fd);
115 errno = errno_copy;
116 return -1;
117 }
118
119 close(fd);
120
121 return 0;
122}
123
The Android Open Source Project35237d12008-12-17 18:08:08 -0800124static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125{
126 void *module;
127 unsigned size;
128 int ret;
129
130 module = read_file(filename, &size);
131 if (!module)
132 return -1;
133
The Android Open Source Project35237d12008-12-17 18:08:08 -0800134 ret = init_module(module, size, options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135
136 free(module);
137
138 return ret;
139}
140
141static int setkey(struct kbentry *kbe)
142{
143 int fd, ret;
144
145 fd = open("/dev/tty0", O_RDWR | O_SYNC);
146 if (fd < 0)
147 return -1;
148
149 ret = ioctl(fd, KDSKBENT, kbe);
150
151 close(fd);
152 return ret;
153}
154
155static int __ifupdown(const char *interface, int up)
156{
157 struct ifreq ifr;
158 int s, ret;
159
160 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
161
162 s = socket(AF_INET, SOCK_DGRAM, 0);
163 if (s < 0)
164 return -1;
165
166 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
167 if (ret < 0) {
168 goto done;
169 }
170
171 if (up)
172 ifr.ifr_flags |= IFF_UP;
173 else
174 ifr.ifr_flags &= ~IFF_UP;
175
176 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
177
178done:
179 close(s);
180 return ret;
181}
182
183static void service_start_if_not_disabled(struct service *svc)
184{
185 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700186 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187 }
188}
189
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000190int do_chdir(int nargs, char **args)
191{
192 chdir(args[1]);
193 return 0;
194}
195
196int do_chroot(int nargs, char **args)
197{
198 chroot(args[1]);
199 return 0;
200}
201
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700202int do_class_start(int nargs, char **args)
203{
204 /* Starting a class does not start services
205 * which are explicitly disabled. They must
206 * be started individually.
207 */
208 service_for_each_class(args[1], service_start_if_not_disabled);
209 return 0;
210}
211
212int do_class_stop(int nargs, char **args)
213{
214 service_for_each_class(args[1], service_stop);
215 return 0;
216}
217
Ken Sumrall752923c2010-12-03 16:33:31 -0800218int do_class_reset(int nargs, char **args)
219{
220 service_for_each_class(args[1], service_reset);
221 return 0;
222}
223
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700224int do_domainname(int nargs, char **args)
225{
226 return write_file("/proc/sys/kernel/domainname", args[1]);
227}
228
229int do_exec(int nargs, char **args)
230{
231 return -1;
232}
233
234int do_export(int nargs, char **args)
235{
236 add_environment(args[1], args[2]);
237 return 0;
238}
239
240int do_hostname(int nargs, char **args)
241{
242 return write_file("/proc/sys/kernel/hostname", args[1]);
243}
244
245int do_ifup(int nargs, char **args)
246{
247 return __ifupdown(args[1], 1);
248}
249
The Android Open Source Project35237d12008-12-17 18:08:08 -0800250
251static int do_insmod_inner(int nargs, char **args, int opt_len)
252{
253 char options[opt_len + 1];
254 int i;
255
256 options[0] = '\0';
257 if (nargs > 2) {
258 strcpy(options, args[2]);
259 for (i = 3; i < nargs; ++i) {
260 strcat(options, " ");
261 strcat(options, args[i]);
262 }
263 }
264
265 return insmod(args[1], options);
266}
267
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268int do_insmod(int nargs, char **args)
269{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800270 int i;
271 int size = 0;
272
273 if (nargs > 2) {
274 for (i = 2; i < nargs; ++i)
275 size += strlen(args[i]) + 1;
276 }
277
278 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279}
280
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281int do_mkdir(int nargs, char **args)
282{
283 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700284 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285
286 /* mkdir <path> [mode] [owner] [group] */
287
288 if (nargs >= 3) {
289 mode = strtoul(args[2], 0, 8);
290 }
291
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700292 ret = mkdir(args[1], mode);
293 /* chmod in case the directory already exists */
294 if (ret == -1 && errno == EEXIST) {
Geremy Condra42a93492012-03-20 12:49:55 -0700295 ret = _chmod(args[1], mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700296 }
297 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 return -errno;
299 }
300
301 if (nargs >= 4) {
302 uid_t uid = decode_uid(args[3]);
303 gid_t gid = -1;
304
305 if (nargs == 5) {
306 gid = decode_uid(args[4]);
307 }
308
Geremy Condra42a93492012-03-20 12:49:55 -0700309 if (_chown(args[1], uid, gid) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 return -errno;
311 }
312 }
313
314 return 0;
315}
316
317static struct {
318 const char *name;
319 unsigned flag;
320} mount_flags[] = {
321 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200322 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 { "nosuid", MS_NOSUID },
324 { "nodev", MS_NODEV },
325 { "nodiratime", MS_NODIRATIME },
326 { "ro", MS_RDONLY },
327 { "rw", 0 },
328 { "remount", MS_REMOUNT },
329 { "defaults", 0 },
330 { 0, 0 },
331};
332
Ken Sumrall752923c2010-12-03 16:33:31 -0800333#define DATA_MNT_POINT "/data"
334
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700335/* mount <type> <device> <path> <flags ...> <options> */
336int do_mount(int nargs, char **args)
337{
338 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000339 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340 char *options = NULL;
341 unsigned flags = 0;
342 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700343 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700344
345 for (n = 4; n < nargs; n++) {
346 for (i = 0; mount_flags[i].name; i++) {
347 if (!strcmp(args[n], mount_flags[i].name)) {
348 flags |= mount_flags[i].flag;
349 break;
350 }
351 }
352
Colin Crosscd0f1732010-04-19 17:10:24 -0700353 if (!mount_flags[i].name) {
354 if (!strcmp(args[n], "wait"))
355 wait = 1;
356 /* if our last argument isn't a flag, wolf it up as an option string */
357 else if (n + 1 == nargs)
358 options = args[n];
359 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 }
361
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 target = args[3];
365
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 if (!strncmp(source, "mtd@", 4)) {
367 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000368 if (n < 0) {
369 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000371
372 sprintf(tmp, "/dev/block/mtdblock%d", n);
373
Colin Crosscd0f1732010-04-19 17:10:24 -0700374 if (wait)
375 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000376 if (mount(tmp, target, system, flags, options) < 0) {
377 return -1;
378 }
379
Ken Sumralldd4d7862011-02-17 18:09:47 -0800380 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000381 } else if (!strncmp(source, "loop@", 5)) {
382 int mode, loop, fd;
383 struct loop_info info;
384
385 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
386 fd = open(source + 5, mode);
387 if (fd < 0) {
388 return -1;
389 }
390
391 for (n = 0; ; n++) {
392 sprintf(tmp, "/dev/block/loop%d", n);
393 loop = open(tmp, mode);
394 if (loop < 0) {
395 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 Sumrall7bc6e9e2011-05-26 20:01:39 -0700425 /* If this fails, it may be an encrypted filesystem
426 * or it could just be wiped. If wiped, that will be
427 * handled later in the boot process.
Ken Sumrall752923c2010-12-03 16:33:31 -0800428 * We only support encrypting /data. Check
429 * if we're trying to mount it, and if so,
430 * assume it's encrypted, mount a tmpfs instead.
431 * Then save the orig mount parms in properties
432 * for vold to query when it mounts the real
433 * encrypted /data.
434 */
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -0700435 if (!strcmp(target, DATA_MNT_POINT) && !partition_wiped(source)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800436 const char *tmpfs_options;
437
438 tmpfs_options = property_get("ro.crypto.tmpfs_options");
439
440 if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
441 tmpfs_options) < 0) {
442 return -1;
443 }
444
445 /* Set the property that triggers the framework to do a minimal
446 * startup and ask the user for a password
447 */
Ken Sumrall4e84d3b2011-01-14 12:44:09 -0800448 property_set("ro.crypto.state", "encrypted");
Ken Sumrall752923c2010-12-03 16:33:31 -0800449 property_set("vold.decrypt", "1");
450 } else {
451 return -1;
452 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000453 }
454
Ken Sumrall752923c2010-12-03 16:33:31 -0800455 if (!strcmp(target, DATA_MNT_POINT)) {
456 char fs_flags[32];
457
458 /* Save the original mount options */
459 property_set("ro.crypto.fs_type", system);
460 property_set("ro.crypto.fs_real_blkdev", source);
461 property_set("ro.crypto.fs_mnt_point", target);
462 if (options) {
463 property_set("ro.crypto.fs_options", options);
464 }
465 snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
466 property_set("ro.crypto.fs_flags", fs_flags);
467 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700468 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800469
470exit_success:
471 /* If not running encrypted, then set the property saying we are
472 * unencrypted, and also trigger the action for a nonencrypted system.
473 */
474 if (!strcmp(target, DATA_MNT_POINT)) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800475 const char *prop;
476
Ken Sumralldd4d7862011-02-17 18:09:47 -0800477 prop = property_get("ro.crypto.state");
478 if (! prop) {
479 prop = "notset";
480 }
481 if (strcmp(prop, "encrypted")) {
482 property_set("ro.crypto.state", "unencrypted");
483 action_for_each_trigger("nonencrypted", action_add_queue_tail);
484 }
485 }
486
487 return 0;
488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489}
490
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500491int do_setcon(int nargs, char **args) {
492#ifdef HAVE_SELINUX
493 if (is_selinux_enabled() <= 0)
494 return 0;
495 if (setcon(args[1]) < 0) {
496 return -errno;
497 }
498#endif
499 return 0;
500}
501
502int do_setenforce(int nargs, char **args) {
503#ifdef HAVE_SELINUX
504 if (is_selinux_enabled() <= 0)
505 return 0;
506 if (security_setenforce(atoi(args[1])) < 0) {
507 return -errno;
508 }
509#endif
510 return 0;
511}
512
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700513int do_setkey(int nargs, char **args)
514{
515 struct kbentry kbe;
516 kbe.kb_table = strtoul(args[1], 0, 0);
517 kbe.kb_index = strtoul(args[2], 0, 0);
518 kbe.kb_value = strtoul(args[3], 0, 0);
519 return setkey(&kbe);
520}
521
522int do_setprop(int nargs, char **args)
523{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700524 const char *name = args[1];
525 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800526 char prop_val[PROP_VALUE_MAX];
527 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700528
Dima Zavin84bf9af2011-12-20 13:44:41 -0800529 ret = expand_props(prop_val, value, sizeof(prop_val));
530 if (ret) {
531 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
532 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700533 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800534 property_set(name, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700535 return 0;
536}
537
538int do_setrlimit(int nargs, char **args)
539{
540 struct rlimit limit;
541 int resource;
542 resource = atoi(args[1]);
543 limit.rlim_cur = atoi(args[2]);
544 limit.rlim_max = atoi(args[3]);
545 return setrlimit(resource, &limit);
546}
547
548int do_start(int nargs, char **args)
549{
550 struct service *svc;
551 svc = service_find_by_name(args[1]);
552 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700553 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554 }
555 return 0;
556}
557
558int do_stop(int nargs, char **args)
559{
560 struct service *svc;
561 svc = service_find_by_name(args[1]);
562 if (svc) {
563 service_stop(svc);
564 }
565 return 0;
566}
567
568int do_restart(int nargs, char **args)
569{
570 struct service *svc;
571 svc = service_find_by_name(args[1]);
572 if (svc) {
573 service_stop(svc);
San Mehatf24e2522009-05-19 13:30:46 -0700574 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700575 }
576 return 0;
577}
578
579int do_trigger(int nargs, char **args)
580{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000581 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582 return 0;
583}
584
585int do_symlink(int nargs, char **args)
586{
587 return symlink(args[1], args[2]);
588}
589
Ken Sumrall203bad52011-01-18 17:37:41 -0800590int do_rm(int nargs, char **args)
591{
592 return unlink(args[1]);
593}
594
595int do_rmdir(int nargs, char **args)
596{
597 return rmdir(args[1]);
598}
599
The Android Open Source Project35237d12008-12-17 18:08:08 -0800600int do_sysclktz(int nargs, char **args)
601{
602 struct timezone tz;
603
604 if (nargs != 2)
605 return -1;
606
607 memset(&tz, 0, sizeof(tz));
608 tz.tz_minuteswest = atoi(args[1]);
609 if (settimeofday(NULL, &tz))
610 return -1;
611 return 0;
612}
613
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700614int do_write(int nargs, char **args)
615{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700616 const char *path = args[1];
617 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800618 char prop_val[PROP_VALUE_MAX];
619 int ret;
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700620
Dima Zavin84bf9af2011-12-20 13:44:41 -0800621 ret = expand_props(prop_val, value, sizeof(prop_val));
622 if (ret) {
623 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
624 return -EINVAL;
625 }
626 return write_file(path, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627}
628
San Mehat7c44fe52009-08-26 16:39:25 -0700629int do_copy(int nargs, char **args)
630{
631 char *buffer = NULL;
632 int rc = 0;
633 int fd1 = -1, fd2 = -1;
634 struct stat info;
635 int brtw, brtr;
636 char *p;
637
638 if (nargs != 3)
639 return -1;
640
641 if (stat(args[1], &info) < 0)
642 return -1;
643
644 if ((fd1 = open(args[1], O_RDONLY)) < 0)
645 goto out_err;
646
Tom Zhu4833d9f2009-09-28 19:53:12 -0500647 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700648 goto out_err;
649
650 if (!(buffer = malloc(info.st_size)))
651 goto out_err;
652
653 p = buffer;
654 brtr = info.st_size;
655 while(brtr) {
656 rc = read(fd1, p, brtr);
657 if (rc < 0)
658 goto out_err;
659 if (rc == 0)
660 break;
661 p += rc;
662 brtr -= rc;
663 }
664
665 p = buffer;
666 brtw = info.st_size;
667 while(brtw) {
668 rc = write(fd2, p, brtw);
669 if (rc < 0)
670 goto out_err;
671 if (rc == 0)
672 break;
673 p += rc;
674 brtw -= rc;
675 }
676
677 rc = 0;
678 goto out;
679out_err:
680 rc = -1;
681out:
682 if (buffer)
683 free(buffer);
684 if (fd1 >= 0)
685 close(fd1);
686 if (fd2 >= 0)
687 close(fd2);
688 return rc;
689}
690
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700691int do_chown(int nargs, char **args) {
692 /* GID is optional. */
693 if (nargs == 3) {
Geremy Condra42a93492012-03-20 12:49:55 -0700694 if (_chown(args[2], decode_uid(args[1]), -1) < 0)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700695 return -errno;
696 } else if (nargs == 4) {
Geremy Condra42a93492012-03-20 12:49:55 -0700697 if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700698 return -errno;
699 } else {
700 return -1;
701 }
702 return 0;
703}
704
705static mode_t get_mode(const char *s) {
706 mode_t mode = 0;
707 while (*s) {
708 if (*s >= '0' && *s <= '7') {
709 mode = (mode<<3) | (*s-'0');
710 } else {
711 return -1;
712 }
713 s++;
714 }
715 return mode;
716}
717
718int do_chmod(int nargs, char **args) {
719 mode_t mode = get_mode(args[1]);
Geremy Condra42a93492012-03-20 12:49:55 -0700720 if (_chmod(args[2], mode) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700721 return -errno;
722 }
723 return 0;
724}
725
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500726int do_restorecon(int nargs, char **args) {
727#ifdef HAVE_SELINUX
728 char *secontext = NULL;
729 struct stat sb;
730 int i;
731
732 if (is_selinux_enabled() <= 0 || !sehandle)
733 return 0;
734
735 for (i = 1; i < nargs; i++) {
736 if (lstat(args[i], &sb) < 0)
737 return -errno;
738 if (selabel_lookup(sehandle, &secontext, args[i], sb.st_mode) < 0)
739 return -errno;
740 if (lsetfilecon(args[i], secontext) < 0) {
741 freecon(secontext);
742 return -errno;
743 }
744 freecon(secontext);
745 }
746#endif
747 return 0;
748}
749
750int do_setsebool(int nargs, char **args) {
751#ifdef HAVE_SELINUX
752 SELboolean *b = alloca(nargs * sizeof(SELboolean));
753 char *v;
754 int i;
755
756 if (is_selinux_enabled() <= 0)
757 return 0;
758
759 for (i = 1; i < nargs; i++) {
760 char *name = args[i];
761 v = strchr(name, '=');
762 if (!v) {
763 ERROR("setsebool: argument %s had no =\n", name);
764 return -EINVAL;
765 }
766 *v++ = 0;
767 b[i-1].name = name;
768 if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
769 b[i-1].value = 1;
770 else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
771 b[i-1].value = 0;
772 else {
773 ERROR("setsebool: invalid value %s\n", v);
774 return -EINVAL;
775 }
776 }
777
778 if (security_set_boolean_list(nargs - 1, b, 0) < 0)
779 return -errno;
780#endif
781 return 0;
782}
783
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784int do_loglevel(int nargs, char **args) {
785 if (nargs == 2) {
Dima Zavin8f912822011-08-31 18:26:17 -0700786 klog_set_level(atoi(args[1]));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787 return 0;
788 }
789 return -1;
790}
791
Ken Sumrallc5c51032011-03-08 17:01:29 -0800792int do_load_persist_props(int nargs, char **args) {
793 if (nargs == 1) {
794 load_persist_props();
795 return 0;
796 }
797 return -1;
798}
799
Colin Crosscd0f1732010-04-19 17:10:24 -0700800int do_wait(int nargs, char **args)
801{
802 if (nargs == 2) {
803 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
804 }
805 return -1;
806}