Project

General

Profile

Feature #2883 ยป testsocketnonblock.c

staples1347, 2018-04-10 07:40

 
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>

int main(void) {
int canuse_cloexec=0;
int canuse_nonblock=0;
#ifdef SOCK_CLOEXEC
{
int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd >= 0) {
canuse_cloexec=1;
int flags=fcntl(fd, F_GETFD, 0);
if ((flags & FD_CLOEXEC)!=FD_CLOEXEC) {
canuse_cloexec=2;
}
close(fd);
}
}
#endif
#ifdef SOCK_NONBLOCK
{
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd >= 0) {
canuse_nonblock=1;
int flags=fcntl(fd, F_GETFL, 0);
if ((flags & O_NONBLOCK)!=O_NONBLOCK) {
canuse_nonblock=2;
}
close(fd);
}
}
#endif
printf("Can Use Cloexec=%d, Can Use Nonblock=%d\n", canuse_cloexec, canuse_nonblock);

return 0;
}

    (1-1/1)