blob: 1e55efc74d16583618309ff7bb7385ceec14d3b5 [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 */
26int backup_service(char* args) {
27 pid_t pid;
28 int s[2];
29
30 D("backup_service(%s)\n", args);
31
32 // set up the pipe from the subprocess to here
33 // parent will read s[0]; child will write s[1]
34 if (adb_socketpair(s)) {
35 D("can't create backup socketpair\n");
36 fprintf(stderr, "unable to create backup socketpair\n");
37 return -1;
38 }
39
40 // spin off the child process to run the backup command
41 pid = fork();
42 if (pid < 0) {
43 // failure
44 D("can't fork for backup\n");
45 fprintf(stderr, "unable to fork for backup\n");
46 adb_close(s[0]);
47 adb_close(s[1]);
48 return -1;
49 }
50
51 // Great, we're off and running.
52 if (pid == 0) {
53 char* p;
54 int argc;
55 char** backup_args;
56
57 // child -- actually run the backup here
Christopher Tate024447c2011-05-16 16:23:04 -070058 argc = 2; // room for the basic 'bu' argv[0] and 'backup' argv[1]
Christopher Tated2f54152011-04-21 12:53:28 -070059 for (p = (char*)args; p && *p; ) {
60 argc++;
61 while (*p && *p != ':') p++;
62 if (*p == ':') p++;
63 }
64
65 backup_args = (char**) alloca(argc*sizeof(char*) + 1);
66 backup_args[0] = "bu";
Christopher Tate024447c2011-05-16 16:23:04 -070067 backup_args[1] = "backup";
68 argc = 2; // run through again to build the argv array
Christopher Tated2f54152011-04-21 12:53:28 -070069 for (p = (char*)args; *p; ) {
70 backup_args[argc++] = p;
71 while (*p && *p != ':') p++;
72 if (*p == ':') {
73 *p = 0;
74 p++;
75 }
76 }
77 backup_args[argc] = NULL;
78
79 // Close the half of the socket that we don't care about, route 'bu's console
80 // to the output socket, and off we go
81 adb_close(s[0]);
82 dup2(s[1], STDOUT_FILENO);
83
84 // off we go
85 execvp("/system/bin/bu", (char * const *)backup_args);
86 // oops error - close up shop and go home
87 fprintf(stderr, "Unable to exec 'bu', bailing\n");
88 exit(-1);
89 } else {
90 // parent, i.e. adbd -- close the sending half of the socket
91 adb_close(s[1]);
92 }
93
94 // we'll be reading from s[0] as the data is sent by the child process
95 return s[0];
96}