test-signal-handling.nut 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. print(os.get_signal_received());
  2. os.set_signal_received(2);
  3. print(os.get_signal_received());
  4. os.set_signal_received(0);
  5. local sig_alarm = os.signal_str2int("SIGALRM");
  6. os.signal(sig_alarm);
  7. print(os.get_signal_received());
  8. for(local i=0; i < 10; ++i) {
  9. if(i % 3) {
  10. os.raise("SIGALRM");
  11. }
  12. local sig_received = os.get_signal_received();
  13. print(i, sig_received, sig_received ? os.signal_int2str(sig_received) : "");
  14. os.set_signal_received(0);
  15. os.sleep(100);
  16. }
  17. os.set_signal_received(0);
  18. local SIGINT = os.signal_str2int("SIGINT");
  19. print(SIGINT, os.signal(SIGINT));
  20. local SIGQUIT = os.signal_str2int("SIGQUIT");
  21. print(SIGQUIT, os.signal(SIGQUIT));
  22. local SIGTERM = os.signal_str2int("SIGTERM");
  23. print(SIGTERM, os.signal(SIGTERM));
  24. local SIGALRM = os.signal_str2int("SIGALRM");
  25. print(SIGALRM, os.signal(SIGALRM));
  26. local SIGHUP = os.signal_str2int("SIGHUP");
  27. print(SIGHUP, os.signal(SIGHUP));
  28. local run_loop = true;
  29. while(run_loop) {
  30. local signal_received = os.get_signal_received();
  31. print(run_loop, signal_received, signal_received ? os.signal_int2str(signal_received) : "", SIGINT, SIGQUIT, SIGTERM);
  32. if(signal_received) {
  33. local sig_name = os.signal_int2str(signal_received);
  34. switch(sig_name) {
  35. case "SIGINT":
  36. case "SIGQUIT":
  37. case "SIGTERM":
  38. run_loop = false;
  39. break;
  40. case "SIGALRM":
  41. run_loop = false;
  42. break;
  43. case "SIGHUP":
  44. run_loop = false;
  45. break;
  46. }
  47. }
  48. /*
  49. switch(signal_received) {
  50. case SIGINT:
  51. case SIGQUIT:
  52. case SIGTERM:
  53. run_loop = false;
  54. break;
  55. case SIGALRM:
  56. run_loop = false;
  57. break;
  58. case SIGHUP:
  59. run_loop = false;
  60. break;
  61. }
  62. */
  63. if(run_loop) {
  64. os.sleep(100);
  65. }
  66. }
  67. /*
  68. run_loop = true;
  69. os.set_signal_received(0);
  70. while(run_loop) {
  71. local signal_received = os.get_signal_received();
  72. print(run_loop, signal_received, signal_received ? os.signal_int2str(signal_received) : "", SIGINT, SIGQUIT, SIGTERM);
  73. switch(signal_received) {
  74. case SIGINT:
  75. case SIGQUIT:
  76. case SIGTERM:
  77. run_loop = false;
  78. break;
  79. case SIGALRM:
  80. run_loop = false;
  81. break;
  82. case SIGHUP:
  83. run_loop = false;
  84. break;
  85. }
  86. if(run_loop) {
  87. os.sleep(100);
  88. } else {
  89. print(signal_received, os.signal_int2str(signal_received));
  90. }
  91. }
  92. */