Changeset 3708


Ignore:
Timestamp:
Mar 17, 2011, 1:53:06 AM (14 years ago)
Author:
bird
Message:

nonblock-1.c: extended the testcase. References #210.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libc/tests/libc/smoketests/nonblock-1.c

    r2438 r3708  
    3131#include <sys/socket.h>
    3232#include <sys/fcntl.h>
     33#include <sys/filio.h>
    3334#include <stdio.h>
    3435#include <string.h>
    3536#include <errno.h>
    3637#include <unistd.h>
     38#include <signal.h>
    3739
     40static void sigAlarmHandler(int iSig)
     41{
     42    /* do nothing */
     43}
    3844
    3945int main(int argc, char **argv)
    4046{
    4147    char    abBuf[128];
     48    int     pair[2];
     49    int     fFlags;
     50    int     fDontBlock;
     51    int     rc;
    4252    ssize_t cb;
    43     int pair[2];
    44     int rc;
    45     int fFlags;
     53
     54    /*
     55     * Install a signal handler for alarms.
     56     */
     57    if (signal(SIGALRM, sigAlarmHandler) == SIG_ERR)
     58    {
     59        printf("nonblock-1: signal(SIGALRM,) failed, errno=%d: %s\n", errno, strerror(errno));
     60        return 1;
     61    }
     62    if (siginterrupt(SIGALRM, 1) != 0)
     63    {
     64        printf("nonblock-1: siginterrupt(SIGALRM, 1) failed, errno=%d: %s\n", errno, strerror(errno));
     65        return 1;
     66    }
    4667
    4768    /*
    4869     * Create a socket pair.
    4970     */
    50     rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, &pair[0]);
     71    rc = socketpair(AF_UNIX, SOCK_STREAM, 0, &pair[0]);
    5172    if (rc < 0)
    5273    {
     
    5475        return 1;
    5576    }
     77
     78    /*
     79     *
     80     * The fcntl way.
     81     *
     82     */
    5683
    5784    /*
     
    78105     */
    79106    /** @todo this should be done with a timer pending or a parent watching. */
     107    alarm(5);
    80108    cb = read(pair[0], abBuf, sizeof(abBuf));
    81109    if (cb != -1 || errno != EWOULDBLOCK)
    82110    {
    83111        printf("nonblock-1: read didn't return -1 and errno=%d! cb=%zd errno=%d: %s\n",
    84                cb, EWOULDBLOCK, errno, strerror(errno));
     112               EWOULDBLOCK, cb, errno, strerror(errno));
    85113        return 1;
    86114    }
     115    alarm(0);
     116    printf("nonblock-1: test #1 - non-blocking test - succeeded\n");
     117
     118
     119    /*
     120     * Change back to blocking mode and try read from the pipe now.
     121     */
     122    rc = fcntl(pair[0], F_SETFL, fFlags & ~O_NONBLOCK);
     123    if (rc < 0)
     124    {
     125        printf("nonblock-1: F_SETFL(%#x) failed, rc=%d errno=%d: %s\n", fFlags & ~O_NONBLOCK, rc, errno, strerror(errno));
     126        return 1;
     127    }
     128    alarm(5);
     129    cb = read(pair[0], abBuf, sizeof(abBuf));
     130    if (cb != -1 || errno != EINTR)
     131    {
     132        alarm(0);
     133        printf("nonblock-1: read didn't return -1 and errno=%d! cb=%zd errno=%d: %s\n",
     134               EINTR, cb, errno, strerror(errno));
     135        return 1;
     136    }
     137    alarm(0);
     138    printf("nonblock-1: test #2 - blocking test - succeeded\n");
     139
     140
     141    /*
     142     *
     143     * Try the IOCTL.
     144     *
     145     */
     146   
     147    /*
     148     * Nonblocking call.
     149     */
     150    fDontBlock = 1;
     151    rc = ioctl(pair[0], FIONBIO, (char *)&fDontBlock);
     152    if (rc < 0)
     153    {
     154        printf("nonblock-1: FIONBIO(1) failed, rc=%d errno=%d: %s\n", rc, errno, strerror(errno));
     155        return 1;
     156    }
     157    alarm(5);
     158    cb = read(pair[0], abBuf, sizeof(abBuf));
     159    if (cb != -1 || errno != EWOULDBLOCK)
     160    {
     161        printf("nonblock-1: read didn't return -1 and errno=%d! cb=%zd errno=%d: %s\n",
     162               EWOULDBLOCK, cb, errno, strerror(errno));
     163        return 1;
     164    }
     165    alarm(0);
     166    printf("nonblock-1: test #3 - non-blocking test - succeeded\n");
     167
     168
     169    /*
     170     * Change back to blocking mode and try read from the pipe now.
     171     */
     172    fDontBlock = 0;
     173    rc = ioctl(pair[0], FIONBIO, (char *)&fDontBlock);
     174    if (rc < 0)
     175    {
     176        printf("nonblock-1: FIONBIO(0) failed, rc=%d errno=%d: %s\n", rc, errno, strerror(errno));
     177        return 1;
     178    }
     179    alarm(5);
     180    cb = read(pair[0], abBuf, sizeof(abBuf));
     181    if (cb != -1 || errno != EINTR)
     182    {
     183        alarm(0);
     184        printf("nonblock-1: read didn't return -1 and errno=%d! cb=%zd errno=%d: %s\n",
     185               EINTR, cb, errno, strerror(errno));
     186        return 1;
     187    }
     188    alarm(0);
     189    printf("nonblock-1: test #4 - blocking test - succeeded\n");
     190   
    87191
    88192    printf("nonblock-1: success\n");
Note: See TracChangeset for help on using the changeset viewer.