2020-08-14 18:52:51 +02:00
|
|
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-08-14 21:56:10 +02:00
|
|
|
#include <string.h>
|
2020-08-14 18:52:51 +02:00
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
|
|
|
const char *serviceDir = "/";
|
|
|
|
struct passwd *serviceUser;
|
|
|
|
struct group *serviceGroup;
|
2020-08-14 21:56:10 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|