From 91c920f4424a843d586f5c1c137ac9eaf343b6fe Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Wed, 8 Apr 2026 18:44:34 -0500 Subject: [PATCH] Initial commit --- LICENSE | 21 +++++++++++++ Makefile | 76 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 24 +++++++++++++++ include/daemon.h | 6 ++++ src/daemon.c | 59 ++++++++++++++++++++++++++++++++++++ tests/01_daemon.c | 27 +++++++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/daemon.h create mode 100644 src/daemon.c create mode 100644 tests/01_daemon.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aa2645 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [fullname] + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..95a2a59 --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +# Compiler Flags +CC := gcc +CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer + +# Directory variables +LIBDIR := lib +OBJ := obj +INC := include +SRC := src +TEST := tests + +# Filepath Pattern Matching +LIB := $(LIBDIR)/lib.a +SRCS := $(wildcard $(SRC)/*.c) +OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS)) +TESTS := $(wildcard $(TEST)/*.c) +TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS)) + +# Commands must be labeled PHONY +.PHONY: all release clean test + +# Compiler Release Flags +release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG +release: clean $(LIB) + +# Target for compilation. +all: $(LIB) + +# Target / Dependencies +$(LIB): $(OBJS) | $(LIBDIR) + $(RM) $(LIB) + ar -cvrs $@ $^ + +$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJ)/%.o: $(SRC)/%.c | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin + $(CC) $(CFLAGS) $< $(LIB) -o $@ + +# Make directories if none. +$(LIBDIR): + mkdir $@ + +$(INC): + mkdir $@ + +$(OBJ): + mkdir $@ + +$(TEST)/bin: + mkdir $@ + +# Run the tests in the bin folder and track results +test: $(LIB) $(TEST)/bin $(TESTBINS) + @SUCCESS_COUNT=0; FAILURE_COUNT=0; \ + for test in $(TESTBINS); do \ + ./$$test; \ + EXIT_CODE=$$?; \ + TEST_NAME=$(notdir $$test); \ + if [ $$EXIT_CODE -eq 0 ]; then \ + echo "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \ + SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \ + else \ + echo "\033[0;31m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (FAILURE)\033[0m"; \ + FAILURE_COUNT=$$((FAILURE_COUNT + 1)); \ + fi; \ + done; \ + echo "\n\nTests completed"; \ + echo "SUCCESS: $$SUCCESS_COUNT"; \ + echo "FAILURE: $$FAILURE_COUNT"; + +clean: + $(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..11404b1 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# daemon + +## Description +Resources and information about creating a daemon process. + +## Table of Contents + +- [Description](#description) +- [Features](#features) +- [Usage](#usage) +- [Credits / Resources](#credits--resources) +- [License](#license) + +## Features / TODOS +- [x] Code Skeleton + +## Usage + +## Credits / Resources +[Unix Network Programming](https://en.wikipedia.org/wiki/UNIX_Network_Programming)
+[ Advanced Programming in the Unix Environment ](https://en.wikipedia.org/wiki/Advanced_Programming_in_the_Unix_Environment)
+ +## License +This project is licensed under MIT - see the [LICENSE](LICENSE) file for details. diff --git a/include/daemon.h b/include/daemon.h new file mode 100644 index 0000000..2a6568d --- /dev/null +++ b/include/daemon.h @@ -0,0 +1,6 @@ +#ifndef DAEMON_INLCUDED +#define DAEMON_INLCUDED + +void skeleton_daemon(); + +#endif diff --git a/src/daemon.c b/src/daemon.c new file mode 100644 index 0000000..bdc139f --- /dev/null +++ b/src/daemon.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +void skeleton_daemon(){ + pid_t pid; + + /* Fork off the parent process */ + pid = fork(); + + /* An error occurred */ + if (pid < 0) + exit(EXIT_FAILURE); + + /* Success: Let the parent terminate */ + if (pid > 0) + exit(EXIT_SUCCESS); + + /* On success: The child process becomes session leader */ + if (setsid() < 0) + exit(EXIT_FAILURE); + + /* Catch, ignore and handle signals */ + //TODO: Implement a working signal handler */ + signal(SIGCHLD, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + /* Fork off for the second time*/ + pid = fork(); + + /* An error occurred */ + if (pid < 0) + exit(EXIT_FAILURE); + + /* Success: Let the parent terminate */ + if (pid > 0) + exit(EXIT_SUCCESS); + + /* Set new file permissions */ + umask(0); + + /* Change the working directory to the root directory */ + /* or another appropriated directory */ + chdir("/"); + + /* Close all open file descriptors */ + int x; + for (x = sysconf(_SC_OPEN_MAX); x>=0; x--) + { + close (x); + } + + /* Open the log file */ + openlog ("firstdaemon", LOG_PID, LOG_DAEMON); +} diff --git a/tests/01_daemon.c b/tests/01_daemon.c new file mode 100644 index 0000000..1402ec6 --- /dev/null +++ b/tests/01_daemon.c @@ -0,0 +1,27 @@ +#include "../include/daemon.h" +#include +#include +#include +int main() +{ + skeleton_daemon(); + + while (1) + { + //TODO: Insert daemon code here. + syslog (LOG_NOTICE, "First daemon started."); + // Calculate 1 GB in bytes (1024 * 1024 * 1024) + size_t mem_size = 1024 * 1024 * 1024; + + // Allocate the memory + char *data = (char*)malloc(mem_size); + sleep (20); + free(data); + break; + } + + syslog (LOG_NOTICE, "First daemon terminated."); + closelog(); + + return EXIT_SUCCESS; +}