spcd_event.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. # define FD_SET_CAST(x) (x)
  41. struct {SbInputCallbackProc handler; void* data; }
  42. SPCD_input_handlers [FD_SETSIZE],
  43. SPCD_except_handlers[FD_SETSIZE];
  44. int SPCD_max_fd = 1;
  45. fd_set Sb_Input_Mask, Sb_Except_Mask;
  46. SbInputId SPCD_AddInput(int fd, SbInputCallbackProc proc, void* data)
  47. {SPCD_input_handlers[fd].handler = proc;
  48. SPCD_input_handlers[fd].data = data;
  49. FD_SET(fd, &Sb_Input_Mask);
  50. if (SPCD_max_fd < fd) SPCD_max_fd = fd;
  51. return fd; }
  52. SbInputId SPCD_AddException(int fd, SbInputCallbackProc proc, void* data)
  53. {SPCD_except_handlers[fd].handler = proc;
  54. SPCD_except_handlers[fd].data = data;
  55. FD_SET(fd, &Sb_Except_Mask);
  56. if (SPCD_max_fd < fd) SPCD_max_fd = fd;
  57. return fd; }
  58. void SPCD_RemoveInput(SbInputId id)
  59. {FD_CLR(id, &Sb_Input_Mask);
  60. }
  61. void SPCD_RemoveException(SbInputId id)
  62. {FD_CLR(id, &Sb_Except_Mask);
  63. }
  64. void SPCD_MainLoopUntil(Boolean *flag)
  65. {
  66. int fd_vec_size = howmany(SPCD_max_fd, NFDBITS);
  67. fd_set input_mask, except_mask;
  68. int n, fd;
  69. int result;
  70. do {
  71. memcpy(&input_mask, &Sb_Input_Mask, sizeof(fd_set));
  72. memcpy(&except_mask, &Sb_Except_Mask, sizeof(fd_set));
  73. do result=select(SPCD_max_fd + 1, FD_SET_CAST(&input_mask),
  74. FD_SET_CAST(NULL),
  75. FD_SET_CAST(&except_mask), NULL);
  76. while(result == -1 && errno==EINTR);
  77. if(result < 0) {
  78. SPC_Error(SPC_Bad_Select);
  79. SPC_Format_Log((XeString)"Exiting server ...");
  80. SPC_Close_Log();
  81. exit (3);
  82. }
  83. /* Modified loop to break after a single hit on the select. This */
  84. /* is necessary because there is nothing which stops the lower */
  85. /* level input handlers from reading the data from a random file */
  86. /* descriptor. If this happens, and the random file descriptor */
  87. /* happens to be one on which this loop detected input, the */
  88. /* process might hang. The solution is therefore to go back to */
  89. /* the select after every input handler call. */
  90. for (fd=0; fd < SPCD_max_fd+1; fd++)
  91. {
  92. SbInputId id = fd;
  93. if(FD_ISSET(fd, &except_mask)) {
  94. (*SPCD_except_handlers[fd].handler)(SPCD_except_handlers[fd].data, &fd, &id);
  95. break;
  96. }
  97. if(FD_ISSET(fd, &input_mask)) {
  98. (*SPCD_input_handlers[fd].handler)(SPCD_input_handlers[fd].data, &fd, &id);
  99. break;
  100. }
  101. }
  102. } while (!*flag);
  103. }
  104. void SPCD_BreakMainLoop(void)
  105. {
  106. /* no need to do anything */
  107. }