From aabab6dff5396ef2afd532eb4ccff1ac1ebd7366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 26 Mar 2026 14:55:46 +0800 Subject: [PATCH] Global (Linux): replaces `strerror(errno)` with `%m` --- src/common/impl/netif_linux.c | 12 ++++++------ src/common/impl/networking_linux.c | 27 +++++++++++++-------------- src/common/impl/smbiosHelper.c | 18 +++++++++--------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/common/impl/netif_linux.c b/src/common/impl/netif_linux.c index 092d919fb..9c2e90a19 100644 --- a/src/common/impl/netif_linux.c +++ b/src/common/impl/netif_linux.c @@ -14,7 +14,7 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) FF_AUTO_CLOSE_FD int sock_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (sock_fd < 0) { - FF_DEBUG("Failed to create netlink socket: %s", strerror(errno)); + FF_DEBUG("Failed to create netlink socket: %m"); return false; } FF_DEBUG("Created netlink socket: fd=%d", sock_fd); @@ -30,7 +30,7 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) }; if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - FF_DEBUG("Failed to bind socket: %s", strerror(errno)); + FF_DEBUG("Failed to bind socket: %m"); return false; } FF_DEBUG("Successfully bound socket"); @@ -97,7 +97,7 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) (struct sockaddr*)&src_addr, &src_addr_len); if (received < 0) { - FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); + FF_DEBUG("Failed to receive netlink response: %m"); return false; } @@ -230,7 +230,7 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) FF_AUTO_CLOSE_FD int sock_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (sock_fd < 0) { - FF_DEBUG("Failed to create netlink socket: %s", strerror(errno)); + FF_DEBUG("Failed to create netlink socket: %m"); return false; } FF_DEBUG("Created netlink socket: fd=%d", sock_fd); @@ -246,7 +246,7 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) }; if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - FF_DEBUG("Failed to bind socket: %s", strerror(errno)); + FF_DEBUG("Failed to bind socket: %m"); return false; } FF_DEBUG("Successfully bound socket"); @@ -313,7 +313,7 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) (struct sockaddr*)&src_addr, &src_addr_len); if (received < 0) { - FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); + FF_DEBUG("Failed to receive netlink response: %m"); return false; } diff --git a/src/common/impl/networking_linux.c b/src/common/impl/networking_linux.c index 0504b0f0f..8b4825140 100644 --- a/src/common/impl/networking_linux.c +++ b/src/common/impl/networking_linux.c @@ -43,7 +43,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) TCP_FASTOPEN #endif , &flag, sizeof(flag)) != 0) { - FF_DEBUG("Failed to set TCP_FASTOPEN option: %s", strerror(errno)); + FF_DEBUG("Failed to set TCP_FASTOPEN option: %m"); return "setsockopt(TCP_FASTOPEN) failed"; } else { #if __linux__ || __GNU__ @@ -73,7 +73,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) state->addr->ai_addrlen); #else if (fcntl(state->sockfd, F_SETFL, O_NONBLOCK) == -1) { - FF_DEBUG("fcntl(F_SETFL) failed: %s", strerror(errno)); + FF_DEBUG("fcntl(F_SETFL) failed: %m"); return "fcntl(F_SETFL) failed"; } FF_DEBUG("Using connectx() to send %u bytes of data", state->command.length); @@ -90,7 +90,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) .iov_len = state->command.length, }, 1, &sent, NULL) != 0) sent = 0; if (fcntl(state->sockfd, F_SETFL, 0) == -1) { - FF_DEBUG("fcntl(F_SETFL) failed: %s", strerror(errno)); + FF_DEBUG("fcntl(F_SETFL) failed: %m"); return "fcntl(F_SETFL) failed"; } #endif @@ -108,8 +108,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) #else "sendto()" #endif - " %s (sent=%zd, errno=%d: %s)", errno == 0 ? "succeeded" : "was in progress", - sent, errno, strerror(errno)); + " %s (sent=%zd, %m)", errno == 0 ? "succeeded" : "was in progress", sent); freeaddrinfo(state->addr); state->addr = NULL; ffStrbufDestroy(&state->command); @@ -122,7 +121,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) #else "sendto()" #endif - " failed: %s (errno=%d)", strerror(errno), errno); + " failed: %m"); #ifdef __APPLE__ return "connectx() failed"; #else @@ -143,7 +142,7 @@ static const char* connectAndSend(FFNetworkingState* state) FF_DEBUG("Attempting connect() to server..."); if(connect(state->sockfd, state->addr->ai_addr, state->addr->ai_addrlen) == -1) { - FF_DEBUG("connect() failed: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("connect() failed: %m"); ret = "connect() failed"; goto error; } @@ -152,7 +151,7 @@ static const char* connectAndSend(FFNetworkingState* state) FF_DEBUG("Attempting to send %u bytes of data...", state->command.length); if(send(state->sockfd, state->command.chars, state->command.length, 0) < 0) { - FF_DEBUG("send() failed: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("send() failed: %m"); ret = "send() failed"; goto error; } @@ -227,7 +226,7 @@ static const char* initNetworkingState(FFNetworkingState* state, const char* hos state->sockfd = socket(state->addr->ai_family, state->addr->ai_socktype, state->addr->ai_protocol); if(state->sockfd == -1) { - FF_DEBUG("socket() failed: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("socket() failed: %m"); ret = "socket() failed"; goto error; } @@ -237,7 +236,7 @@ static const char* initNetworkingState(FFNetworkingState* state, const char* hos #ifdef TCP_NODELAY // Disable Nagle's algorithm to reduce small packet transmission delay if (setsockopt(state->sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) != 0) { - FF_DEBUG("Failed to set TCP_NODELAY: %s", strerror(errno)); + FF_DEBUG("Failed to set TCP_NODELAY: %m"); } else { FF_DEBUG("Successfully disabled Nagle's algorithm"); } @@ -246,7 +245,7 @@ static const char* initNetworkingState(FFNetworkingState* state, const char* hos #ifdef TCP_QUICKACK // Set TCP_QUICKACK option to avoid delayed acknowledgments if (setsockopt(state->sockfd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag)) != 0) { - FF_DEBUG("Failed to set TCP_QUICKACK: %s", strerror(errno)); + FF_DEBUG("Failed to set TCP_QUICKACK: %m"); } else { FF_DEBUG("Successfully enabled TCP quick acknowledgment"); } @@ -399,7 +398,7 @@ const char* ffNetworkingRecvHttpResponse(FFNetworkingState* state, FFstrbuf* buf } else if (pollRes == -1) { - FF_DEBUG("poll() failed: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("poll() failed: %m"); close(state->sockfd); state->sockfd = -1; return "poll() failed"; @@ -419,7 +418,7 @@ const char* ffNetworkingRecvHttpResponse(FFNetworkingState* state, FFstrbuf* buf if (shutdown(state->sockfd, SHUT_WR) == -1) { - FF_DEBUG("Failed to shutdown socket send: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("Failed to shutdown socket send: %m"); // Not a critical error, continue anyway } @@ -440,7 +439,7 @@ const char* ffNetworkingRecvHttpResponse(FFNetworkingState* state, FFstrbuf* buf if (received == 0) { FF_DEBUG("Connection closed (received=0)"); } else { - FF_DEBUG("Reception failed: %s (errno=%d)", strerror(errno), errno); + FF_DEBUG("Reception failed: %m"); } break; } diff --git a/src/common/impl/smbiosHelper.c b/src/common/impl/smbiosHelper.c index a41638ccb..5d1d6f50d 100644 --- a/src/common/impl/smbiosHelper.c +++ b/src/common/impl/smbiosHelper.c @@ -286,7 +286,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() FF_AUTO_CLOSE_FD int fd = open("/dev/mem", O_RDONLY | O_CLOEXEC); if (fd < 0) { - FF_DEBUG("Failed to open /dev/mem: %s", strerror(errno)); + FF_DEBUG("Failed to open /dev/mem: %m"); return NULL; } FF_DEBUG("/dev/mem opened successfully with fd=%d", fd); @@ -301,7 +301,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() // https://stackoverflow.com/questions/69372330/how-to-read-dev-mem-using-read void* p = mmap(NULL, sizeof(entryPoint), PROT_READ, MAP_SHARED, fd, entryAddress); if (p == MAP_FAILED) { - FF_DEBUG("mmap failed: %s", strerror(errno)); + FF_DEBUG("mmap failed: %m"); return NULL; } memcpy(&entryPoint, p, sizeof(entryPoint)); @@ -322,7 +322,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() FF_AUTO_CLOSE_FD int fd = open("/dev/smbios", O_RDONLY | O_CLOEXEC); if (fd < 0) { - FF_DEBUG("Failed to open /dev/smbios: %s", strerror(errno)); + FF_DEBUG("Failed to open /dev/smbios: %m"); return NULL; } FF_DEBUG("/dev/smbios opened successfully with fd=%d", fd); @@ -337,14 +337,14 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() FF_DEBUG("Got SMBIOS address from sysctl: 0x%lx", (unsigned long)addr); if (pread(fd, &entryPoint, sizeof(entryPoint), addr) < 1) { - FF_DEBUG("Failed to read SMBIOS entry point: %s", strerror(errno)); + FF_DEBUG("Failed to read SMBIOS entry point: %m"); return NULL; } FF_DEBUG("Successfully read SMBIOS entry point"); #else FF_DEBUG("Reading SMBIOS entry point from /dev/smbios"); if (ffReadFDData(fd, sizeof(entryPoint), &entryPoint) < 1) { - FF_DEBUG("Failed to read SMBIOS entry point: %s", strerror(errno)); + FF_DEBUG("Failed to read SMBIOS entry point: %m"); return NULL; } FF_DEBUG("Successfully read SMBIOS entry point"); @@ -402,7 +402,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() void* p = mmap(NULL, tableLength, PROT_READ, MAP_SHARED, fd, tableAddress); if (p == MAP_FAILED) { - FF_DEBUG("mmap failed: %s", strerror(errno)); + FF_DEBUG("mmap failed: %m"); ffStrbufDestroy(&buffer); // free buffer and reset state return NULL; } @@ -431,7 +431,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() #endif , O_RDONLY | O_CLOEXEC); if (fd < 0) { - FF_DEBUG("Failed to open memory device: %s", strerror(errno)); + FF_DEBUG("Failed to open memory device: %m"); return NULL; } FF_DEBUG("Memory device opened successfully with fd=%d", fd); @@ -443,7 +443,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() // which is not available via EFIIOC_GET_TABLE. FF_AUTO_FREE uint8_t* smBiosBase = malloc(0x10000); if (pread(fd, smBiosBase, 0x10000, 0xF0000) != 0x10000) { - FF_DEBUG("Failed to read SMBIOS memory region: %s", strerror(errno)); + FF_DEBUG("Failed to read SMBIOS memory region: %m"); return NULL; } FF_DEBUG("Successfully read 0x10000 bytes from physical address 0xF0000"); @@ -496,7 +496,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() FF_DEBUG("Successfully read SMBIOS table data: %u bytes", tableLength); } else { - FF_DEBUG("Failed to read SMBIOS table data: %s", strerror(errno)); + FF_DEBUG("Failed to read SMBIOS table data: %m"); return NULL; } }