qlen.lc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "unpxti.h"## 1 ##src/debug/qlen.c##
  2. #define PORT 9999## 2 ##src/debug/qlen.c##
  3. #define ADDR "127.0.0.1"## 3 ##src/debug/qlen.c##
  4. #define MAXBACKLOG 100## 4 ##src/debug/qlen.c##
  5. /* globals */## 5 ##src/debug/qlen.c##
  6. struct sockaddr_in serv;## 6 ##src/debug/qlen.c##
  7. pid_t pid; /* of child */## 7 ##src/debug/qlen.c##
  8. int pipefd[2];## 8 ##src/debug/qlen.c##
  9. #define pfd pipefd[1] /* parent's end */## 9 ##src/debug/qlen.c##
  10. #define cfd pipefd[0] /* child's end */## 10 ##src/debug/qlen.c##
  11. /* function prototypes */## 11 ##src/debug/qlen.c##
  12. void do_parent(void);## 12 ##src/debug/qlen.c##
  13. void do_child(void);## 13 ##src/debug/qlen.c##
  14. int## 14 ##src/debug/qlen.c##
  15. main(int argc, char **argv)## 15 ##src/debug/qlen.c##
  16. {## 16 ##src/debug/qlen.c##
  17. if (argc != 1)## 17 ##src/debug/qlen.c##
  18. err_quit("usage: qlen");## 18 ##src/debug/qlen.c##
  19. Socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd);## 19 ##src/debug/qlen.c##
  20. bzero(&serv, sizeof(serv));## 20 ##src/debug/qlen.c##
  21. serv.sin_family = AF_INET;## 21 ##src/debug/qlen.c##
  22. serv.sin_port = htons(PORT);## 22 ##src/debug/qlen.c##
  23. Inet_pton(AF_INET, ADDR, &serv.sin_addr);## 23 ##src/debug/qlen.c##
  24. if ((pid = Fork()) == 0)## 24 ##src/debug/qlen.c##
  25. do_child();## 25 ##src/debug/qlen.c##
  26. else## 26 ##src/debug/qlen.c##
  27. do_parent();## 27 ##src/debug/qlen.c##
  28. exit(0);## 28 ##src/debug/qlen.c##
  29. }## 29 ##src/debug/qlen.c##
  30. void## 30 ##src/debug/qlen.c##
  31. parent_alrm(int signo)## 31 ##src/debug/qlen.c##
  32. {## 32 ##src/debug/qlen.c##
  33. return; /* just interrupt blocked connect() */## 33 ##src/debug/qlen.c##
  34. }## 34 ##src/debug/qlen.c##
  35. /* include qlen */
  36. void## 35 ##src/debug/qlen.c##
  37. do_parent(void)## 36 ##src/debug/qlen.c##
  38. {## 37 ##src/debug/qlen.c##
  39. int qlen, j, k, junk, fd[MAXBACKLOG + 1];## 38 ##src/debug/qlen.c##
  40. struct t_call tcall;## 39 ##src/debug/qlen.c##
  41. Close(cfd);## 40 ##src/debug/qlen.c##
  42. Signal(SIGALRM, parent_alrm);## 41 ##src/debug/qlen.c##
  43. for (qlen = 0; qlen <= 14; qlen++) {## 42 ##src/debug/qlen.c##
  44. printf("qlen = %d: ", qlen);## 43 ##src/debug/qlen.c##
  45. Write(pfd, &qlen, sizeof(int)); /* tell child value */## 44 ##src/debug/qlen.c##
  46. Read(pfd, &junk, sizeof(int)); /* wait for child */## 45 ##src/debug/qlen.c##
  47. for (j = 0; j <= MAXBACKLOG; j++) {## 46 ##src/debug/qlen.c##
  48. fd[j] = T_open(XTI_TCP, O_RDWR, NULL);## 47 ##src/debug/qlen.c##
  49. T_bind(fd[j], NULL, NULL);## 48 ##src/debug/qlen.c##
  50. tcall.addr.maxlen = sizeof(serv);## 49 ##src/debug/qlen.c##
  51. tcall.addr.len = sizeof(serv);## 50 ##src/debug/qlen.c##
  52. tcall.addr.buf = &serv;## 51 ##src/debug/qlen.c##
  53. tcall.opt.len = 0;## 52 ##src/debug/qlen.c##
  54. tcall.udata.len = 0;## 53 ##src/debug/qlen.c##
  55. alarm(2);## 54 ##src/debug/qlen.c##
  56. if (t_connect(fd[j], &tcall, NULL) < 0) {## 55 ##src/debug/qlen.c##
  57. if (errno != EINTR)## 56 ##src/debug/qlen.c##
  58. err_xti("t_connect error, j = %d", j);## 57 ##src/debug/qlen.c##
  59. printf("timeout, %d connections completed\n", j - 1);## 58 ##src/debug/qlen.c##
  60. for (k = 1; k < j; k++)## 59 ##src/debug/qlen.c##
  61. T_close(fd[k]);## 60 ##src/debug/qlen.c##
  62. break; /* next value of qlen */## 61 ##src/debug/qlen.c##
  63. }## 62 ##src/debug/qlen.c##
  64. alarm(0);## 63 ##src/debug/qlen.c##
  65. }## 64 ##src/debug/qlen.c##
  66. if (j > MAXBACKLOG)## 65 ##src/debug/qlen.c##
  67. printf("%d connections?\n", MAXBACKLOG);## 66 ##src/debug/qlen.c##
  68. }## 67 ##src/debug/qlen.c##
  69. qlen = -1; /* tell child we're all done */## 68 ##src/debug/qlen.c##
  70. Write(pfd, &qlen, sizeof(int));## 69 ##src/debug/qlen.c##
  71. }## 70 ##src/debug/qlen.c##
  72. void## 71 ##src/debug/qlen.c##
  73. do_child(void)## 72 ##src/debug/qlen.c##
  74. {## 73 ##src/debug/qlen.c##
  75. int listenfd, qlen, junk;## 74 ##src/debug/qlen.c##
  76. struct t_bind tbind, tbindret;## 75 ##src/debug/qlen.c##
  77. Close(pipefd[1]);## 76 ##src/debug/qlen.c##
  78. Read(cfd, &qlen, sizeof(int)); /* wait for parent */## 77 ##src/debug/qlen.c##
  79. while (qlen >= 0) {## 78 ##src/debug/qlen.c##
  80. listenfd = T_open(XTI_TCP, O_RDWR, NULL);## 79 ##src/debug/qlen.c##
  81. tbind.addr.maxlen = sizeof(serv);## 80 ##src/debug/qlen.c##
  82. tbind.addr.len = sizeof(serv);## 81 ##src/debug/qlen.c##
  83. tbind.addr.buf = &serv;## 82 ##src/debug/qlen.c##
  84. tbind.qlen = qlen;## 83 ##src/debug/qlen.c##
  85. tbindret.addr.maxlen = 0;## 84 ##src/debug/qlen.c##
  86. tbindret.addr.len = 0;## 85 ##src/debug/qlen.c##
  87. T_bind(listenfd, &tbind, &tbindret);## 86 ##src/debug/qlen.c##
  88. printf("returned qlen = %d, ", tbindret.qlen);## 87 ##src/debug/qlen.c##
  89. fflush(stdout);## 88 ##src/debug/qlen.c##
  90. Write(cfd, &junk, sizeof(int)); /* tell parent */## 89 ##src/debug/qlen.c##
  91. Read(cfd, &qlen, sizeof(int)); /* just wait for parent */## 90 ##src/debug/qlen.c##
  92. T_close(listenfd); /* closes all queued connections too */## 91 ##src/debug/qlen.c##
  93. }## 92 ##src/debug/qlen.c##
  94. }## 93 ##src/debug/qlen.c##
  95. /* end qlen */