mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 14:45:24 +00:00
Fix log locking problem when using fork() in the library
Ad pointed out by Dan Berrange: So if some thread in libvirtd is currently executing a logging call, while another thread calls virExec(), that other thread no longer exists in the child, but its lock is never released. So when the child then does virLogReset() it deadlocks. The only way I see to address this, is for the parent process to call virLogLock(), immediately before fork(), and then virLogUnlock() afterwards in both parent & child. This will ensure that no other thread can be holding the lock across fork(). * src/util/logging.[ch] src/libvirt_private.syms: export virLogLock() and virLogUnlock() * src/util/util.c: lock just before forking and unlock just after - in both parent and child.
This commit is contained in:
parent
8d42b9b4e8
commit
cd0ef0e09b
@ -366,6 +366,8 @@ virLogParseOutputs;
|
||||
virLogStartup;
|
||||
virLogShutdown;
|
||||
virLogReset;
|
||||
virLogLock;
|
||||
virLogUnlock;
|
||||
|
||||
|
||||
# memory.h
|
||||
|
@ -133,11 +133,11 @@ static int virLogResetOutputs(void);
|
||||
*/
|
||||
virMutex virLogMutex;
|
||||
|
||||
static void virLogLock(void)
|
||||
void virLogLock(void)
|
||||
{
|
||||
virMutexLock(&virLogMutex);
|
||||
}
|
||||
static void virLogUnlock(void)
|
||||
void virLogUnlock(void)
|
||||
{
|
||||
virMutexUnlock(&virLogMutex);
|
||||
}
|
||||
|
@ -128,6 +128,8 @@ extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c, void *data,
|
||||
* Internal logging API
|
||||
*/
|
||||
|
||||
extern void virLogLock(void);
|
||||
extern void virLogUnlock(void);
|
||||
extern int virLogStartup(void);
|
||||
extern int virLogReset(void);
|
||||
extern void virLogShutdown(void);
|
||||
|
@ -415,7 +415,15 @@ __virExec(virConnectPtr conn,
|
||||
childerr = null;
|
||||
}
|
||||
|
||||
if ((pid = fork()) < 0) {
|
||||
/* Ensure we hold the logging lock, to protect child processes
|
||||
* from deadlocking on another threads inheirited mutex state */
|
||||
virLogLock();
|
||||
pid = fork();
|
||||
|
||||
/* Unlock for both parent and child process */
|
||||
virLogUnlock();
|
||||
|
||||
if (pid < 0) {
|
||||
virReportSystemError(conn, errno,
|
||||
"%s", _("cannot fork child process"));
|
||||
goto cleanup;
|
||||
|
Loading…
Reference in New Issue
Block a user