Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented non-blocking pipe and socketpair #101

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion newlib/libc/sys/vita/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AM_CCASFLAGS = $(INCLUDES)

noinst_LIBRARIES = lib.a

SOCKET_OBJS = accept.o bind.o connect.o getpeername.o getsockname.o getsockopt.o listen.o recv.o recvfrom.o recvmsg.o send.o sendto.o sendmsg.o setsockopt.o shutdown.o socket.o
SOCKET_OBJS = accept.o bind.o connect.o getpeername.o getsockname.o getsockopt.o listen.o recv.o recvfrom.o recvmsg.o send.o sendto.o sendmsg.o setsockopt.o shutdown.o socket.o socketpair.o
DIRENT_OBJS = dirfd.o closedir.o opendir.o readdir.o readdir_r.o rewinddir.o scandir.o seekdir.o telldir.o

NET_SOURCES = net/gethostbyaddr.c \
Expand Down
24 changes: 24 additions & 0 deletions newlib/libc/sys/vita/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ DEALINGS IN THE SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
#include <sys/syslimits.h>
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>

#include <psp2/types.h>
#include <psp2/kernel/threadmgr.h>
Expand Down Expand Up @@ -75,4 +77,26 @@ int pipe(int pipefd[2])
pipefd[1] = fd2;

return 0;
}


int pipe2(int pipefd[2], int flags) {
if (flags & O_NONBLOCK) {
if (socketpair(AF_INET, SOCK_STREAM, 0, pipefd) == -1) {
return -1;
}

int val = 1;
if (setsockopt(pipefd[0], SOL_SOCKET, SO_NONBLOCK, &val, sizeof(val)) == -1 ||
setsockopt(pipefd[1], SOL_SOCKET, SO_NONBLOCK, &val, sizeof(val)) == -1) {

close(pipefd[0]);
close(pipefd[1]);
return -1;
}

return 0;
} else {
return pipe(pipefd);
}
}
57 changes: 57 additions & 0 deletions newlib/libc/sys/vita/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ DEALINGS IN THE SOFTWARE.
#include <sys/time.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>

#include <psp2/net/net.h>
#include <psp2/types.h>
Expand Down Expand Up @@ -491,3 +493,58 @@ int socket(int domain, int type, int protocol)
return s;
}
#endif

#ifdef F_socket
int socketpair(int domain, int type, int protocol, int sockfds[2])
{
// Usually socketpair is used with AF_UNIX, simulate that with INET.
int listener;
if ((listener = socket(AF_INET, type, protocol)) == -1) {
return -1;
}

struct sockaddr_in server_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
server_addr.sin_port = 0;

if (bind(listener, (struct sockaddr*)&server_addr, addr_len) == -1) {
close(listener);
return -1;
}

if (listen(listener, 1) == -1) {
close(listener);
return -1;
}

if (getsockname(listener, (struct sockaddr *)&server_addr, &addr_len) == -1) {
close(listener);
return -1;
}

if ((sockfds[1] = socket(AF_INET, type, protocol)) == -1) {
close(listener);
return -1;
}

if (connect(sockfds[1], (struct sockaddr*)&server_addr, addr_len) == -1) {
close(sockfds[1]);
close(listener);
return -1;
}

if ((sockfds[0] = accept(listener, (struct sockaddr*)&server_addr, &addr_len)) == -1) {
close(sockfds[1]);
close(listener);
return -1;
}
Comment on lines +514 to +544
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikarh i love this pr.
just one question, don't we need to set errno?

maybe almost errors already set by these base calls.
if you confirm it, i'll merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this impl delegates to other newlib methods (connect, accept, socket), they already set errno, so this shouldn't be a problem


close(listener);

return 0;
}
#endif
1 change: 1 addition & 0 deletions newlib/libc/sys/vita/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
int setsockopt(int, int, int, const void *, socklen_t);
int shutdown(int, int);
int socket(int, int, int);
int socketpair(int, int, int, int *);

#ifdef __cplusplus
}
Expand Down
10 changes: 6 additions & 4 deletions newlib/libc/sys/vita/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ _write_r(struct _reent * reent, int fd, const void *buf, size_t nbytes)
{
size_t len = nbytes;
if (len > 4 * 4096) len = 4 * 4096;
ret = sceKernelSendMsgPipe(fdmap->sce_uid, buf, len, 1, NULL, NULL);
if (ret == 0) ret = len;
size_t p_res = 0;
ret = sceKernelSendMsgPipe(fdmap->sce_uid, buf, len, 1, &p_res, NULL);
if (ret == 0) ret = p_res;
break;
}
}
Expand Down Expand Up @@ -336,8 +337,9 @@ _read_r(struct _reent *reent, int fd, void *ptr, size_t len)
{
size_t rlen = len;
if (rlen > 4 * 4096) rlen = 4 * 4096;
ret = sceKernelReceiveMsgPipe(fdmap->sce_uid, ptr, rlen, 1, NULL, NULL);
if (ret == 0) ret = rlen;
size_t p_res = 0;
ret = sceKernelReceiveMsgPipe(fdmap->sce_uid, ptr, rlen, 1, &p_res, NULL);
if (ret == 0) ret = p_res;
break;
}
}
Expand Down