8000 minix/minix/tests/test56.c at master · tomialagbe/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
3542 lines (2783 loc) · 78.2 KB

test56.c

File metadata and controls

3542 lines (2783 loc) · 78.2 KB
/*
* Test Program for Unix Domain Sockets
*
* Overview: This program tests Unix Domain Sockets. It attempts
* to exercise the functions associated with Unix Domain Sockets.
* It also attempts to make sure all of the functions which handle
* file/socket descriptors work correctly when given a socket
* descriptor for a Unix domain socket. It also implicitly checks
* for the existance of constants like AF_UNIX and structures like
* sockaddr_un (it won't compile if they aren't defined). Besides
* checking that the sockets work properly, this test program also
* checks that the errors returned conform to the POSIX 2008
* standards. Some tests are omitted as they could adversely affect
* the operation of the host system. For example, implementing a test
* for socket() failing with errno = ENFILE would require using up all
* of the file descriptors supported by the OS (defined in
* /proc/sys/fs/file-max on Linux); this could cause problems for
* daemons and other processes running on the system. Some tests are
* omitted because they would require changes to libc or the kernel.
* For example, getting EINTR would require delaying the system call
* execution time long enough to raise a signal to interupt it. Some
* tests were omitted because the particular errors cannot occur when
* using Unix domain sockets. For example, write() will never fail with
* ENETDOWN because Unix domain sockets don't use network interfaces.
*
* Structure: Some functions can be tested or partially tested without
* making a connection, socket() for example. These have test
* functions like test_NAME(). The functionality that needs two way
* communication is contained within test_xfer().
*
* Functions Tested: accept(), bind(), close(), connect(), dup(),
* dup2(), fstat(), getpeername(), getsockname(), getsockopt(),
* listen(), read(), readv(), recv(), recvfrom(), recvmsg(), select(),
* send(), sendmsg(), sendto(), setsockopt(), shutdown(), socket(),
* socketpair(), write(), writev()
*/
#define DEBUG 0
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ucred.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
/* Maximum number of errors that we'll allow to occur before this test
* program gives us and quits.
*/
int max_error = 4;
#include "common.h"
/* Use the common testing code instead of reinventing the wheel. */
/* path of the unix domain socket */
#define TEST_SUN_PATH "test.sock"
#define TEST_SUN_PATHB "testb.sock"
/* filenames for symlinks -- we link these to each other to test ELOOP .*/
#define TEST_SYM_A "test.a"
#define TEST_SYM_B "test.b"
/* text file and test phrase for testing file descriptor passing */
#define TEST_TXT_FILE "test.txt"
#define MSG "This raccoon loves to eat bugs.\n"
/* buffer for send/recv */
#define BUFSIZE (128)
#define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
/* socket types supported */
static int types[3] = {SOCK_STREAM, SOCK_SEQPACKET, SOCK_DGRAM};
static char sock_fullpath[PATH_MAX + 1];
static void test_abort_client_server(int abort_type);
static void test_abort_client(int abort_type);
static void test_abort_server(pid_t pid, int abort_type);
/* timestamps for debug and error logs */
static char *get_timestamp(void)
{
struct tm *tm;
time_t t;
size_t len;
char *s;
len = sizeof(char) * 32;
t = time(NULL);
if (t == -1) {
return NULL;
}
tm = gmtime(&t);
if (tm == NULL) {
return NULL;
}
s = (char *) malloc(len);
if (!s) {
perror("malloc");
return NULL;
}
memset(s, '\0', len);
strftime(s, len - 1, ISO8601_FORMAT, tm);
return s;
}
/* macro to display information about a failed test and increment the errct */
static void test_fail_fl(char *msg, char *file, int line)
{
char *timestamp;
timestamp = get_timestamp();
if (errct == 0) fprintf(stderr, "\n");
fprintf(stderr, "[ERROR][%s] (%s Line %d) %s [pid=%d:errno=%d:%s]\n",
timestamp, file, line, msg, getpid(),
errno, strerror(errno));
fflush(stderr);
if (timestamp != NULL) {
free(timestamp);
timestamp = NULL;
}
e(7);
}
#define test_fail(msg) test_fail_fl(msg, __FILE__, __LINE__)
/* Convert name to the full path of the socket. Assumes name is in cwd. */
static char *fullpath(char *name)
{
char cwd[PATH_MAX + 1];
if (realpath(".", cwd) == NULL)
test_fail("Couldn't retrieve current working dir");
snprintf(sock_fullpath, PATH_MAX, "%s/%s", cwd, name);
return(sock_fullpath);
}
#if DEBUG == 1
/* macros to display debugging information */
static void debug_fl(char *msg, char *file, int line)
{
char *timestamp;
timestamp = get_timestamp();
fprintf(stdout,"[DEBUG][%s] (%s:%d) %s [pid=%d]\n",
timestamp, __FILE__, __LINE__, msg, getpid());
fflush(stdout);
if (timestamp != NULL) {
free(timestamp);
timestamp = NULL;
}
}
#define debug(msg) debug_fl(msg, __FILE__, __LINE__)
#else
#define debug(msg)
#endif
#define SOCKET(sd,domain,type,protocol) \
do { \
errno = 0; \
sd = socket(domain, type, protocol); \
if (sd == -1) { \
test_fail("sd = socket(domain, type, protocol) failed");\
} \
} while (0)
#define UNLINK(path) \
do { \
int rc; \
errno = 0; \
rc = unlink(path); \
if (rc == -1 && errno != ENOENT) { \
test_fail("unlink(path) failed"); \
} \
} while(0)
#define SYMLINK(oldpath,newpath) \
do { \
int rc; \
errno = 0; \
rc = symlink(oldpath,newpath); \
if (rc == -1) { \
test_fail("symlink(oldpath,newpath) failed"); \
} \
} while(0)
#define CLOSE(sd) \
do { \
int rc; \
errno = 0; \
rc = close(sd); \
if (rc == -1) { \
test_fail("close(sd) failed"); \
} \
} while (0)
static void test_socket(void)
{
struct stat statbuf, statbuf2;
int sd, sd2;
int rc;
int i;
debug("entering test_socket()");
debug("Test socket() with an unsupported address family");
errno = 0;
sd = socket(-1, SOCK_STREAM, 0);
if (!(sd == -1 && errno == EAFNOSUPPORT)) {
test_fail("socket");
if (sd != -1) {
CLOSE(sd);
}
}
debug("Test socket() with all available FDs open by this process");
for (i = 3; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
errno = 0;
sd = socket(PF_UNIX, SOCK_STREAM, 0);
if (!(sd == -1 && errno == EMFILE)) {
test_fail("socket() call with all fds open should fail");
if (sd != -1) {
CLOSE(sd);
}
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
debug("Test socket() with an mismatched protocol");
errno = 0;
sd = socket(PF_UNIX, SOCK_STREAM, 4);
if (!(sd == -1 && errno == EPROTONOSUPPORT)) {
test_fail("socket() should fail with errno = EPROTONOSUPPORT");
if (sd != -1) {
CLOSE(sd);
}
}
debug("Test socket() success");
/*
* open 2 sockets at once and *then* close them.
* This will test that /dev/uds is cloning properly.
*/
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
SOCKET(sd2, PF_UNIX, SOCK_STREAM, 0);
rc = fstat(sd, &statbuf);
if (rc == -1) {
test_fail("fstat failed on sd");
}
rc = fstat(sd2, &statbuf2);
if (rc == -1) {
test_fail("fstat failed on sd2");
}
if (statbuf.st_dev == statbuf2.st_dev) {
test_fail("/dev/uds isn't being cloned");
}
CLOSE(sd2);
CLOSE(sd);
debug("leaving test_socket()");
}
static void test_header(void)
{
struct sockaddr_un sun;
debug("entering test_header()");
sun.sun_family = AF_UNIX;
sun.sun_path[0] = 'x';
sun.sun_path[1] = 'x';
sun.sun_path[2] = 'x';
sun.sun_path[3] = '\0';
if (SUN_LEN(&sun) != 5) {
test_fail("SUN_LEN(&sun) should be 5");
}
if (PF_UNIX != PF_LOCAL || PF_UNIX != AF_UNIX) {
test_fail("PF_UNIX, PF_LOCAL and AF_UNIX");
}
}
static void test_socketpair(void)
{
char buf[128];
struct sockaddr_un addr;
int socket_vector[2];
int rc;
int i;
debug("entering test_socketpair()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
debug("Testing socketpair() success");
rc = socketpair(PF_UNIX, SOCK_STREAM, 0, socket_vector);
if (rc == -1) {
test_fail("socketpair() should have worked");
}
debug("Testing a simple read/write using sockets from socketpair()");
memset(buf, '\0', sizeof(buf));
strncpy(buf, "Howdy Partner", sizeof(buf) - 1);
rc = write(socket_vector[0], buf, sizeof(buf));
if (rc == -1) {
test_fail("write(sd, buf, sizeof(buf)) failed unexpectedly");
}
memset(buf, '\0', sizeof(buf));
rc = read(socket_vector[1], buf, sizeof(buf));
if (rc == -1) {
test_fail("read() failed unexpectedly");
}
if (strncmp(buf, "Howdy Partner", strlen("Howdy Partner")) != 0) {
test_fail("We did not read what we wrote");
}
CLOSE(socket_vector[0]);
CLOSE(socket_vector[1]);
debug("Test socketpair() with all FDs open by this process");
for (i = 3; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
rc = socketpair(PF_UNIX, SOCK_STREAM, 0, socket_vector);
if (!(rc == -1 && errno == EMFILE)) {
test_fail("socketpair() should have failed with EMFILE");
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
rc = socketpair(PF_UNIX, SOCK_STREAM, 4, socket_vector);
if (!(rc == -1 && errno == EPROTONOSUPPORT)) {
test_fail("socketpair() should have failed");
}
debug("leaving test_socketpair()");
}
static void test_ucred(void)
{
struct uucred credentials;
socklen_t ucred_length;
uid_t euid = geteuid();
gid_t egid = getegid();
int sv[2];
int rc;
debug("Test credentials passing");
ucred_length = sizeof(struct uucred);
rc = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
if (rc == -1) {
test_fail("socketpair(PF_UNIX, SOCK_STREAM, 0, sv) failed");
}
memset(&credentials, '\0', ucred_length);
rc = getsockopt(sv[0], SOL_SOCKET, SO_PEERCRED, &credentials,
&ucred_length);
if (rc == -1) {
test_fail("getsockopt(SO_PEERCRED) failed");
} else if (credentials.cr_ngroups != 0 ||
credentials.cr_uid != geteuid() ||
credentials.cr_gid != getegid()) {
/* printf("%d=%d %d=%d %d=%d",credentials.cr_ngroups, 0,
credentials.cr_uid, geteuid(), credentials.cr_gid, getegid()); */
test_fail("Credential passing gave us the wrong cred");
}
rc = getpeereid(sv[0], &euid, &egid);
if (rc == -1) {
test_fail("getpeereid(sv[0], &euid, &egid) failed");
} else if (credentials.cr_uid != euid || credentials.cr_gid != egid) {
test_fail("getpeereid() didn't give the correct euid/egid");
}
CLOSE(sv[0]);
CLOSE(sv[1]);
}
static void test_getsockname(void)
{
int sd;
int rc;
struct sockaddr_un addr, sock_addr;
socklen_t sock_addr_len;
memset(&addr, '\0', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(struct sockaddr_un));
sock_addr_len = sizeof(struct sockaddr_un);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
if (!(sock_addr.sun_family == AF_UNIX && strncmp(sock_addr.sun_path,
fullpath(TEST_SUN_PATH),
sizeof(sock_addr.sun_path) - 1) == 0)) {
test_fail("getsockname() did return the right address");
fprintf(stderr, "exp: '%s' | got: '%s'\n", addr.sun_path,
sock_addr.sun_path);
}
CLOSE(sd);
}
static void test_bind(void)
{
struct sockaddr_un addr;
struct sockaddr_un sock_addr;
socklen_t sock_addr_len;
int sd;
int sd2;
int rc;
debug("entering test_bind()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
debug("Test bind() success");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(struct sockaddr_un));
sock_addr_len = sizeof(struct sockaddr_un);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
if (!(sock_addr.sun_family == AF_UNIX &&
strncmp(sock_addr.sun_path,
fullpath(TEST_SUN_PATH),
sizeof(sock_addr.sun_path) - 1) == 0)) {
test_fail("getsockname() didn't return the right addr");
fprintf(stderr, "exp: '%s' | got: '%s'\n", addr.sun_path,
sock_addr.sun_path);
}
debug("Test bind() with a address that has already been bind()'d");
SOCKET(sd2, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = bind(sd2, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == EADDRINUSE))) {
test_fail("bind() should have failed with EADDRINUSE");
}
CLOSE(sd2);
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
debug("Test bind() with an empty sun_path");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
memset(addr.sun_path, '\0', sizeof(addr.sun_path));
errno = 0;
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!(rc == -1 && errno == ENOENT)) {
test_fail("bind() should have failed with ENOENT");
}
CLOSE(sd);
debug("Test bind() with a NULL address");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = bind(sd, (struct sockaddr *) NULL, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == EFAULT))) {
test_fail("bind() should have failed with EFAULT");
}
CLOSE(sd);
debug("Test bind() using a symlink loop");
UNLINK(TEST_SUN_PATH);
UNLINK(TEST_SYM_A);
UNLINK(TEST_SYM_B);
SYMLINK(TEST_SYM_A, TEST_SYM_B);
SYMLINK(TEST_SYM_B, TEST_SYM_A);
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
strncpy(addr.sun_path, TEST_SYM_A, sizeof(addr.sun_path) - 1);
errno = 0;
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == ELOOP))) {
test_fail("bind() should have failed with ELOOP");
}
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
UNLINK(TEST_SYM_A);
UNLINK(TEST_SYM_B);
/* Test bind with garbage in sockaddr_un */
memset(&addr, '?', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
addr.sun_path[0] = 'f';
addr.sun_path[1] = 'o';
addr.sun_path[2] = 'o';
addr.sun_path[3] = '\0';
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, strlen(addr.sun_path) + 1);
if (rc == -1) {
test_fail("bind() should have worked");
}
CLOSE(sd);
UNLINK("foo");
debug("leaving test_bind()");
}
static void test_listen(void)
{
int rc;
debug("entering test_listen()");
debug("Test listen() with a bad file descriptor");
errno = 0;
rc = listen(-1, 0);
if (!(rc == -1 && errno == EBADF)) {
test_fail("listen(-1, 0) should have failed");
}
debug("Test listen() with a non-socket file descriptor");
errno = 0;
rc = listen(0, 0);
/* Test on errno disabled here: there's currently no telling what this
* will return. POSIX says it should be ENOTSOCK, MINIX3 libc returns
* ENOSYS, and we used to test for ENOTTY here..
*/
if (!(rc == -1)) {
test_fail("listen(0, 0) should have failed");
}
debug("leaving test_listen()");
}
static void test_shutdown(void)
{
int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
int sd;
int rc;
int i;
debug("entering test_shutdown()");
/* test for each direction (read, write, read-write) */
for (i = 0; i < 3; i++) {
debug("test shutdown() with an invalid descriptor");
errno = 0;
rc = shutdown(-1, how[i]);
if (!(rc == -1 && errno == EBADF)) {
test_fail("shutdown(-1, how[i]) should have failed");
}
debug("test shutdown() with a non-socket descriptor");
errno = 0;
rc = shutdown(0, how[i]);
if (!(rc == -1 && errno == ENOSYS)) {
test_fail("shutdown() should have failed with ENOSYS");
}
debug("test shutdown() with a socket that is not connected");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = shutdown(sd, how[i]);
if (!(rc == -1 && errno == ENOTCONN)) {
test_fail("shutdown() should have failed");
}
CLOSE(sd);
}
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = shutdown(sd, -1);
if (!(rc == -1 && errno == ENOTCONN)) {
test_fail("shutdown(sd, -1) should have failed with ENOTCONN");
}
CLOSE(sd);
debug("leaving test_shutdown()");
}
static void test_close(void)
{
struct sockaddr_un addr;
int sd, sd2;
int rc, i;
debug("entering test_close()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
debug("Test close() success");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
CLOSE(sd);
debug("Close an already closed file descriptor");
errno = 0;
rc = close(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("close(sd) should have failed with EBADF");
}
UNLINK(TEST_SUN_PATH);
debug("dup()'ing a file descriptor and closing both should work");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
} else {
CLOSE(sd2);
CLOSE(sd);
}
UNLINK(TEST_SUN_PATH);
/* Create and close a socket a bunch of times.
* If the implementation doesn't properly free the
* socket during close(), eventually socket() will
* fail when the internal descriptor table is full.
*/
for (i = 0; i < 1024; i++) {
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
CLOSE(sd);
}
debug("leaving test_close()");
}
static void test_sockopts(void)
{
int i;
int rc;
int sd;
int option_value;
socklen_t option_len;
debug("entering test_sockopts()");
for (i = 0; i < 3; i++) {
SOCKET(sd, PF_UNIX, types[i], 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
&option_len);
if (rc != 0) {
test_fail("setsockopt() should have worked");
}
if (option_value != types[i]) {
test_fail("SO_TYPE didn't seem to work.");
}
CLOSE(sd);
}
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
if (rc != 0) {
test_fail("getsockopt() should have worked");
}
if (option_value != PIPE_BUF) {
test_fail("SO_SNDBUF didn't seem to work.");
}
CLOSE(sd);
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
if (rc != 0) {
test_fail("getsockopt() should have worked");
}
if (option_value != PIPE_BUF) {
test_fail("SO_RCVBUF didn't seem to work.");
}
CLOSE(sd);
debug("leaving test_sockopts()");
}
static void test_read(void)
{
int rc;
int fd;
char buf[BUFSIZE];
debug("entering test_read()");
errno = 0;
rc = read(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("read() should have failed with EBADF");
}
fd = open("/tmp", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/tmp\", O_RDONLY) should have worked");
}
CLOSE(fd);
debug("leaving test_read()");
}
static void test_write(void)
{
int rc;
char buf[BUFSIZE];
debug("entering test_write()");
errno = 0;
rc = write(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("write() should have failed with EBADF");
}
debug("leaving test_write()");
}
static void test_dup(void)
{
struct stat info1;
struct stat info2;
struct sockaddr_un addr;
int sd, sd2;
int rc;
int i;
debug("entering test_dup()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
debug("Test dup()");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
}
rc = fstat(sd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd2, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (info1.st_ino != info2.st_ino) {
test_fail("dup() failed info1.st_ino != info2.st_ino");
}
CLOSE(sd);
CLOSE(sd2);
debug("Test dup() with a closed socket");
errno = 0;
rc = dup(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(sd) on a closed socket shouldn't have worked");
}
debug("Test dup() with socket descriptor of -1");
errno = 0;
rc = dup(-1);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(-1) shouldn't have worked");
}
debug("Test dup() when all of the file descriptors are taken");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
for (i = 4; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
errno = 0;
sd2 = dup(sd);
if (!(sd2 == -1 && errno == EMFILE)) {
test_fail("dup(sd) should have failed with errno = EMFILE");
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
UNLINK(TEST_SUN_PATH);
debug("leaving test_dup()");
}
static void test_dup2(void)
{
struct stat info1;
struct stat info2;
struct sockaddr_un addr;
int sd;
int fd;
int rc;
debug("entering test_dup2()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
fd = open("/dev/null", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/dev/null\", O_RDONLY) failed");
}
fd = dup2(sd, fd);
if (fd == -1) {
test_fail("dup2(sd, fd) failed.");
}
memset(&info1, '\0', sizeof(struct stat));
memset(&info2, '\0', sizeof(struct stat));
rc = fstat(fd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (!(info1.st_ino == info2.st_ino &&
major(info1.st_dev) == major(info2.st_dev) &&
minor(info1.st_dev) == minor(info2.st_dev))) {
test_fail("dup2() failed");
}
CLOSE(fd);
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
debug("leaving test_dup2()");
}
/*
* A toupper() server. This toy server converts a string to upper case.
*/
static void test_xfer_server(pid_t pid)
{
0