| 1427 | |
| 1428 | |
| 1429 | int pipe_command(struct child_process *cmd, |
| 1430 | const char *in, size_t in_len, |
| 1431 | struct strbuf *out, size_t out_hint, |
| 1432 | struct strbuf *err, size_t err_hint) |
| 1433 | { |
| 1434 | struct io_pump io[3]; |
| 1435 | int nr = 0; |
| 1436 | |
| 1437 | if (in) |
| 1438 | cmd->in = -1; |
| 1439 | if (out) |
| 1440 | cmd->out = -1; |
| 1441 | if (err) |
| 1442 | cmd->err = -1; |
| 1443 | |
| 1444 | if (start_command(cmd) < 0) |
| 1445 | return -1; |
| 1446 | |
| 1447 | if (in) { |
| 1448 | if (enable_pipe_nonblock(cmd->in) < 0) { |
| 1449 | error_errno("unable to make pipe non-blocking"); |
| 1450 | close(cmd->in); |
| 1451 | if (out) |
| 1452 | close(cmd->out); |
| 1453 | if (err) |
| 1454 | close(cmd->err); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | io[nr].fd = cmd->in; |
| 1458 | io[nr].type = POLLOUT; |
| 1459 | io[nr].u.out.buf = in; |
| 1460 | io[nr].u.out.len = in_len; |
| 1461 | nr++; |
| 1462 | } |
| 1463 | if (out) { |
| 1464 | io[nr].fd = cmd->out; |
| 1465 | io[nr].type = POLLIN; |
| 1466 | io[nr].u.in.buf = out; |
| 1467 | io[nr].u.in.hint = out_hint; |
| 1468 | nr++; |
| 1469 | } |
| 1470 | if (err) { |
| 1471 | io[nr].fd = cmd->err; |
| 1472 | io[nr].type = POLLIN; |
| 1473 | io[nr].u.in.buf = err; |
| 1474 | io[nr].u.in.hint = err_hint; |
| 1475 | nr++; |
| 1476 | } |
| 1477 | |
| 1478 | if (pump_io(io, nr) < 0) { |
| 1479 | finish_command(cmd); /* throw away exit code */ |
| 1480 | return -1; |
| 1481 | } |
| 1482 | |
| 1483 | return finish_command(cmd); |
| 1484 | } |
| 1485 | |
| 1486 | enum child_state { |