diff options
Diffstat (limited to 'service.c')
-rw-r--r-- | service.c | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/service.c b/service.c index a81a588..b6e6ce1 100644 --- a/service.c +++ b/service.c @@ -14,18 +14,63 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <err.h> #include <grp.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h> -#include <sysexits.h> +#include <string.h> #include "daemon.h" -struct Set256 stopExits; -int restartInterval = 1000; - const char *serviceDir = "/"; struct passwd *serviceUser; struct group *serviceGroup; + +struct Prepend prepend; +struct Services services; + +static void serviceFree(struct Service *service) { + free(service->name); + free(service->command); +} + +int serviceAdd(const char *name, const char *command) { + struct Service *service = NULL; + for (size_t i = 0; i < services.len; ++i) { + if (!strcmp(services.ptr[i].name, name)) { + service = &services.ptr[i]; + break; + } + } + + if (service) { + char *dup = strdup(command); + if (!dup) return -1; + free(service->command); + service->command = dup; + return 0; + } + + if (services.len == services.cap) { + size_t cap = (services.cap ? services.cap * 2 : 8); + void *ptr = realloc(services.ptr, sizeof(*services.ptr) * cap); + if (!ptr) return -1; + services.cap = cap; + services.ptr = ptr; + } + service = &services.ptr[services.len]; + memset(service, 0, sizeof(*service)); + + service->name = strdup(name); + if (!service->name) goto err; + + service->command = strdup(command); + if (!service->command) goto err; + + services.len++; + return 0; + +err: + serviceFree(service); + return -1; +} |