builder

package builder
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/builder

commit 5c44520f14b27a0f146ae0167656eac83a3b4fa3
parent 50e176598f7e1f950b4f34dc80f2abee0bde68a3
Author: enerdhil <enerdhil@bob.lan>
Date:   Sun, 22 Jan 2017 13:46:20 +0100

Add(daemonize function)

Diffstat:
Mbuilder | 0
Ainc/args.h | 25+++++++++++++++++++++++++
Minc/builder.h | 11+++++------
Asrc/args.c | 14++++++++++++++
Msrc/main.c | 70++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mtests/Makefile | 4++--
Mtests/main.c | 12+++++++++---
Dtests/test.h | 21---------------------
Dtests/test_args.c | 106-------------------------------------------------------------------------------
Dtests/test_infos.c | 98-------------------------------------------------------------------------------
10 files changed, 99 insertions(+), 262 deletions(-)

diff --git a/builder b/builder Binary files differ. diff --git a/inc/args.h b/inc/args.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhezard <jhezard@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/21 19:21:05 by jhezard #+# #+# */ +/* Updated: 2017/01/21 19:30:05 by jhezard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARGS_H +# define ARGS_H + +typedef struct s_opts { + bool verbose; + bool detach; + bool quiet; + u32_t port; + char *pid_file; +} opts_t; + + +#endif /* ARGS_H */ diff --git a/inc/builder.h b/inc/builder.h @@ -6,18 +6,17 @@ /* By: jhezard <jhezard@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 17:43:23 by jhezard #+# #+# */ -/* Updated: 2017/01/21 17:54:39 by jhezard ### ########.fr */ +/* Updated: 2017/01/21 19:30:53 by jhezard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BUILDER_H # define BUILDER_H -# include <morphux.h> +# define PID_FILE "/var/run/"NAME".pid" +# define LOG_FILE "/var/log/"NAME".log" -typedef struct s_opts { - bool force; - char *str; -} opts_t; +# include <morphux.h> +# include <args.h> #endif /* BUILDER_H */ diff --git a/src/args.c b/src/args.c @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhezard <jhezard@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/21 19:20:41 by jhezard #+# #+# */ +/* Updated: 2017/01/21 19:37:34 by jhezard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <builder.h> + diff --git a/src/main.c b/src/main.c @@ -6,49 +6,67 @@ /* By: jhezard <jhezard@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 17:04:29 by jhezard #+# #+# */ -/* Updated: 2017/01/21 18:04:29 by jhezard ### ########.fr */ +/* Updated: 2017/01/22 13:44:33 by jhezard ### ########.fr */ /* */ /* ************************************************************************** */ + #include <builder.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> + -void force(const char *str) { - m_info("%s\n", str); +void nothing(const char *str) { + (void)str; } -int list_opt_free(void *tofree) { - opts_t *tmp = tofree; - if (tofree) - free(tmp->str); - return 0; +void daemonize(void) { + pid_t pid = 0, sid = 0; + int ret = 0; + + pid = fork(); + if (pid < 0) { + m_panic("Fork failed\n"); + } else if (pid > 0) { + FILE *fp_pid; + + if (!(fp_pid = fopen(PID_FILE, "w+"))) { + m_panic("Open of the PID file "PID_FILE" failed\n"); + } + fprintf(fp_pid, "%d\n", pid); + m_info("PID of the child process is : %d\n", pid); + fclose(fp_pid); + _exit(0); + } + + waitpid(pid, &ret, 0); + if (WEXITSTATUS(ret)) + _exit(1); + + /* Reseting default file permissions */ + umask(0); + + sid = setsid(); + if (sid < 0) + m_panic("Setsid failed\n"); + + /* Closing all standard fd, we will not use it. */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); } int main(int ac, char *av[]) { margs_t opts[] = { - {'f', "force", "Do not launch the program as a daemon.", false, &force}, + {'f', "force", "Do not launch the program as a daemon.", false, &nothing}, ARGS_EOL }; - opts_t *opts2; - opts_t *opts_tmp = NULL; - mlist_t *opt_lst = NULL; - mlist_t *tmp_lst = NULL; set_program_name(NAME); set_version(VERSION); set_maintainer(MAINTAINER); read_opt(ac, av, opts); - opts2 = malloc(sizeof(opts_t)); - - opts2->force = true; - opts2->str = strdup("toto"); - list_add(opt_lst, &opts2, sizeof(opts2)); - free(opts2); - - m_info("lstsize : %d\n", list_size(opt_lst)); - - list_for_each(opt_lst, tmp_lst, opts_tmp) { - m_info("bool : %d|%s\n", opts_tmp->force, opts_tmp->str); - } - list_free(opt_lst, &list_opt_free); + daemonize(); return 0; } diff --git a/tests/Makefile b/tests/Makefile @@ -1,13 +1,13 @@ NAME = test CC = gcc -CFLAGS = -Wall -Wextra -g -O3 -I../inc -lmorphux +CFLAGS = -Wall -Wextra -g -O3 -I../lib/inc -lmorphux SRCS = $(wildcard *.c) OBJS = $(SRCS:%.c=%.o) all: $(NAME) $(NAME): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../ -lmorphux + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../lib/ -lmorphux check: $(NAME) ./$(NAME) diff --git a/tests/main.c b/tests/main.c @@ -1,10 +1,16 @@ -#include "test.h" +#include <morphux.h> + +TEST(main) { + int i = 0; + + TEST_ASSERT(i == 1, "Nope"); + return TEST_SUCCESS; +} int main(void) { u32_t ret; - register_infos_tests(); - register_args_tests(); + reg_test("main", main); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/test.h b/tests/test.h @@ -1,21 +0,0 @@ -#ifndef TEST_H -# define TEST_H - -# include <morphux.h> -# include <unistd.h> -# include <sys/types.h> -# include <sys/wait.h> - -void register_infos_tests(void); -void register_args_tests(void); - -struct margs_tests { - bool opt_q; - bool opt_w; - bool opt_e; - bool opt_r; - bool opt_t; - bool opt_y; -}; - -#endif /* TEST_H */ diff --git a/tests/test_args.c b/tests/test_args.c @@ -1,106 +0,0 @@ -#include "test.h" - -static struct margs_tests args; - -TEST(args_NULL) { - TEST_ASSERT(read_opt(0, NULL, NULL) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_NULL_1) { - TEST_ASSERT(read_opt(10, NULL, NULL) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_NULL_2) { - char *tab[] = {"test", "test2"}; - TEST_ASSERT(read_opt(0, tab, NULL) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_NULL_3) { - char *tab[] = {"test", "test2"}; - TEST_ASSERT(read_opt(10, tab, NULL) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_NULL_4) { - char *tab[] = {"test", "test2"}; - margs_t t; - - TEST_ASSERT(read_opt(0, tab, &t) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_NULL_5) { - margs_t t; - - TEST_ASSERT(read_opt(10, NULL, &t) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_empty_1) { - margs_t t; - char *tab[] = {"", "", ""}; - - TEST_ASSERT(read_opt(3, tab, &t) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - -TEST(args_empty_2) { - margs_t t; - char *tab[] = {NULL, NULL, NULL}; - - TEST_ASSERT(read_opt(3, tab, &t) == 0, "Not handling null arguments"); - return TEST_SUCCESS; -} - - -TEST(args_unhandled_1) { - fflush(stdout); - margs_t opt[] = { - {'z', "--zoiberg", "No idea.", false, NULL}, - ARGS_EOL - }; - char *av[] = {"./tests", "oui", "--allow"}; - pid_t pid; - int st, fd[2]; - - pipe(fd); - - if ((pid = fork()) == 0) { - - dup2(fd[1], 1); - dup2(fd[1], 2); - close(fd[1]); - - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling properly unknown arguments"); - } else { - pid = waitpid(pid, &st, 0); - close(fd[1]); - TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); - } - return TEST_SUCCESS; -} - - -void reset_args(void) { - args.opt_q = false; - args.opt_w = false; - args.opt_e = false; - args.opt_r = false; - args.opt_t = false; - args.opt_y = false; -} - -void register_args_tests(void) { - reg_test("m_args", args_NULL); - reg_test("m_args", args_NULL_1); - reg_test("m_args", args_NULL_2); - reg_test("m_args", args_NULL_3); - reg_test("m_args", args_NULL_4); - reg_test("m_args", args_NULL_5); - reg_test("m_args", args_empty_1); - reg_test("m_args", args_empty_2); - reg_test("m_args", args_unhandled_1); -} diff --git a/tests/test_infos.c b/tests/test_infos.c @@ -1,98 +0,0 @@ -#include "test.h" - -TEST(program_name_1) { - TEST_ASSERT((get_program_name() == NULL), "Wrong program name init"); - return TEST_SUCCESS; -} - -TEST(program_name_2) { - set_program_name("Hey !"); - TEST_ASSERT(strcmp(get_program_name(), "Hey !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(program_name_3) { - TEST_ASSERT(strcmp(get_program_name(), "Hey !") == 0, "Wrong program_name return"); - set_program_name("Yo !"); - TEST_ASSERT(strcmp(get_program_name(), "Yo !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(program_name_4) { - TEST_ASSERT(strcmp(get_program_name(), "Yo !") == 0, "Wrong program_name return"); - set_program_name(""); - TEST_ASSERT((get_program_name() == NULL), "Wrong program_name return"); - set_program_name(NULL); - TEST_ASSERT((get_program_name() == NULL), "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(version_1) { - TEST_ASSERT((get_version() == NULL), "Wrong program name init"); - return TEST_SUCCESS; -} - -TEST(version_2) { - set_version("Hey !"); - TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(version_3) { - TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); - set_version("Yo !"); - TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(version_4) { - TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); - set_version(""); - TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); - set_version(NULL); - TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(maintainer_1) { - TEST_ASSERT((get_version() == NULL), "Wrong program name init"); - return TEST_SUCCESS; -} - -TEST(maintainer_2) { - set_version("Hey !"); - TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(maintainer_3) { - TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); - set_version("Yo !"); - TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); - return TEST_SUCCESS; -} - -TEST(maintainer_4) { - TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); - set_version(""); - TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); - set_version(NULL); - TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); - return TEST_SUCCESS; -} - - -void register_infos_tests(void) { - reg_test("m_infos", program_name_1); - reg_test("m_infos", program_name_2); - reg_test("m_infos", program_name_3); - reg_test("m_infos", program_name_4); - reg_test("m_infos", version_1); - reg_test("m_infos", version_2); - reg_test("m_infos", version_3); - reg_test("m_infos", version_4); - reg_test("m_infos", maintainer_1); - reg_test("m_infos", maintainer_2); - reg_test("m_infos", maintainer_3); - reg_test("m_infos", maintainer_4); -}