From: Eric Paris <eparis@redhat.com>
To: selinux@tycho.nsa.gov
Subject: __thread and fork() is a big no-no
Date: Tue, 15 Jan 2013 15:35:03 -0500 [thread overview]
Message-ID: <1358282103.2081.13.camel@localhost> (raw)
Looking at the set*createcon cache patch we realized it wasn't working
right. Quickly putting together a test program we found that:
static __thread pid_t tid = 0;
int main(void)
{
pid_t child;
tid = syscall(__NR_gettid);
printf("parent=%d\n", tid);
child = fork();
if (child == 0)
printf("child=%d\n", tid);
return 0;
}
We expected to get results like:
parent=6500
child=0
Instead you find result like:
parent=6500
child=6500
Thread local storage is NOT initialized across fork(). It's always been
known that threads and fork() don't play well together:
http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them
But we weren't threading the parent or the child, so we expected it to
work. Instead we've had to add a call to pthread_atfork() which
explicitly resets tid at fork. Basically just:
static void clear(void)
{
tid = 0;
}
int main(void)
{
...
int rc;
rc = pthread_atfork(NULL, NULL, clear);
if (rc)
return rc;
...
}
We actually call this ptherad_atfork() inside the init_procattr()
function which is only called once. I'm wondering if we suffer from the
same problems with __thread variables and fork() in the library callers
elsewhere in the code. I'll take a look.
Just letting folks know something interesting we found yesterday, in
case they find it interesting...
-Eric
--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.
next reply other threads:[~2013-01-15 20:44 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-15 20:35 Eric Paris [this message]
2013-01-15 21:01 ` __thread and fork() is a big no-no Stephen Smalley
2013-01-15 21:11 ` Stephen Smalley
-- strict thread matches above, loose matches on Subject: below --
2013-01-15 21:58 Eric Paris
2013-01-15 22:27 ` Colin Walters
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1358282103.2081.13.camel@localhost \
--to=eparis@redhat.com \
--cc=selinux@tycho.nsa.gov \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.