+ /* If a transfer script was specified, launch it and connect to both stdin
+ * and stdout. fd_in is stdin of the child, and fd_out is stdout for the
+ * child. */
+ pid_t pid = 0;
+ FILE *fd_in = NULL, *fd_out = NULL;
+
+ if (backup_script != "") {
+ int fds[4];
+
+ if (pipe(&fds[0]) < 0) {
+ fatal("Unable to create pipe for upload script");
+ }
+ if (pipe(&fds[2]) < 0) {
+ fatal("Unable to create pipe for upload script");
+ }
+
+ pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "Unable to fork for upload script: %m\n");
+ fatal("fork: upload script");
+ }
+
+ if (pid > 0) {
+ /* Parent */
+ close(fds[0]);
+ close(fds[3]);
+ cloexec(fds[1]); fd_in = fdopen(fds[1], "w");
+ cloexec(fds[2]); fd_out = fdopen(fds[2], "r");
+ } else if (pid == 0) {
+ /* Child */
+ if (dup2(fds[0], 0) < 0)
+ exit(1);
+ if (dup2(fds[3], 1) < 0)
+ exit(1);
+ for (int i = 0; i < 3; i++)
+ close(fds[i]);
+
+ execlp("/bin/sh", "/bin/sh", "-c", backup_script.c_str(), NULL);
+ fatal("exec failed");
+ }
+ }
+