tst-sock-nonblock.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vi: set sw=4 ts=4 sts=4: */
  2. /*
  3. * Nonblocking socket test for uClibc
  4. * Copyright (C) 2012 by Kevin Cernekee <cernekee@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <error.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <sys/un.h>
  17. #include <sys/fcntl.h>
  18. static int
  19. do_test(void)
  20. {
  21. int fd, ret, result = 0;
  22. struct sockaddr_un sa;
  23. char buf;
  24. fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
  25. if (fd < 0) {
  26. perror("socket()");
  27. result = 1;
  28. }
  29. memset(&sa, 0, sizeof(sa));
  30. sa.sun_family = AF_UNIX;
  31. strcpy(sa.sun_path, "socktest");
  32. unlink("socktest");
  33. if (bind(fd, (const struct sockaddr *)&sa, sizeof(sa)) < 0) {
  34. perror("bind()");
  35. result = 1;
  36. }
  37. ret = read(fd, &buf, sizeof(buf));
  38. if (ret != -1 || errno != EAGAIN) {
  39. error(0, 0, "Nonblocking read returned %d", ret);
  40. result = 1;
  41. }
  42. return result;
  43. }
  44. #define TIMEOUT 5
  45. #define TEST_FUNCTION do_test ()
  46. #include "../test-skeleton.c"