backup_service.c revision 702967afb1bebc97c0b8a23c075d4932820ef7a3
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <unistd.h> 18#include <stdio.h> 19 20#include "sysdeps.h" 21 22#define TRACE_TAG TRACE_ADB 23#include "adb.h" 24 25/* returns the data socket passing the backup data here for forwarding */ 26int backup_service(BackupOperation op, char* args) { 27 pid_t pid; 28 int s[2]; 29 char* operation; 30 int socketnum; 31 32 // Command string and choice of stdin/stdout for the pipe depend on our invocation 33 if (op == BACKUP) { 34 operation = "backup"; 35 socketnum = STDOUT_FILENO; 36 } else { 37 operation = "restore"; 38 socketnum = STDIN_FILENO; 39 } 40 41 D("backup_service(%s, %s)\n", operation, args); 42 43 // set up the pipe from the subprocess to here 44 // parent will read s[0]; child will write s[1] 45 if (adb_socketpair(s)) { 46 D("can't create backup/restore socketpair\n"); 47 fprintf(stderr, "unable to create backup/restore socketpair\n"); 48 return -1; 49 } 50 51 // spin off the child process to run the backup command 52 pid = fork(); 53 if (pid < 0) { 54 // failure 55 D("can't fork for %s\n", operation); 56 fprintf(stderr, "unable to fork for %s\n", operation); 57 adb_close(s[0]); 58 adb_close(s[1]); 59 return -1; 60 } 61 62 // Great, we're off and running. 63 if (pid == 0) { 64 char* p; 65 int argc; 66 char** bu_args; 67 68 // child -- actually run the backup here 69 argc = 2; // room for the basic 'bu' argv[0] and '[operation]' argv[1] 70 for (p = (char*)args; p && *p; ) { 71 argc++; 72 while (*p && *p != ':') p++; 73 if (*p == ':') p++; 74 } 75 76 bu_args = (char**) alloca(argc*sizeof(char*) + 1); 77 bu_args[0] = "bu"; 78 bu_args[1] = operation; 79 argc = 2; // run through again to build the argv array 80 for (p = (char*)args; p && *p; ) { 81 bu_args[argc++] = p; 82 while (*p && *p != ':') p++; 83 if (*p == ':') { 84 *p = 0; 85 p++; 86 } 87 } 88 bu_args[argc] = NULL; 89 90 // Close the half of the socket that we don't care about, route 'bu's console 91 // to the output socket, and off we go 92 adb_close(s[0]); 93 dup2(s[1], socketnum); 94 95 // off we go 96 execvp("/system/bin/bu", (char * const *)bu_args); 97 // oops error - close up shop and go home 98 fprintf(stderr, "Unable to exec 'bu', bailing\n"); 99 exit(-1); 100 } else { 101 // parent, i.e. adbd -- close the sending half of the socket 102 adb_close(s[1]); 103 } 104 105 // we'll be reading from s[0] as the data is sent by the child process 106 return s[0]; 107} 108