spcd_event.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * File: spcd_event.c $XConsortium: spcd_event.c /main/4 1996/01/15 13:49:32 rswiston $
  25. * Language: C
  26. *
  27. * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
  28. *
  29. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  30. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  31. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  32. * (c) Copyright 1993, 1994 Novell, Inc. *
  33. */
  34. #define __need_fd_set
  35. #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
  36. #include <SPC/spcP.h>
  37. #include <bms/SbEvent.h>
  38. #include <sys/types.h> /* for fd_set, FD_SET macros, et. al. */
  39. #include <errno.h>
  40. #ifndef __hpux
  41. # define FD_SET_CAST(x) (x)
  42. #else
  43. # define FD_SET_CAST(x) ((int *)(x))
  44. #endif /* __hpux */
  45. struct {SbInputCallbackProc handler; void* data; }
  46. SPCD_input_handlers [FD_SETSIZE],
  47. SPCD_except_handlers[FD_SETSIZE];
  48. int SPCD_max_fd = 1;
  49. fd_set Sb_Input_Mask, Sb_Except_Mask;
  50. SbInputId SPCD_AddInput(int fd, SbInputCallbackProc proc, void* data)
  51. {SPCD_input_handlers[fd].handler = proc;
  52. SPCD_input_handlers[fd].data = data;
  53. FD_SET(fd, &Sb_Input_Mask);
  54. if (SPCD_max_fd < fd) SPCD_max_fd = fd;
  55. return fd; }
  56. SbInputId SPCD_AddException(int fd, SbInputCallbackProc proc, void* data)
  57. {SPCD_except_handlers[fd].handler = proc;
  58. SPCD_except_handlers[fd].data = data;
  59. FD_SET(fd, &Sb_Except_Mask);
  60. if (SPCD_max_fd < fd) SPCD_max_fd = fd;
  61. return fd; }
  62. void SPCD_RemoveInput(SbInputId id)
  63. {FD_CLR(id, &Sb_Input_Mask);
  64. }
  65. void SPCD_RemoveException(SbInputId id)
  66. {FD_CLR(id, &Sb_Except_Mask);
  67. }
  68. void SPCD_MainLoopUntil(Boolean *flag)
  69. {
  70. int fd_vec_size = howmany(SPCD_max_fd, NFDBITS);
  71. fd_set input_mask, except_mask;
  72. int n, fd;
  73. int result;
  74. do {
  75. memcpy(&input_mask, &Sb_Input_Mask, sizeof(fd_set));
  76. memcpy(&except_mask, &Sb_Except_Mask, sizeof(fd_set));
  77. do result=select(SPCD_max_fd + 1, FD_SET_CAST(&input_mask),
  78. FD_SET_CAST(NULL),
  79. FD_SET_CAST(&except_mask), NULL);
  80. while(result == -1 && errno==EINTR);
  81. if(result < 0) {
  82. SPC_Error(SPC_Bad_Select);
  83. SPC_Format_Log((XeString)"Exiting server ...");
  84. SPC_Close_Log();
  85. exit (3);
  86. }
  87. /* Modified loop to break after a single hit on the select. This */
  88. /* is necessary because there is nothing which stops the lower */
  89. /* level input handlers from reading the data from a random file */
  90. /* descriptor. If this happens, and the random file descriptor */
  91. /* happens to be one on which this loop detected input, the */
  92. /* process might hang. The solution is therefore to go back to */
  93. /* the select after every input handler call. */
  94. for (fd=0; fd < SPCD_max_fd+1; fd++)
  95. {
  96. SbInputId id = fd;
  97. if(FD_ISSET(fd, &except_mask)) {
  98. (*SPCD_except_handlers[fd].handler)(SPCD_except_handlers[fd].data, &fd, &id);
  99. break;
  100. }
  101. if(FD_ISSET(fd, &input_mask)) {
  102. (*SPCD_input_handlers[fd].handler)(SPCD_input_handlers[fd].data, &fd, &id);
  103. break;
  104. }
  105. }
  106. } while (!*flag);
  107. }
  108. void SPCD_BreakMainLoop(void)
  109. {
  110. /* no need to do anything */
  111. }