Global: prefers gnu::cleanup

This commit is contained in:
Carter Li
2026-10-03 17:45:44 +08:00
committed by Carter Li
parent 537ffac39c
commit 8102478f2f
7 changed files with 52 additions and 59 deletions
+3 -3
View File
@@ -1,5 +1,6 @@
#include "common/android/binder.h"
#include "common/debug.h"
#include "common/io.h"
#include <errno.h>
#include <fcntl.h>
@@ -9,7 +10,7 @@
const char* ffBinderOpen(FFBinder* binder) {
*binder = (FFBinder) { .fd = -1, .shared = nullptr, .sharedSize = 0, .protocolVersion = 0 };
const int fd = open(FF_BINDER_DEVICE, O_RDWR | O_CLOEXEC);
FF_AUTO_CLOSE_FD int fd = open(FF_BINDER_DEVICE, O_RDWR | O_CLOEXEC);
if (fd < 0) {
FF_DEBUG("open(" FF_BINDER_DEVICE ") failed: %s", strerror(errno));
return "Failed to open " FF_BINDER_DEVICE;
@@ -21,7 +22,6 @@ const char* ffBinderOpen(FFBinder* binder) {
FF_DEBUG("BINDER_VERSION reported protocol %u, expected %u (%s)",
version.protocol_version, BINDER_CURRENT_PROTOCOL_VERSION,
versionIoctl != 0 ? strerror(errno) : "version mismatch");
close(fd);
return "Unsupported binder protocol version";
}
@@ -30,7 +30,6 @@ const char* ffBinderOpen(FFBinder* binder) {
void* shared = mmap(nullptr, FF_BINDER_SHARED_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
if (shared == MAP_FAILED) {
FF_DEBUG("mmap(" FF_BINDER_DEVICE ") failed: %s", strerror(errno));
close(fd);
return "Failed to mmap " FF_BINDER_DEVICE;
}
@@ -40,6 +39,7 @@ const char* ffBinderOpen(FFBinder* binder) {
.sharedSize = FF_BINDER_SHARED_SIZE,
.protocolVersion = version.protocol_version,
};
fd = -1; // ownership moved into *binder, which ffBinderClose() lets go
return nullptr;
}
+22 -25
View File
@@ -1,14 +1,12 @@
#include "common/android/dex.h"
#include "common/debug.h"
#include "common/io.h"
#include "common/mallocHelper.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef FF_HAVE_ZLIB
#include "common/library.h"
@@ -599,29 +597,28 @@ const char* ffDexStaticInts(const char* jarPath, const FFDexStaticIntRequest* re
[[gnu::cleanup(wrapDexMapping)]] FFDexMapping mapping = {};
const int fd = open(jarPath, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
FF_DEBUG("open(%s) failed: %s", jarPath, strerror(errno));
return "open(jar) failed";
}
{
FF_AUTO_CLOSE_FD const int fd = open(jarPath, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
FF_DEBUG("open(%s) failed: %s", jarPath, strerror(errno));
return "open(jar) failed";
}
struct stat st = {};
const int statStatus = fstat(fd, &st);
if (statStatus != 0 || st.st_size <= 0) {
FF_DEBUG("fstat(%s) reported %s (%lld bytes)", jarPath,
statStatus != 0 ? strerror(errno) : "an empty file", (long long) st.st_size);
close(fd);
return "fstat(jar) failed";
}
mapping.mappedSize = (size_t) st.st_size;
mapping.mapped = mmap(nullptr, mapping.mappedSize, PROT_READ, MAP_PRIVATE, fd, 0);
// The mapping outlives the descriptor, so the descriptor can go either way from here.
const int mmapErrno = mapping.mapped == MAP_FAILED ? errno : 0;
close(fd);
if (mapping.mapped == MAP_FAILED) {
FF_DEBUG("mmap(%s, %zu bytes) failed: %s", jarPath, mapping.mappedSize, strerror(mmapErrno));
mapping.mapped = nullptr;
return "mmap(jar) failed";
struct stat st = {};
const int statStatus = fstat(fd, &st);
if (statStatus != 0 || st.st_size <= 0) {
FF_DEBUG("fstat(%s) reported %s (%lld bytes)", jarPath,
statStatus != 0 ? strerror(errno) : "an empty file", (long long) st.st_size);
return "fstat(jar) failed";
}
mapping.mappedSize = (size_t) st.st_size;
mapping.mapped = mmap(nullptr, mapping.mappedSize, PROT_READ, MAP_PRIVATE, fd, 0);
// The mapping outlives the descriptor, so the descriptor can go either way from here.
if (mapping.mapped == MAP_FAILED) {
FF_DEBUG("mmap(%s, %zu bytes) failed: %s", jarPath, mapping.mappedSize, strerror(errno));
mapping.mapped = nullptr;
return "mmap(jar) failed";
}
}
// One byte per request saying whether it is closed -- answered, or given up on -- and the type
+1 -2
View File
@@ -1,7 +1,6 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include "fastfetch.h"
// The one package name the shell UID owns. android.os.Process.SHELL_UID is a single app UID that
// owns exactly one package, so a process that cannot derive a package of its own -- a static build
+15 -13
View File
@@ -43,23 +43,29 @@ extern char** environ;
enum { FF_PIPE_BUFSIZ = 8192 };
static inline int ffPipe2(int* fds, int flags) {
static inline int ffPipe2(int* readEnd, int* writeEnd, int flags) {
int fds[2];
#ifndef FF_HAVE_PIPE2
if (pipe(fds) == -1) {
return -1;
}
fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | flags);
fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL) | flags);
return 0;
#else
return pipe2(fds, flags);
if (pipe2(fds, flags) == -1) {
return -1;
}
#endif
*readEnd = fds[0];
*writeEnd = fds[1];
return 0;
}
// Not thread-safe
const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFNativeFD stdinFd, FFProcessHandle* outHandle) {
int pipes[2];
if (ffPipe2(pipes, O_CLOEXEC) == -1) {
FF_AUTO_CLOSE_FD int pipeRead = -1;
FF_AUTO_CLOSE_FD int pipeWrite = -1;
if (ffPipe2(&pipeRead, &pipeWrite, O_CLOEXEC) == -1) {
FF_DEBUG("pipe() failed: %s", strerror(errno));
return "pipe() failed";
}
@@ -77,7 +83,7 @@ const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFNativeFD stdinF
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&file_actions, pipeWrite, useStdErr ? STDERR_FILENO : STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&file_actions, nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
if (ffIsValidNativeFD(stdinFd)) {
posix_spawn_file_actions_adddup2(&file_actions, stdinFd, STDIN_FILENO);
@@ -126,8 +132,6 @@ const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFNativeFD stdinF
if (ret != 0) {
FF_DEBUG("posix_spawnp() failed: %s", strerror(ret));
close(pipes[0]);
close(pipes[1]);
if (ret == ENOENT) {
return "command not found";
}
@@ -140,14 +144,12 @@ const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFNativeFD stdinF
childPid = fork();
if (childPid == -1) {
FF_DEBUG("fork() failed: %s", strerror(errno));
close(pipes[0]);
close(pipes[1]);
return "fork() failed";
}
if (childPid == 0) {
// Child process
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
dup2(pipeWrite, useStdErr ? STDERR_FILENO : STDOUT_FILENO);
dup2(nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
if (ffIsValidNativeFD(stdinFd)) {
dup2(stdinFd, STDIN_FILENO);
@@ -159,9 +161,9 @@ const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFNativeFD stdinF
#endif
close(pipes[1]);
outHandle->pid = childPid;
outHandle->pipeRead = pipes[0];
outHandle->pipeRead = pipeRead;
pipeRead = -1; // ownership moved into outHandle, which ffProcessReadOutput() lets go
return nullptr;
}
@@ -74,9 +74,11 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */)
}
do {
FF_AUTO_CLOSE_FD HANDLE hCurrent = hRadio;
BTH_LOCAL_RADIO_INFO blri;
DWORD returned;
if (!DeviceIoControl(hRadio, IOCTL_BTH_GET_LOCAL_INFO, nullptr, 0, &blri, sizeof(blri), &returned, nullptr)) {
if (!DeviceIoControl(hCurrent, IOCTL_BTH_GET_LOCAL_INFO, nullptr, 0, &blri, sizeof(blri), &returned, nullptr)) {
continue;
}
@@ -90,10 +92,8 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */)
device->lmpSubversion = blri.radioInfo.lmpSubversion;
ffStrbufInitStatic(&device->vendor, ffBluetoothRadioGetVendor(blri.radioInfo.mfg));
device->enabled = true;
device->connectable = ffBluetoothIsConnectable(hRadio);
device->discoverable = ffBluetoothIsDiscoverable(hRadio);
NtClose(hRadio);
device->connectable = ffBluetoothIsConnectable(hCurrent);
device->discoverable = ffBluetoothIsDiscoverable(hCurrent);
} while (ffBluetoothFindNextRadio(hFind, &hRadio));
ffBluetoothFindRadioClose(hFind);
+4 -6
View File
@@ -1,7 +1,9 @@
#include "fastfetch.h"
#include "common/properties.h"
#include "common/io.h"
#include "common/strutil.h"
#include "common/FFcache.h"
#include "common/mallocHelper.h"
#include "detection/gtk_qt/gtk_qt.h"
#include "detection/displayserver/displayserver.h"
@@ -24,12 +26,12 @@ typedef enum PlasmaCategory: uint8_t {
} PlasmaCategory;
static bool detectPlasmaFromFile(const char* filename, FFQtResult* result) {
FILE* kdeglobals = fopen(filename, "r");
FF_AUTO_CLOSE_FILE FILE* kdeglobals = fopen(filename, "r");
if (kdeglobals == nullptr) {
return false;
}
char* line = nullptr;
FF_AUTO_FREE char* line = nullptr;
size_t len = 0;
PlasmaCategory category = PLASMA_CATEGORY_OTHER;
@@ -72,10 +74,6 @@ static bool detectPlasmaFromFile(const char* filename, FFQtResult* result) {
}
}
free(line);
fclose(kdeglobals);
return true;
}
@@ -169,10 +169,10 @@ static void detectDeepinTerminal(FFTerminalFontResult* terminalFont) {
FF_STRBUF_AUTO_DESTROY profile = ffStrbufCreateA(64);
ffSearchUserConfigFile(&instance.state.platform.configDirs, "deepin/deepin-terminal/config.conf", &profile);
FILE* file = fopen(profile.chars, "r");
FF_AUTO_CLOSE_FILE FILE* file = fopen(profile.chars, "r");
if (file) {
char* line = nullptr;
FF_AUTO_FREE char* line = nullptr;
size_t len = 0;
for (int count = 0; getline(&line, &len, file) != -1 && count < 2;) {
@@ -188,9 +188,6 @@ static void detectDeepinTerminal(FFTerminalFontResult* terminalFont) {
++count;
}
}
free(line);
fclose(file);
}
if (fontName.length == 0) {