title: How to set up sigaltstack correctly?
body: <p>I have seen at least three different approaches to set up the alternative stack for <em>sigaltstack()</em>. I'm wondering which one is the best approach:</p>
  <p>Approach #1</p>
  <pre><code>stack_t sigstk;
  sigstk.ss_size = 0;
  sigstk.ss_flags = 0;
  sigstk.ss_sp = mmap (NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  if (sigstk.ss_sp != MAP_FAILED) {
      sigstk.ss_size = SIGSTKSZ;
      if (sigaltstack (&amp;sigstk, 0) &lt; 0) {
          sigstk.ss_size = 0;
          printf (&quot;sigaltstack errno=%d\n&quot;, errno);
      }
  } else {
      printf (&quot;malloc (SIGSTKSZ) failed!\n&quot;);
  }
  </code></pre>
  <p>Approach #2
  (We've been using this for a while, but the memory allocated here shows up in leak detection (<code>leaks</code> command))</p>
  <pre><code>stack_t sigstk;
  sigstk.ss_size = 0;
  sigstk.ss_flags = 0;
  sigstk.ss_sp = malloc (SIGSTKSZ);
  if (sigstk.ss_sp != NULL) {
      sigstk.ss_size = SIGSTKSZ;
      if (sigaltstack (&amp;sigstk, 0) &lt; 0) {
          sigstk.ss_size = 0;
          free (sigstk.ss_sp);
          printf (&quot;sigaltstack errno=%d\n&quot;, errno);
      }
  } else {
      printf (&quot;malloc (SIGSTKSZ) failed!\n&quot;);
  }
  </code></pre>
  <p>Approach #3</p>
  <pre><code>stack_t sigstk;
  static char ssp[SIGSTKSZ];
  sigstk.ss_size = SIGSTKSZ;
  sigstk.ss_flags = 0;
  sigstk.ss_sp = ssp;
  sigstk.ss_size = SIGSTKSZ;
  if (sigaltstack (&amp;sigstk, 0) &lt; 0) {
      sigstk.ss_size = 0;
      free (sigstk.ss_sp);
      printf (&quot;sigaltstack errno=%d\n&quot;, errno);
  }
  </code></pre>
  <p>Thanks, Ákos
  (Mac OS X 10.8.2)</p>