Blame src/fork-detect.c

Packit 3ae693
/*-*- Mode: C; c-basic-offset: 8 -*-*/
Packit 3ae693
Packit 3ae693
/***
Packit 3ae693
  This file is part of libcanberra.
Packit 3ae693
Packit 3ae693
  Copyright 2009 Lennart Poettering
Packit 3ae693
Packit 3ae693
  libcanberra is free software; you can redistribute it and/or modify
Packit 3ae693
  it under the terms of the GNU Lesser General Public License as
Packit 3ae693
  published by the Free Software Foundation, either version 2.1 of the
Packit 3ae693
  License, or (at your option) any later version.
Packit 3ae693
Packit 3ae693
  libcanberra is distributed in the hope that it will be useful, but
Packit 3ae693
  WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ae693
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit 3ae693
  Lesser General Public License for more details.
Packit 3ae693
Packit 3ae693
  You should have received a copy of the GNU Lesser General Public
Packit 3ae693
  License along with libcanberra. If not, see
Packit 3ae693
  <http://www.gnu.org/licenses/>.
Packit 3ae693
***/
Packit 3ae693
Packit 3ae693
#ifdef HAVE_CONFIG_H
Packit 3ae693
#include <config.h>
Packit 3ae693
#endif
Packit 3ae693
Packit 3ae693
#include <unistd.h>
Packit 3ae693
#include <sys/types.h>
Packit 3ae693
Packit 3ae693
#include "fork-detect.h"
Packit 3ae693
Packit 3ae693
int ca_detect_fork(void) {
Packit 3ae693
        static volatile pid_t pid = (pid_t) -1;
Packit 3ae693
        pid_t v, we;
Packit 3ae693
Packit 3ae693
        /* Some really stupid applications (Hey, vim, that means you!)
Packit 3ae693
         * love to fork after initializing gtk/libcanberra. This is really
Packit 3ae693
         * bad style. We however have to deal with this cleanly, so we try
Packit 3ae693
         * to detect the forks making sure all our calls fail cleanly
Packit 3ae693
         * after the fork. */
Packit 3ae693
Packit 3ae693
        /* Ideally we'd use atomic operations here, but we don't have them
Packit 3ae693
         * and this is not exactly crucial, so we don't care */
Packit 3ae693
Packit 3ae693
        v = pid;
Packit 3ae693
        we = getpid();
Packit 3ae693
Packit 3ae693
        if (v == we || v == (pid_t) -1) {
Packit 3ae693
                pid = we;
Packit 3ae693
                return 0;
Packit 3ae693
        }
Packit 3ae693
Packit 3ae693
        return 1;
Packit 3ae693
}