Xbox Cloud Gaming app for PS5 Homebrew
C
fix(storage, log): harden data dir permissions, in-memory log buffer and issue template
Rafael Abud Ferreiracbdb9f90c941
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex 509a2d8..31e190e 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -6,7 +6,8 @@ body:attributes:value: |Thanks for testing! The log is the most useful thing you can send:- it is at `/data/homebrew/PPSA99810/xcloud.log` on the PS5 (copy it over FTP).+ it is at `/data/psbox/xcloud.log` on the PS5 (copy it over FTP;+ before v0.9.7 it was at `/data/homebrew/PPSA99810/xcloud.log`).Recent versions keep nothing that identifies you in it (no sign-incode, gamertag or PS5 user names); still, have a look before posting it.@@ -53,7 +54,7 @@ body:id: logattributes:label: xcloud.log- description: Paste the full contents of /data/homebrew/PPSA99810/xcloud.log (or attach the file).+ description: Paste the full contents of /data/psbox/xcloud.log (or /data/homebrew/PPSA99810/xcloud.log before v0.9.7, or attach the file).render: textvalidations:required: truediff --git a/src/app/app_core.cpp b/src/app/app_core.cppindex e5a1fb6..9d03ae7 100644--- a/src/app/app_core.cpp+++ b/src/app/app_core.cpp@@ -57,11 +57,20 @@ std::string crashMarkerPath() { return platform::dataDir() + "/crash.marker"; }void openReport(const std::string& what, bool previousSession, const std::string& heading, const std::string& crash = {}) {std::string logText;platform::readFile(platform::dataDir() + "/xcloud.log", logText);+ if (logText.empty()) {+ logText = log::recentLogs();+ }std::vector<std::string> privateWords = {g_ui->gamertag()};for (int i = 0; i < input::kMaxPads; ++i) privateWords.push_back(input::padUserName(i));for (auto& name : g_ui->consoleNames()) privateWords.push_back(std::move(name));report::Info info;- info.what = report::sanitize(what.empty() ? "Reported from Settings" : what, privateWords);+ std::string whatText = what.empty() ? "Reported from Settings" : what;+ if (!platform::dataDirWritable() || !platform::dataDirNote().empty()) {+ whatText += " [storage=" + platform::dataDir() + " writable=" + (platform::dataDirWritable() ? "yes" : "no");+ if (!platform::dataDirNote().empty()) whatText += " note=" + platform::dataDirNote();+ whatText += "]";+ }+ info.what = report::sanitize(whatText, privateWords);info.title = what.empty() ? "Problem report" : report::shorten(info.what, 70);info.release = XC_APP_VERSION;info.firmware = platform::firmwareVersion();@@ -239,9 +248,11 @@ ui::UiCaps uiCaps() {bool coreStartup() {platform::init();platform::setThreadAffinity(platform::ThreadRole::Main, "xcloud_main");- log::setFile((platform::dataDir() + "/xcloud.log").c_str());+ bool logOk = log::setFile((platform::dataDir() + "/xcloud.log").c_str());if (!platform::dataDirNote().empty())XC_LOGI("data folder %s: %s", platform::dataDir().c_str(), platform::dataDirNote().c_str());+ if (!logOk)+ XC_LOGW("could not open xcloud.log in %s: log kept in memory", platform::dataDir().c_str());XC_LOGI("=== PSBox Cloud Gaming starting ===");XC_LOGI("firmware %s, PSBox %s", platform::firmwareVersion().c_str(), XC_APP_VERSION);// Did the last session go down? (A crash leaves a marker; being closeddiff --git a/src/app/worker.cpp b/src/app/worker.cppindex d625798..01df2b9 100644--- a/src/app/worker.cpp+++ b/src/app/worker.cpp@@ -583,6 +583,11 @@ std::string g_subscription;} // namespacevoid signInAndLoad(auth::AuthManager& am, xcloud::GssvClient& gssv) {+ if (!platform::dataDirWritable()) {+ std::string pathAndErr = platform::dataDir() + " (errno " + std::to_string(platform::dataDirErrno()) + ")";+ platform::notify(ui::trf(ui::Str::StorageNotWritable, pathAndErr),+ "storage not writable (notification)");+ }g_ui->showSplash(ui::tr(am.hasStoredAccount() ? ui::Str::SigningIn : ui::Str::RequestingCode));std::string err;auto onCode = [](const auth::DeviceCode& dc) { g_ui->showSignIn(dc.userCode, dc.verificationUri); };diff --git a/src/input/controller.h b/src/input/controller.hindex 72fa9a8..b73d155 100644--- a/src/input/controller.h+++ b/src/input/controller.h@@ -82,6 +82,8 @@ std::string padDeviceName(int index);void setRumble(uint8_t large, uint8_t small, uint32_t durationMs, int pad = 0);// Sub-bass audio-to-haptics pulse (when no active game rumble is running)void setAudioHaptics(uint8_t kick, uint32_t durationMs = 60, int pad = 0);+void setAudioHapticsEnabled(bool on);+bool audioHapticsEnabled();// Vibration in the triggers (Xbox impulse triggers), 0..255 each; the// DualSense's adaptive triggers vibrate along their whole travel.void setTriggerRumble(uint8_t left, uint8_t right, uint32_t durationMs, int pad = 0);diff --git a/src/input/controller_common.cpp b/src/input/controller_common.cppindex d606bdc..cf7b921 100644--- a/src/input/controller_common.cpp+++ b/src/input/controller_common.cpp@@ -194,7 +194,13 @@ void setRumble(uint8_t large, uint8_t small, uint32_t durationMs, int pad) {if (pad >= 0 && pad < kMaxPads) g_pads[pad].motors.set(large, small, durationMs);}+std::atomic<bool> g_audioHapticsEnabled{false};++void setAudioHapticsEnabled(bool on) { g_audioHapticsEnabled.store(on, std::memory_order_relaxed); }+bool audioHapticsEnabled() { return g_audioHapticsEnabled.load(std::memory_order_relaxed); }+void setAudioHaptics(uint8_t kick, uint32_t durationMs, int pad) {+ if (!g_audioHapticsEnabled.load(std::memory_order_relaxed)) return;if (pad >= 0 && pad < kMaxPads && kick > 0) {Pad& p = g_pads[pad];if (p.motors.current() == 0) {diff --git a/src/media/audio_out_ps5.cpp b/src/media/audio_out_ps5.cppindex 6a98031..965fefd 100644--- a/src/media/audio_out_ps5.cpp+++ b/src/media/audio_out_ps5.cpp@@ -50,16 +50,18 @@ void loop() {}// Audio-to-Haptics: detect deep sub-bass impacts (cutoff ~100 Hz at 48 kHz)- float energy = 0.0f;- for (unsigned i = 0; i < kGrain; ++i) {- float mono = 0.5f * (grain[i * 2] + grain[i * 2 + 1]);- lowPass += 0.013f * (mono - lowPass);- energy += lowPass * lowPass;- }- energy /= static_cast<float>(kGrain);- if (energy > 0.06f) {- uint8_t kick = static_cast<uint8_t>(std::clamp((energy - 0.06f) * 1400.0f, 40.0f, 180.0f));- input::setAudioHaptics(kick, 60);+ if (input::audioHapticsEnabled()) {+ float energy = 0.0f;+ for (unsigned i = 0; i < kGrain; ++i) {+ float mono = 0.5f * (grain[i * 2] + grain[i * 2 + 1]);+ lowPass += 0.013f * (mono - lowPass);+ energy += lowPass * lowPass;+ }+ energy /= static_cast<float>(kGrain);+ if (energy > 0.06f) {+ uint8_t kick = static_cast<uint8_t>(std::clamp((energy - 0.06f) * 1400.0f, 40.0f, 180.0f));+ input::setAudioHaptics(kick, 60);+ }}// Blocks until the hardware wants the next grain (5.3 ms).diff --git a/src/platform/linux/platform_linux.cpp b/src/platform/linux/platform_linux.cppindex be09933..cf29d21 100644--- a/src/platform/linux/platform_linux.cpp+++ b/src/platform/linux/platform_linux.cpp@@ -91,6 +91,8 @@ void shutdown() {}std::string dataDir() { return g_dataDir; }std::string dataDirNote() { return g_dataDirNote; }+bool dataDirWritable() { return true; }+int dataDirErrno() { return 0; }// Where the executable is (an update would go there): never the data folder.std::string appDir() { return exeDir().empty() ? std::string(".") : exeDir(); }diff --git a/src/platform/platform.h b/src/platform/platform.hindex b27ad2f..887de1b 100644--- a/src/platform/platform.h+++ b/src/platform/platform.h@@ -24,6 +24,10 @@ std::string dataDir();// What start-up did about the data folder (moved from the app's folder,// fell back...), for the log once it is open; empty when nothing.std::string dataDirNote();+// Whether the data folder is writable (false when permission or filesystem error occurred).+bool dataDirWritable();+// If dataDirWritable() is false, the errno from the write probe.+int dataDirErrno();// The folder the app runs from (updates are installed there).std::string appDir();// Read-only directory with the bundled assets (fonts, certificates).diff --git a/src/platform/ps5/platform_ps5.cpp b/src/platform/ps5/platform_ps5.cppindex 1cbe92c..fb3f6c0 100644--- a/src/platform/ps5/platform_ps5.cpp+++ b/src/platform/ps5/platform_ps5.cpp@@ -48,15 +48,29 @@ constexpr const char* kDataDir = "/data/psbox";constexpr const char* kLegacyDir = "/data/homebrew/PPSA99810";std::string g_dataDir = kDataDir;std::string g_dataNote;+bool g_dataDirWritable = false;+int g_dataDirErrno = 0;std::atomic<int> g_netPool{-1};}// True when files can be made in `dir` (made first if missing).-bool writable(const std::string& dir) {+bool writable(const std::string& dir, int& errOut) {+ errOut = 0;struct stat st{};- if (::stat(dir.c_str(), &st) != 0 && ::mkdir(dir.c_str(), 0777) != 0) return false;+ if (::stat(dir.c_str(), &st) != 0) {+ if (::mkdir(dir.c_str(), 0777) != 0) {+ errOut = errno;+ return false;+ }+ } else {+ // Folder exists: ensure permissions allow writing by uid 1+ ::chmod(dir.c_str(), 0777);+ }std::string probe = dir + "/.write-test";- if (!writeFileAtomic(probe, "ok")) return false;+ if (!writeFileAtomic(probe, "ok")) {+ errOut = errno;+ return false;+ }::unlink(probe.c_str());return true;}@@ -81,14 +95,37 @@ void migrateLegacy() {}bool init() {- if (writable(kDataDir)) {+ int errPsbox = 0;+ int errLegacy = 0;+ if (writable(kDataDir, errPsbox)) {+ g_dataDirWritable = true;migrateLegacy();- } else if (writable(kLegacyDir)) { // as before 0.9.7- int err = errno;+ } else if (writable(kLegacyDir, errLegacy)) { // as before 0.9.7+ g_dataDirWritable = true;g_dataDir = kLegacyDir;- g_dataNote = std::string(kDataDir) + " not writable (errno " + std::to_string(err) + "); using " + kLegacyDir;+ g_dataNote = std::string(kDataDir) + " not writable (errno " + std::to_string(errPsbox) + "); using " + kLegacyDir;} else {- g_dataNote = std::string("neither ") + kDataDir + " nor " + kLegacyDir + " is writable: nothing will be kept";+ // Try fallback directories on PS5 sandbox+ bool found = false;+ for (const char* fb : {"/download0/psbox", "/user/home/psbox", "/temp0/psbox"}) {+ int errFb = 0;+ if (writable(fb, errFb)) {+ g_dataDir = fb;+ g_dataDirWritable = true;+ g_dataNote = std::string("neither ") + kDataDir + " (errno " + std::to_string(errPsbox) ++ ") nor " + kLegacyDir + " (errno " + std::to_string(errLegacy) ++ ") writable; fallback to " + fb;+ found = true;+ break;+ }+ }+ if (!found) {+ g_dataDirErrno = errPsbox ? errPsbox : errLegacy;+ g_dataDirWritable = false;+ g_dataNote = std::string("neither ") + kDataDir + " (errno " + std::to_string(errPsbox) ++ ") nor " + kLegacyDir + " (errno " + std::to_string(errLegacy) ++ ") is writable: nothing will be kept";+ }}// libSceNet's resolver: 32 KiB pool.int rc = sceNetInit();@@ -106,6 +143,8 @@ std::string dataDir() {}std::string dataDirNote() { return g_dataNote; }+bool dataDirWritable() { return g_dataDirWritable; }+int dataDirErrno() { return g_dataDirErrno; }std::string appDir() {// /app0 is the app's own folder; its real path where it has beendiff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cppindex 9d99c32..1b711d2 100644--- a/src/ui/app_ui.cpp+++ b/src/ui/app_ui.cpp@@ -8,6 +8,7 @@#include "xcloud/catalog.h"#include "ui/accent_color.h"+#include "platform/platform.h"#include "platform/toasts.h"#include "ui/brand.h"#include "ui/strings.h"@@ -2949,6 +2950,14 @@ void AppUi::drawSignIn(Canvas& c, uint64_t nowMs) {drawSpinner(c, left + 18, 830, 14, nowMs);fonts_.regular.draw(c, tr(Str::SignInWaiting), left + 48, 815, 26, kDim);+ if (!platform::dataDirWritable()) {+ std::string pathAndErr = platform::dataDir() + " (errno " + std::to_string(platform::dataDirErrno()) + ")";+ std::string warn1 = trf(Str::StorageNotWritable, pathAndErr);+ std::string warn2 = trf(Str::StorageFixHint, platform::dataDir());+ fonts_.semibold.draw(c, warn1, left, 875, 22, rgba(255, 160, 40));+ fonts_.regular.draw(c, warn2, left, 905, 20, kGray);+ }+// QR code for the sign-in page. Not "?otc=<code>": that prefilled link// goes through a consent flow that rejects this client id ("first party// application ... pre-authorization"), so the code is typed on the phone.diff --git a/src/ui/strings.cpp b/src/ui/strings.cppindex 4775c05..4fef8e7 100644--- a/src/ui/strings.cpp+++ b/src/ui/strings.cpp@@ -321,6 +321,8 @@ constexpr Column kEnglish = {"Standard", // TriggerProfileStd"Shooter (Hair Trigger)", // TriggerProfileShooter"Racing (ABS)", // TriggerProfileRacing+ "Storage %s is not writable: sign-in and settings will not be saved", // StorageNotWritable+ "FTP: run 'chmod 777 %s' or delete the folder and relaunch", // StorageFixHint};constexpr Column kPortugueseBR = {@@ -612,6 +614,8 @@ constexpr Column kPortugueseBR = {"Padrão", // TriggerProfileStd"Tiroteio (Hair Trigger)", // TriggerProfileShooter"Corrida (ABS)", // TriggerProfileRacing+ "Armazenamento %s não permite gravação: login e ajustes não serão salvos", // StorageNotWritable+ "FTP: use 'chmod 777 %s' ou exclua a pasta e reabra o app", // StorageFixHint};constexpr Column kSpanish = {@@ -903,6 +907,8 @@ constexpr Column kSpanish = {"Estándar", // TriggerProfileStd"Disparos (Hair Trigger)", // TriggerProfileShooter"Carreras (ABS)", // TriggerProfileRacing+ "El almacenamiento %s no permite escritura: la sesión y ajustes no se guardarán", // StorageNotWritable+ "FTP: usa 'chmod 777 %s' o borra la carpeta y reinicia la app", // StorageFixHint};constexpr Column kFrench = {@@ -1194,6 +1200,8 @@ constexpr Column kFrench = {"Standard", // TriggerProfileStd"Tir (Hair Trigger)", // TriggerProfileShooter"Course (ABS)", // TriggerProfileRacing+ "Le stockage %s n'est pas accessible en écriture : connexion et réglages non enregistrés", // StorageNotWritable+ "FTP : faites 'chmod 777 %s' ou supprimez le dossier et relancez", // StorageFixHint};constexpr Column kGerman = {@@ -1485,6 +1493,8 @@ constexpr Column kGerman = {"Standard", // TriggerProfileStd"Shooter (Hair Trigger)", // TriggerProfileShooter"Rennen (ABS)", // TriggerProfileRacing+ "Speicher %s ist schreibgeschützt: Anmeldung und Einstellungen werden nicht gespeichert", // StorageNotWritable+ "FTP: 'chmod 777 %s' ausführen oder Ordner löschen und neu starten", // StorageFixHint};constexpr Column kItalian = {@@ -1776,6 +1786,8 @@ constexpr Column kItalian = {"Standard", // TriggerProfileStd"Sparatutto (Hair Trigger)", // TriggerProfileShooter"Corsa (ABS)", // TriggerProfileRacing+ "La memoria %s non è scrivibile: accesso e impostazioni non verranno salvati", // StorageNotWritable+ "FTP: esegui 'chmod 777 %s' o elimina la cartella e riavvia", // StorageFixHint};constexpr std::array<const Column*, kLangs> kTable = {&kEnglish, &kPortugueseBR, &kSpanish,diff --git a/src/ui/strings.h b/src/ui/strings.hindex 18b7cab..b3a75b5 100644--- a/src/ui/strings.h+++ b/src/ui/strings.h@@ -298,6 +298,8 @@ enum class Str {TriggerProfileStd, // StandardTriggerProfileShooter, // Shooter (Hair Trigger)TriggerProfileRacing, // Racing (ABS)+ StorageNotWritable, // sign-in / notification: storage not writable (%s: path, %d: errno)+ StorageFixHint, // sign-in: hint to fix folder permissions (%s: path)Count};diff --git a/src/util/log.cpp b/src/util/log.cppindex 6f93ab6..70db4bf 100644--- a/src/util/log.cpp+++ b/src/util/log.cpp@@ -6,6 +6,7 @@#include <cstdio>#include <ctime>#include <mutex>+#include <string>namespace xc::log {@@ -13,6 +14,8 @@ namespace {std::mutex g_mutex;std::atomic<Level> g_level{Level::Info};FILE* g_file = nullptr;+std::string g_recentLogs;+constexpr size_t kMaxRecentLogs = 64 * 1024; // 64 KiB ring bufferconst char* tag(Level l) {switch (l) {@@ -27,10 +30,20 @@ const char* tag(Level l) {void setLevel(Level level) { g_level = level; }-void setFile(const char* path) {+bool setFile(const char* path) {std::lock_guard<std::mutex> lock(g_mutex);- if (g_file) std::fclose(g_file);- g_file = path ? std::fopen(path, "a") : nullptr;+ if (g_file) {+ std::fclose(g_file);+ g_file = nullptr;+ }+ if (!path) return true;+ g_file = std::fopen(path, "a");+ return g_file != nullptr;+}++std::string recentLogs() {+ std::lock_guard<std::mutex> lock(g_mutex);+ return g_recentLogs;}void write(Level level, const char* fmt, ...) {@@ -47,12 +60,27 @@ void write(Level level, const char* fmt, ...) {char ts[32];std::strftime(ts, sizeof ts, "%H:%M:%S", &local);+ char line[2304];+ int lineLen = std::snprintf(line, sizeof line, "%s [%s] %s\n", ts, tag(level), msg);+std::lock_guard<std::mutex> lock(g_mutex);- std::fprintf(stderr, "%s [%s] %s\n", ts, tag(level), msg);+ std::fprintf(stderr, "%s", line);if (g_file) {- std::fprintf(g_file, "%s [%s] %s\n", ts, tag(level), msg);+ std::fputs(line, g_file);std::fflush(g_file);}+ if (lineLen > 0) {+ g_recentLogs.append(line, static_cast<size_t>(lineLen));+ if (g_recentLogs.size() > kMaxRecentLogs) {+ size_t excess = g_recentLogs.size() - kMaxRecentLogs;+ size_t nextNl = g_recentLogs.find('\n', excess);+ if (nextNl != std::string::npos && nextNl + 1 < g_recentLogs.size()) {+ g_recentLogs.erase(0, nextNl + 1);+ } else {+ g_recentLogs.erase(0, excess);+ }+ }+ }}} // namespace xc::logdiff --git a/src/util/log.h b/src/util/log.hindex de7e293..5a886ed 100644--- a/src/util/log.h+++ b/src/util/log.h@@ -3,6 +3,7 @@#pragma once#include <cstdarg>+#include <string>namespace xc::log {@@ -10,7 +11,11 @@ enum class Level { Debug, Info, Warn, Error };void setLevel(Level level);// Also appends every line to this file (created if missing). nullptr disables.-void setFile(const char* path);+// Returns true on success (or if path is nullptr), false if fopen failed.+bool setFile(const char* path);++// Returns recent log lines kept in memory (up to 64 KiB).+std::string recentLogs();void write(Level level, const char* fmt, ...) __attribute__((format(printf, 2, 3)));