<commit_before>/*
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of libqb.
 *
 * libqb 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.
 *
 * libqb 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 libqb.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "os_base.h"
#include <poll.h>
#include <signal.h>
#include <setjmp.h>

#include "ipc_int.h"
#include "util_int.h"
#include "ringbuffer_int.h"
#include <qb/qbdefs.h>
#include <qb/qbatomic.h>
#include <qb/qbloop.h>
#include <qb/qbrb.h>

/*
 * client functions
 * --------------------------------------------------------
 */
static void
qb_ipcc_shm_disconnect(struct qb_ipcc_connection *c)
{
	void (*rb_destructor)(struct qb_ringbuffer_s *);

	rb_destructor = qb_rb_close;
	if (!c->is_connected && (!c->server_pid || (kill(c->server_pid, 0) == -1 && errno == ESRCH))) {
		rb_destructor = qb_rb_force_close;<commit_msg>ipcc: Have a few goes at tidying up after a dead server (#434)<commit_after>/*
 * Copyright (C) 2010-2021 Red Hat, Inc.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of libqb.
 *
 * libqb 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.
 *
 * libqb 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 libqb.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "os_base.h"
#include <poll.h>
#include <signal.h>
#include <setjmp.h>

#include "ipc_int.h"
#include "util_int.h"
#include "ringbuffer_int.h"
#include <qb/qbdefs.h>
#include <qb/qbatomic.h>
#include <qb/qbloop.h>
#include <qb/qbrb.h>

/*
 * client functions
 * --------------------------------------------------------
 */
static void
qb_ipcc_shm_disconnect(struct qb_ipcc_connection *c)
{
	void (*rb_destructor)(struct qb_ringbuffer_s *);
	rb_destructor = qb_rb_close;

	/* This is an attempt to make sure that /dev/shm is cleaned up when a
	 * server exits unexpectedly. Normally it's the server's responsibility
	 * to tidy up sockets, but if it crashes or is killed with SIGKILL then
	 * the client (us) makes a reasonable attempt to tidy up the server sockets
	 * we have connected. The extra delay here just gives the server chance to
	 * disappear fully. As a client we can get here pretty quickly but shutting
	 * down a large server may take a little longer even when SIGKILLed.
	 * The 1/100th of a second is an arbitrary delay (of course) but seems to
	 * catch most servers in 2 tries or less.
	 */
	if (!c->is_connected && c->server_pid) {
		int attempt = 0;
		while (attempt++ <= 3 && rb_destructor == qb_rb_close) {
			if (kill(c->server_pid, 0) == -1 && errno == ESRCH) {
				rb_destructor = qb_rb_force_close;
			} else {
				struct timespec ts = {0, 10*QB_TIME_NS_IN_MSEC};
				struct timespec ts_left = {0, 0};
				nanosleep(&ts, &ts_left);
			}
		}
	}
	/*
	 * On FreeBSD we don't have a server PID so tidy up anyway. The
	 * server traps SIGBUS when cleaning up so will cope fine.
	 */
	if (!c->is_connected && !c->server_pid) {
		rb_destructor = qb_rb_force_close;
	}

	if (rb_destructor == qb_rb_force_close) {
		qb_util_log(LOG_DEBUG,
			    "FORCE closing server sockets\n");
{'fraction_non_alphanumeric': 0.13053152039555005, 'fraction_numerical': 0.00865265760197775, 'mean_word_length': 3.3180362860192103, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7168584', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1301, 'n_words': 496}