Restart on crash
This commit is contained in:
@@ -32,6 +32,13 @@
|
||||
#include "common.h"
|
||||
#include "../server.h"
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
#include <errno.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#ifdef HAVE_RSVG
|
||||
#include <librsvg/rsvg.h>
|
||||
@@ -620,3 +627,112 @@ GList *g_list_copy_deep(GList *list, GCopyFunc func, gpointer user_data)
|
||||
return list;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Based loosely on close_allv from
|
||||
// https://git.gnome.org/browse/glib/tree/gio/libasyncns/asyncns.c?h=2.21.0#n205
|
||||
// license: LGPL version 2.1
|
||||
// but with all the junk removed
|
||||
// and
|
||||
// https://opensource.apple.com/source/sudo/sudo-46/src/closefrom.c
|
||||
// license: BSD
|
||||
void close_all_fds()
|
||||
{
|
||||
const int from_fd = 3;
|
||||
|
||||
#ifdef __linux__
|
||||
DIR *d = opendir("/proc/self/fd");
|
||||
if (d) {
|
||||
for (struct dirent *de = readdir(d); de; de = readdir(d)) {
|
||||
if (de->d_name[0] == '.')
|
||||
continue;
|
||||
int fd = atoi(de->d_name);
|
||||
if (fd < from_fd)
|
||||
continue;
|
||||
if (fd == dirfd(d))
|
||||
continue;
|
||||
close(fd);
|
||||
}
|
||||
closedir(d);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
closefrom(from_fd);
|
||||
return;
|
||||
#endif
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
fcntl(from_fd, F_CLOSEM, 0);
|
||||
#endif
|
||||
|
||||
// Worst case scenario: iterate over all possible fds
|
||||
int max_fd = sysconf(_SC_OPEN_MAX);
|
||||
for (int fd = from_fd; fd < max_fd; fd++) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
char* get_own_path()
|
||||
{
|
||||
const int buf_size = 4096;
|
||||
char *buf = calloc(buf_size, 1);
|
||||
|
||||
#ifdef __linux__
|
||||
if (readlink("/proc/self/exe", buf, buf_size) > 0)
|
||||
return buf;
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
int mib[4] = {
|
||||
CTL_KERN,
|
||||
KERN_PROC,
|
||||
KERN_PROC_PATHNAME,
|
||||
getpid()
|
||||
};
|
||||
|
||||
size_t max_len = buf_size;
|
||||
if (sysctl(mib, 4, buf, &max_len, NULL, 0) == 0)
|
||||
return buf;
|
||||
#endif
|
||||
|
||||
#if defined(__DragonFly__)
|
||||
if (readlink("/proc/curproc/file", buf, buf_size) > 0)
|
||||
return buf;
|
||||
#endif
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
if (readlink("/proc/curproc/exe", buf, buf_size) > 0)
|
||||
return buf;
|
||||
#endif
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
int mib[4] = {
|
||||
CTL_KERN,
|
||||
KERN_PROC_ARGS,
|
||||
getpid(),
|
||||
KERN_PROC_ARGV
|
||||
};
|
||||
|
||||
char *path = NULL;
|
||||
size_t len;
|
||||
if (sysctl(mib, 4, NULL, &len, NULL, 0) == 0 && len > 0) {
|
||||
char **argv = malloc(len);
|
||||
if (argv) {
|
||||
if (sysctl(mib, 4, argv, &len, NULL, 0) == 0) {
|
||||
path = realpath(argv[0], NULL);
|
||||
}
|
||||
}
|
||||
free(argv);
|
||||
}
|
||||
if (path) {
|
||||
free(buf);
|
||||
return path;
|
||||
}
|
||||
#endif
|
||||
|
||||
sprintf(buf, "tint2");
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#define GREEN "\033[1;32m"
|
||||
#define YELLOW "\033[1;33m"
|
||||
#define RED "\033[31m"
|
||||
#define RED "\033[1;31m"
|
||||
#define BLUE "\033[1;34m"
|
||||
#define RESET "\033[0m"
|
||||
|
||||
@@ -108,4 +108,8 @@ GList *g_list_copy_deep(GList *list, GCopyFunc func, gpointer user_data);
|
||||
#define g_assert_null(expr) g_assert((expr) == NULL)
|
||||
#endif
|
||||
|
||||
void close_all_fds();
|
||||
|
||||
char* get_own_path();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -435,11 +435,16 @@ void stop_multi_timeout(timeout *t)
|
||||
|
||||
double profiling_get_time_old_time = 0;
|
||||
|
||||
double profiling_get_time()
|
||||
double get_time()
|
||||
{
|
||||
struct timespec cur_time;
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur_time);
|
||||
double t = cur_time.tv_sec + cur_time.tv_nsec * 1.0e-9;
|
||||
return cur_time.tv_sec + cur_time.tv_nsec * 1.0e-9;
|
||||
}
|
||||
|
||||
double profiling_get_time()
|
||||
{
|
||||
double t = get_time();
|
||||
if (profiling_get_time_old_time == 0)
|
||||
profiling_get_time_old_time = t;
|
||||
double delta = t - profiling_get_time_old_time;
|
||||
|
||||
@@ -69,4 +69,6 @@ struct timespec add_msec_to_timespec(struct timespec ts, int msec);
|
||||
// At the first call returns zero.
|
||||
double profiling_get_time();
|
||||
|
||||
double get_time();
|
||||
|
||||
#endif // TIMER_H
|
||||
|
||||
Reference in New Issue
Block a user