From 212bafd6109eb01654065527a8dd55206ecaf535 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 Mar 2009 21:37:17 +0100 Subject: Detect forks Some really stupid applications (Hey, vim, that means you!) love to fork after initializing gtk/libcanberra. This is really bad style. We however have to deal with this cleanly, so we try to detect the forks making sure all our calls fail cleanly after the fork. --- src/fork-detect.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/fork-detect.c (limited to 'src/fork-detect.c') diff --git a/src/fork-detect.c b/src/fork-detect.c new file mode 100644 index 0000000..622037c --- /dev/null +++ b/src/fork-detect.c @@ -0,0 +1,51 @@ +/*** + This file is part of libcanberra. + + Copyright 2009 Lennart Poettering + + libcanberra is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 2.1 of the + License, or (at your option) any later version. + + libcanberra is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with libcanberra. If not, see + . +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include "fork-detect.h" + +int ca_detect_fork(void) { + static volatile pid_t pid = (pid_t) -1; + pid_t v; + + /* Some really stupid applications (Hey, vim, that means you!) + * love to fork after initializing gtk/libcanberra. This is really + * bad style. We however have to deal with this cleanly, so we try + * to detect the forks making sure all our calls fail cleanly + * after the fork. */ + + /* Ideally we'd use atomic operations here, but we don't have them + * and this is not exactly crucial, so we don't care */ + + v = pid; + + if (v == getpid() || v == (pid_t) -1) { + pid = getpid(); + return 0; + } + + return 1; +} -- cgit