Browse Source

Install signal handlers and store the originals in the ckpool_t

master
Con Kolivas 11 years ago
parent
commit
f864e1176c
  1. 21
      src/ckpool.c
  2. 7
      src/ckpool.h
  3. 21
      src/libckpool.h

21
src/ckpool.c

@ -21,6 +21,9 @@
#include "libckpool.h" #include "libckpool.h"
#include "generator.h" #include "generator.h"
/* Only global variable, to be used only by sighandler */
static ckpool_t *global_ckp;
static void rename_proc(const char *name) static void rename_proc(const char *name)
{ {
char buf[16]; char buf[16];
@ -137,14 +140,23 @@ static void launch_process(proc_instance_t *pi)
} }
} }
static void sighandler(int __maybe_unused sig)
{
/* Restore signal handlers so we can still quit if shutdown fails */
sigaction(SIGTERM, &global_ckp->termhandler, NULL);
sigaction(SIGINT, &global_ckp->inthandler, NULL);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct sigaction handler;
proc_instance_t proc_main; proc_instance_t proc_main;
proc_instance_t proc_generator; proc_instance_t proc_generator;
pthread_t pth_listener; pthread_t pth_listener;
ckpool_t ckp; ckpool_t ckp;
int c, ret; int c, ret;
global_ckp = &ckp;
memset(&ckp, 0, sizeof(ckp)); memset(&ckp, 0, sizeof(ckp));
while ((c = getopt(argc, argv, "c:gn:s:")) != -1) { while ((c = getopt(argc, argv, "c:gn:s:")) != -1) {
switch (c) { switch (c) {
@ -167,6 +179,15 @@ int main(int argc, char **argv)
ckp.socket_dir = strdup("/tmp/ckpool"); ckp.socket_dir = strdup("/tmp/ckpool");
realloc_strcat(&ckp.socket_dir, "/"); realloc_strcat(&ckp.socket_dir, "/");
/* Install signal handlers to shut down all child processes */
handler.sa_handler = &sighandler;
handler.sa_flags = 0;
sigemptyset(&handler.sa_mask);
sigaction(SIGTERM, &handler, &ckp.termhandler);
sigaction(SIGINT, &handler, &ckp.inthandler);
signal(SIGPIPE, SIG_IGN);
ret = mkdir(ckp.socket_dir, 0700); ret = mkdir(ckp.socket_dir, 0700);
if (ret && errno != EEXIST) if (ret && errno != EEXIST)
quit(1, "Failed to make directory %s", ckp.socket_dir); quit(1, "Failed to make directory %s", ckp.socket_dir);

7
src/ckpool.h

@ -121,11 +121,4 @@ static inline void flip_80(void *dest_p, const void *src_p)
dest[i] = bswap_32(src[i]); dest[i] = bswap_32(src[i]);
} }
struct ckpool_instance {
char *socket_dir;
char *config;
char *name;
};
typedef struct ckpool_instance ckpool_t;
#endif /* CKPOOL_H */ #endif /* CKPOOL_H */

21
src/libckpool.h

@ -12,12 +12,13 @@
#ifndef LIBCKPOOL_H #ifndef LIBCKPOOL_H
#define LIBCKPOOL_H #define LIBCKPOOL_H
#include <stdbool.h>
#include <errno.h> #include <errno.h>
#include <jansson.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <syslog.h> #include <syslog.h>
#include <jansson.h>
#define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__) #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
#define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__) #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
@ -105,6 +106,19 @@ struct cklock {
typedef struct cklock cklock_t; typedef struct cklock cklock_t;
struct proc_instance;
typedef struct proc_instance proc_instance_t;
struct ckpool_instance {
char *socket_dir;
char *config;
char *name;
struct sigaction termhandler;
struct sigaction inthandler;
};
typedef struct ckpool_instance ckpool_t;
struct connsock { struct connsock {
int fd; int fd;
char *url; char *url;
@ -122,9 +136,6 @@ struct unixsock {
typedef struct unixsock unixsock_t; typedef struct unixsock unixsock_t;
struct proc_instance;
typedef struct proc_instance proc_instance_t;
struct proc_instance { struct proc_instance {
ckpool_t *ckp; ckpool_t *ckp;
unixsock_t us; unixsock_t us;

Loading…
Cancel
Save