2011-04-22 03:53:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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 <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
#include "adb.h"
|
|
|
|
|
2011-06-22 06:59:25 +08:00
|
|
|
typedef struct {
|
|
|
|
pid_t pid;
|
|
|
|
int fd;
|
|
|
|
} backup_harvest_params;
|
|
|
|
|
2011-06-11 02:38:37 +08:00
|
|
|
// socketpair but do *not* mark as close_on_exec
|
|
|
|
static int backup_socketpair(int sv[2]) {
|
|
|
|
int rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-22 06:59:25 +08:00
|
|
|
// harvest the child process then close the read end of the socketpair
|
|
|
|
static void* backup_child_waiter(void* args) {
|
2011-06-11 02:38:37 +08:00
|
|
|
int status;
|
2011-06-22 06:59:25 +08:00
|
|
|
backup_harvest_params* params = (backup_harvest_params*) args;
|
2011-06-11 02:38:37 +08:00
|
|
|
|
2011-06-22 06:59:25 +08:00
|
|
|
waitpid(params->pid, &status, 0);
|
|
|
|
adb_close(params->fd);
|
|
|
|
free(params);
|
2011-06-11 02:38:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-22 03:53:28 +08:00
|
|
|
/* returns the data socket passing the backup data here for forwarding */
|
2011-05-18 06:52:54 +08:00
|
|
|
int backup_service(BackupOperation op, char* args) {
|
2011-04-22 03:53:28 +08:00
|
|
|
pid_t pid;
|
|
|
|
int s[2];
|
2011-05-18 06:52:54 +08:00
|
|
|
char* operation;
|
|
|
|
int socketnum;
|
2011-04-22 03:53:28 +08:00
|
|
|
|
2011-05-18 06:52:54 +08:00
|
|
|
// Command string and choice of stdin/stdout for the pipe depend on our invocation
|
|
|
|
if (op == BACKUP) {
|
|
|
|
operation = "backup";
|
|
|
|
socketnum = STDOUT_FILENO;
|
|
|
|
} else {
|
|
|
|
operation = "restore";
|
|
|
|
socketnum = STDIN_FILENO;
|
|
|
|
}
|
|
|
|
|
|
|
|
D("backup_service(%s, %s)\n", operation, args);
|
2011-04-22 03:53:28 +08:00
|
|
|
|
|
|
|
// set up the pipe from the subprocess to here
|
|
|
|
// parent will read s[0]; child will write s[1]
|
2011-06-11 02:38:37 +08:00
|
|
|
if (backup_socketpair(s)) {
|
2011-05-18 06:52:54 +08:00
|
|
|
D("can't create backup/restore socketpair\n");
|
|
|
|
fprintf(stderr, "unable to create backup/restore socketpair\n");
|
2011-04-22 03:53:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-11 02:38:37 +08:00
|
|
|
D("Backup/restore socket pair: (send=%d, receive=%d)\n", s[1], s[0]);
|
|
|
|
close_on_exec(s[0]); // only the side we hold on to
|
|
|
|
|
2011-04-22 03:53:28 +08:00
|
|
|
// spin off the child process to run the backup command
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
// failure
|
2011-05-18 06:52:54 +08:00
|
|
|
D("can't fork for %s\n", operation);
|
|
|
|
fprintf(stderr, "unable to fork for %s\n", operation);
|
2011-04-22 03:53:28 +08:00
|
|
|
adb_close(s[0]);
|
|
|
|
adb_close(s[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Great, we're off and running.
|
|
|
|
if (pid == 0) {
|
2011-06-11 02:38:37 +08:00
|
|
|
// child -- actually run the backup here
|
2011-04-22 03:53:28 +08:00
|
|
|
char* p;
|
|
|
|
int argc;
|
2011-06-11 02:38:37 +08:00
|
|
|
char portnum[16];
|
2011-05-18 06:52:54 +08:00
|
|
|
char** bu_args;
|
2011-04-22 03:53:28 +08:00
|
|
|
|
2011-06-11 02:38:37 +08:00
|
|
|
// fixed args: [0] is 'bu', [1] is the port number, [2] is the 'operation' string
|
|
|
|
argc = 3;
|
2011-04-22 03:53:28 +08:00
|
|
|
for (p = (char*)args; p && *p; ) {
|
|
|
|
argc++;
|
|
|
|
while (*p && *p != ':') p++;
|
|
|
|
if (*p == ':') p++;
|
|
|
|
}
|
|
|
|
|
2011-05-18 06:52:54 +08:00
|
|
|
bu_args = (char**) alloca(argc*sizeof(char*) + 1);
|
2011-06-11 02:38:37 +08:00
|
|
|
|
|
|
|
// run through again to build the argv array
|
|
|
|
argc = 0;
|
|
|
|
bu_args[argc++] = "bu";
|
|
|
|
snprintf(portnum, sizeof(portnum), "%d", s[1]);
|
|
|
|
bu_args[argc++] = portnum;
|
|
|
|
bu_args[argc++] = operation;
|
2011-05-18 06:52:54 +08:00
|
|
|
for (p = (char*)args; p && *p; ) {
|
|
|
|
bu_args[argc++] = p;
|
2011-04-22 03:53:28 +08:00
|
|
|
while (*p && *p != ':') p++;
|
|
|
|
if (*p == ':') {
|
|
|
|
*p = 0;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 06:52:54 +08:00
|
|
|
bu_args[argc] = NULL;
|
2011-04-22 03:53:28 +08:00
|
|
|
|
|
|
|
// Close the half of the socket that we don't care about, route 'bu's console
|
|
|
|
// to the output socket, and off we go
|
|
|
|
adb_close(s[0]);
|
|
|
|
|
|
|
|
// off we go
|
2011-05-18 06:52:54 +08:00
|
|
|
execvp("/system/bin/bu", (char * const *)bu_args);
|
2011-04-22 03:53:28 +08:00
|
|
|
// oops error - close up shop and go home
|
|
|
|
fprintf(stderr, "Unable to exec 'bu', bailing\n");
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
2011-06-11 02:38:37 +08:00
|
|
|
adb_thread_t t;
|
2011-06-22 06:59:25 +08:00
|
|
|
backup_harvest_params* params;
|
2011-06-11 02:38:37 +08:00
|
|
|
|
2011-04-22 03:53:28 +08:00
|
|
|
// parent, i.e. adbd -- close the sending half of the socket
|
2011-06-11 02:38:37 +08:00
|
|
|
D("fork() returned pid %d\n", pid);
|
2011-04-22 03:53:28 +08:00
|
|
|
adb_close(s[1]);
|
2011-06-11 02:38:37 +08:00
|
|
|
|
|
|
|
// spin a thread to harvest the child process
|
2011-06-22 06:59:25 +08:00
|
|
|
params = (backup_harvest_params*) malloc(sizeof(backup_harvest_params));
|
|
|
|
params->pid = pid;
|
|
|
|
params->fd = s[0];
|
|
|
|
if (adb_thread_create(&t, backup_child_waiter, params)) {
|
2011-06-11 02:38:37 +08:00
|
|
|
adb_close(s[0]);
|
2011-06-22 06:59:25 +08:00
|
|
|
free(params);
|
2011-06-11 02:38:37 +08:00
|
|
|
D("Unable to create child harvester\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// we'll be reading from s[0] as the data is sent by the child process
|
|
|
|
return s[0];
|
|
|
|
}
|