|
14 | 14 | #include <netdb.h> |
15 | 15 | #include <net/if.h> |
16 | 16 | #include <ifaddrs.h> |
| 17 | +#include <sys/wait.h> |
17 | 18 |
|
18 | 19 | #include "hyper.h" |
19 | 20 | #include "util.h" |
@@ -1359,3 +1360,139 @@ void hyper_cleanup_dns(struct hyper_pod *pod) |
1359 | 1360 |
|
1360 | 1361 | close(fd); |
1361 | 1362 | } |
| 1363 | + |
| 1364 | +int hyper_setup_iptables(struct hyper_pod *pod) { |
| 1365 | + int pid, status; |
| 1366 | + int pipefd[2] = {-1, -1}; |
| 1367 | + int pipe_parent_err[2] = {-1, -1}; |
| 1368 | + int ret = -1; |
| 1369 | + size_t size, offset = 0; |
| 1370 | + int pipe_size = 0; |
| 1371 | + const char *cmd = "iptables-restore"; |
| 1372 | + |
| 1373 | + if (! pod) { |
| 1374 | + return -1; |
| 1375 | + } |
| 1376 | + |
| 1377 | + if (! pod->iptable_rules) { |
| 1378 | + return 0; |
| 1379 | + } |
| 1380 | + |
| 1381 | + if (! *(pod->iptable_rules)) { |
| 1382 | + free(pod->iptable_rules); |
| 1383 | + return 0; |
| 1384 | + } |
| 1385 | + |
| 1386 | + if (pipe(pipefd) == -1) { |
| 1387 | + fprintf(stderr, "Error while creating pipe for iptables: %s\n", |
| 1388 | + strerror(errno)); |
| 1389 | + goto err; |
| 1390 | + } |
| 1391 | + |
| 1392 | + if (pipe2(pipe_parent_err, O_CLOEXEC) == -1) { |
| 1393 | + fprintf(stderr, "Error while creating parent error pipe for " |
| 1394 | + "iptables: %s\n", |
| 1395 | + strerror(errno)); |
| 1396 | + goto err; |
| 1397 | + } |
| 1398 | + |
| 1399 | + pid = fork(); |
| 1400 | + if ( pid == -1) { |
| 1401 | + fprintf(stderr, "Failed to spawn child: %s\n", strerror(errno)); |
| 1402 | + goto err; |
| 1403 | + } |
| 1404 | + |
| 1405 | + if (pid == 0) { |
| 1406 | + char c; |
| 1407 | + close_if_set(pipefd[1]); |
| 1408 | + close_if_set(pipe_parent_err[1]); |
| 1409 | + |
| 1410 | + if (read(pipe_parent_err[0], &c, sizeof(c)) != 0) { |
| 1411 | + fprintf(stderr, "Parent setup failed for command %s\n", |
| 1412 | + cmd); |
| 1413 | + } |
| 1414 | + |
| 1415 | + /* The rules are sent to the stdin of iptables-restore |
| 1416 | + */ |
| 1417 | + if (dup2(pipefd[0], STDIN_FILENO) == -1) { |
| 1418 | + fprintf(stderr, "Dup call failed : %s\n", |
| 1419 | + strerror(errno)); |
| 1420 | + goto err_child; |
| 1421 | + } |
| 1422 | + |
| 1423 | + fprintf(stdout, "execing %s\n", cmd); |
| 1424 | + if (execlp(cmd, cmd, "-v", NULL) == -1) { |
| 1425 | + fprintf(stderr, "Exec call for %s failed :%s\n", |
| 1426 | + cmd, strerror(errno)); |
| 1427 | + } |
| 1428 | + |
| 1429 | +err_child: |
| 1430 | + close_if_set(pipefd[0]); |
| 1431 | + close_if_set(pipe_parent_err[0]); |
| 1432 | + exit(EXIT_FAILURE); |
| 1433 | + } |
| 1434 | + |
| 1435 | + //parent |
| 1436 | + close_if_set(pipefd[0]); |
| 1437 | + close_if_set(pipe_parent_err[0]); |
| 1438 | + |
| 1439 | + size = strlen(pod->iptable_rules); |
| 1440 | + |
| 1441 | + pipe_size = fcntl(pipefd[1], F_GETPIPE_SZ); |
| 1442 | + if (pipe_size <= size) { |
| 1443 | + if (fcntl(pipefd[1], F_SETPIPE_SZ, size+1) < 0) { |
| 1444 | + fprintf(stderr, "failed to change pipe size: %s", |
| 1445 | + strerror(errno)); |
| 1446 | + goto err_parent; |
| 1447 | + } |
| 1448 | + } |
| 1449 | + |
| 1450 | + while (offset < size) { |
| 1451 | + ret = write(pipefd[1], pod->iptable_rules+offset, size-offset); |
| 1452 | + if (ret < 0 && ret != EINTR) { |
| 1453 | + break; |
| 1454 | + } |
| 1455 | + offset += ret; |
| 1456 | + } |
| 1457 | + |
| 1458 | +err_parent: |
| 1459 | + if (offset < size) { |
| 1460 | + fprintf(stderr, "Pipe Write err : %s\n", strerror(errno)); |
| 1461 | + if (write(pipe_parent_err[1], "E", 1) == -1) { |
| 1462 | + fprintf(stderr, "Error writing to parent err pipe: " |
| 1463 | + "%s\n", strerror(errno)); |
| 1464 | + } |
| 1465 | + } |
| 1466 | + |
| 1467 | + close_if_set(pipe_parent_err[1]); |
| 1468 | + close_if_set(pipefd[1]); |
| 1469 | + |
| 1470 | + if (waitpid(pid, &status, 0) <= 0) { |
| 1471 | + fprintf(stderr, "Error waiting for child for %s: %s", |
| 1472 | + cmd, strerror(errno)); |
| 1473 | + goto err; |
| 1474 | + } |
| 1475 | + |
| 1476 | + if (WIFEXITED(status)) { |
| 1477 | + ret = WEXITSTATUS(status); |
| 1478 | + fprintf(stdout, "Command %s exited normally, " |
| 1479 | + "status %" PRIu8 "\n", |
| 1480 | + cmd, ret); |
| 1481 | + if (ret == 0) { |
| 1482 | + free(pod->iptable_rules); |
| 1483 | + return 0; |
| 1484 | + } |
| 1485 | + } |
| 1486 | + |
| 1487 | + fprintf(stdout, "Command %s exit unexpectedly, " |
| 1488 | + "status %" PRIu8 "\n", |
| 1489 | + cmd, status); |
| 1490 | + |
| 1491 | +err: |
| 1492 | + close_if_set(pipefd[0]); |
| 1493 | + close_if_set(pipefd[1]); |
| 1494 | + close_if_set(pipe_parent_err[0]); |
| 1495 | + close_if_set(pipe_parent_err[1]); |
| 1496 | + free(pod->iptable_rules); |
| 1497 | + return -1; |
| 1498 | +} |
0 commit comments