diff --git a/Makefile b/Makefile index 744a79c4..2af49ae3 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ PLUGIN_DIR ?= /lib/finit/plugins VERSION = 1.0-pre PKG = finit-$(VERSION) EXEC = finit -OBJS = finit.o helpers.o signal.o svc.o plugin.o +OBJS = finit.o conf.o helpers.o signal.o svc.o plugin.o OBJS += strlcpy.o SRCS = $(OBJS:.o=.c) DEPS = $(addprefix .,$(SRCS:.c=.d)) diff --git a/conf.c b/conf.c new file mode 100644 index 00000000..15f8aa69 --- /dev/null +++ b/conf.c @@ -0,0 +1,147 @@ +/* Parser for finit.conf + * + * Copyright (c) 2012 Joachim Nilsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "finit.h" +#include "svc.h" +#include "helpers.h" + +/* Match one command. */ +#define MATCH_CMD(l, c, x) \ + (!strncmp(l, c, strlen(c)) && (x = (l) + strlen(c))) + + +static char *build_cmd(char *cmd, char *line, int len) +{ + int l; + char *c; + + /* Trim leading whitespace */ + while (*line && (*line == ' ' || *line == '\t')) + line++; + + if (!cmd) { + cmd = malloc (strlen(line) + 1); + if (!cmd) { + _e("No memory left for '%s'", line); + return NULL; + } + *cmd = 0; + } + c = cmd + strlen(cmd); + for (l = 0; *line && *line != '#' && *line != '\t' && l < len; l++) + *c++ = *line++; + *c = 0; + + _d("cmd = %s", cmd); + return cmd; +} + +void parse_finit_conf(char *file) +{ + FILE *fp; + char line[LINE_SIZE]; + char cmd[CMD_SIZE]; + + /* Default username and hostname */ + username = strdup(DEFUSER); + hostname = strdup(DEFHOST); + + if ((fp = fopen(file, "r")) != NULL) { + char *x; + + _d("Parse %s ...", file); + while (!feof(fp)) { + if (!fgets(line, sizeof(line), fp)) + continue; + chomp(line); + + _d("conf: %s", line); + + /* Skip comments. */ + if (MATCH_CMD(line, "#", x)) { + continue; + } + /* Do this before mounting / read-write */ + if (MATCH_CMD(line, "check ", x)) { + strcpy(cmd, "/sbin/fsck -C -a "); + build_cmd(cmd, x, CMD_SIZE); + run_interactive(cmd, "Checking file system integrity on %s", x); + continue; + } + if (MATCH_CMD(line, "user ", x)) { + if (username) free(username); + username = build_cmd(NULL, x, USERNAME_SIZE); + continue; + } + if (MATCH_CMD(line, "host ", x)) { + if (hostname) free(hostname); + hostname = build_cmd(NULL, x, HOSTNAME_SIZE); + continue; + } + if (MATCH_CMD(line, "shutdown ", x)) { + if (sdown) free(sdown); + sdown = build_cmd(NULL, x, CMD_SIZE); + continue; + } + if (MATCH_CMD(line, "module ", x)) { + strcpy(cmd, "/sbin/modprobe "); + build_cmd(cmd, x, CMD_SIZE); + run_interactive(cmd, " Loading module %s", x); + continue; + } + if (MATCH_CMD(line, "mknod ", x)) { + strcpy(cmd, "/bin/mknod "); + build_cmd(cmd, x, CMD_SIZE); + run_interactive(cmd, " Creating device node %s", x); + continue; + } + if (MATCH_CMD(line, "network ", x)) { + if (network) free(network); + network = build_cmd(NULL, x, CMD_SIZE); + continue; + } + if (MATCH_CMD(line, "startx ", x)) { + if (startx) free(startx); + startx = build_cmd(NULL, x, CMD_SIZE); + continue; + } + if (MATCH_CMD(line, "service ", x)) { + if (svc_register(x)) + _e("Failed, too many services to monitor.\n"); + continue; + } + } + fclose(fp); + } +} + + +/** + * Local Variables: + * version-control: t + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/finit.c b/finit.c index 10b9f886..5a6914d6 100644 --- a/finit.c +++ b/finit.c @@ -22,7 +22,6 @@ * THE SOFTWARE. */ -#include #include #include #include @@ -35,10 +34,6 @@ #include "svc.h" #include "signal.h" -/* Match one command. */ -#define MATCH_CMD(l, c, x) \ - (!strncmp(l, c, strlen(c)) && (x = (l) + strlen(c))) - int debug = 0; int verbose = 1; char *sdown = NULL; @@ -66,111 +61,6 @@ static void parse_kernel_cmdline(void) } } -static char *build_cmd(char *cmd, char *line, int len) -{ - int l; - char *c; - - /* Trim leading whitespace */ - while (*line && (*line == ' ' || *line == '\t')) - line++; - - if (!cmd) { - cmd = malloc (strlen(line) + 1); - if (!cmd) { - _e("No memory left for '%s'", line); - return NULL; - } - *cmd = 0; - } - c = cmd + strlen(cmd); - for (l = 0; *line && *line != '#' && *line != '\t' && l < len; l++) - *c++ = *line++; - *c = 0; - - _d("cmd = %s", cmd); - return cmd; -} - -static void parse_finit_conf(char *file) -{ - FILE *fp; - char line[LINE_SIZE]; - char cmd[CMD_SIZE]; - - /* Default username and hostname */ - username = strdup(DEFUSER); - hostname = strdup(DEFHOST); - - if ((fp = fopen(file, "r")) != NULL) { - char *x; - - _d("Parse %s ...", file); - while (!feof(fp)) { - if (!fgets(line, sizeof(line), fp)) - continue; - chomp(line); - - _d("conf: %s", line); - - /* Skip comments. */ - if (MATCH_CMD(line, "#", x)) { - continue; - } - /* Do this before mounting / read-write */ - if (MATCH_CMD(line, "check ", x)) { - strcpy(cmd, "/sbin/fsck -C -a "); - build_cmd(cmd, x, CMD_SIZE); - run_interactive(cmd, "Checking file system integrity on %s", x); - continue; - } - if (MATCH_CMD(line, "user ", x)) { - if (username) free(username); - username = build_cmd(NULL, x, USERNAME_SIZE); - continue; - } - if (MATCH_CMD(line, "host ", x)) { - if (hostname) free(hostname); - hostname = build_cmd(NULL, x, HOSTNAME_SIZE); - continue; - } - if (MATCH_CMD(line, "shutdown ", x)) { - if (sdown) free(sdown); - sdown = build_cmd(NULL, x, CMD_SIZE); - continue; - } - if (MATCH_CMD(line, "module ", x)) { - strcpy(cmd, "/sbin/modprobe "); - build_cmd(cmd, x, CMD_SIZE); - run_interactive(cmd, " Loading module %s", x); - continue; - } - if (MATCH_CMD(line, "mknod ", x)) { - strcpy(cmd, "/bin/mknod "); - build_cmd(cmd, x, CMD_SIZE); - run_interactive(cmd, " Creating device node %s", x); - continue; - } - if (MATCH_CMD(line, "network ", x)) { - if (network) free(network); - network = build_cmd(NULL, x, CMD_SIZE); - continue; - } - if (MATCH_CMD(line, "startx ", x)) { - if (startx) free(startx); - startx = build_cmd(NULL, x, CMD_SIZE); - continue; - } - if (MATCH_CMD(line, "service ", x)) { - if (svc_register(x)) - _e("Failed, too many services to monitor.\n"); - continue; - } - } - fclose(fp); - } -} - int main(int UNUSED(args), char *argv[]) { struct ev_loop *loop = ev_default_loop(0); diff --git a/finit.h b/finit.h index e7e87520..816641f8 100644 --- a/finit.h +++ b/finit.h @@ -110,6 +110,10 @@ extern char *username; /* strlcpy.c */ size_t strlcpy(char *dst, const char *src, size_t siz); +/* conf.c */ +void parse_finit_conf(char *file); + + #endif /* FINIT_H_ */ /** diff --git a/svc.h b/svc.h index e6b1c983..940885ce 100644 --- a/svc.h +++ b/svc.h @@ -26,10 +26,12 @@ #define FINIT_SVC_H_ #include +#include /* strerror() */ #include /* shmat() */ #include /* pid_t */ #include "finit.h" +#include "plugin.h" typedef enum { SVC_STOP = 0, /* Disabled */