summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/utils.py
diff options
context:
space:
mode:
authorMattias Jernberg <mattiasj@axis.com>2022-09-29 18:37:13 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-09-29 21:26:09 +0100
commit74877d2dc267c958d02342d16cebeaeded23df8c (patch)
tree8549fde53687f6d0b4efbca7fcc950d112d4b605 /bitbake/lib/bb/utils.py
parent764bbf2a9b001f7bf78b689613d8993a66b87430 (diff)
downloadpoky-74877d2dc267c958d02342d16cebeaeded23df8c.tar.gz
bitbake: utils: Add enable_loopback_networking()
It can be used to enable the loopback interface, typically after calling disable_network(). Also correct a typo in a debug message. (Bitbake rev: 0d317209d4234c5f05a9fcdc13c52f502f104018) Signed-off-by: Mattias Jernberg <mattias.jernberg@axis.com> Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r--bitbake/lib/bb/utils.py42
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
29import copy 29import copy
30import ctypes 30import ctypes
31import random 31import random
32import socket
33import struct
32import tempfile 34import tempfile
33from subprocess import getstatusoutput 35from subprocess import getstatusoutput
34from contextlib import contextmanager 36from contextlib import contextmanager
@@ -1603,6 +1605,44 @@ def set_process_name(name):
1603 except: 1605 except:
1604 pass 1606 pass
1605 1607
1608def 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
1606def disable_network(uid=None, gid=None): 1646def 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))