kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 9ef669ff6fa8842de25815f8f8e4e779d564aef8
parent 443b1c8c5f23b7eb7e651248495c2bf288183765
Author: Joris Vink <joris@coders.se>
Date:   Tue,  4 Jun 2013 16:53:30 +0200

write main process pid to /var/run/kore.pid (changable in configuration)

Diffstat:
example/src/example.c | 2+-
includes/kore.h | 3+++
src/config.c | 33+++++++++++++++++++++++++--------
src/kore.c | 30++++++++++++++++++++++--------
4 files changed, 51 insertions(+), 17 deletions(-)

diff --git a/example/src/example.c b/example/src/example.c @@ -56,7 +56,7 @@ serve_style_css(struct http_request *req) tstamp = kore_date_to_time(date); free(date); - kore_log("header was present with %ld", tstamp); + kore_debug("header was present with %ld", tstamp); } tstamp = 0; diff --git a/includes/kore.h b/includes/kore.h @@ -24,6 +24,8 @@ #define errno_s strerror(errno) #define ssl_errno_s ERR_error_string(ERR_get_error(), NULL) +#define KORE_PIDFILE_DEFAULT "/var/run/kore.pid" + //#define KORE_DEBUG 1 #if defined(KORE_DEBUG) #define kore_debug(fmt, ...) \ @@ -132,6 +134,7 @@ extern char *server_ip; extern char *chroot_path; extern char *runas_user; extern char *kore_module_onload; +extern char *kore_pidfile; extern u_int8_t worker_count; extern pid_t mypid; diff --git a/src/config.c b/src/config.c @@ -38,14 +38,15 @@ #include "spdy.h" #include "kore.h" -static int configure_bind(char **); -static int configure_load(char **); -static int configure_onload(char **); -static int configure_handler(char **); -static int configure_domain(char **); -static int configure_chroot(char **); -static int configure_runas(char **); -static int configure_workers(char **); +static int configure_bind(char **); +static int configure_load(char **); +static int configure_onload(char **); +static int configure_handler(char **); +static int configure_domain(char **); +static int configure_chroot(char **); +static int configure_runas(char **); +static int configure_workers(char **); +static int configure_pidfile(char **); static struct { const char *name; @@ -60,6 +61,7 @@ static struct { { "chroot", configure_chroot }, { "runas", configure_runas }, { "workers", configure_workers }, + { "pidfile", configure_pidfile }, { NULL, NULL }, }; @@ -250,3 +252,18 @@ configure_workers(char **argv) return (KORE_RESULT_OK); } + +static int +configure_pidfile(char **argv) +{ + if (argv[1] == NULL) + return (KORE_RESULT_ERROR); + + if (strcmp(kore_pidfile, KORE_PIDFILE_DEFAULT)) { + kore_debug("duplicate pidfile directive specified"); + return (KORE_RESULT_ERROR); + } + + kore_pidfile = kore_strdup(argv[1]); + return (KORE_RESULT_OK); +} diff --git a/src/kore.c b/src/kore.c @@ -60,16 +60,18 @@ static struct passwd *pw = NULL; static u_int16_t workerid = 0; static u_int16_t cpu_count = 1; +pid_t mypid = -1; int server_port = 0; +u_int8_t worker_count = 0; char *server_ip = NULL; char *chroot_path = NULL; char *runas_user = NULL; -u_int8_t worker_count = 0; -pid_t mypid = -1; +char *kore_pidfile = KORE_PIDFILE_DEFAULT; static void kore_signal(int); static void kore_worker_wait(int); static void kore_worker_init(void); +static void kore_write_mypid(void); static int kore_socket_nonblock(int); static int kore_server_sslstart(void); static void kore_event(int, int, void *); @@ -88,8 +90,11 @@ main(int argc, char *argv[]) struct kore_worker *kw, *next; mypid = getpid(); + if (argc != 2) fatal("Usage: kore [config file]"); + if (getuid() != 0) + fatal("kore must be started as root"); kore_parse_config(argv[1]); if (!kore_module_loaded()) @@ -114,10 +119,7 @@ main(int argc, char *argv[]) fatal("cannot daemon(): %s", errno_s); mypid = getpid(); - if (chroot(chroot_path) == -1) - fatal("chroot(%s): %s", chroot_path, errno_s); - if (chdir("/") == -1) - fatal("chdir(/): %s", errno_s); + kore_write_mypid(); kore_worker_init(); @@ -125,8 +127,6 @@ main(int argc, char *argv[]) signal(SIGQUIT, kore_signal); signal(SIGHUP, kore_signal); - printf("kore has been started\n"); - for (;;) { if (sig_recv != 0) { if (sig_recv == SIGHUP) { @@ -157,6 +157,7 @@ main(int argc, char *argv[]) kore_worker_wait(1); kore_debug("server shutting down"); + unlink(kore_pidfile); close(server.fd); return (0); @@ -684,6 +685,19 @@ kore_event(int fd, int flags, void *udata) } static void +kore_write_mypid(void) +{ + FILE *fp; + + if ((fp = fopen(kore_pidfile, "w+")) == NULL) { + kore_debug("kore_write_mypid(): fopen() %s", errno_s); + } else { + fprintf(fp, "%d\n", mypid); + fclose(fp); + } +} + +static void kore_signal(int sig) { sig_recv = sig;