Skip to content

Commit

Permalink
Com: posix warn of use of epicsThread from child after fork()
Browse files Browse the repository at this point in the history
  • Loading branch information
mdavidsaver committed Nov 13, 2021
1 parent 8fa1af4 commit fe81286
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion modules/libcom/src/osi/os/posix/osdThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,22 @@ int status;
a_p->usePolicy = arg.ok;
}
#endif


static int child_after_fork;

static void child_hook(void)
{
epicsAtomicSetIntT(&child_after_fork, 1);
}

static void once(void)
{
epicsThreadOSD *pthreadInfo;
int status;

status = pthread_atfork(NULL, NULL, &child_hook);
checkStatusOnce(status, "pthread_atfork");

pthread_key_create(&getpthreadInfo,0);
status = osdPosixMutexInit(&onceLock,PTHREAD_MUTEX_DEFAULT);
checkStatusOnceQuit(status,"osdPosixMutexInit","epicsThreadInit");
Expand Down Expand Up @@ -431,6 +440,11 @@ static void epicsThreadInit(void)
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
int status = pthread_once(&once_control,once);
checkStatusQuit(status,"pthread_once","epicsThreadInit");

if(epicsAtomicCmpAndSwapIntT(&child_after_fork, 1, 2)==1) {
fprintf(stderr, "Warning: Undefined Behavior!\n"
" Detected use of epicsThread from child process after fork()\n");
}
}

LIBCOM_API
Expand Down

0 comments on commit fe81286

Please sign in to comment.