Changeset 3708
- Timestamp:
- Mar 17, 2011, 1:53:06 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libc/tests/libc/smoketests/nonblock-1.c
r2438 r3708 31 31 #include <sys/socket.h> 32 32 #include <sys/fcntl.h> 33 #include <sys/filio.h> 33 34 #include <stdio.h> 34 35 #include <string.h> 35 36 #include <errno.h> 36 37 #include <unistd.h> 38 #include <signal.h> 37 39 40 static void sigAlarmHandler(int iSig) 41 { 42 /* do nothing */ 43 } 38 44 39 45 int main(int argc, char **argv) 40 46 { 41 47 char abBuf[128]; 48 int pair[2]; 49 int fFlags; 50 int fDontBlock; 51 int rc; 42 52 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 } 46 67 47 68 /* 48 69 * Create a socket pair. 49 70 */ 50 rc = socketpair(AF_ LOCAL, SOCK_STREAM, 0, &pair[0]);71 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, &pair[0]); 51 72 if (rc < 0) 52 73 { … … 54 75 return 1; 55 76 } 77 78 /* 79 * 80 * The fcntl way. 81 * 82 */ 56 83 57 84 /* … … 78 105 */ 79 106 /** @todo this should be done with a timer pending or a parent watching. */ 107 alarm(5); 80 108 cb = read(pair[0], abBuf, sizeof(abBuf)); 81 109 if (cb != -1 || errno != EWOULDBLOCK) 82 110 { 83 111 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)); 85 113 return 1; 86 114 } 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 87 191 88 192 printf("nonblock-1: success\n");
Note:
See TracChangeset
for help on using the changeset viewer.