diff options
-rw-r--r-- | bitbake/lib/bb/utils.py | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 92d44c5260..e6e21e20fe 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -29,6 +29,8 @@ import collections | |||
29 | import copy | 29 | import copy |
30 | import ctypes | 30 | import ctypes |
31 | import random | 31 | import random |
32 | import socket | ||
33 | import struct | ||
32 | import tempfile | 34 | import tempfile |
33 | from subprocess import getstatusoutput | 35 | from subprocess import getstatusoutput |
34 | from contextlib import contextmanager | 36 | from contextlib import contextmanager |
@@ -1603,6 +1605,44 @@ def set_process_name(name): | |||
1603 | except: | 1605 | except: |
1604 | pass | 1606 | pass |
1605 | 1607 | ||
1608 | def enable_loopback_networking(): | ||
1609 | # From bits/ioctls.h | ||
1610 | SIOCGIFFLAGS = 0x8913 | ||
1611 | SIOCSIFFLAGS = 0x8914 | ||
1612 | SIOCSIFADDR = 0x8916 | ||
1613 | SIOCSIFNETMASK = 0x891C | ||
1614 | |||
1615 | # if.h | ||
1616 | IFF_UP = 0x1 | ||
1617 | IFF_RUNNING = 0x40 | ||
1618 | |||
1619 | # bits/socket.h | ||
1620 | AF_INET = 2 | ||
1621 | |||
1622 | # char ifr_name[IFNAMSIZ=16] | ||
1623 | ifr_name = struct.pack("@16s", b"lo") | ||
1624 | def netdev_req(fd, req, data = b""): | ||
1625 | # Pad and add interface name | ||
1626 | data = ifr_name + data + (b'\x00' * (16 - len(data))) | ||
1627 | # Return all data after interface name | ||
1628 | return fcntl.ioctl(fd, req, data)[16:] | ||
1629 | |||
1630 | with socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) as sock: | ||
1631 | fd = sock.fileno() | ||
1632 | |||
1633 | # struct sockaddr_in ifr_addr { unsigned short family; uint16_t sin_port ; uint32_t in_addr; } | ||
1634 | req = struct.pack("@H", AF_INET) + struct.pack("=H4B", 0, 127, 0, 0, 1) | ||
1635 | netdev_req(fd, SIOCSIFADDR, req) | ||
1636 | |||
1637 | # short ifr_flags | ||
1638 | flags = struct.unpack_from('@h', netdev_req(fd, SIOCGIFFLAGS))[0] | ||
1639 | flags |= IFF_UP | IFF_RUNNING | ||
1640 | netdev_req(fd, SIOCSIFFLAGS, struct.pack('@h', flags)) | ||
1641 | |||
1642 | # struct sockaddr_in ifr_netmask | ||
1643 | req = struct.pack("@H", AF_INET) + struct.pack("=H4B", 0, 255, 0, 0, 0) | ||
1644 | netdev_req(fd, SIOCSIFNETMASK, req) | ||
1645 | |||
1606 | def disable_network(uid=None, gid=None): | 1646 | def disable_network(uid=None, gid=None): |
1607 | """ | 1647 | """ |
1608 | Disable networking in the current process if the kernel supports it, else | 1648 | Disable networking in the current process if the kernel supports it, else |
@@ -1624,7 +1664,7 @@ def disable_network(uid=None, gid=None): | |||
1624 | 1664 | ||
1625 | ret = libc.unshare(CLONE_NEWNET | CLONE_NEWUSER) | 1665 | ret = libc.unshare(CLONE_NEWNET | CLONE_NEWUSER) |
1626 | if ret != 0: | 1666 | if ret != 0: |
1627 | logger.debug("System doesn't suport disabling network without admin privs") | 1667 | logger.debug("System doesn't support disabling network without admin privs") |
1628 | return | 1668 | return |
1629 | with open("/proc/self/uid_map", "w") as f: | 1669 | with open("/proc/self/uid_map", "w") as f: |
1630 | f.write("%s %s 1" % (uid, uid)) | 1670 | f.write("%s %s 1" % (uid, uid)) |