/core/externals/google-toolbox-for-mac/Foundation/GTMSignalHandler.h

http://macfuse.googlecode.com/ · C Header · 76 lines · 12 code · 5 blank · 59 comment · 0 complexity · b050f2af796f07339bd90cb593fcfd9c MD5 · raw file

  1. //
  2. // GTMSignalHandler.h
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <Foundation/Foundation.h>
  19. // GTMSignalHandler.
  20. //
  21. // This is a very simple, easy-to-use class for registering handlers that get
  22. // called when a specific signal is delivered. Also handy for ignoring
  23. // inconvenient signals. Ignoring SIGKILL is not support for what should be
  24. // obvious reasons. You can pass nil for target & action to ignore the signal.
  25. //
  26. // Example of how to catch SIGABRT and SIGTERM while ignring SIGWINCH:
  27. // GTMSignalHandler *abrt, *term, *winch;
  28. // abrt = [[GTMSignalHandler alloc]
  29. // initWithSignal:SIGABRT
  30. // target:self
  31. // action:@selector(handleAbort:)];
  32. //
  33. // term = [[GTMSignalHandler alloc]
  34. // initWithSignal:SIGTERM
  35. // target:self
  36. // action:@selector(handleTerm:)];
  37. //
  38. // winch = [[GTMSignalHandler alloc] initWithSignal:SIGWINCH
  39. // initWithSignal:SIGWINCH
  40. // target:nil
  41. // action:NULL
  42. //
  43. // -(void)handleTerm:(int)signo {
  44. // .. do stuff ..
  45. // }
  46. //
  47. // Release the handler when you're no longer interested in handling that signal.
  48. // Note that signal(SIG_IGN, signo) is performed on each signal handled by
  49. // objects of this class, and those do not get un-done.
  50. //
  51. // Multiple handlers for the same signal is NOT supported.
  52. //
  53. // kqueue() is used to handle the signals, and the default runloop for the first
  54. // signal handler is used for signal delivery, so keep that in mind when you're
  55. // using this class. This class explicitly does not handle arbitrary runloops
  56. // and threading.
  57. //
  58. @interface GTMSignalHandler : NSObject {
  59. @private
  60. int signo_;
  61. __weak id target_;
  62. SEL action_;
  63. }
  64. // Returns a retained signal handler object that will invoke |handler| on the
  65. // |target| whenever a signal of number |signo| is delivered to the process.
  66. -(id)initWithSignal:(int)signo
  67. target:(id)target
  68. action:(SEL)action;
  69. // Invalidates the handler so that it isn't listening anymore.
  70. - (void)invalidate;
  71. @end