14f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/*
24f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
34f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
44f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
54f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * you may not use this file except in compliance with the License.
64f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * You may obtain a copy of the License at
74f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
84f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
94f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * See the License for the specific language governing permissions and
144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * limitations under the License.
154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project */
164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h>
184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h>
194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h>
204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h>
214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h>
224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h>
234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/kd.h>
244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <errno.h>
254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h>
264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <netinet/in.h>
274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/if.h>
284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <arpa/inet.h>
294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h>
304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/mount.h>
314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/resource.h>
320e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall#include <sys/wait.h>
33e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)#include <linux/loop.h>
347bc6e9ebe4783d5fbae17d7249cb80e997531598Ken Sumrall#include <cutils/partition_utils.h>
3584bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin#include <sys/system_properties.h>
360e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall#include <fs_mgr.h>
374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
38e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
39e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/selinux.h>
40e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/label.h>
41e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
42e46f9d510db9351682cf17c49115110870147335Stephen Smalley
434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "init.h"
444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "keywords.h"
454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "property_service.h"
464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "devices.h"
476310a8261c922533a692fb3e74ece2da98d4bafaColin Cross#include "init_parser.h"
483899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross#include "util.h"
49ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h"
504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h>
524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectvoid add_environment(const char *name, const char *value);
544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectextern int init_module(void *, unsigned long, const char *);
564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int write_file(const char *path, const char *value)
584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int fd, ret, len;
604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = open(path, O_WRONLY|O_CREAT, 0622);
624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (fd < 0)
64008abac082f1c098d402f944d9287dce67ffce0aMike Chan        return -errno;
654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len = strlen(value);
674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    do {
694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ret = write(fd, value, len);
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    } while (ret < 0 && errno == EINTR);
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fd);
734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (ret < 0) {
74008abac082f1c098d402f944d9287dce67ffce0aMike Chan        return -errno;
754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    } else {
764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return 0;
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8058903013084b649fa81e9fd3310517d0e35e7c5dBenoit Gobystatic int _open(const char *path)
8158903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby{
8258903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    int fd;
8358903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby
8458903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    fd = open(path, O_RDONLY | O_NOFOLLOW);
8558903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    if (fd < 0)
8658903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby        fd = open(path, O_WRONLY | O_NOFOLLOW);
8758903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby
8858903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    return fd;
8958903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby}
9058903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby
919ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condrastatic int _chown(const char *path, unsigned int uid, unsigned int gid)
929ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra{
939ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    int fd;
949ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    int ret;
959ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
9658903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    fd = _open(path);
979ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    if (fd < 0) {
989ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        return -1;
999ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    }
1009ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1019ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    ret = fchown(fd, uid, gid);
1029ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    if (ret < 0) {
1039ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        int errno_copy = errno;
1049ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        close(fd);
1059ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        errno = errno_copy;
1069ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        return -1;
1079ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    }
1089ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1099ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    close(fd);
1109ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1119ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    return 0;
1129ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra}
1139ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1149ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condrastatic int _chmod(const char *path, mode_t mode)
1159ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra{
1169ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    int fd;
1179ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    int ret;
1189ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
11958903013084b649fa81e9fd3310517d0e35e7c5dBenoit Goby    fd = _open(path);
1209ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    if (fd < 0) {
1219ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        return -1;
1229ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    }
1239ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1249ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    ret = fchmod(fd, mode);
1259ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    if (ret < 0) {
1269ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        int errno_copy = errno;
1279ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        close(fd);
1289ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        errno = errno_copy;
1299ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        return -1;
1309ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    }
1319ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1329ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    close(fd);
1339ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
1349ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    return 0;
1359ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra}
1369ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra
13735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Projectstatic int insmod(const char *filename, char *options)
1384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    void *module;
1404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned size;
1414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret;
1424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    module = read_file(filename, &size);
1444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!module)
1454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
1464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
14735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    ret = init_module(module, size, options);
1484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(module);
1504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
1524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int setkey(struct kbentry *kbe)
1554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int fd, ret;
1574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = open("/dev/tty0", O_RDWR | O_SYNC);
1594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (fd < 0)
1604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
1614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    ret = ioctl(fd, KDSKBENT, kbe);
1634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fd);
1654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
1664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int __ifupdown(const char *interface, int up)
1694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct ifreq ifr;
1714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int s, ret;
1724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
1744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    s = socket(AF_INET, SOCK_DGRAM, 0);
1764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (s < 0)
1774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
1784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    ret = ioctl(s, SIOCGIFFLAGS, &ifr);
1804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (ret < 0) {
1814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto done;
1824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (up)
1854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ifr.ifr_flags |= IFF_UP;
1864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
1874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ifr.ifr_flags &= ~IFF_UP;
1884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    ret = ioctl(s, SIOCSIFFLAGS, &ifr);
1904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdone:
1924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(s);
1934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
1944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void service_start_if_not_disabled(struct service *svc)
1974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!(svc->flags & SVC_DISABLED)) {
199f24e252903ca0f71c7fbfb135cf17e83e0c2ea90San Mehat        service_start(svc, NULL);
2004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
2014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
203e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)int do_chdir(int nargs, char **args)
204e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik){
205e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)    chdir(args[1]);
206e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)    return 0;
207e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)}
208e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)
209e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)int do_chroot(int nargs, char **args)
210e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik){
211e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)    chroot(args[1]);
212e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)    return 0;
213e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)}
214e7cb137f32fbc7caf97b80fac152b4bd3ac242cdJay Freeman (saurik)
2154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_class_start(int nargs, char **args)
2164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* Starting a class does not start services
2184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project         * which are explicitly disabled.  They must
2194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project         * be started individually.
2204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project         */
2214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    service_for_each_class(args[1], service_start_if_not_disabled);
2224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
2234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_class_stop(int nargs, char **args)
2264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    service_for_each_class(args[1], service_stop);
2284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
2294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
231752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrallint do_class_reset(int nargs, char **args)
232752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall{
233752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall    service_for_each_class(args[1], service_reset);
234752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall    return 0;
235752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall}
236752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall
2374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_domainname(int nargs, char **args)
2384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return write_file("/proc/sys/kernel/domainname", args[1]);
2404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_exec(int nargs, char **args)
2434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return -1;
2454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_export(int nargs, char **args)
2484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    add_environment(args[1], args[2]);
2504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
2514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_hostname(int nargs, char **args)
2544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return write_file("/proc/sys/kernel/hostname", args[1]);
2564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_ifup(int nargs, char **args)
2594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return __ifupdown(args[1], 1);
2614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
26335237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
26435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Projectstatic int do_insmod_inner(int nargs, char **args, int opt_len)
26535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project{
26635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    char options[opt_len + 1];
26735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    int i;
26835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
26935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    options[0] = '\0';
27035237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    if (nargs > 2) {
27135237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        strcpy(options, args[2]);
27235237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        for (i = 3; i < nargs; ++i) {
27335237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project            strcat(options, " ");
27435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project            strcat(options, args[i]);
27535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        }
27635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    }
27735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
27835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    return insmod(args[1], options);
27935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project}
28035237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
2814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_insmod(int nargs, char **args)
2824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
28335237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    int i;
28435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    int size = 0;
28535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
28635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    if (nargs > 2) {
28735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        for (i = 2; i < nargs; ++i)
28835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project            size += strlen(args[i]) + 1;
28935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    }
29035237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
29135237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    return do_insmod_inner(nargs, args, size);
2924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_mkdir(int nargs, char **args)
2954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode = 0755;
29727164dce5f0788476449057c978fa24e62245cb8Chia-chi Yeh    int ret;
2984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* mkdir <path> [mode] [owner] [group] */
3004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (nargs >= 3) {
3024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        mode = strtoul(args[2], 0, 8);
3034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
305e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalley    ret = make_dir(args[1], mode);
30627164dce5f0788476449057c978fa24e62245cb8Chia-chi Yeh    /* chmod in case the directory already exists */
30727164dce5f0788476449057c978fa24e62245cb8Chia-chi Yeh    if (ret == -1 && errno == EEXIST) {
3089ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        ret = _chmod(args[1], mode);
30927164dce5f0788476449057c978fa24e62245cb8Chia-chi Yeh    }
31027164dce5f0788476449057c978fa24e62245cb8Chia-chi Yeh    if (ret == -1) {
3114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -errno;
3124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (nargs >= 4) {
3154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        uid_t uid = decode_uid(args[3]);
3164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        gid_t gid = -1;
3174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if (nargs == 5) {
3194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            gid = decode_uid(args[4]);
3204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3229ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        if (_chown(args[1], uid, gid) < 0) {
3234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            return -errno;
3244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3255c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby
3265c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby        /* chown may have cleared S_ISUID and S_ISGID, chmod again */
3275c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby        if (mode & (S_ISUID | S_ISGID)) {
3285c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby            ret = _chmod(args[1], mode);
3295c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby            if (ret == -1) {
3305c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby                return -errno;
3315c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby            }
3325c8574b51210be53efbb488965db0b8591c8d1cfBenoit Goby        }
3334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
3364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
3374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic struct {
3394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *name;
3404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned flag;
3414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} mount_flags[] = {
3424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "noatime",    MS_NOATIME },
343b6ee25e3ad4cffa2b0f5bb734df4b503e1e367baLars Svensson    { "noexec",     MS_NOEXEC },
3444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "nosuid",     MS_NOSUID },
3454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "nodev",      MS_NODEV },
3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "nodiratime", MS_NODIRATIME },
3474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "ro",         MS_RDONLY },
3484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "rw",         0 },
3494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "remount",    MS_REMOUNT },
350e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "bind",       MS_BIND },
351e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "rec",        MS_REC },
352e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "unbindable", MS_UNBINDABLE },
353e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "private",    MS_PRIVATE },
354e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "slave",      MS_SLAVE },
355e50ac5f7771872331df70251d23d6bd8155da4a7Jeff Sharkey    { "shared",     MS_SHARED },
3564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { "defaults",   0 },
3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    { 0,            0 },
3584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
3594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
360752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall#define DATA_MNT_POINT "/data"
361752923c168009d03e9e00e590155fbd0a2880ccbKen Sumrall
3624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* mount <type> <device> <path> <flags ...> <options> */
3634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_mount(int nargs, char **args)
3644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char tmp[64];
366e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)    char *source, *target, *system;
3674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *options = NULL;
3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned flags = 0;
3694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int n, i;
370cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross    int wait = 0;
3714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    for (n = 4; n < nargs; n++) {
3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        for (i = 0; mount_flags[i].name; i++) {
3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if (!strcmp(args[n], mount_flags[i].name)) {
3754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                flags |= mount_flags[i].flag;
3764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                break;
3774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
3784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
380cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross        if (!mount_flags[i].name) {
381cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross            if (!strcmp(args[n], "wait"))
382cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross                wait = 1;
383cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross            /* if our last argument isn't a flag, wolf it up as an option string */
384cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross            else if (n + 1 == nargs)
385cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross                options = args[n];
386cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross        }
3874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
389e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)    system = args[1];
3904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    source = args[2];
391e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)    target = args[3];
392e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
3934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!strncmp(source, "mtd@", 4)) {
3944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        n = mtd_name_to_number(source + 4);
395e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        if (n < 0) {
396e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            return -1;
397e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        }
398e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
399e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        sprintf(tmp, "/dev/block/mtdblock%d", n);
400e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
401cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross        if (wait)
402cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross            wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
403e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        if (mount(tmp, target, system, flags, options) < 0) {
404e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            return -1;
405e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        }
406e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
407dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall        goto exit_success;
408e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)    } else if (!strncmp(source, "loop@", 5)) {
409e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        int mode, loop, fd;
410e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        struct loop_info info;
411e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
412e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
413e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        fd = open(source + 5, mode);
414e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        if (fd < 0) {
415e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            return -1;
4164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
417e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
418e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        for (n = 0; ; n++) {
419e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            sprintf(tmp, "/dev/block/loop%d", n);
420e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            loop = open(tmp, mode);
421e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            if (loop < 0) {
422e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                return -1;
423e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            }
424e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
425e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            /* if it is a blank loop device */
426e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
427e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                /* if it becomes our loop device */
428e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
429e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                    close(fd);
430e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
431e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                    if (mount(tmp, target, system, flags, options) < 0) {
432e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                        ioctl(loop, LOOP_CLR_FD, 0);
433e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                        close(loop);
434e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                        return -1;
435e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                    }
436e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
437e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                    close(loop);
438dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall                    goto exit_success;
439e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)                }
440e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            }
441e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
442e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)            close(loop);
443e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        }
444e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
445e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        close(fd);
446e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        ERROR("out of loopback devices");
447e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        return -1;
448e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)    } else {
449cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross        if (wait)
450cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross            wait_for_file(source, COMMAND_RETRY_TIMEOUT);
451e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        if (mount(source, target, system, flags, options) < 0) {
4520e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall            return -1;
453e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)        }
454e520d036165b36cf5c4cb305f9cec7d183977b61Jay Freeman (saurik)
4554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
456dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall
457dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrallexit_success:
4580e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    return 0;
4590e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall
4600e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall}
461c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall
4620e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrallint do_mount_all(int nargs, char **args)
4630e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall{
4640e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    pid_t pid;
4650e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    int ret = -1;
4660e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    int child_ret = -1;
4670e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    int status;
4680e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    const char *prop;
4690e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall
4700e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    if (nargs != 2) {
4710e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        return -1;
4720e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    }
4730e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall
4740e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    /*
4750e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall     * Call fs_mgr_mount_all() to mount all filesystems.  We fork(2) and
4760e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall     * do the call in the child to provide protection to the main init
4770e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall     * process if anything goes wrong (crash or memory leak), and wait for
4780e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall     * the child to finish in the parent.
4790e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall     */
4800e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    pid = fork();
4810e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    if (pid > 0) {
4820e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        /* Parent.  Wait for the child to return */
4830e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        waitpid(pid, &status, 0);
4840e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        if (WIFEXITED(status)) {
4850e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall            ret = WEXITSTATUS(status);
4860e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        } else {
4870e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall            ret = -1;
488dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall        }
4890e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    } else if (pid == 0) {
4900e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        /* child, call fs_mgr_mount_all() */
4910e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        klog_set_level(6);  /* So we can see what fs_mgr_mount_all() does */
4920e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        child_ret = fs_mgr_mount_all(args[1]);
4930e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        if (child_ret == -1) {
4940e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall            ERROR("fs_mgr_mount_all returned an error\n");
495dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall        }
4960e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        exit(child_ret);
4970e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    } else {
4980e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        /* fork failed, return an error */
4990e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        return -1;
500dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall    }
501dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall
5020e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */
5030e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    if (ret == 1) {
5040e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        property_set("ro.crypto.state", "encrypted");
5050e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        property_set("vold.decrypt", "1");
5060e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    } else if (ret == 0) {
5070e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        property_set("ro.crypto.state", "unencrypted");
5080e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        /* If fs_mgr determined this is an unencrypted device, then trigger
5090e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall         * that action.
5100e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall         */
5110e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall        action_for_each_trigger("nonencrypted", action_add_queue_tail);
5120e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    }
513dd4d786cbf993a024fcd0f926fd40b90c8a08d51Ken Sumrall
5140e9dd90ac336278659477cad0b9e9f1004e2b338Ken Sumrall    return ret;
5154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
517e46f9d510db9351682cf17c49115110870147335Stephen Smalleyint do_setcon(int nargs, char **args) {
518e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
519e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (is_selinux_enabled() <= 0)
520e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return 0;
521e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (setcon(args[1]) < 0) {
522e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return -errno;
523e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
524e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
525e46f9d510db9351682cf17c49115110870147335Stephen Smalley    return 0;
526e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
527e46f9d510db9351682cf17c49115110870147335Stephen Smalley
528e46f9d510db9351682cf17c49115110870147335Stephen Smalleyint do_setenforce(int nargs, char **args) {
529e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
530e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (is_selinux_enabled() <= 0)
531e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return 0;
532e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (security_setenforce(atoi(args[1])) < 0) {
533e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return -errno;
534e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
535e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
536e46f9d510db9351682cf17c49115110870147335Stephen Smalley    return 0;
537e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
538e46f9d510db9351682cf17c49115110870147335Stephen Smalley
5394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_setkey(int nargs, char **args)
5404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct kbentry kbe;
5424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    kbe.kb_table = strtoul(args[1], 0, 0);
5434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    kbe.kb_index = strtoul(args[2], 0, 0);
5444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    kbe.kb_value = strtoul(args[3], 0, 0);
5454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return setkey(&kbe);
5464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_setprop(int nargs, char **args)
5494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5501f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood    const char *name = args[1];
5511f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood    const char *value = args[2];
55284bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    char prop_val[PROP_VALUE_MAX];
55384bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    int ret;
5541f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood
55584bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    ret = expand_props(prop_val, value, sizeof(prop_val));
55684bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    if (ret) {
55784bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin        ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
55884bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin        return -EINVAL;
5591f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood    }
56084bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    property_set(name, prop_val);
5614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
5624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_setrlimit(int nargs, char **args)
5654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct rlimit limit;
5674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int resource;
5684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    resource = atoi(args[1]);
5694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    limit.rlim_cur = atoi(args[2]);
5704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    limit.rlim_max = atoi(args[3]);
5714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return setrlimit(resource, &limit);
5724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_start(int nargs, char **args)
5754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct service *svc;
5774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    svc = service_find_by_name(args[1]);
5784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (svc) {
579f24e252903ca0f71c7fbfb135cf17e83e0c2ea90San Mehat        service_start(svc, NULL);
5804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
5824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_stop(int nargs, char **args)
5854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct service *svc;
5874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    svc = service_find_by_name(args[1]);
5884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (svc) {
5894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        service_stop(svc);
5904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
5924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_restart(int nargs, char **args)
5954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct service *svc;
5974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    svc = service_find_by_name(args[1]);
5984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (svc) {
5994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        service_stop(svc);
600f24e252903ca0f71c7fbfb135cf17e83e0c2ea90San Mehat        service_start(svc, NULL);
6014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
6034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_trigger(int nargs, char **args)
6064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
60711e1c42df69b15c938aa1b7570e4370e6ab15a86Jay Freeman (saurik)    action_for_each_trigger(args[1], action_add_queue_tail);
6084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
6094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_symlink(int nargs, char **args)
6124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return symlink(args[1], args[2]);
6144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
616203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrallint do_rm(int nargs, char **args)
617203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall{
618203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall    return unlink(args[1]);
619203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall}
620203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall
621203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrallint do_rmdir(int nargs, char **args)
622203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall{
623203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall    return rmdir(args[1]);
624203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall}
625203bad597fb4e46ddda051add28400070b5c3a5aKen Sumrall
62635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Projectint do_sysclktz(int nargs, char **args)
62735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project{
62835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    struct timezone tz;
62935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
63035237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    if (nargs != 2)
63135237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        return -1;
63235237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
63335237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    memset(&tz, 0, sizeof(tz));
63435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    tz.tz_minuteswest = atoi(args[1]);
63535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    if (settimeofday(NULL, &tz))
63635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        return -1;
63735237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    return 0;
63835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project}
63935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project
6404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_write(int nargs, char **args)
6414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6421f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood    const char *path = args[1];
6431f0bd32f90161940d531040677099f32eef21fc4Mike Lockwood    const char *value = args[2];
64484bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    char prop_val[PROP_VALUE_MAX];
64584bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    int ret;
6462c4d5dc41505d28cff369cfbe8b17ac81106d936Mike Lockwood
64784bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    ret = expand_props(prop_val, value, sizeof(prop_val));
64884bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    if (ret) {
64984bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin        ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
65084bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin        return -EINVAL;
65184bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    }
65284bf9af758c84da7d9ff2929bce06bf2b2f239b8Dima Zavin    return write_file(path, prop_val);
6534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6557c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehatint do_copy(int nargs, char **args)
6567c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat{
6577c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    char *buffer = NULL;
6587c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    int rc = 0;
6597c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    int fd1 = -1, fd2 = -1;
6607c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    struct stat info;
6617c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    int brtw, brtr;
6627c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    char *p;
6637c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6647c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (nargs != 3)
6657c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        return -1;
6667c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6677c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (stat(args[1], &info) < 0)
6687c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        return -1;
6697c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6707c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if ((fd1 = open(args[1], O_RDONLY)) < 0)
6717c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        goto out_err;
6727c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6734833d9f4986d6dafacff841beb3bf93172c84bdcTom Zhu    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
6747c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        goto out_err;
6757c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6767c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (!(buffer = malloc(info.st_size)))
6777c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        goto out_err;
6787c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6797c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    p = buffer;
6807c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    brtr = info.st_size;
6817c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    while(brtr) {
6827c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        rc = read(fd1, p, brtr);
6837c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        if (rc < 0)
6847c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat            goto out_err;
6857c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        if (rc == 0)
6867c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat            break;
6877c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        p += rc;
6887c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        brtr -= rc;
6897c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    }
6907c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
6917c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    p = buffer;
6927c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    brtw = info.st_size;
6937c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    while(brtw) {
6947c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        rc = write(fd2, p, brtw);
6957c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        if (rc < 0)
6967c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat            goto out_err;
6977c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        if (rc == 0)
6987c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat            break;
6997c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        p += rc;
7007c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        brtw -= rc;
7017c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    }
7027c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
7037c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    rc = 0;
7047c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    goto out;
7057c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehatout_err:
7067c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    rc = -1;
7077c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehatout:
7087c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (buffer)
7097c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        free(buffer);
7107c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (fd1 >= 0)
7117c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        close(fd1);
7127c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    if (fd2 >= 0)
7137c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat        close(fd2);
7147c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat    return rc;
7157c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat}
7167c44fe5925b6dd51166f73d30de0a2f22d66373eSan Mehat
7174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_chown(int nargs, char **args) {
7184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* GID is optional. */
7194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (nargs == 3) {
7209ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        if (_chown(args[2], decode_uid(args[1]), -1) < 0)
7214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            return -errno;
7224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    } else if (nargs == 4) {
7239ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra        if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
7244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            return -errno;
7254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    } else {
7264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
7274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
7294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_mode(const char *s) {
7324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode = 0;
7334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (*s) {
7344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if (*s >= '0' && *s <= '7') {
7354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mode = (mode<<3) | (*s-'0');
7364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
7374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            return -1;
7384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
7394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        s++;
7404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return mode;
7424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_chmod(int nargs, char **args) {
7454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode = get_mode(args[1]);
7469ed1fe77322384552d7d9905ffc54c9681d3b73fGeremy Condra    if (_chmod(args[2], mode) < 0) {
7474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -errno;
7484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
7504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
752e46f9d510db9351682cf17c49115110870147335Stephen Smalleyint do_restorecon(int nargs, char **args) {
753e46f9d510db9351682cf17c49115110870147335Stephen Smalley    int i;
754e46f9d510db9351682cf17c49115110870147335Stephen Smalley
755e46f9d510db9351682cf17c49115110870147335Stephen Smalley    for (i = 1; i < nargs; i++) {
756e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalley        if (restorecon(args[i]) < 0)
757e46f9d510db9351682cf17c49115110870147335Stephen Smalley            return -errno;
758e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
759e46f9d510db9351682cf17c49115110870147335Stephen Smalley    return 0;
760e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
761e46f9d510db9351682cf17c49115110870147335Stephen Smalley
762e46f9d510db9351682cf17c49115110870147335Stephen Smalleyint do_setsebool(int nargs, char **args) {
763e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
764e46f9d510db9351682cf17c49115110870147335Stephen Smalley    SELboolean *b = alloca(nargs * sizeof(SELboolean));
765e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *v;
766e46f9d510db9351682cf17c49115110870147335Stephen Smalley    int i;
767e46f9d510db9351682cf17c49115110870147335Stephen Smalley
768e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (is_selinux_enabled() <= 0)
769e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return 0;
770e46f9d510db9351682cf17c49115110870147335Stephen Smalley
771e46f9d510db9351682cf17c49115110870147335Stephen Smalley    for (i = 1; i < nargs; i++) {
772e46f9d510db9351682cf17c49115110870147335Stephen Smalley        char *name = args[i];
773e46f9d510db9351682cf17c49115110870147335Stephen Smalley        v = strchr(name, '=');
774e46f9d510db9351682cf17c49115110870147335Stephen Smalley        if (!v) {
775e46f9d510db9351682cf17c49115110870147335Stephen Smalley            ERROR("setsebool: argument %s had no =\n", name);
776e46f9d510db9351682cf17c49115110870147335Stephen Smalley            return -EINVAL;
777e46f9d510db9351682cf17c49115110870147335Stephen Smalley        }
778e46f9d510db9351682cf17c49115110870147335Stephen Smalley        *v++ = 0;
779e46f9d510db9351682cf17c49115110870147335Stephen Smalley        b[i-1].name = name;
780e46f9d510db9351682cf17c49115110870147335Stephen Smalley        if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
781e46f9d510db9351682cf17c49115110870147335Stephen Smalley            b[i-1].value = 1;
782e46f9d510db9351682cf17c49115110870147335Stephen Smalley        else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
783e46f9d510db9351682cf17c49115110870147335Stephen Smalley            b[i-1].value = 0;
784e46f9d510db9351682cf17c49115110870147335Stephen Smalley        else {
785e46f9d510db9351682cf17c49115110870147335Stephen Smalley            ERROR("setsebool: invalid value %s\n", v);
786e46f9d510db9351682cf17c49115110870147335Stephen Smalley            return -EINVAL;
787e46f9d510db9351682cf17c49115110870147335Stephen Smalley        }
788e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
789e46f9d510db9351682cf17c49115110870147335Stephen Smalley
790e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (security_set_boolean_list(nargs - 1, b, 0) < 0)
791e46f9d510db9351682cf17c49115110870147335Stephen Smalley        return -errno;
792e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
793e46f9d510db9351682cf17c49115110870147335Stephen Smalley    return 0;
794e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
795e46f9d510db9351682cf17c49115110870147335Stephen Smalley
7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectint do_loglevel(int nargs, char **args) {
7974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (nargs == 2) {
7988f91282ebe1963b9d27f8779ad1342302b293bd2Dima Zavin        klog_set_level(atoi(args[1]));
7994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return 0;
8004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return -1;
8024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
804c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrallint do_load_persist_props(int nargs, char **args) {
805c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall    if (nargs == 1) {
806c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall        load_persist_props();
807c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall        return 0;
808c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall    }
809c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall    return -1;
810c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall}
811c5c51033c86353d1a448c72e0e9388906eed7114Ken Sumrall
812cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Crossint do_wait(int nargs, char **args)
813cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross{
814cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross    if (nargs == 2) {
815cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
81696d0a4dda81aaff3f72b9690d88eb40387ee9630Patrick McCormick    } else if (nargs == 3) {
81796d0a4dda81aaff3f72b9690d88eb40387ee9630Patrick McCormick        return wait_for_file(args[1], atoi(args[2]));
81896d0a4dda81aaff3f72b9690d88eb40387ee9630Patrick McCormick    } else
81996d0a4dda81aaff3f72b9690d88eb40387ee9630Patrick McCormick        return -1;
820cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Cross}
821