This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/sysmodules/sm/source/processes.c
TuxSH 1572bfd989 [sm] Fix process unregistering logic bug (fixes #984)
In this case pm+sm were supposed to automatically unregister ro's services (the kernel can close a dying process's handle automatically). Not doing this apparently induced a reference leak of DevMenu's as a process, preventing it to be destroyed and the memory allocation reslimit counter to be updated by KProcess's destructor accordingly.
2017-12-27 19:30:57 +01:00

76 lines
2.0 KiB
C

/*
processes.c
(c) TuxSH, 2017
This is part of 3ds_sm, which is licensed under the MIT license (see LICENSE for details).
*/
#include "list.h"
#include "processes.h"
#include "services.h"
ProcessDataList processDataInUseList = { NULL, NULL }, freeProcessDataList = { NULL, NULL };
ProcessData *findProcessData(u32 pid)
{
for(ProcessData *node = processDataInUseList.first; node != NULL; node = node->next)
{
if(node->pid == pid)
return node;
}
return NULL;
}
ProcessData *doRegisterProcess(u32 pid, char (*serviceAccessList)[8], u32 serviceAccessListSize)
{
(void)serviceAccessList; // Service access list checks removed for Luma3DS, see original 3ds_sm for implementation details.
(void)serviceAccessListSize;
ProcessData *processData = (ProcessData *)allocateNode(&processDataInUseList, &freeProcessDataList, sizeof(ProcessData), false);
assertSuccess(svcCreateSemaphore(&processData->notificationSemaphore, 0, 0x10));
processData->pid = pid;
return processData;
}
Result RegisterProcess(u32 pid, char (*serviceAccessList)[8], u32 serviceAccessListSize)
{
serviceAccessListSize = serviceAccessListSize - (serviceAccessListSize % 8);
if(findProcessData(pid) != NULL)
return 0xD9006403;
else if(serviceAccessListSize > 8 * (IS_PRE_93 ? 32 : 34))
return 0xD9006405;
else
{
doRegisterProcess(pid, serviceAccessList, serviceAccessListSize);
return 0;
}
}
Result UnregisterProcess(u32 pid)
{
ProcessData *processData = findProcessData(pid);
if(processData == NULL)
return 0xD8806404;
svcCloseHandle(processData->notificationSemaphore);
// Unregister the services registered by the process
u32 i = 0;
while(i < nbServices)
{
if(servicesInfo[i].pid == pid)
{
svcCloseHandle(servicesInfo[i].clientPort);
servicesInfo[i] = servicesInfo[--nbServices];
}
else
++i;
}
moveNode(processData, &freeProcessDataList, false);
return 0;
}