blob: 2e6e7546367f46e9d9b82dbcaf01b0c367033da5 [file] [log] [blame]
Christopher Tated2f54152011-04-21 12:53:28 -07001/*
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 Tate702967a2011-05-17 15:52:54 -070026int backup_service(BackupOperation op, char* args) {
Christopher Tated2f54152011-04-21 12:53:28 -070027 pid_t pid;
28 int s[2];
Christopher Tate702967a2011-05-17 15:52:54 -070029 char* operation;
30 int socketnum;
Christopher Tated2f54152011-04-21 12:53:28 -070031
Christopher Tate702967a2011-05-17 15:52:54 -070032 // 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 Tated2f54152011-04-21 12:53:28 -070042
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 Tate702967a2011-05-17 15:52:54 -070046 D("can't create backup/restore socketpair\n");
47 fprintf(stderr, "unable to create backup/restore socketpair\n");
Christopher Tated2f54152011-04-21 12:53:28 -070048 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 Tate702967a2011-05-17 15:52:54 -070055 D("can't fork for %s\n", operation);
56 fprintf(stderr, "unable to fork for %s\n", operation);
Christopher Tated2f54152011-04-21 12:53:28 -070057 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 Tate702967a2011-05-17 15:52:54 -070066 char** bu_args;
Christopher Tated2f54152011-04-21 12:53:28 -070067
68 // child -- actually run the backup here
Christopher Tate702967a2011-05-17 15:52:54 -070069 argc = 2; // room for the basic 'bu' argv[0] and '[operation]' argv[1]
Christopher Tated2f54152011-04-21 12:53:28 -070070 for (p = (char*)args; p && *p; ) {
71 argc++;
72 while (*p && *p != ':') p++;
73 if (*p == ':') p++;
74 }
75
Christopher Tate702967a2011-05-17 15:52:54 -070076 bu_args = (char**) alloca(argc*sizeof(char*) + 1);
77 bu_args[0] = "bu";
78 bu_args[1] = operation;
Christopher Tate024447c2011-05-16 16:23:04 -070079 argc = 2; // run through again to build the argv array
Christopher Tate702967a2011-05-17 15:52:54 -070080 for (p = (char*)args; p && *p; ) {
81 bu_args[argc++] = p;
Christopher Tated2f54152011-04-21 12:53:28 -070082 while (*p && *p != ':') p++;
83 if (*p == ':') {
84 *p = 0;
85 p++;
86 }
87 }
Christopher Tate702967a2011-05-17 15:52:54 -070088 bu_args[argc] = NULL;
Christopher Tated2f54152011-04-21 12:53:28 -070089
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 Tate702967a2011-05-17 15:52:54 -070093 dup2(s[1], socketnum);
Christopher Tated2f54152011-04-21 12:53:28 -070094
95 // off we go
Christopher Tate702967a2011-05-17 15:52:54 -070096 execvp("/system/bin/bu", (char * const *)bu_args);
Christopher Tated2f54152011-04-21 12:53:28 -070097 // 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}