socket.c 606 B

123456789101112131415161718192021
  1. #include <sys/socket.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include "syscall.h"
  5. int socket(int domain, int type, int protocol)
  6. {
  7. int s = __socketcall(socket, domain, type, protocol, 0, 0, 0);
  8. if ((s==-EINVAL || s==-EPROTONOSUPPORT)
  9. && (type&(SOCK_CLOEXEC|SOCK_NONBLOCK))) {
  10. s = __socketcall(socket, domain,
  11. type & ~(SOCK_CLOEXEC|SOCK_NONBLOCK),
  12. protocol, 0, 0, 0);
  13. if (s < 0) return __syscall_ret(s);
  14. if (type & SOCK_CLOEXEC)
  15. __syscall(SYS_fcntl, s, F_SETFD, FD_CLOEXEC);
  16. if (type & SOCK_NONBLOCK)
  17. __syscall(SYS_fcntl, s, F_SETFL, O_NONBLOCK);
  18. }
  19. return __syscall_ret(s);
  20. }