Move orbit functionality into kore directly.

Makes more sense and reads easier:

kore create myapp
kore build myapp
kore run myapp

Note that kore retains its cli options (if no command was given),
meaning you can still start kore in the traditional way as well.

The command options are simply to make development easier.
This commit is contained in:
Joris Vink 2014-08-01 13:59:47 +02:00
parent 818dd84acd
commit ea5b89d20b
4 changed files with 139 additions and 115 deletions

View File

@ -2,25 +2,20 @@
CC=gcc CC=gcc
KORE=kore KORE=kore
ORBIT=orbit
INSTALL_DIR=/usr/local/bin INSTALL_DIR=/usr/local/bin
INCLUDE_DIR=/usr/local/include/kore INCLUDE_DIR=/usr/local/include/kore
S_SRC= src/kore.c src/accesslog.c src/auth.c src/buf.c src/config.c \ S_SRC= src/kore.c src/accesslog.c src/auth.c src/buf.c src/cli.c src/config.c \
src/connection.c src/domain.c src/http.c src/mem.c src/module.c \ src/connection.c src/domain.c src/http.c src/mem.c src/module.c \
src/net.c src/pool.c src/spdy.c src/validator.c src/utils.c \ src/net.c src/pool.c src/spdy.c src/validator.c src/utils.c \
src/worker.c src/zlib_dict.c src/worker.c src/zlib_dict.c
S_OBJS= $(S_SRC:.c=.o) S_OBJS= $(S_SRC:.c=.o)
O_SRC= src/orbit.c
CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+=-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual CFLAGS+=-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual
CFLAGS+=-Wsign-compare -Iincludes -g CFLAGS+=-Wsign-compare -Iincludes -g
LDFLAGS+=-rdynamic -lssl -lcrypto -lz LDFLAGS+=-rdynamic -lssl -lcrypto -lz
ORBIT_CFLAGS=$(CFLAGS)
ifneq ("$(DEBUG)", "") ifneq ("$(DEBUG)", "")
CFLAGS+=-DKORE_DEBUG CFLAGS+=-DKORE_DEBUG
endif endif
@ -59,19 +54,16 @@ else
S_SRC+=src/bsd.c S_SRC+=src/bsd.c
endif endif
all: $(S_OBJS) $(O_SRC) all: $(S_OBJS)
$(CC) $(ORBIT_CFLAGS) $(O_SRC) -o $(ORBIT)
$(CC) $(LDFLAGS) $(S_OBJS) -o $(KORE) $(CC) $(LDFLAGS) $(S_OBJS) -o $(KORE)
install: install:
mkdir -p $(INCLUDE_DIR) mkdir -p $(INCLUDE_DIR)
install -m 555 $(KORE) $(INSTALL_DIR)/$(KORE) install -m 555 $(KORE) $(INSTALL_DIR)/$(KORE)
install -m 555 $(ORBIT) $(INSTALL_DIR)/$(ORBIT)
install -m 644 includes/*.h $(INCLUDE_DIR) install -m 644 includes/*.h $(INCLUDE_DIR)
uninstall: uninstall:
rm -f $(INSTALL_DIR)/$(KORE) rm -f $(INSTALL_DIR)/$(KORE)
rm -f $(INSTALL_DIR)/$(ORBIT)
rm -rf $(INCLUDE_DIR) rm -rf $(INCLUDE_DIR)
.c.o: .c.o:
@ -79,6 +71,6 @@ uninstall:
clean: clean:
find . -type f -name \*.o -exec rm {} \; find . -type f -name \*.o -exec rm {} \;
rm -f $(KORE) $(ORBIT) rm -f $(KORE)
.PHONY: clean .PHONY: clean

View File

@ -331,6 +331,9 @@ extern struct kore_domain *primary_dom;
extern struct passwd *pw; extern struct passwd *pw;
extern struct kore_pool nb_pool; extern struct kore_pool nb_pool;
void kore_cli_usage(int);
int kore_cli_main(int, char **);
void kore_signal(int); void kore_signal(int);
void kore_worker_wait(int); void kore_worker_wait(int);
void kore_worker_init(void); void kore_worker_init(void);

View File

@ -29,7 +29,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#define errno_s strerror(errno) #include "kore.h"
struct cmd { struct cmd {
const char *name; const char *name;
@ -49,35 +49,36 @@ struct cfile {
TAILQ_HEAD(cfile_list, cfile); TAILQ_HEAD(cfile_list, cfile);
static void usage(void); static void cli_fatal(const char *, ...);
static void fatal(const char *, ...);
static void *orbit_malloc(size_t); static void *cli_malloc(size_t);
static void orbit_run_kore(void *); static void cli_run_kore(void *);
static void orbit_link_library(void *); static void cli_link_library(void *);
static void orbit_compile_cfile(void *); static void cli_compile_cfile(void *);
static void orbit_mkdir(const char *, int); static void cli_mkdir(const char *, int);
static int orbit_dir_exists(const char *); static int cli_dir_exists(const char *);
static void orbit_find_cfiles(const char *); static void cli_find_cfiles(const char *);
static void orbit_file_open(const char *, int *); static void cli_file_open(const char *, int *);
static void orbit_file_write(int, const void *, size_t); static void cli_file_write(int, const void *, size_t);
static int orbit_vasprintf(char **, const char *, ...); static int cli_vasprintf(char **, const char *, ...);
static void orbit_spawn_proc(void (*cb)(void *), void *); static void cli_spawn_proc(void (*cb)(void *), void *);
static void orbit_file_create(const char *, const char *, size_t); static void cli_file_create(const char *, const char *, size_t);
static void orbit_run(int, char **); static void cli_run(int, char **);
static void orbit_build(int, char **); static void cli_help(int, char **);
static void orbit_create(int, char **); static void cli_build(int, char **);
static void cli_create(int, char **);
static void file_create_src(void); static void file_create_src(void);
static void file_create_config(void); static void file_create_config(void);
static void file_create_gitignore(void); static void file_create_gitignore(void);
static struct cmd cmds[] = { static struct cmd cmds[] = {
{ "create", "Create a new application", orbit_create }, { "help", "This help text", cli_help },
{ "run", "Run an application", orbit_run }, { "run", "Run an application", cli_run },
{ "build", "Builds an application", orbit_build }, { "build", "Build an application", cli_build },
{ NULL, NULL, NULL } { "create", "Create a new application skeleton", cli_create },
{ NULL, NULL, NULL }
}; };
static struct filegen gen_files[] = { static struct filegen gen_files[] = {
@ -126,28 +127,29 @@ static struct cfile_list source_files;
static int cfiles_count; static int cfiles_count;
static struct cmd *command = NULL; static struct cmd *command = NULL;
static void void
usage(void) kore_cli_usage(int local)
{ {
int i; int i;
fprintf(stderr, "Usage: orbit [command]\n"); if (local)
for (i = 0; cmds[i].name != NULL; i++) fprintf(stderr, "Usage: kore [command]\n");
printf("\t%s - %s\n", cmds[i].name, cmds[i].descr);
fprintf(stderr, "\nAvailable commands:\n");
for (i = 0; cmds[i].name != NULL; i++)
printf("\t%s\t%s\n", cmds[i].name, cmds[i].descr);
fprintf(stderr, "\nFind more information on https://kore.io\n");
exit(1); exit(1);
} }
int int
main(int argc, char *argv[]) kore_cli_main(int argc, char **argv)
{ {
int i; int i;
if (argc < 2) if (argc < 1)
usage(); kore_cli_usage(1);
argc--;
argv++;
for (i = 0; cmds[i].name != NULL; i++) { for (i = 0; cmds[i].name != NULL; i++) {
if (!strcmp(argv[0], cmds[i].name)) { if (!strcmp(argv[0], cmds[i].name)) {
@ -161,26 +163,32 @@ main(int argc, char *argv[])
if (cmds[i].name == NULL) { if (cmds[i].name == NULL) {
fprintf(stderr, "No such command: %s\n", argv[0]); fprintf(stderr, "No such command: %s\n", argv[0]);
usage(); kore_cli_usage(1);
} }
return (0); return (0);
} }
static void static void
orbit_create(int argc, char **argv) cli_help(int argc, char **argv)
{
kore_cli_usage(1);
}
static void
cli_create(int argc, char **argv)
{ {
int i; int i;
char *fpath; char *fpath;
if (argc != 1) if (argc != 1)
fatal("missing application name"); cli_fatal("missing application name");
appl = argv[0]; appl = argv[0];
orbit_mkdir(appl, 0755); cli_mkdir(appl, 0755);
for (i = 0; gen_dirs[i] != NULL; i++) { for (i = 0; gen_dirs[i] != NULL; i++) {
orbit_vasprintf(&fpath, "%s/%s", appl, gen_dirs[i]); cli_vasprintf(&fpath, "%s/%s", appl, gen_dirs[i]);
orbit_mkdir(fpath, 0755); cli_mkdir(fpath, 0755);
free(fpath); free(fpath);
} }
@ -189,44 +197,44 @@ orbit_create(int argc, char **argv)
} }
static void static void
orbit_build(int argc, char **argv) cli_build(int argc, char **argv)
{ {
struct cfile *cf; struct cfile *cf;
char pwd[PATH_MAX], *spath; char pwd[PATH_MAX], *spath;
if (argc == 0) { if (argc == 0) {
if (getcwd(pwd, sizeof(pwd)) == NULL) if (getcwd(pwd, sizeof(pwd)) == NULL)
fatal("could not get cwd: %s", errno_s); cli_fatal("could not get cwd: %s", errno_s);
rootdir = "."; rootdir = ".";
appl = basename(pwd); appl = basename(pwd);
orbit_vasprintf(&spath, "./src"); cli_vasprintf(&spath, "./src");
} else { } else {
appl = argv[0]; appl = argv[0];
rootdir = appl; rootdir = appl;
orbit_vasprintf(&spath, "%s/src", appl); cli_vasprintf(&spath, "%s/src", appl);
if (!orbit_dir_exists(spath)) if (!cli_dir_exists(spath))
fatal("%s doesn't appear to be an app", appl); cli_fatal("%s doesn't appear to be an app", appl);
} }
cfiles_count = 0; cfiles_count = 0;
TAILQ_INIT(&source_files); TAILQ_INIT(&source_files);
/* orbit_build_statics("static"); */ /* cli_build_statics("static"); */
orbit_find_cfiles(spath); cli_find_cfiles(spath);
free(spath); free(spath);
orbit_vasprintf(&spath, "%s/.objs", rootdir); cli_vasprintf(&spath, "%s/.objs", rootdir);
if (!orbit_dir_exists(spath)) if (!cli_dir_exists(spath))
orbit_mkdir(spath, 0755); cli_mkdir(spath, 0755);
TAILQ_FOREACH(cf, &source_files, list) { TAILQ_FOREACH(cf, &source_files, list) {
printf("compiling %s\n", cf->fpath); printf("compiling %s\n", cf->fpath);
orbit_spawn_proc(orbit_compile_cfile, cf); cli_spawn_proc(cli_compile_cfile, cf);
} }
orbit_spawn_proc(orbit_link_library, NULL); cli_spawn_proc(cli_link_library, NULL);
TAILQ_FOREACH(cf, &source_files, list) { TAILQ_FOREACH(cf, &source_files, list) {
if (unlink(cf->opath) == -1) if (unlink(cf->opath) == -1)
@ -240,14 +248,14 @@ orbit_build(int argc, char **argv)
} }
static void static void
orbit_run(int argc, char **argv) cli_run(int argc, char **argv)
{ {
orbit_build(argc, argv); cli_build(argc, argv);
if (chdir(rootdir) == -1) if (chdir(rootdir) == -1)
fatal("couldn't change directory to %s", rootdir); cli_fatal("couldn't change directory to %s", rootdir);
orbit_run_kore(NULL); cli_run_kore(NULL);
} }
static void static void
@ -255,8 +263,8 @@ file_create_src(void)
{ {
char *name; char *name;
(void)orbit_vasprintf(&name, "src/%s.c", appl); (void)cli_vasprintf(&name, "src/%s.c", appl);
orbit_file_create(name, src_data, strlen(src_data)); cli_file_create(name, src_data, strlen(src_data));
free(name); free(name);
} }
@ -266,9 +274,9 @@ file_create_config(void)
int l; int l;
char *name, *data; char *name, *data;
(void)orbit_vasprintf(&name, "conf/%s.conf", appl); (void)cli_vasprintf(&name, "conf/%s.conf", appl);
l = orbit_vasprintf(&data, config_data, appl); l = cli_vasprintf(&data, config_data, appl);
orbit_file_create(name, data, l); cli_file_create(name, data, l);
free(name); free(name);
free(data); free(data);
@ -280,20 +288,20 @@ file_create_gitignore(void)
int l; int l;
char *data; char *data;
l = orbit_vasprintf(&data, gitignore_data, appl); l = cli_vasprintf(&data, gitignore_data, appl);
orbit_file_create(".gitignore", data, l); cli_file_create(".gitignore", data, l);
free(data); free(data);
} }
static void static void
orbit_mkdir(const char *fpath, int mode) cli_mkdir(const char *fpath, int mode)
{ {
if (mkdir(fpath, mode) == -1) if (mkdir(fpath, mode) == -1)
fatal("orbit_mkdir(%s): %s", fpath, errno_s); cli_fatal("cli_mkdir(%s): %s", fpath, errno_s);
} }
static int static int
orbit_dir_exists(const char *fpath) cli_dir_exists(const char *fpath)
{ {
struct stat st; struct stat st;
@ -307,21 +315,21 @@ orbit_dir_exists(const char *fpath)
} }
static void static void
orbit_file_open(const char *fpath, int *fd) cli_file_open(const char *fpath, int *fd)
{ {
if ((*fd = open(fpath, O_CREAT | O_TRUNC | O_WRONLY, 0755)) == -1) if ((*fd = open(fpath, O_CREAT | O_TRUNC | O_WRONLY, 0755)) == -1)
fatal("orbit_file_open(%s): %s", fpath, errno_s); cli_fatal("cli_file_open(%s): %s", fpath, errno_s);
} }
static void static void
orbit_file_close(int fd) cli_file_close(int fd)
{ {
if (close(fd) == -1) if (close(fd) == -1)
printf("warning: close() %s\n", errno_s); printf("warning: close() %s\n", errno_s);
} }
static void static void
orbit_file_write(int fd, const void *buf, size_t len) cli_file_write(int fd, const void *buf, size_t len)
{ {
ssize_t r; ssize_t r;
const u_int8_t *d; const u_int8_t *d;
@ -334,7 +342,7 @@ orbit_file_write(int fd, const void *buf, size_t len)
if (r == -1) { if (r == -1) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
fatal("orbit_file_write: %s", errno_s); cli_fatal("cli_file_write: %s", errno_s);
} }
written += r; written += r;
@ -342,23 +350,23 @@ orbit_file_write(int fd, const void *buf, size_t len)
} }
static void static void
orbit_file_create(const char *name, const char *data, size_t len) cli_file_create(const char *name, const char *data, size_t len)
{ {
int fd; int fd;
char *fpath; char *fpath;
orbit_vasprintf(&fpath, "%s/%s", appl, name); cli_vasprintf(&fpath, "%s/%s", appl, name);
orbit_file_open(fpath, &fd); cli_file_open(fpath, &fd);
orbit_file_write(fd, data, len); cli_file_write(fd, data, len);
orbit_file_close(fd); cli_file_close(fd);
printf("created %s\n", fpath); printf("created %s\n", fpath);
free(fpath); free(fpath);
} }
static void static void
orbit_find_cfiles(const char *path) cli_find_cfiles(const char *path)
{ {
DIR *d; DIR *d;
struct cfile *cf; struct cfile *cf;
@ -366,23 +374,23 @@ orbit_find_cfiles(const char *path)
char *fpath; char *fpath;
if ((d = opendir(path)) == NULL) if ((d = opendir(path)) == NULL)
fatal("orbit_find_cfiles: opendir(%s): %s", path, errno_s); cli_fatal("cli_find_cfiles: opendir(%s): %s", path, errno_s);
while ((dp = readdir(d)) != NULL) { while ((dp = readdir(d)) != NULL) {
if (!strcmp(dp->d_name, ".") || if (!strcmp(dp->d_name, ".") ||
!strcmp(dp->d_name, "..")) !strcmp(dp->d_name, ".."))
continue; continue;
orbit_vasprintf(&fpath, "%s/%s", path, dp->d_name); cli_vasprintf(&fpath, "%s/%s", path, dp->d_name);
if (dp->d_type == DT_DIR) { if (dp->d_type == DT_DIR) {
orbit_find_cfiles(fpath); cli_find_cfiles(fpath);
free(fpath); free(fpath);
} else { } else {
cfiles_count++; cfiles_count++;
cf = orbit_malloc(sizeof(*cf)); cf = cli_malloc(sizeof(*cf));
cf->fpath = fpath; cf->fpath = fpath;
orbit_vasprintf(&(cf->opath), cli_vasprintf(&(cf->opath),
"%s/.objs/%s.o", rootdir, dp->d_name); "%s/.objs/%s.o", rootdir, dp->d_name);
TAILQ_INSERT_TAIL(&source_files, cf, list); TAILQ_INSERT_TAIL(&source_files, cf, list);
} }
@ -390,12 +398,12 @@ orbit_find_cfiles(const char *path)
} }
static void static void
orbit_compile_cfile(void *arg) cli_compile_cfile(void *arg)
{ {
struct cfile *cf = arg; struct cfile *cf = arg;
char *args[18], *ipath; char *args[18], *ipath;
orbit_vasprintf(&ipath, "-I%s/src", appl); cli_vasprintf(&ipath, "-I%s/src", appl);
args[0] = "gcc"; args[0] = "gcc";
args[1] = ipath; args[1] = ipath;
@ -421,13 +429,13 @@ orbit_compile_cfile(void *arg)
} }
static void static void
orbit_link_library(void *arg) cli_link_library(void *arg)
{ {
int idx; int idx;
struct cfile *cf; struct cfile *cf;
char *args[cfiles_count + 10], *libname; char *args[cfiles_count + 10], *libname;
orbit_vasprintf(&libname, "%s/%s.so", rootdir, appl); cli_vasprintf(&libname, "%s/%s.so", rootdir, appl);
idx = 0; idx = 0;
args[idx++] = "gcc"; args[idx++] = "gcc";
@ -452,11 +460,11 @@ orbit_link_library(void *arg)
} }
static void static void
orbit_run_kore(void *arg) cli_run_kore(void *arg)
{ {
char *args[4], *cpath; char *args[4], *cpath;
orbit_vasprintf(&cpath, "conf/%s.conf", appl); cli_vasprintf(&cpath, "conf/%s.conf", appl);
args[0] = "kore"; args[0] = "kore";
args[1] = "-fnc"; args[1] = "-fnc";
@ -467,7 +475,7 @@ orbit_run_kore(void *arg)
} }
static void static void
orbit_spawn_proc(void (*cb)(void *), void *arg) cli_spawn_proc(void (*cb)(void *), void *arg)
{ {
pid_t pid; pid_t pid;
int status; int status;
@ -475,36 +483,36 @@ orbit_spawn_proc(void (*cb)(void *), void *arg)
pid = fork(); pid = fork();
switch (pid) { switch (pid) {
case -1: case -1:
fatal("orbit_compile_cfile: fork() %s", errno_s); cli_fatal("cli_compile_cfile: fork() %s", errno_s);
/* NOTREACHED */ /* NOTREACHED */
case 0: case 0:
cb(arg); cb(arg);
fatal("orbit_spawn_proc: %s", errno_s); cli_fatal("cli_spawn_proc: %s", errno_s);
/* NOTREACHED */ /* NOTREACHED */
default: default:
break; break;
} }
if (waitpid(pid, &status, 0) == -1) if (waitpid(pid, &status, 0) == -1)
fatal("couldn't wait for child %d", pid); cli_fatal("couldn't wait for child %d", pid);
if (WEXITSTATUS(status) || WTERMSIG(status) || WCOREDUMP(status)) if (WEXITSTATUS(status) || WTERMSIG(status) || WCOREDUMP(status))
fatal("subprocess trouble, check output"); cli_fatal("subprocess trouble, check output");
} }
static void * static void *
orbit_malloc(size_t len) cli_malloc(size_t len)
{ {
void *ptr; void *ptr;
if ((ptr = malloc(len)) == NULL) if ((ptr = malloc(len)) == NULL)
fatal("orbit_malloc: %s", errno_s); cli_fatal("cli_malloc: %s", errno_s);
return (ptr); return (ptr);
} }
static int static int
orbit_vasprintf(char **out, const char *fmt, ...) cli_vasprintf(char **out, const char *fmt, ...)
{ {
int l; int l;
va_list args; va_list args;
@ -514,13 +522,13 @@ orbit_vasprintf(char **out, const char *fmt, ...)
va_end(args); va_end(args);
if (l == -1) if (l == -1)
fatal("orbit_vasprintf"); cli_fatal("cli_vasprintf");
return (l); return (l);
} }
static void static void
fatal(const char *fmt, ...) cli_fatal(const char *fmt, ...)
{ {
va_list args; va_list args;
char buf[2048]; char buf[2048];
@ -530,8 +538,8 @@ fatal(const char *fmt, ...)
va_end(args); va_end(args);
if (command != NULL) if (command != NULL)
printf("orbit %s: %s\n", command->name, buf); printf("kore %s: %s\n", command->name, buf);
else else
printf("orbit: %s\n", buf); printf("kore: %s\n", buf);
exit(1); exit(1);
} }

View File

@ -49,8 +49,17 @@ static void kore_server_sslstart(void);
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "Usage: kore [-c config] [-dfnv]\n"); fprintf(stderr, "Usage: kore [options | command]\n");
exit(1); fprintf(stderr, "\n");
fprintf(stderr, "Available options:\n");
fprintf(stderr, "\t-c\tSpecify the configuration file to use\n");
fprintf(stderr, "\t-d\tRun with debug on (if compiled in)\n");
fprintf(stderr, "\t-f\tStart kore in foreground mode\n");
fprintf(stderr, "\t-h\tThis help text\n");
fprintf(stderr, "\t-n\tDo not chroot (if not starting kore as root)\n");
fprintf(stderr, "\t-v\tDisplay kore's version information\n");
kore_cli_usage(0);
} }
static void static void
@ -72,10 +81,13 @@ version(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch;
struct listener *l; struct listener *l;
int ch, flags;
while ((ch = getopt(argc, argv, "c:dfnv")) != -1) { flags = 0;
while ((ch = getopt(argc, argv, "c:dfhnv")) != -1) {
flags++;
switch (ch) { switch (ch) {
case 'c': case 'c':
config_file = optarg; config_file = optarg;
@ -90,6 +102,9 @@ main(int argc, char *argv[])
case 'f': case 'f':
foreground = 1; foreground = 1;
break; break;
case 'h':
usage();
break;
case 'n': case 'n':
skip_chroot = 1; skip_chroot = 1;
break; break;
@ -104,6 +119,12 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc > 0) {
if (flags)
fatal("You cannot specify kore flags and a command");
return (kore_cli_main(argc, argv));
}
kore_pid = getpid(); kore_pid = getpid();
nlisteners = 0; nlisteners = 0;
LIST_INIT(&listeners); LIST_INIT(&listeners);