From b99729142b8853576dd033b0a25212e827184b95 Mon Sep 17 00:00:00 2001 From: shadowninja108 Date: Sun, 3 Jun 2018 14:44:28 -0700 Subject: [PATCH] Fixed IGeneralService leak IGeneralService is now cached, removing the necessity for it to be closed by user (as long as they close nifm) --- nx/include/switch/services/nifm.h | 3 +- nx/source/runtime/devices/socket.c | 13 +++------ nx/source/services/nifm.c | 44 ++++++++++++++++++------------ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/nx/include/switch/services/nifm.h b/nx/include/switch/services/nifm.h index 5920ef7c..9160175b 100644 --- a/nx/include/switch/services/nifm.h +++ b/nx/include/switch/services/nifm.h @@ -19,8 +19,7 @@ typedef struct { Result nifmInitialize(); void nifmExit(void); -Result CreateGeneralService(IGeneralService*out); Result _CreateGeneralService(IGeneralService* out, u64 in); Result _CreateGeneralServiceOld(IGeneralService* out); -Result GetCurrentIpAddress(IGeneralService* srv, u32* out); \ No newline at end of file +Result GetCurrentIpAddress(u32* out); \ No newline at end of file diff --git a/nx/source/runtime/devices/socket.c b/nx/source/runtime/devices/socket.c index d88a445f..54de1b01 100644 --- a/nx/source/runtime/devices/socket.c +++ b/nx/source/runtime/devices/socket.c @@ -1584,15 +1584,10 @@ cleanup: long gethostid(void) { Result rc; - IGeneralService igs; - rc = CreateGeneralService(&igs); - if(R_SUCCEEDED(rc)){ - u32 id; - rc = GetCurrentIpAddress(&igs, &id); - if(R_SUCCEEDED(rc)){ - return id; - } - } + u32 id; + rc = GetCurrentIpAddress(&id); + if(R_SUCCEEDED(rc)) + return id; return INADDR_LOOPBACK; } diff --git a/nx/source/services/nifm.c b/nx/source/services/nifm.c index 29e625b3..319a3411 100644 --- a/nx/source/services/nifm.c +++ b/nx/source/services/nifm.c @@ -7,33 +7,44 @@ #include "services/nifm.h" static Service g_nifmSrv; +static IGeneralService g_nifmIGS; Result nifmInitialize(){ if (serviceIsActive(&g_nifmSrv)) return 0; - return smGetService(&g_nifmSrv, "nifm:u"); + Result rc; + + rc = smGetService(&g_nifmSrv, "nifm:u"); + + if(R_SUCCEEDED(rc)){ + if(kernelAbove200()) + rc = _CreateGeneralService(&g_nifmIGS, 0); // What does this parameter do? + else + rc = _CreateGeneralServiceOld(&g_nifmIGS); + } + + if(R_FAILED(rc)) + nifmExit(); + + return rc; } void nifmExit(void){ - serviceClose(&g_nifmSrv); -} - -Result CreateGeneralService(IGeneralService *out){ - if(kernelAbove200()) - return _CreateGeneralService(out, 0); // What does this parameter do? - else - return _CreateGeneralServiceOld(out); + if(serviceIsActive(&g_nifmIGS.s)) + serviceClose(&g_nifmIGS.s); + if(serviceIsActive(&g_nifmSrv)) + serviceClose(&g_nifmSrv); } Result _CreateGeneralService(IGeneralService* out, u64 in){ IpcCommand c; - ipcInitialize(&c); - ipcSendPid(&c); + ipcInitialize(&c); + ipcSendPid(&c); struct { u64 magic; u64 cmd_id; - u64 param; + u64 param; } PACKED *raw; raw = ipcPrepareHeader(&c, sizeof(*raw)); @@ -55,9 +66,8 @@ Result _CreateGeneralService(IGeneralService* out, u64 in){ rc = resp->result; - if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); - } + if (R_SUCCEEDED(rc)) + serviceCreate(&out->s, r.Handles[0]); } return rc; @@ -98,7 +108,7 @@ Result _CreateGeneralServiceOld(IGeneralService* out){ return rc; } -Result GetCurrentIpAddress(IGeneralService* srv, u32* out){ +Result GetCurrentIpAddress(u32* out){ IpcCommand c; ipcInitialize(&c); @@ -112,7 +122,7 @@ Result GetCurrentIpAddress(IGeneralService* srv, u32* out){ raw->magic = SFCI_MAGIC; raw->cmd_id = 12; - Result rc = serviceIpcDispatch(&srv->s); + Result rc = serviceIpcDispatch(&g_nifmIGS.s); if (R_SUCCEEDED(rc)) {