272 lines
7.0 KiB
C
Raw Normal View History

/*
** Copyright 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 "installd.h"
#define BUFFER_MAX 1024 /* input buffer for commands */
#define TOKEN_MAX 8 /* max number of arguments in buffer */
#define REPLY_MAX 256 /* largest reply allowed */
static int do_ping(char **arg, char reply[REPLY_MAX])
{
return 0;
}
static int do_install(char **arg, char reply[REPLY_MAX])
{
return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
}
static int do_dexopt(char **arg, char reply[REPLY_MAX])
{
/* apk_path, uid, is_public */
return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
}
static int do_move_dex(char **arg, char reply[REPLY_MAX])
{
return move_dex(arg[0], arg[1]); /* src, dst */
}
static int do_rm_dex(char **arg, char reply[REPLY_MAX])
{
return rm_dex(arg[0]); /* pkgname */
}
static int do_remove(char **arg, char reply[REPLY_MAX])
{
return uninstall(arg[0]); /* pkgname */
}
Implement system data migration support. This adds three new features: - <original-package android:name="com.foo" /> manifest tag. This allows an .apk to specify another package it originally came from, propagating all state and data from the old to new package. - <adopt-permissions android:name="com.foo" /> manifest tag. In some more complicated cases, a new .apk may be a combination of multiple older .apks that each declared their own permissions. This allows you to propagate the permissions from these other .apks into the new one. - A new system/etc/updatecmds directory. You can place files here which describe data files to move from one package to another. (See below for details.) Also in this change: we now clean up the data directories of .apks that disappear from the system image, and some improvements to logging and reporting error messages. A typical file in the updatecmds directory looks like this: ------- com.google.android.gsf:com.google.android.providers.talk databases/talk.db com.google.android.gsf:com.google.android.googleapps databases/gls.db ------- This says that for com.google.android.sfs, there are two packages to move files from: From com.google.android.providers.talk, the file databases/talk.db. From com.google.android.googleapps, the file databases/gls.db As part of moving the file, its owner will be changed from the old package to whoever is the owner of the new package's data directory. If those two files had existed, after booting you would now have the files: /data/data/com.google.android.gsf/databases/talk.db /data/data/com.google.android.gsf/databases/gls.db Note that all three of these facilities assume that the older .apk is completely removed from the newer system. The WILL NOT work correctly if the older .apk still remains.
2010-02-02 10:49:14 -08:00
static int do_rename(char **arg, char reply[REPLY_MAX])
{
return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
Implement system data migration support. This adds three new features: - <original-package android:name="com.foo" /> manifest tag. This allows an .apk to specify another package it originally came from, propagating all state and data from the old to new package. - <adopt-permissions android:name="com.foo" /> manifest tag. In some more complicated cases, a new .apk may be a combination of multiple older .apks that each declared their own permissions. This allows you to propagate the permissions from these other .apks into the new one. - A new system/etc/updatecmds directory. You can place files here which describe data files to move from one package to another. (See below for details.) Also in this change: we now clean up the data directories of .apks that disappear from the system image, and some improvements to logging and reporting error messages. A typical file in the updatecmds directory looks like this: ------- com.google.android.gsf:com.google.android.providers.talk databases/talk.db com.google.android.gsf:com.google.android.googleapps databases/gls.db ------- This says that for com.google.android.sfs, there are two packages to move files from: From com.google.android.providers.talk, the file databases/talk.db. From com.google.android.googleapps, the file databases/gls.db As part of moving the file, its owner will be changed from the old package to whoever is the owner of the new package's data directory. If those two files had existed, after booting you would now have the files: /data/data/com.google.android.gsf/databases/talk.db /data/data/com.google.android.gsf/databases/gls.db Note that all three of these facilities assume that the older .apk is completely removed from the newer system. The WILL NOT work correctly if the older .apk still remains.
2010-02-02 10:49:14 -08:00
}
static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
{
return free_cache(atoi(arg[0])); /* free_size */
}
static int do_rm_cache(char **arg, char reply[REPLY_MAX])
{
return delete_cache(arg[0]); /* pkgname */
}
static int do_protect(char **arg, char reply[REPLY_MAX])
{
return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
}
static int do_get_size(char **arg, char reply[REPLY_MAX])
{
int codesize = 0;
int datasize = 0;
int cachesize = 0;
int res = 0;
/* pkgdir, apkpath */
res = get_size(arg[0], arg[1], arg[2], &codesize, &datasize, &cachesize);
sprintf(reply,"%d %d %d", codesize, datasize, cachesize);
return res;
}
static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
{
return delete_user_data(arg[0]); /* pkgname */
}
Implement system data migration support. This adds three new features: - <original-package android:name="com.foo" /> manifest tag. This allows an .apk to specify another package it originally came from, propagating all state and data from the old to new package. - <adopt-permissions android:name="com.foo" /> manifest tag. In some more complicated cases, a new .apk may be a combination of multiple older .apks that each declared their own permissions. This allows you to propagate the permissions from these other .apks into the new one. - A new system/etc/updatecmds directory. You can place files here which describe data files to move from one package to another. (See below for details.) Also in this change: we now clean up the data directories of .apks that disappear from the system image, and some improvements to logging and reporting error messages. A typical file in the updatecmds directory looks like this: ------- com.google.android.gsf:com.google.android.providers.talk databases/talk.db com.google.android.gsf:com.google.android.googleapps databases/gls.db ------- This says that for com.google.android.sfs, there are two packages to move files from: From com.google.android.providers.talk, the file databases/talk.db. From com.google.android.googleapps, the file databases/gls.db As part of moving the file, its owner will be changed from the old package to whoever is the owner of the new package's data directory. If those two files had existed, after booting you would now have the files: /data/data/com.google.android.gsf/databases/talk.db /data/data/com.google.android.gsf/databases/gls.db Note that all three of these facilities assume that the older .apk is completely removed from the newer system. The WILL NOT work correctly if the older .apk still remains.
2010-02-02 10:49:14 -08:00
static int do_movefiles(char **arg, char reply[REPLY_MAX])
{
return movefiles();
}
struct cmdinfo {
const char *name;
unsigned numargs;
int (*func)(char **arg, char reply[REPLY_MAX]);
};
struct cmdinfo cmds[] = {
{ "ping", 0, do_ping },
{ "install", 3, do_install },
{ "dexopt", 3, do_dexopt },
{ "movedex", 2, do_move_dex },
{ "rmdex", 1, do_rm_dex },
{ "remove", 1, do_remove },
{ "rename", 2, do_rename },
{ "freecache", 1, do_free_cache },
{ "rmcache", 1, do_rm_cache },
{ "protect", 2, do_protect },
{ "getsize", 3, do_get_size },
{ "rmuserdata", 1, do_rm_user_data },
Implement system data migration support. This adds three new features: - <original-package android:name="com.foo" /> manifest tag. This allows an .apk to specify another package it originally came from, propagating all state and data from the old to new package. - <adopt-permissions android:name="com.foo" /> manifest tag. In some more complicated cases, a new .apk may be a combination of multiple older .apks that each declared their own permissions. This allows you to propagate the permissions from these other .apks into the new one. - A new system/etc/updatecmds directory. You can place files here which describe data files to move from one package to another. (See below for details.) Also in this change: we now clean up the data directories of .apks that disappear from the system image, and some improvements to logging and reporting error messages. A typical file in the updatecmds directory looks like this: ------- com.google.android.gsf:com.google.android.providers.talk databases/talk.db com.google.android.gsf:com.google.android.googleapps databases/gls.db ------- This says that for com.google.android.sfs, there are two packages to move files from: From com.google.android.providers.talk, the file databases/talk.db. From com.google.android.googleapps, the file databases/gls.db As part of moving the file, its owner will be changed from the old package to whoever is the owner of the new package's data directory. If those two files had existed, after booting you would now have the files: /data/data/com.google.android.gsf/databases/talk.db /data/data/com.google.android.gsf/databases/gls.db Note that all three of these facilities assume that the older .apk is completely removed from the newer system. The WILL NOT work correctly if the older .apk still remains.
2010-02-02 10:49:14 -08:00
{ "movefiles", 0, do_movefiles },
};
static int readx(int s, void *_buf, int count)
{
char *buf = _buf;
int n = 0, r;
if (count < 0) return -1;
while (n < count) {
r = read(s, buf + n, count - n);
if (r < 0) {
if (errno == EINTR) continue;
LOGE("read error: %s\n", strerror(errno));
return -1;
}
if (r == 0) {
LOGE("eof\n");
return -1; /* EOF */
}
n += r;
}
return 0;
}
static int writex(int s, const void *_buf, int count)
{
const char *buf = _buf;
int n = 0, r;
if (count < 0) return -1;
while (n < count) {
r = write(s, buf + n, count - n);
if (r < 0) {
if (errno == EINTR) continue;
LOGE("write error: %s\n", strerror(errno));
return -1;
}
n += r;
}
return 0;
}
/* Tokenize the command buffer, locate a matching command,
* ensure that the required number of arguments are provided,
* call the function(), return the result.
*/
static int execute(int s, char cmd[BUFFER_MAX])
{
char reply[REPLY_MAX];
char *arg[TOKEN_MAX+1];
unsigned i;
unsigned n = 0;
unsigned short count;
int ret = -1;
// LOGI("execute('%s')\n", cmd);
/* default reply is "" */
reply[0] = 0;
/* n is number of args (not counting arg[0]) */
arg[0] = cmd;
while (*cmd) {
if (isspace(*cmd)) {
*cmd++ = 0;
n++;
arg[n] = cmd;
if (n == TOKEN_MAX) {
LOGE("too many arguments\n");
goto done;
}
}
cmd++;
}
for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
if (!strcmp(cmds[i].name,arg[0])) {
if (n != cmds[i].numargs) {
LOGE("%s requires %d arguments (%d given)\n",
cmds[i].name, cmds[i].numargs, n);
} else {
ret = cmds[i].func(arg + 1, reply);
}
goto done;
}
}
LOGE("unsupported command '%s'\n", arg[0]);
done:
if (reply[0]) {
n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
} else {
n = snprintf(cmd, BUFFER_MAX, "%d", ret);
}
if (n > BUFFER_MAX) n = BUFFER_MAX;
count = n;
// LOGI("reply: '%s'\n", cmd);
if (writex(s, &count, sizeof(count))) return -1;
if (writex(s, cmd, count)) return -1;
return 0;
}
int main(const int argc, const char *argv[]) {
char buf[BUFFER_MAX];
struct sockaddr addr;
socklen_t alen;
int lsocket, s, count;
lsocket = android_get_control_socket(SOCKET_PATH);
if (lsocket < 0) {
LOGE("Failed to get socket from environment: %s\n", strerror(errno));
exit(1);
}
if (listen(lsocket, 5)) {
LOGE("Listen on socket failed: %s\n", strerror(errno));
exit(1);
}
fcntl(lsocket, F_SETFD, FD_CLOEXEC);
for (;;) {
alen = sizeof(addr);
s = accept(lsocket, &addr, &alen);
if (s < 0) {
LOGE("Accept failed: %s\n", strerror(errno));
continue;
}
fcntl(s, F_SETFD, FD_CLOEXEC);
LOGI("new connection\n");
for (;;) {
unsigned short count;
if (readx(s, &count, sizeof(count))) {
LOGE("failed to read size\n");
break;
}
if ((count < 1) || (count >= BUFFER_MAX)) {
LOGE("invalid size %d\n", count);
break;
}
if (readx(s, buf, count)) {
LOGE("failed to read command\n");
break;
}
buf[count] = 0;
if (execute(s, buf)) break;
}
LOGI("closing connection\n");
close(s);
}
return 0;
}