userint.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * COMPONENT_NAME: austext
  25. *
  26. * FUNCTIONS: flag_shutdown
  27. * init_user_interrupt
  28. * main
  29. * user_interrupt
  30. *
  31. * ORIGINS: 27
  32. *
  33. * (C) COPYRIGHT International Business Machines Corp. 1992,1995
  34. * All Rights Reserved
  35. * US Government Users Restricted Rights - Use, duplication or
  36. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  37. */
  38. /******************** USERINT.C *************************
  39. * $XConsortium: userint.c /main/5 1996/05/07 13:49:00 drk $
  40. * April 1992.
  41. *
  42. * Init_user_interrupt() initializes interrupt handler.
  43. *
  44. * User_interrupt() returns TRUE if any of several signals
  45. * has been received anytime since initialization or last call.
  46. * These occur when user pushes CTRL-C, CTRL-BREAK,
  47. * or when process is killed by external process.
  48. * It's usually called in a major loop in a program at a place
  49. * where it is convenient to allow a graceful exit.
  50. * SIGHUP is not trapped because this module is only used in
  51. * offline programs and users often want to nohup their
  52. * offline scripts.
  53. *
  54. * Replaces quit_escape() after version 4.0 because
  55. * quit_escape() only worked on dos platform because of
  56. * the dependency on conio/curses functions.
  57. * Current user_interrupt() works on all C platforms.
  58. *
  59. * $Log$
  60. * Revision 2.4 1996/03/13 22:59:54 miker
  61. * Removed trap of SIGABRT to enable external kills with core dump.
  62. *
  63. * Revision 2.3 1996/02/01 19:28:30 miker
  64. * Made shutdown_now flag volatile.
  65. *
  66. * Revision 2.2 1995/10/25 15:03:09 miker
  67. * Added prolog.
  68. *
  69. * Revision 2.1 1995/09/22 22:21:36 miker
  70. * Freeze DtSearch 0.1, AusText 2.1.8
  71. *
  72. * Revision 1.2 1995/09/05 19:18:14 miker
  73. * Minor name changes for DtSearch.
  74. */
  75. #include "SearchP.h"
  76. #include <signal.h>
  77. /***********#define INCLUDE_MAIN******************/
  78. volatile int shutdown_now = 0; /* i.e. FALSE */
  79. /************************************************/
  80. /* */
  81. /* flag_shutdown */
  82. /* */
  83. /************************************************/
  84. /* interrupt handler for termination signals */
  85. static void flag_shutdown (int sig)
  86. {
  87. shutdown_now = sig;
  88. return;
  89. }
  90. /************************************************/
  91. /* */
  92. /* init_user_interrupt */
  93. /* */
  94. /************************************************/
  95. void init_user_interrupt (void)
  96. {
  97. signal (SIGINT, flag_shutdown); /* interrupt (ctrl-c) */
  98. signal (SIGQUIT, flag_shutdown); /* quit (ctrl-d) */
  99. signal (SIGTRAP, flag_shutdown); /* trace trap */
  100. signal (SIGKILL, flag_shutdown); /* kill -9, cannot be trapped */
  101. signal (SIGALRM, flag_shutdown); /* called alarm() polling timer */
  102. signal (SIGTERM, flag_shutdown); /* kill [-15], sfwr terminate */
  103. #ifdef SIGPWR
  104. signal (SIGPWR, flag_shutdown); /* power failure imminent */
  105. #endif
  106. signal (SIGUSR1, flag_shutdown); /* kill -30, "pings" OE */
  107. #ifdef _AIX
  108. signal (SIGXCPU, flag_shutdown); /* cpu time limit exceeded */
  109. signal (SIGDANGER, flag_shutdown); /* out of paging space,
  110. * crash immin */
  111. #endif
  112. return;
  113. } /* init_user_interrupt() */
  114. /************************************************/
  115. /* */
  116. /* user_interrupt */
  117. /* */
  118. /************************************************/
  119. int user_interrupt (void)
  120. { return shutdown_now; }
  121. #ifdef INCLUDE_MAIN
  122. /******************** main for testing ****************************/
  123. main ()
  124. {
  125. int done = FALSE;
  126. int i = 0;
  127. init_user_interrupt ();
  128. while (!user_interrupt ())
  129. printf ("\r%5d: Push CTRL-C or CTRL-BREAK to exit... ", ++i);
  130. printf ("\nWhew! Thank-you.\n");
  131. return;
  132. } /* main() */
  133. #endif
  134. /******************** USERINT.C *************************/