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 | |
| 25 | /* returns the data socket passing the backup data here for forwarding */ |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 26 | int backup_service(BackupOperation op, char* args) { |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 27 | pid_t pid; |
| 28 | int s[2]; |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 29 | char* operation; |
| 30 | int socketnum; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 31 | |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 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); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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)) { |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 46 | D("can't create backup/restore socketpair\n"); |
| 47 | fprintf(stderr, "unable to create backup/restore socketpair\n"); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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 |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 55 | D("can't fork for %s\n", operation); |
| 56 | fprintf(stderr, "unable to fork for %s\n", operation); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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; |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 66 | char** bu_args; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 67 | |
| 68 | // child -- actually run the backup here |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 69 | argc = 2; // room for the basic 'bu' argv[0] and '[operation]' argv[1] |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 70 | for (p = (char*)args; p && *p; ) { |
| 71 | argc++; |
| 72 | while (*p && *p != ':') p++; |
| 73 | if (*p == ':') p++; |
| 74 | } |
| 75 | |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 76 | bu_args = (char**) alloca(argc*sizeof(char*) + 1); |
| 77 | bu_args[0] = "bu"; |
| 78 | bu_args[1] = operation; |
Christopher Tate | 024447c | 2011-05-16 16:23:04 -0700 | [diff] [blame] | 79 | argc = 2; // run through again to build the argv array |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 80 | for (p = (char*)args; p && *p; ) { |
| 81 | bu_args[argc++] = p; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 82 | while (*p && *p != ':') p++; |
| 83 | if (*p == ':') { |
| 84 | *p = 0; |
| 85 | p++; |
| 86 | } |
| 87 | } |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 88 | bu_args[argc] = NULL; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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]); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 93 | dup2(s[1], socketnum); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 94 | |
| 95 | // off we go |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame^] | 96 | execvp("/system/bin/bu", (char * const *)bu_args); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 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 | } |