backup_service.c 4.5 KB

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