Project

General

Profile

testsocketnonblock.c

staples1347, 2018-04-10 07:40

 
1
#include <stdio.h>
2
#include <sys/types.h>
3
#include <sys/socket.h>
4
#include <unistd.h>
5
#include <fcntl.h>
6

    
7
int main(void) {
8
  int canuse_cloexec=0;
9
  int canuse_nonblock=0;
10
#ifdef SOCK_CLOEXEC
11
        {
12
                int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
13
                if (fd >= 0) {
14
                        canuse_cloexec=1;
15
                        int flags=fcntl(fd, F_GETFD, 0);
16
                        if ((flags & FD_CLOEXEC)!=FD_CLOEXEC) {
17
                          canuse_cloexec=2;
18
                        }
19
                        close(fd);
20
                }
21
        }
22
#endif
23
#ifdef SOCK_NONBLOCK
24
        {
25
                int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
26
                if (fd >= 0) {
27
                        canuse_nonblock=1;
28
                        int flags=fcntl(fd, F_GETFL, 0);
29
                        if ((flags & O_NONBLOCK)!=O_NONBLOCK) {
30
                                canuse_nonblock=2;
31
                        }
32
                        close(fd);
33
                }
34
        }
35
#endif
36
        printf("Can Use Cloexec=%d, Can Use Nonblock=%d\n", canuse_cloexec, canuse_nonblock);
37

    
38
        return 0;
39
}
40