Subject: Re: sigjmp_buf, jmp_buf, & sigcontext
To: Matt Thomas <>
From: Charles M. Hannum <>
List: tech-userlevel
Date: 03/31/2001 01:33:10
On Sat, Mar 31, 2001 at 12:28:31AM -0800, Matt Thomas wrote:
> I'm tyring to cleanup the implementation of setjmp/longjmp &
> sigsetjmp/siglongjmp on the vax.
> A sigjmp_buf has one more long than a jmp_buf.  What is it for?

This is pretty much all covered in the setjmp(3) man page, although the
way it's written requires you to think more.

siglongjmp() and longjmp() both implement non-local gotos.  If the
`savemask' argument is true, sigsetjmp()/siglongjmp() save and restore
the signal mask.  This requires extra space in the buffer for saving
the mask, and for saving a bit that indicates whether the mask was
saved.  setjmp()/longjmp() don't do this, so the jmp_buf can be smaller
(though it doesn't actually have to be).

Typically this is done by putting the mask and the flag at the end of
sigjmp_buf, and having the initial portion overlap completely with the
jmp_buf, so that sigsetjmp() and siglongjmp() can just branch to
setjmp() and longjmp() if `savemask' was false.  However, this is an
implementation detail.

> How does sigjmp_buf interact with sigcontext and sigreturn()?

There is no particularly special interaction.  If you siglongjmp() off
the signal's stack frame, then the sigcontext may be lost -- just like
if you use longjmp().  The only difference is the restore of the mask,
which is largely orthogonal (though it may often be desirable in the
case of jumping out of a signal handler, since signal entry implicitly
frobs the mask; your other option would be to apply some foreknowledge
to set the mask to a known state).

> Does jmp_buf & sigjmp_buf need to contain a copy of the sigset_t?

sigjmp_buf certainly does!