0.3.0: converge relibc to upstream 0.6.0 + Red Bear patches
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
void print_sockaddr(char *ctx, struct sockaddr *addr) {
|
||||
char ip_string[INET6_ADDRSTRLEN];
|
||||
void *raw_ip_addr;
|
||||
char *fam;
|
||||
in_port_t port;
|
||||
|
||||
if (addr->sa_family == AF_INET) {
|
||||
struct sockaddr_in *ipv4 = (struct sockaddr_in *)addr;
|
||||
raw_ip_addr = &(ipv4->sin_addr);
|
||||
port = ntohs(ipv4->sin_port);
|
||||
fam = "AF_INET";
|
||||
} else if (addr->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)addr;
|
||||
raw_ip_addr = &(ipv6->sin6_addr);
|
||||
port = ntohs(ipv6->sin6_port);
|
||||
fam = "AF_INET6";
|
||||
} else {
|
||||
printf("Unknown address family: %d\n", addr->sa_family);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (inet_ntop(addr->sa_family, raw_ip_addr, ip_string, sizeof(ip_string)) == NULL) {
|
||||
perror("inet_ntop failed");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("%s [%s]: %s:%u\n", ctx, fam, ip_string, port);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, listen_fd, == -1);
|
||||
|
||||
struct sockaddr_in addr =
|
||||
{
|
||||
.sin_family = AF_INET,
|
||||
.sin_addr = { .s_addr = htonl(0x7F000001 /* 127.0.0.1 */) },
|
||||
.sin_port = htons(0),
|
||||
};
|
||||
struct sockaddr* saddr = (struct sockaddr*)&addr;
|
||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||
status = bind(listen_fd, saddr, addr_len);
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = listen(listen_fd, 1);
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = getsockname(listen_fd, saddr, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
print_sockaddr("listen sockname", saddr);
|
||||
|
||||
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, saddr, addr_len);
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
struct sockaddr_in peer;
|
||||
struct sockaddr* speer = (struct sockaddr*)&peer;
|
||||
socklen_t peer_len = sizeof(struct sockaddr_in);
|
||||
status = getpeername(client_fd, speer, &peer_len);
|
||||
ERROR_IF(getpeername, status, == -1);
|
||||
print_sockaddr("client peername", speer);
|
||||
|
||||
status = memcmp(saddr, speer, sizeof(struct sockaddr_in));
|
||||
UNEXP_IF(memcmp, status, != 0);
|
||||
|
||||
status = getsockname(client_fd, saddr, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
print_sockaddr("client sockname", saddr);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, listen_fd, == -1);
|
||||
|
||||
struct sockaddr_in addr =
|
||||
{
|
||||
.sin_family = AF_INET,
|
||||
.sin_addr = { .s_addr = htonl(0x7F000001 /* 127.0.0.1 */) },
|
||||
.sin_port = htons(0),
|
||||
};
|
||||
struct sockaddr* saddr = (struct sockaddr*)&addr;
|
||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||
status = bind(listen_fd, saddr, addr_len);
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = listen(listen_fd, 1);
|
||||
ERROR_IF(listen, status, == -1);
|
||||
|
||||
status = getsockname(listen_fd, saddr, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
|
||||
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, saddr, addr_len);
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
int server_fd = accept(listen_fd, NULL, NULL);
|
||||
ERROR_IF(accept, status, == -1);
|
||||
|
||||
char *c = "foo";
|
||||
status = send(server_fd, c, 4, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
char x[4];
|
||||
ssize_t amount = recv(client_fd, x, 4, 0);
|
||||
ERROR_IF(recv, amount, == -1);
|
||||
UNEXP_IF(recv, amount, != 4);
|
||||
|
||||
status = strcmp(c, x);
|
||||
printf("send %s\n", c);
|
||||
printf("recv %s\n", x);
|
||||
UNEXP_IF(strcmp, status, != 0);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
ERROR_IF(socket, server_fd, == -1);
|
||||
|
||||
struct sockaddr_in addr =
|
||||
{
|
||||
.sin_family = AF_INET,
|
||||
.sin_addr = { .s_addr = htonl(0x7F000001 /* 127.0.0.1 */) },
|
||||
.sin_port = htons(0),
|
||||
};
|
||||
struct sockaddr* saddr = (struct sockaddr*)&addr;
|
||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||
status = bind(server_fd, saddr, addr_len);
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = getsockname(server_fd, saddr, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
|
||||
int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, saddr, addr_len);
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
struct sockaddr_in name;
|
||||
struct sockaddr* sname = (struct sockaddr*)&name;
|
||||
socklen_t name_len = sizeof(struct sockaddr_in);
|
||||
status = getsockname(client_fd, sname, &name_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
|
||||
char *c = "bar";
|
||||
status = sendto(server_fd, c, 4, 0, sname, name_len);
|
||||
ERROR_IF(sendto, status, == -1);
|
||||
|
||||
struct sockaddr_in from;
|
||||
struct sockaddr* sfrom = (struct sockaddr*)&from;
|
||||
socklen_t from_len = sizeof(struct sockaddr_in);
|
||||
char x[4];
|
||||
ssize_t amount = recvfrom(client_fd, x, 4, 0, sfrom, &from_len);
|
||||
ERROR_IF(recvfrom, amount, == -1);
|
||||
UNEXP_IF(recvfrom, amount, != 4);
|
||||
|
||||
status = strcmp(c, x);
|
||||
printf("sendto %s\n", c);
|
||||
printf("recvfrom %s\n", x);
|
||||
UNEXP_IF(strcmp, status, != 0);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void) {
|
||||
int sv[2];
|
||||
pid_t pid;
|
||||
char buf[64];
|
||||
const char *parent_msg = "ping";
|
||||
const char *child_msg = "pong";
|
||||
|
||||
int status = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
|
||||
ERROR_IF(socketpair, status, == -1);
|
||||
|
||||
pid = fork();
|
||||
ERROR_IF(fork, pid, == -1);
|
||||
|
||||
if (pid == 0) {
|
||||
close(sv[0]);
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
ssize_t n = recv(sv[1], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
printf("child: received '%s'\n", buf);
|
||||
|
||||
n = recv(sv[1], buf, sizeof(buf), 0);
|
||||
if (n != 0) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"FAILURE: Expected EOF (0) after parent shutdown, got %ld\n",
|
||||
(long)n);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("child: verified EOF from parent (SHUT_WR worked)\n");
|
||||
|
||||
status = send(sv[1], child_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(sv[1]);
|
||||
exit(0);
|
||||
} else {
|
||||
close(sv[1]);
|
||||
|
||||
status = send(sv[0], parent_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
status = shutdown(sv[0], SHUT_WR);
|
||||
ERROR_IF(shutdown, status, == -1);
|
||||
printf("parent: shutdown(SHUT_WR) performed\n");
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
ssize_t n = recv(sv[0], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
|
||||
if (n == 0) {
|
||||
fprintf(stderr,
|
||||
"FAILURE: Parent received EOF, but expected 'pong'\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("parent: received '%s' (Shutdown allows reading)\n", buf);
|
||||
|
||||
status = send(sv[0], "garbage", 7, MSG_NOSIGNAL);
|
||||
if (status != -1 || errno != EPIPE) {
|
||||
fprintf(stderr,
|
||||
"WARNING: Expected EPIPE on write after shutdown, got "
|
||||
"status %d\n",
|
||||
status);
|
||||
} else {
|
||||
printf("parent: verified EPIPE on write after shutdown\n");
|
||||
}
|
||||
|
||||
close(sv[0]);
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
void print_sockaddr(char *ctx, struct sockaddr *addr, socklen_t len) {
|
||||
if (addr->sa_family == AF_UNIX) {
|
||||
struct sockaddr_un *un = (struct sockaddr_un *)addr;
|
||||
if (len <= sizeof(sa_family_t)) {
|
||||
printf("%s [AF_UNIX]: (unnamed)\n", ctx);
|
||||
} else {
|
||||
printf("%s [AF_UNIX]: %s\n", ctx, un->sun_path);
|
||||
}
|
||||
} else {
|
||||
printf("%s: Unknown address family %d\n", ctx, addr->sa_family);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
// TODO: in linux the sockname shown exactly like this string, not the case with redox.
|
||||
const char* socket_path = "unixpeername.sock";
|
||||
unlink(socket_path);
|
||||
|
||||
int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, listen_fd, == -1);
|
||||
|
||||
struct sockaddr_un addr = { .sun_family = AF_UNIX };
|
||||
struct sockaddr* saddr = (struct sockaddr*)&addr;
|
||||
socklen_t addr_len = sizeof(struct sockaddr_un);
|
||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
status = bind(listen_fd, saddr, addr_len);
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
// TODO: If this commented out, connect() should error with connection refused
|
||||
status = listen(listen_fd, 1);
|
||||
ERROR_IF(listen, status, == -1);
|
||||
|
||||
struct sockaddr_un check;
|
||||
struct sockaddr* scheck = (struct sockaddr*)✓
|
||||
status = getsockname(listen_fd, scheck, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
print_sockaddr("listen sockname", scheck, addr_len);
|
||||
|
||||
int client_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, saddr, addr_len);
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
struct sockaddr_un peer;
|
||||
struct sockaddr* speer = (struct sockaddr*)&peer;
|
||||
socklen_t peer_len = sizeof(struct sockaddr_un);
|
||||
status = getpeername(client_fd, speer, &peer_len);
|
||||
ERROR_IF(getpeername, status, == -1);
|
||||
print_sockaddr("client peername", speer, peer_len);
|
||||
|
||||
status = strcmp(check.sun_path, peer.sun_path);
|
||||
UNEXP_IF(strcmp, status, != 0);
|
||||
|
||||
status = getsockname(client_fd, scheck, &addr_len);
|
||||
ERROR_IF(getsockname, status, == -1);
|
||||
print_sockaddr("client sockname", scheck, addr_len);
|
||||
|
||||
close(client_fd);
|
||||
close(listen_fd);
|
||||
unlink(socket_path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
const char* socket_path = "unix_stream.sock";
|
||||
unlink(socket_path);
|
||||
|
||||
int server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, server_fd, == -1);
|
||||
|
||||
struct sockaddr_un addr = { .sun_family = AF_UNIX };
|
||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
status = bind(server_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = listen(server_fd, 5);
|
||||
ERROR_IF(listen, status, == -1);
|
||||
|
||||
pid_t pid = fork();
|
||||
ERROR_IF(fork, pid, == -1);
|
||||
|
||||
if (pid == 0) {
|
||||
// to test that blocking in accept() works
|
||||
usleep(500000);
|
||||
|
||||
int client_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
char *msg = "ipsum";
|
||||
printf("send %s\n", msg);
|
||||
status = send(client_fd, msg, 6, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(client_fd);
|
||||
return 0;
|
||||
} else {
|
||||
int accepted_fd = accept(server_fd, NULL, NULL);
|
||||
ERROR_IF(accept, accepted_fd, == -1);
|
||||
|
||||
char x[6];
|
||||
ssize_t amount = recv(accepted_fd, x, 6, 0);
|
||||
ERROR_IF(recv, amount, == -1);
|
||||
|
||||
printf("recv %s\n", x);
|
||||
close(accepted_fd);
|
||||
close(server_fd);
|
||||
|
||||
wait(NULL);
|
||||
unlink(socket_path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
// similar to "unixrecv", but this test if recv() works before accept().
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
const char* socket_path = "unix_read.sock";
|
||||
unlink(socket_path);
|
||||
|
||||
int server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, server_fd, == -1);
|
||||
|
||||
struct sockaddr_un addr = { .sun_family = AF_UNIX };
|
||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
status = bind(server_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = listen(server_fd, 5);
|
||||
ERROR_IF(listen, status, == -1);
|
||||
|
||||
pid_t pid = fork();
|
||||
ERROR_IF(fork, pid, == -1);
|
||||
|
||||
if (pid == 0) {
|
||||
int client_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
char x[6];
|
||||
ssize_t amount = recv(client_fd, x, 6, 0);
|
||||
ERROR_IF(recv, amount, == -1);
|
||||
printf("recv %s\n", x);
|
||||
|
||||
close(client_fd);
|
||||
return 0;
|
||||
} else {
|
||||
// to test that blocking in recv() works
|
||||
usleep(500000);
|
||||
|
||||
int accepted_fd = accept(server_fd, NULL, NULL);
|
||||
ERROR_IF(accept, accepted_fd, == -1);
|
||||
|
||||
char *msg = "ipsum";
|
||||
printf("send %s\n", msg);
|
||||
status = send(accepted_fd, msg, 6, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(accepted_fd);
|
||||
close(server_fd);
|
||||
|
||||
wait(NULL);
|
||||
unlink(socket_path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
const char* socket_path = "unixrecvfrom.sock";
|
||||
|
||||
unlink(socket_path);
|
||||
|
||||
int server_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
ERROR_IF(socket, server_fd, == -1);
|
||||
|
||||
struct sockaddr_un addr = { .sun_family = AF_UNIX };
|
||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
status = bind(server_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
int client_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
char *c = "lorem";
|
||||
status = send(client_fd, c, 6, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
char x[6];
|
||||
struct sockaddr_un from;
|
||||
socklen_t from_len = sizeof(struct sockaddr_un);
|
||||
|
||||
ssize_t amount = recvfrom(server_fd, x, 6, 0, (struct sockaddr*)&from, &from_len);
|
||||
ERROR_IF(recvfrom, amount, == -1);
|
||||
UNEXP_IF(recvfrom, amount, != 6);
|
||||
|
||||
status = strcmp(c, x);
|
||||
printf("send %s\n", c);
|
||||
printf("recvfrom %s\n", x);
|
||||
UNEXP_IF(strcmp, status, != 0);
|
||||
|
||||
close(server_fd);
|
||||
close(client_fd);
|
||||
unlink(socket_path);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
void socketpair_test(int flags) {
|
||||
int sv[2];
|
||||
pid_t pid;
|
||||
char buf[64];
|
||||
const char *parent_msg = "ping";
|
||||
const char *child_msg = "pong";
|
||||
|
||||
int status = socketpair(AF_UNIX, flags, 0, sv);
|
||||
ERROR_IF(socketpair, status, == -1);
|
||||
|
||||
pid = fork();
|
||||
ERROR_IF(fork, pid, == -1);
|
||||
|
||||
if (pid == 0) {
|
||||
close(sv[0]);
|
||||
|
||||
ssize_t n = recv(sv[1], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
printf("child: %s\n", buf);
|
||||
|
||||
status = send(sv[1], child_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(sv[1]);
|
||||
exit(0);
|
||||
} else {
|
||||
close(sv[1]);
|
||||
|
||||
status = send(sv[0], parent_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
ssize_t n = recv(sv[0], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
printf("parent: %s\n", buf);
|
||||
|
||||
close(sv[0]);
|
||||
wait(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("SOCK_STREAM\n");
|
||||
socketpair_test(SOCK_STREAM);
|
||||
printf("SOCK_DGRAM\n");
|
||||
socketpair_test(SOCK_DGRAM);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
// similar to "unixrecv", but this test if write() works before accept().
|
||||
// "unixrecvfrom" is the similar test to this one, but using SOCK_DGRAM.
|
||||
// "unixrecvearly" is also the similar test to this one, but using recv().
|
||||
int main() {
|
||||
int status;
|
||||
const char* socket_path = "unix_write.sock";
|
||||
unlink(socket_path);
|
||||
|
||||
int server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, server_fd, == -1);
|
||||
|
||||
struct sockaddr_un addr = { .sun_family = AF_UNIX };
|
||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
unlink(socket_path);
|
||||
|
||||
status = bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
|
||||
ERROR_IF(bind, status, == -1);
|
||||
|
||||
status = listen(server_fd, 1);
|
||||
ERROR_IF(listen, status, == -1);
|
||||
|
||||
int client_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ERROR_IF(socket, client_fd, == -1);
|
||||
|
||||
status = connect(client_fd, (struct sockaddr*)&addr, sizeof(addr));
|
||||
ERROR_IF(connect, status, == -1);
|
||||
|
||||
char *msg = "yes";
|
||||
ssize_t ret = write(client_fd, msg, 4);
|
||||
ERROR_IF(write, ret, == -1);
|
||||
UNEXP_IF(raise, ret, != 4);
|
||||
|
||||
int accepted_fd = accept(server_fd, NULL, NULL);
|
||||
ERROR_IF(accept, accepted_fd, == -1);
|
||||
|
||||
char x[4];
|
||||
ssize_t amount = read(accepted_fd, x, 4);
|
||||
ERROR_IF(read, amount, == -1);
|
||||
|
||||
close(accepted_fd);
|
||||
close(client_fd);
|
||||
close(server_fd);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user