main2.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #define fd (30)
  11. // Tests libgo2.so, which does not export any functions.
  12. // Read a string from the file descriptor and print it.
  13. int main(void) {
  14. int i;
  15. ssize_t n;
  16. char buf[20];
  17. struct timespec ts;
  18. // The descriptor will be initialized in a thread, so we have to
  19. // give a chance to get opened.
  20. for (i = 0; i < 200; i++) {
  21. n = read(fd, buf, sizeof buf);
  22. if (n >= 0)
  23. break;
  24. if (errno != EBADF && errno != EINVAL) {
  25. fprintf(stderr, "BUG: read: %s\n", strerror(errno));
  26. return 2;
  27. }
  28. // An EBADF error means that the shared library has not opened the
  29. // descriptor yet.
  30. ts.tv_sec = 0;
  31. ts.tv_nsec = 10000000;
  32. nanosleep(&ts, NULL);
  33. }
  34. if (n < 0) {
  35. fprintf(stderr, "BUG: failed to read any data from pipe\n");
  36. return 2;
  37. }
  38. if (n == 0) {
  39. fprintf(stderr, "BUG: unexpected EOF\n");
  40. return 2;
  41. }
  42. if (n == sizeof buf) {
  43. n--;
  44. }
  45. buf[n] = '\0';
  46. printf("%s\n", buf);
  47. return 0;
  48. }