| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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 |  | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 25 | typedef struct { | 
|  | 26 | pid_t pid; | 
|  | 27 | int fd; | 
|  | 28 | } backup_harvest_params; | 
|  | 29 |  | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 30 | // socketpair but do *not* mark as close_on_exec | 
|  | 31 | static int backup_socketpair(int sv[2]) { | 
|  | 32 | int rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); | 
|  | 33 | if (rc < 0) | 
|  | 34 | return -1; | 
|  | 35 |  | 
|  | 36 | return 0; | 
|  | 37 | } | 
|  | 38 |  | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 39 | // harvest the child process then close the read end of the socketpair | 
|  | 40 | static void* backup_child_waiter(void* args) { | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 41 | int status; | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 42 | backup_harvest_params* params = (backup_harvest_params*) args; | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 43 |  | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 44 | waitpid(params->pid, &status, 0); | 
|  | 45 | adb_close(params->fd); | 
|  | 46 | free(params); | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 47 | return NULL; | 
|  | 48 | } | 
|  | 49 |  | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 50 | /* returns the data socket passing the backup data here for forwarding */ | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 51 | int backup_service(BackupOperation op, char* args) { | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 52 | pid_t pid; | 
|  | 53 | int s[2]; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 54 | char* operation; | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 55 |  | 
| Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 56 | // Command string depends on our invocation | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 57 | if (op == BACKUP) { | 
|  | 58 | operation = "backup"; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 59 | } else { | 
|  | 60 | operation = "restore"; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | D("backup_service(%s, %s)\n", operation, args); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | // set up the pipe from the subprocess to here | 
|  | 66 | // parent will read s[0]; child will write s[1] | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 67 | if (backup_socketpair(s)) { | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 68 | D("can't create backup/restore socketpair\n"); | 
|  | 69 | fprintf(stderr, "unable to create backup/restore socketpair\n"); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 70 | return -1; | 
|  | 71 | } | 
|  | 72 |  | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 73 | D("Backup/restore socket pair: (send=%d, receive=%d)\n", s[1], s[0]); | 
|  | 74 | close_on_exec(s[0]);    // only the side we hold on to | 
|  | 75 |  | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 76 | // spin off the child process to run the backup command | 
|  | 77 | pid = fork(); | 
|  | 78 | if (pid < 0) { | 
|  | 79 | // failure | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 80 | D("can't fork for %s\n", operation); | 
|  | 81 | fprintf(stderr, "unable to fork for %s\n", operation); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 82 | adb_close(s[0]); | 
|  | 83 | adb_close(s[1]); | 
|  | 84 | return -1; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | // Great, we're off and running. | 
|  | 88 | if (pid == 0) { | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 89 | // child -- actually run the backup here | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 90 | char* p; | 
|  | 91 | int argc; | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 92 | char portnum[16]; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 93 | char** bu_args; | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 94 |  | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 95 | // fixed args:  [0] is 'bu', [1] is the port number, [2] is the 'operation' string | 
|  | 96 | argc = 3; | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 97 | for (p = (char*)args; p && *p; ) { | 
|  | 98 | argc++; | 
|  | 99 | while (*p && *p != ':') p++; | 
|  | 100 | if (*p == ':') p++; | 
|  | 101 | } | 
|  | 102 |  | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 103 | bu_args = (char**) alloca(argc*sizeof(char*) + 1); | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | // run through again to build the argv array | 
|  | 106 | argc = 0; | 
|  | 107 | bu_args[argc++] = "bu"; | 
|  | 108 | snprintf(portnum, sizeof(portnum), "%d", s[1]); | 
|  | 109 | bu_args[argc++] = portnum; | 
|  | 110 | bu_args[argc++] = operation; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 111 | for (p = (char*)args; p && *p; ) { | 
|  | 112 | bu_args[argc++] = p; | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 113 | while (*p && *p != ':') p++; | 
|  | 114 | if (*p == ':') { | 
|  | 115 | *p = 0; | 
|  | 116 | p++; | 
|  | 117 | } | 
|  | 118 | } | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 119 | bu_args[argc] = NULL; | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | // Close the half of the socket that we don't care about, route 'bu's console | 
|  | 122 | // to the output socket, and off we go | 
|  | 123 | adb_close(s[0]); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | // off we go | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 126 | execvp("/system/bin/bu", (char * const *)bu_args); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 127 | // oops error - close up shop and go home | 
|  | 128 | fprintf(stderr, "Unable to exec 'bu', bailing\n"); | 
|  | 129 | exit(-1); | 
|  | 130 | } else { | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 131 | adb_thread_t t; | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 132 | backup_harvest_params* params; | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 133 |  | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 134 | // parent, i.e. adbd -- close the sending half of the socket | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 135 | D("fork() returned pid %d\n", pid); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 136 | adb_close(s[1]); | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | // spin a thread to harvest the child process | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 139 | params = (backup_harvest_params*) malloc(sizeof(backup_harvest_params)); | 
|  | 140 | params->pid = pid; | 
|  | 141 | params->fd = s[0]; | 
|  | 142 | if (adb_thread_create(&t, backup_child_waiter, params)) { | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 143 | adb_close(s[0]); | 
| Christopher Tate | 10f129c | 2011-06-21 15:59:25 -0700 | [diff] [blame] | 144 | free(params); | 
| Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 145 | D("Unable to create child harvester\n"); | 
|  | 146 | return -1; | 
|  | 147 | } | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
|  | 150 | // we'll be reading from s[0] as the data is sent by the child process | 
|  | 151 | return s[0]; | 
|  | 152 | } |