2008-10-21 16:00:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <linux/kd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/resource.h>
|
2008-11-20 04:37:30 +01:00
|
|
|
#include <linux/loop.h>
|
2011-05-27 05:01:39 +02:00
|
|
|
#include <cutils/partition_utils.h>
|
2011-12-20 22:44:41 +01:00
|
|
|
#include <sys/system_properties.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
#include "init.h"
|
|
|
|
#include "keywords.h"
|
|
|
|
#include "property_service.h"
|
|
|
|
#include "devices.h"
|
2010-04-20 23:29:05 +02:00
|
|
|
#include "init_parser.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
|
|
|
void add_environment(const char *name, const char *value);
|
|
|
|
|
|
|
|
extern int init_module(void *, unsigned long, const char *);
|
|
|
|
|
|
|
|
static int write_file(const char *path, const char *value)
|
|
|
|
{
|
|
|
|
int fd, ret, len;
|
|
|
|
|
|
|
|
fd = open(path, O_WRONLY|O_CREAT, 0622);
|
|
|
|
|
|
|
|
if (fd < 0)
|
2009-06-30 05:30:55 +02:00
|
|
|
return -errno;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
len = strlen(value);
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = write(fd, value, len);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
if (ret < 0) {
|
2009-06-30 05:30:55 +02:00
|
|
|
return -errno;
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 03:08:08 +01:00
|
|
|
static int insmod(const char *filename, char *options)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
void *module;
|
|
|
|
unsigned size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
module = read_file(filename, &size);
|
|
|
|
if (!module)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-18 03:08:08 +01:00
|
|
|
ret = init_module(module, size, options);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
free(module);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setkey(struct kbentry *kbe)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = open("/dev/tty0", O_RDWR | O_SYNC);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = ioctl(fd, KDSKBENT, kbe);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __ifupdown(const char *interface, int up)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s, ret;
|
|
|
|
|
|
|
|
strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
|
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up)
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
else
|
|
|
|
ifr.ifr_flags &= ~IFF_UP;
|
|
|
|
|
|
|
|
ret = ioctl(s, SIOCSIFFLAGS, &ifr);
|
|
|
|
|
|
|
|
done:
|
|
|
|
close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void service_start_if_not_disabled(struct service *svc)
|
|
|
|
{
|
|
|
|
if (!(svc->flags & SVC_DISABLED)) {
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, NULL);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 07:41:10 +01:00
|
|
|
int do_chdir(int nargs, char **args)
|
|
|
|
{
|
|
|
|
chdir(args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_chroot(int nargs, char **args)
|
|
|
|
{
|
|
|
|
chroot(args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
int do_class_start(int nargs, char **args)
|
|
|
|
{
|
|
|
|
/* Starting a class does not start services
|
|
|
|
* which are explicitly disabled. They must
|
|
|
|
* be started individually.
|
|
|
|
*/
|
|
|
|
service_for_each_class(args[1], service_start_if_not_disabled);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_class_stop(int nargs, char **args)
|
|
|
|
{
|
|
|
|
service_for_each_class(args[1], service_stop);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
int do_class_reset(int nargs, char **args)
|
|
|
|
{
|
|
|
|
service_for_each_class(args[1], service_reset);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
int do_domainname(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return write_file("/proc/sys/kernel/domainname", args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_exec(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_export(int nargs, char **args)
|
|
|
|
{
|
|
|
|
add_environment(args[1], args[2]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_hostname(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return write_file("/proc/sys/kernel/hostname", args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_ifup(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return __ifupdown(args[1], 1);
|
|
|
|
}
|
|
|
|
|
2008-12-18 03:08:08 +01:00
|
|
|
|
|
|
|
static int do_insmod_inner(int nargs, char **args, int opt_len)
|
|
|
|
{
|
|
|
|
char options[opt_len + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
options[0] = '\0';
|
|
|
|
if (nargs > 2) {
|
|
|
|
strcpy(options, args[2]);
|
|
|
|
for (i = 3; i < nargs; ++i) {
|
|
|
|
strcat(options, " ");
|
|
|
|
strcat(options, args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return insmod(args[1], options);
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
int do_insmod(int nargs, char **args)
|
|
|
|
{
|
2008-12-18 03:08:08 +01:00
|
|
|
int i;
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if (nargs > 2) {
|
|
|
|
for (i = 2; i < nargs; ++i)
|
|
|
|
size += strlen(args[i]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_insmod_inner(nargs, args, size);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_mkdir(int nargs, char **args)
|
|
|
|
{
|
|
|
|
mode_t mode = 0755;
|
2011-07-08 21:57:36 +02:00
|
|
|
int ret;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
/* mkdir <path> [mode] [owner] [group] */
|
|
|
|
|
|
|
|
if (nargs >= 3) {
|
|
|
|
mode = strtoul(args[2], 0, 8);
|
|
|
|
}
|
|
|
|
|
2011-07-08 21:57:36 +02:00
|
|
|
ret = mkdir(args[1], mode);
|
|
|
|
/* chmod in case the directory already exists */
|
|
|
|
if (ret == -1 && errno == EEXIST) {
|
|
|
|
ret = chmod(args[1], mode);
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2008-10-21 16:00:00 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nargs >= 4) {
|
|
|
|
uid_t uid = decode_uid(args[3]);
|
|
|
|
gid_t gid = -1;
|
|
|
|
|
|
|
|
if (nargs == 5) {
|
|
|
|
gid = decode_uid(args[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chown(args[1], uid, gid)) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned flag;
|
|
|
|
} mount_flags[] = {
|
|
|
|
{ "noatime", MS_NOATIME },
|
|
|
|
{ "nosuid", MS_NOSUID },
|
|
|
|
{ "nodev", MS_NODEV },
|
|
|
|
{ "nodiratime", MS_NODIRATIME },
|
|
|
|
{ "ro", MS_RDONLY },
|
|
|
|
{ "rw", 0 },
|
|
|
|
{ "remount", MS_REMOUNT },
|
|
|
|
{ "defaults", 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
};
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
#define DATA_MNT_POINT "/data"
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
/* mount <type> <device> <path> <flags ...> <options> */
|
|
|
|
int do_mount(int nargs, char **args)
|
|
|
|
{
|
|
|
|
char tmp[64];
|
2008-11-20 04:37:30 +01:00
|
|
|
char *source, *target, *system;
|
2008-10-21 16:00:00 +02:00
|
|
|
char *options = NULL;
|
|
|
|
unsigned flags = 0;
|
|
|
|
int n, i;
|
2010-04-20 02:10:24 +02:00
|
|
|
int wait = 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
for (n = 4; n < nargs; n++) {
|
|
|
|
for (i = 0; mount_flags[i].name; i++) {
|
|
|
|
if (!strcmp(args[n], mount_flags[i].name)) {
|
|
|
|
flags |= mount_flags[i].flag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 02:10:24 +02:00
|
|
|
if (!mount_flags[i].name) {
|
|
|
|
if (!strcmp(args[n], "wait"))
|
|
|
|
wait = 1;
|
|
|
|
/* if our last argument isn't a flag, wolf it up as an option string */
|
|
|
|
else if (n + 1 == nargs)
|
|
|
|
options = args[n];
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2008-11-20 04:37:30 +01:00
|
|
|
system = args[1];
|
2008-10-21 16:00:00 +02:00
|
|
|
source = args[2];
|
2008-11-20 04:37:30 +01:00
|
|
|
target = args[3];
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (!strncmp(source, "mtd@", 4)) {
|
|
|
|
n = mtd_name_to_number(source + 4);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (n < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(tmp, "/dev/block/mtdblock%d", n);
|
|
|
|
|
2010-04-20 02:10:24 +02:00
|
|
|
if (wait)
|
|
|
|
wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (mount(tmp, target, system, flags, options) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-18 03:09:47 +01:00
|
|
|
goto exit_success;
|
2008-11-20 04:37:30 +01:00
|
|
|
} else if (!strncmp(source, "loop@", 5)) {
|
|
|
|
int mode, loop, fd;
|
|
|
|
struct loop_info info;
|
|
|
|
|
|
|
|
mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
|
|
|
|
fd = open(source + 5, mode);
|
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2008-11-20 04:37:30 +01:00
|
|
|
|
|
|
|
for (n = 0; ; n++) {
|
|
|
|
sprintf(tmp, "/dev/block/loop%d", n);
|
|
|
|
loop = open(tmp, mode);
|
|
|
|
if (loop < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it is a blank loop device */
|
|
|
|
if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
|
|
|
|
/* if it becomes our loop device */
|
|
|
|
if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (mount(tmp, target, system, flags, options) < 0) {
|
|
|
|
ioctl(loop, LOOP_CLR_FD, 0);
|
|
|
|
close(loop);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(loop);
|
2011-02-18 03:09:47 +01:00
|
|
|
goto exit_success;
|
2008-11-20 04:37:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
ERROR("out of loopback devices");
|
|
|
|
return -1;
|
|
|
|
} else {
|
2010-04-20 02:10:24 +02:00
|
|
|
if (wait)
|
|
|
|
wait_for_file(source, COMMAND_RETRY_TIMEOUT);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (mount(source, target, system, flags, options) < 0) {
|
2011-05-27 05:01:39 +02:00
|
|
|
/* If this fails, it may be an encrypted filesystem
|
|
|
|
* or it could just be wiped. If wiped, that will be
|
|
|
|
* handled later in the boot process.
|
2010-12-04 01:33:31 +01:00
|
|
|
* We only support encrypting /data. Check
|
|
|
|
* if we're trying to mount it, and if so,
|
|
|
|
* assume it's encrypted, mount a tmpfs instead.
|
|
|
|
* Then save the orig mount parms in properties
|
|
|
|
* for vold to query when it mounts the real
|
|
|
|
* encrypted /data.
|
|
|
|
*/
|
2011-05-27 05:01:39 +02:00
|
|
|
if (!strcmp(target, DATA_MNT_POINT) && !partition_wiped(source)) {
|
2010-12-04 01:33:31 +01:00
|
|
|
const char *tmpfs_options;
|
|
|
|
|
|
|
|
tmpfs_options = property_get("ro.crypto.tmpfs_options");
|
|
|
|
|
|
|
|
if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
|
|
|
|
tmpfs_options) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the property that triggers the framework to do a minimal
|
|
|
|
* startup and ask the user for a password
|
|
|
|
*/
|
2011-01-14 21:44:09 +01:00
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2010-12-04 01:33:31 +01:00
|
|
|
property_set("vold.decrypt", "1");
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-20 04:37:30 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
if (!strcmp(target, DATA_MNT_POINT)) {
|
|
|
|
char fs_flags[32];
|
|
|
|
|
|
|
|
/* Save the original mount options */
|
|
|
|
property_set("ro.crypto.fs_type", system);
|
|
|
|
property_set("ro.crypto.fs_real_blkdev", source);
|
|
|
|
property_set("ro.crypto.fs_mnt_point", target);
|
|
|
|
if (options) {
|
|
|
|
property_set("ro.crypto.fs_options", options);
|
|
|
|
}
|
|
|
|
snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
|
|
|
|
property_set("ro.crypto.fs_flags", fs_flags);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2011-02-18 03:09:47 +01:00
|
|
|
|
|
|
|
exit_success:
|
|
|
|
/* If not running encrypted, then set the property saying we are
|
|
|
|
* unencrypted, and also trigger the action for a nonencrypted system.
|
|
|
|
*/
|
|
|
|
if (!strcmp(target, DATA_MNT_POINT)) {
|
2011-03-09 02:01:29 +01:00
|
|
|
const char *prop;
|
|
|
|
|
2011-02-18 03:09:47 +01:00
|
|
|
prop = property_get("ro.crypto.state");
|
|
|
|
if (! prop) {
|
|
|
|
prop = "notset";
|
|
|
|
}
|
|
|
|
if (strcmp(prop, "encrypted")) {
|
|
|
|
property_set("ro.crypto.state", "unencrypted");
|
|
|
|
action_for_each_trigger("nonencrypted", action_add_queue_tail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_setkey(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct kbentry kbe;
|
|
|
|
kbe.kb_table = strtoul(args[1], 0, 0);
|
|
|
|
kbe.kb_index = strtoul(args[2], 0, 0);
|
|
|
|
kbe.kb_value = strtoul(args[3], 0, 0);
|
|
|
|
return setkey(&kbe);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_setprop(int nargs, char **args)
|
|
|
|
{
|
2011-06-09 02:31:27 +02:00
|
|
|
const char *name = args[1];
|
|
|
|
const char *value = args[2];
|
2011-12-20 22:44:41 +01:00
|
|
|
char prop_val[PROP_VALUE_MAX];
|
|
|
|
int ret;
|
2011-06-09 02:31:27 +02:00
|
|
|
|
2011-12-20 22:44:41 +01:00
|
|
|
ret = expand_props(prop_val, value, sizeof(prop_val));
|
|
|
|
if (ret) {
|
|
|
|
ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
|
|
|
|
return -EINVAL;
|
2011-06-09 02:31:27 +02:00
|
|
|
}
|
2011-12-20 22:44:41 +01:00
|
|
|
property_set(name, prop_val);
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_setrlimit(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct rlimit limit;
|
|
|
|
int resource;
|
|
|
|
resource = atoi(args[1]);
|
|
|
|
limit.rlim_cur = atoi(args[2]);
|
|
|
|
limit.rlim_max = atoi(args[3]);
|
|
|
|
return setrlimit(resource, &limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_start(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct service *svc;
|
|
|
|
svc = service_find_by_name(args[1]);
|
|
|
|
if (svc) {
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, NULL);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_stop(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct service *svc;
|
|
|
|
svc = service_find_by_name(args[1]);
|
|
|
|
if (svc) {
|
|
|
|
service_stop(svc);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_restart(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct service *svc;
|
|
|
|
svc = service_find_by_name(args[1]);
|
|
|
|
if (svc) {
|
|
|
|
service_stop(svc);
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, NULL);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_trigger(int nargs, char **args)
|
|
|
|
{
|
2008-11-17 07:35:08 +01:00
|
|
|
action_for_each_trigger(args[1], action_add_queue_tail);
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_symlink(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return symlink(args[1], args[2]);
|
|
|
|
}
|
|
|
|
|
2011-01-19 02:37:41 +01:00
|
|
|
int do_rm(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return unlink(args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_rmdir(int nargs, char **args)
|
|
|
|
{
|
|
|
|
return rmdir(args[1]);
|
|
|
|
}
|
|
|
|
|
2008-12-18 03:08:08 +01:00
|
|
|
int do_sysclktz(int nargs, char **args)
|
|
|
|
{
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
if (nargs != 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&tz, 0, sizeof(tz));
|
|
|
|
tz.tz_minuteswest = atoi(args[1]);
|
|
|
|
if (settimeofday(NULL, &tz))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
int do_write(int nargs, char **args)
|
|
|
|
{
|
2011-06-09 02:31:27 +02:00
|
|
|
const char *path = args[1];
|
|
|
|
const char *value = args[2];
|
2011-12-20 22:44:41 +01:00
|
|
|
char prop_val[PROP_VALUE_MAX];
|
|
|
|
int ret;
|
2011-06-08 02:30:11 +02:00
|
|
|
|
2011-12-20 22:44:41 +01:00
|
|
|
ret = expand_props(prop_val, value, sizeof(prop_val));
|
|
|
|
if (ret) {
|
|
|
|
ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return write_file(path, prop_val);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 01:39:25 +02:00
|
|
|
int do_copy(int nargs, char **args)
|
|
|
|
{
|
|
|
|
char *buffer = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
int fd1 = -1, fd2 = -1;
|
|
|
|
struct stat info;
|
|
|
|
int brtw, brtr;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (nargs != 3)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (stat(args[1], &info) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((fd1 = open(args[1], O_RDONLY)) < 0)
|
|
|
|
goto out_err;
|
|
|
|
|
2009-09-29 02:53:12 +02:00
|
|
|
if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
|
2009-08-27 01:39:25 +02:00
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
if (!(buffer = malloc(info.st_size)))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
brtr = info.st_size;
|
|
|
|
while(brtr) {
|
|
|
|
rc = read(fd1, p, brtr);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out_err;
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
p += rc;
|
|
|
|
brtr -= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
brtw = info.st_size;
|
|
|
|
while(brtw) {
|
|
|
|
rc = write(fd2, p, brtw);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out_err;
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
p += rc;
|
|
|
|
brtw -= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
|
|
|
out_err:
|
|
|
|
rc = -1;
|
|
|
|
out:
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
|
|
|
if (fd1 >= 0)
|
|
|
|
close(fd1);
|
|
|
|
if (fd2 >= 0)
|
|
|
|
close(fd2);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
int do_chown(int nargs, char **args) {
|
|
|
|
/* GID is optional. */
|
|
|
|
if (nargs == 3) {
|
|
|
|
if (chown(args[2], decode_uid(args[1]), -1) < 0)
|
|
|
|
return -errno;
|
|
|
|
} else if (nargs == 4) {
|
|
|
|
if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
|
|
|
|
return -errno;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_t get_mode(const char *s) {
|
|
|
|
mode_t mode = 0;
|
|
|
|
while (*s) {
|
|
|
|
if (*s >= '0' && *s <= '7') {
|
|
|
|
mode = (mode<<3) | (*s-'0');
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_chmod(int nargs, char **args) {
|
|
|
|
mode_t mode = get_mode(args[1]);
|
|
|
|
if (chmod(args[2], mode) < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_loglevel(int nargs, char **args) {
|
|
|
|
if (nargs == 2) {
|
2011-09-01 03:26:17 +02:00
|
|
|
klog_set_level(atoi(args[1]));
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:01:29 +01:00
|
|
|
int do_load_persist_props(int nargs, char **args) {
|
|
|
|
if (nargs == 1) {
|
|
|
|
load_persist_props();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-20 02:10:24 +02:00
|
|
|
int do_wait(int nargs, char **args)
|
|
|
|
{
|
|
|
|
if (nargs == 2) {
|
|
|
|
return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|