2022-04-13 11:00:35 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: UnixIO.cpp
|
|
|
|
Date: 2022-4-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "UnixIO.hpp"
|
2022-04-14 16:13:19 +00:00
|
|
|
#include "FDIpcServer.hpp"
|
2022-04-13 11:00:35 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::UNIX
|
|
|
|
{
|
2022-04-14 16:13:19 +00:00
|
|
|
AUKN_SYM AuString ShareFileDescriptor(int fd)
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
2022-04-14 16:13:19 +00:00
|
|
|
IPC::IPCHandle handle;
|
2022-04-13 11:00:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!FDServe(fd, token))
|
2022-04-14 16:13:19 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2022-04-13 11:00:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
handle.PushId(IPC::EIPCHandleType::eIPCSharedFd, token);
|
|
|
|
|
2022-04-14 16:13:19 +00:00
|
|
|
return handle.ToString();
|
|
|
|
}
|
2022-04-13 11:00:35 +00:00
|
|
|
|
2022-04-14 16:13:19 +00:00
|
|
|
AUKN_SYM void ShareFileDescriptorStop(const AuString &handle)
|
|
|
|
{
|
|
|
|
IPC::IPCHandle handle2;
|
|
|
|
if (!handle2.FromString(handle))
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Invalid handle string");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-13 11:00:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
if (auto val = handle2.GetToken(IPC::EIPCHandleType::eIPCSharedFd, 0))
|
|
|
|
{
|
|
|
|
FDServeEnd(val->token);
|
|
|
|
}
|
2022-04-14 16:13:19 +00:00
|
|
|
}
|
2022-04-13 11:00:35 +00:00
|
|
|
|
2022-04-14 16:13:19 +00:00
|
|
|
AUKN_SYM int ShareFileDescriptorAccept(const AuString &handle)
|
|
|
|
{
|
|
|
|
IPC::IPCHandle handle2;
|
|
|
|
if (!handle2.FromString(handle))
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Invalid handle string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
auto val = handle2.GetToken(IPC::EIPCHandleType::eIPCSharedFd, 0);
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:13:19 +00:00
|
|
|
int fd {-1};
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!FDAccept(val->token, fd))
|
2022-04-14 16:13:19 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitUnixIO()
|
|
|
|
{
|
|
|
|
InitIPCBackend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitUnixIO()
|
2022-04-13 11:00:35 +00:00
|
|
|
{
|
2022-04-14 16:13:19 +00:00
|
|
|
DeinitIPCBackend();
|
2022-04-13 11:00:35 +00:00
|
|
|
}
|
|
|
|
}
|