backup_service.c revision d2f5415c603f7d9961f7a0b05579a0768e071410
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(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
58        argc = 1; // room for the basic 'bu' argv[0]
59        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";
67        argc = 1;   // run through again to build the argv array
68        for (p = (char*)args; *p; ) {
69            backup_args[argc++] = p;
70            while (*p && *p != ':') p++;
71            if (*p == ':') {
72                *p = 0;
73                p++;
74            }
75        }
76        backup_args[argc] = NULL;
77
78        // Close the half of the socket that we don't care about, route 'bu's console
79        // to the output socket, and off we go
80        adb_close(s[0]);
81        dup2(s[1], STDOUT_FILENO);
82
83        // off we go
84        execvp("/system/bin/bu", (char * const *)backup_args);
85        // oops error - close up shop and go home
86        fprintf(stderr, "Unable to exec 'bu', bailing\n");
87        exit(-1);
88    } else {
89        // parent, i.e. adbd -- close the sending half of the socket
90        adb_close(s[1]);
91    }
92
93    // we'll be reading from s[0] as the data is sent by the child process
94    return s[0];
95}
96