Skip to content

Commit 730e2a2

Browse files
committed
make SOC thread-safe
1 parent 36fe1ad commit 730e2a2

28 files changed

+201
-47
lines changed

libctru/include/3ds/services/soc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
* After initializing this service you will be able to use system calls from netdb.h, sys/socket.h etc.
66
*/
77
#pragma once
8+
#include <3ds/types.h>
89
#include <netinet/in.h>
910
#include <sys/socket.h>
1011

1112
/// The config level to be used with @ref SOCU_GetNetworkOpt
1213
#define SOL_CONFIG 0xfffe
1314

15+
/// Default number of SOC sessions to initialize
16+
#define SOC_DEFAULT_NUM_SESSIONS 3
17+
1418
/// Options to be used with @ref SOCU_GetNetworkOpt
1519
typedef enum
1620
{
@@ -100,9 +104,10 @@ typedef struct
100104
* @brief Initializes the SOC service.
101105
* @param context_addr Address of a page-aligned (0x1000) buffer to be used.
102106
* @param context_size Size of the buffer, a multiple of 0x1000.
107+
* @param num_sessions The number of sessions to initialize. This value determines how many socket syscalls can be run concurrently. Setting it to 1 means only one thread can safely use socket syscalls.
103108
* @note The specified context buffer can no longer be accessed by the process which called this function, since the userland permissions for this block are set to no-access.
104109
*/
105-
Result socInit(u32 *context_addr, u32 context_size);
110+
Result socInit(u32 *context_addr, u32 context_size, u32 num_sessions);
106111

107112
/**
108113
* @brief Closes the soc service.

libctru/source/services/soc/soc_accept.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
4141
staticbufs[0] = IPC_Desc_StaticBuffer(tmp_addrlen,0);
4242
staticbufs[1] = (u32)tmpaddr;
4343

44-
ret = svcSendSyncRequest(SOCU_handle);
44+
ret = socSendSyncRequest();
4545

4646
staticbufs[0] = saved_threadstorage[0];
4747
staticbufs[1] = saved_threadstorage[1];

libctru/source/services/soc/soc_addglobalsocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int SOCU_AddGlobalSocket(int sockfd)
1515
cmdbuf[0] = IPC_MakeHeader(0x23,1,0); // 0x230040
1616
cmdbuf[1] = (u32)sockfd;
1717

18-
int ret = svcSendSyncRequest(SOCU_handle);
18+
int ret = socSendSyncRequest();
1919
if(R_FAILED(ret))return ret;
2020
return cmdbuf[1];
2121
}

libctru/source/services/soc/soc_bind.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
3939
cmdbuf[5] = IPC_Desc_StaticBuffer(tmp_addrlen,0);
4040
cmdbuf[6] = (u32)tmpaddr;
4141

42-
ret = svcSendSyncRequest(SOCU_handle);
42+
ret = socSendSyncRequest();
4343
if(ret != 0) {
4444
errno = SYNC_ERROR;
4545
return ret;

libctru/source/services/soc/soc_closesockets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int SOCU_CloseSockets(void)
99
cmdbuf[0] = IPC_MakeHeader(0x21,0,2); // 0x210002;
1010
cmdbuf[1] = IPC_Desc_CurProcessId();
1111

12-
int ret = svcSendSyncRequest(SOCU_handle);
12+
int ret = socSendSyncRequest();
1313
if(R_FAILED(ret))return ret;
1414
return cmdbuf[1];
1515
}

libctru/source/services/soc/soc_common.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#include "soc_common.h"
2-
#include <errno.h>
31
#include <sys/iosupport.h>
2+
#include <errno.h>
3+
#include "soc_common.h"
44

5-
Handle SOCU_handle = 0;
6-
Handle socMemhandle = 0;
75
int h_errno = 0;
86

97
//This is based on the array from libogc network_wii.c.

libctru/source/services/soc/soc_common.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <errno.h>
43
#include <string.h>
4+
#include <sys/errno.h>
55
#include <sys/iosupport.h>
66
#include <sys/socket.h>
77
#include <3ds/types.h>
@@ -12,8 +12,7 @@
1212
#define SYNC_ERROR ENODEV
1313
#define ADDR_STORAGE_LEN sizeof(struct sockaddr_storage)
1414

15-
extern Handle SOCU_handle;
16-
extern Handle socMemhandle;
15+
Result socSendSyncRequest();
1716

1817
static inline int
1918
soc_get_fd(int fd)

libctru/source/services/soc/soc_connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
3939
cmdbuf[5] = IPC_Desc_StaticBuffer(tmp_addrlen,0);
4040
cmdbuf[6] = (u32)tmpaddr;
4141

42-
ret = svcSendSyncRequest(SOCU_handle);
42+
ret = socSendSyncRequest();
4343
if(ret != 0) {
4444
errno = SYNC_ERROR;
4545
return ret;

libctru/source/services/soc/soc_fcntl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int fcntl(int sockfd, int cmd, ...)
7070
cmdbuf[3] = (u32)arg;
7171
cmdbuf[4] = IPC_Desc_CurProcessId();
7272

73-
ret = svcSendSyncRequest(SOCU_handle);
73+
ret = socSendSyncRequest();
7474
if(ret != 0) {
7575
errno = SYNC_ERROR;
7676
return ret;

libctru/source/services/soc/soc_getaddrinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int getaddrinfo_detail(const char *node, const char *service, const struc
9999
staticbufs[0] = IPC_Desc_StaticBuffer(sizeof(addrinfo_3ds_t) * info_count, 0);
100100
staticbufs[1] = (u32)info;
101101

102-
int ret = svcSendSyncRequest(SOCU_handle);
102+
int ret = socSendSyncRequest();
103103

104104
// Restore the thread storage values
105105
for(i = 0 ; i < 2 ; ++i)

0 commit comments

Comments
 (0)