setsignal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <netdissect-stdinc.h>
  25. #include <signal.h>
  26. #ifdef HAVE_SIGACTION
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_OS_PROTO_H
  30. #include "os-proto.h"
  31. #endif
  32. #include "setsignal.h"
  33. /*
  34. * An OS-independent signal() with, whenever possible, partial BSD
  35. * semantics, i.e. the signal handler is restored following service
  36. * of the signal, but system calls are *not* restarted, so that if
  37. * "pcap_breakloop()" is called in a signal handler in a live capture,
  38. * the read/recvfrom/whatever in the live capture doesn't get restarted,
  39. * it returns -1 and sets "errno" to EINTR, so we can break out of the
  40. * live capture loop.
  41. *
  42. * We use "sigaction()" if available. We don't specify that the signal
  43. * should restart system calls, so that should always do what we want.
  44. *
  45. * Otherwise, if "sigset()" is available, it probably has BSD semantics
  46. * while "signal()" has traditional semantics, so we use "sigset()"; it
  47. * might cause system calls to be restarted for the signal, however.
  48. * I don't know whether, in any systems where it did cause system calls to
  49. * be restarted, there was a way to ask it not to do so; there may no
  50. * longer be any interesting systems without "sigaction()", however,
  51. * and, if there are, they might have "sigvec()" with SV_INTERRUPT
  52. * (which I think first appeared in 4.3BSD).
  53. *
  54. * Otherwise, we use "signal()" - which means we might get traditional
  55. * semantics, wherein system calls don't get restarted *but* the
  56. * signal handler is reset to SIG_DFL and the signal is not blocked,
  57. * so that a subsequent signal would kill the process immediately.
  58. *
  59. * Did I mention that signals suck? At least in POSIX-compliant systems
  60. * they suck far less, as those systems have "sigaction()".
  61. */
  62. RETSIGTYPE
  63. (*setsignal (int sig, RETSIGTYPE (*func)(int)))(int)
  64. {
  65. #ifdef HAVE_SIGACTION
  66. struct sigaction old, new;
  67. memset(&new, 0, sizeof(new));
  68. new.sa_handler = func;
  69. if (sig == SIGCHLD)
  70. new.sa_flags = SA_RESTART;
  71. if (sigaction(sig, &new, &old) < 0)
  72. return (SIG_ERR);
  73. return (old.sa_handler);
  74. #else
  75. #ifdef HAVE_SIGSET
  76. return (sigset(sig, func));
  77. #else
  78. return (signal(sig, func));
  79. #endif
  80. #endif
  81. }