authorgravatar for subi.the.dream.walker@gmail.comBing Sun <subi.the.dream.walker@gmail.com> 2025-01-30 03:16:08+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-29 19:16:08+00:00
log798bb0bc61284be3948272bb2085912534feb8bd
treef8010dfc1b18ecc43506fc4337f472cada28c35c
parent249a20d972489cf8eb0e9c3b126f358fc1643eb2
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.c: android bionic compatibility fixes (getrandom & getaddressinfo) (#22143)

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

1 files changed, 153 insertions(+), 6 deletions(-)

lib/std/c.zig+153-6
...@@ -3990,7 +3990,16 @@ pub const sigval = switch (native_os) {...@@ -3990,7 +3990,16 @@ pub const sigval = switch (native_os) {
3990 else => void,3990 else => void,
3991};3991};
39923992
3993pub const addrinfo = switch (native_os) {3993pub const addrinfo = if (builtin.abi.isAndroid()) extern struct {
3994 flags: AI,
3995 family: i32,
3996 socktype: i32,
3997 protocol: i32,
3998 addrlen: socklen_t,
3999 canonname: ?[*:0]u8,
4000 addr: ?*sockaddr,
4001 next: ?*addrinfo,
4002} else switch (native_os) {
3994 .linux, .emscripten => linux.addrinfo,4003 .linux, .emscripten => linux.addrinfo,
3995 .windows => ws2_32.addrinfo,4004 .windows => ws2_32.addrinfo,
3996 .freebsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {4005 .freebsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
...@@ -4374,7 +4383,52 @@ pub const sa_family_t = switch (native_os) {...@@ -4374,7 +4383,52 @@ pub const sa_family_t = switch (native_os) {
4374 .solaris, .illumos => u16,4383 .solaris, .illumos => u16,
4375 else => void,4384 else => void,
4376};4385};
4377pub const AF = switch (native_os) {4386pub const AF = if (builtin.abi.isAndroid()) struct {
4387 pub const UNSPEC = 0;
4388 pub const UNIX = 1;
4389 pub const LOCAL = 1;
4390 pub const INET = 2;
4391 pub const AX25 = 3;
4392 pub const IPX = 4;
4393 pub const APPLETALK = 5;
4394 pub const NETROM = 6;
4395 pub const BRIDGE = 7;
4396 pub const ATMPVC = 8;
4397 pub const X25 = 9;
4398 pub const INET6 = 10;
4399 pub const ROSE = 11;
4400 pub const DECnet = 12;
4401 pub const NETBEUI = 13;
4402 pub const SECURITY = 14;
4403 pub const KEY = 15;
4404 pub const NETLINK = 16;
4405 pub const ROUTE = NETLINK;
4406 pub const PACKET = 17;
4407 pub const ASH = 18;
4408 pub const ECONET = 19;
4409 pub const ATMSVC = 20;
4410 pub const RDS = 21;
4411 pub const SNA = 22;
4412 pub const IRDA = 23;
4413 pub const PPPOX = 24;
4414 pub const WANPIPE = 25;
4415 pub const LLC = 26;
4416 pub const CAN = 29;
4417 pub const TIPC = 30;
4418 pub const BLUETOOTH = 31;
4419 pub const IUCV = 32;
4420 pub const RXRPC = 33;
4421 pub const ISDN = 34;
4422 pub const PHONET = 35;
4423 pub const IEEE802154 = 36;
4424 pub const CAIF = 37;
4425 pub const ALG = 38;
4426 pub const NFC = 39;
4427 pub const VSOCK = 40;
4428 pub const KCM = 41;
4429 pub const QIPCRTR = 42;
4430 pub const MAX = 43;
4431} else switch (native_os) {
4378 .linux, .emscripten => linux.AF,4432 .linux, .emscripten => linux.AF,
4379 .windows => ws2_32.AF,4433 .windows => ws2_32.AF,
4380 .macos, .ios, .tvos, .watchos, .visionos => struct {4434 .macos, .ios, .tvos, .watchos, .visionos => struct {
...@@ -4607,7 +4661,52 @@ pub const AF = switch (native_os) {...@@ -4607,7 +4661,52 @@ pub const AF = switch (native_os) {
4607 },4661 },
4608 else => void,4662 else => void,
4609};4663};
4610pub const PF = switch (native_os) {4664pub const PF = if (builtin.abi.isAndroid()) struct {
4665 pub const UNSPEC = AF.UNSPEC;
4666 pub const UNIX = AF.UNIX;
4667 pub const LOCAL = AF.LOCAL;
4668 pub const INET = AF.INET;
4669 pub const AX25 = AF.AX25;
4670 pub const IPX = AF.IPX;
4671 pub const APPLETALK = AF.APPLETALK;
4672 pub const NETROM = AF.NETROM;
4673 pub const BRIDGE = AF.BRIDGE;
4674 pub const ATMPVC = AF.ATMPVC;
4675 pub const X25 = AF.X25;
4676 pub const PF_INET6 = AF.INET6;
4677 pub const PF_ROSE = AF.ROSE;
4678 pub const PF_DECnet = AF.DECnet;
4679 pub const PF_NETBEUI = AF.NETBEUI;
4680 pub const PF_SECURITY = AF.SECURITY;
4681 pub const PF_KEY = AF.KEY;
4682 pub const PF_NETLINK = AF.NETLINK;
4683 pub const PF_ROUTE = AF.ROUTE;
4684 pub const PF_PACKET = AF.PACKET;
4685 pub const PF_ASH = AF.ASH;
4686 pub const PF_ECONET = AF.ECONET;
4687 pub const PF_ATMSVC = AF.ATMSVC;
4688 pub const PF_RDS = AF.RDS;
4689 pub const PF_SNA = AF.SNA;
4690 pub const PF_IRDA = AF.IRDA;
4691 pub const PF_PPPOX = AF.PPPOX;
4692 pub const PF_WANPIPE = AF.WANPIPE;
4693 pub const PF_LLC = AF.LLC;
4694 pub const PF_CAN = AF.CAN;
4695 pub const PF_TIPC = AF.TIPC;
4696 pub const PF_BLUETOOTH = AF.BLUETOOTH;
4697 pub const PF_IUCV = AF.IUCV;
4698 pub const PF_RXRPC = AF.RXRPC;
4699 pub const PF_ISDN = AF.ISDN;
4700 pub const PF_PHONET = AF.PHONET;
4701 pub const PF_IEEE802154 = AF.IEEE802154;
4702 pub const PF_CAIF = AF.CAIF;
4703 pub const PF_ALG = AF.ALG;
4704 pub const PF_NFC = AF.NFC;
4705 pub const PF_VSOCK = AF.VSOCK;
4706 pub const PF_KCM = AF.KCM;
4707 pub const PF_QIPCRTR = AF.QIPCRTR;
4708 pub const PF_MAX = AF.MAX;
4709} else switch (native_os) {
4611 .linux, .emscripten => linux.PF,4710 .linux, .emscripten => linux.PF,
4612 .macos, .ios, .tvos, .watchos, .visionos => struct {4711 .macos, .ios, .tvos, .watchos, .visionos => struct {
4613 pub const UNSPEC = AF.UNSPEC;4712 pub const UNSPEC = AF.UNSPEC;
...@@ -6241,7 +6340,18 @@ pub const dirent64 = switch (native_os) {...@@ -6241,7 +6340,18 @@ pub const dirent64 = switch (native_os) {
6241 else => void,6340 else => void,
6242};6341};
62436342
6244pub const AI = switch (native_os) {6343pub const AI = if (builtin.abi.isAndroid()) packed struct(u32) {
6344 PASSIVE: bool = false,
6345 CANONNAME: bool = false,
6346 NUMERICHOST: bool = false,
6347 NUMERICSERV: bool = false,
6348 _4: u4 = 0,
6349 ALL: bool = false,
6350 V4MAPPED_CFG: bool = false,
6351 ADDRCONFIG: bool = false,
6352 V4MAPPED: bool = false,
6353 _: u20 = 0,
6354} else switch (native_os) {
6245 .linux, .emscripten => linux.AI,6355 .linux, .emscripten => linux.AI,
6246 .dragonfly, .haiku, .freebsd => packed struct(u32) {6356 .dragonfly, .haiku, .freebsd => packed struct(u32) {
6247 PASSIVE: bool = false,6357 PASSIVE: bool = false,
...@@ -6320,7 +6430,40 @@ pub const NI = switch (native_os) {...@@ -6320,7 +6430,40 @@ pub const NI = switch (native_os) {
6320 else => void,6430 else => void,
6321};6431};
63226432
6323pub const EAI = switch (native_os) {6433pub const EAI = if (builtin.abi.isAndroid()) enum(c_int) {
6434 /// address family for hostname not supported
6435 ADDRFAMILY = 1,
6436 /// temporary failure in name resolution
6437 AGAIN = 2,
6438 /// invalid value for ai_flags
6439 BADFLAGS = 3,
6440 /// non-recoverable failure in name resolution
6441 FAIL = 4,
6442 /// ai_family not supported
6443 FAMILY = 5,
6444 /// memory allocation failure
6445 MEMORY = 6,
6446 /// no address associated with hostname
6447 NODATA = 7,
6448 /// hostname nor servname provided, or not known
6449 NONAME = 8,
6450 /// servname not supported for ai_socktype
6451 SERVICE = 9,
6452 /// ai_socktype not supported
6453 SOCKTYPE = 10,
6454 /// system error returned in errno
6455 SYSTEM = 11,
6456 /// invalid value for hints
6457 BADHINTS = 12,
6458 /// resolved protocol is unknown
6459 PROTOCOL = 13,
6460 /// argument buffer overflow
6461 OVERFLOW = 14,
6462
6463 MAX = 15,
6464
6465 _,
6466} else switch (native_os) {
6324 .linux, .emscripten => enum(c_int) {6467 .linux, .emscripten => enum(c_int) {
6325 BADFLAGS = -1,6468 BADFLAGS = -1,
6326 NONAME = -2,6469 NONAME = -2,
...@@ -9087,7 +9230,11 @@ pub const getentropy = switch (native_os) {...@@ -9087,7 +9230,11 @@ pub const getentropy = switch (native_os) {
9087};9230};
9088pub const getrandom = switch (native_os) {9231pub const getrandom = switch (native_os) {
9089 .freebsd => private.getrandom,9232 .freebsd => private.getrandom,
9090 .linux => if (versionCheck(.{ .major = 2, .minor = 25, .patch = 0 })) private.getrandom else {},9233 .linux => if (builtin.abi.isMusl() or
9234 (builtin.abi.isGnu() and versionCheck(.{ .major = 2, .minor = 25, .patch = 0 })) or
9235 (builtin.abi.isAndroid() and versionCheck(.{ .major = 28, .minor = 0, .patch = 0 })))
9236 private.getrandom
9237 else {},
9091 else => {},9238 else => {},
9092};9239};
90939240