Add service(8) config support.
This commit is contained in:
parent
4e72a157fa
commit
818f4c4ecd
253
init/service.c
253
init/service.c
|
@ -18,9 +18,12 @@
|
|||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
@ -29,6 +32,141 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static bool array_add(void*** array_ptr,
|
||||
size_t* used_ptr,
|
||||
size_t* length_ptr,
|
||||
void* value)
|
||||
{
|
||||
void** array;
|
||||
memcpy(&array, array_ptr, sizeof(array)); // Strict aliasing.
|
||||
|
||||
if ( *used_ptr == *length_ptr )
|
||||
{
|
||||
size_t length = *length_ptr;
|
||||
if ( !length )
|
||||
length = 4;
|
||||
void** new_array = reallocarray(array, length, 2 * sizeof(void*));
|
||||
if ( !new_array )
|
||||
return false;
|
||||
array = new_array;
|
||||
memcpy(array_ptr, &array, sizeof(array)); // Strict aliasing.
|
||||
*length_ptr = length * 2;
|
||||
}
|
||||
|
||||
memcpy(array + (*used_ptr)++, &value, sizeof(value)); // Strict aliasing.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static char** tokenize(size_t* out_tokens_used, const char* string)
|
||||
{
|
||||
size_t tokens_used = 0;
|
||||
size_t tokens_length = 0;
|
||||
char** tokens = malloc(sizeof(char*));
|
||||
if ( !tokens )
|
||||
return NULL;
|
||||
bool failed = false;
|
||||
bool invalid = false;
|
||||
while ( *string )
|
||||
{
|
||||
if ( isspace((unsigned char) *string) )
|
||||
{
|
||||
string++;
|
||||
continue;
|
||||
}
|
||||
if ( *string == '#' )
|
||||
break;
|
||||
char* token;
|
||||
size_t token_size;
|
||||
FILE* fp = open_memstream(&token, &token_size);
|
||||
if ( !fp )
|
||||
{
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
bool singly = false;
|
||||
bool doubly = false;
|
||||
bool escaped = false;
|
||||
for ( char c = *string++; c; c = *string++ )
|
||||
{
|
||||
if ( !escaped && !singly && !doubly && isspace((unsigned char) c) )
|
||||
break;
|
||||
if ( !escaped && !doubly && c == '\'' )
|
||||
{
|
||||
singly = !singly;
|
||||
continue;
|
||||
}
|
||||
if ( !escaped && !singly && c == '"' )
|
||||
{
|
||||
doubly = !doubly;
|
||||
continue;
|
||||
}
|
||||
if ( !singly && !escaped && c == '\\' )
|
||||
{
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
if ( escaped )
|
||||
{
|
||||
switch ( c )
|
||||
{
|
||||
case 'a': c = '\a'; break;
|
||||
case 'b': c = '\b'; break;
|
||||
case 'e': c = '\e'; break;
|
||||
case 'f': c = '\f'; break;
|
||||
case 'n': c = '\n'; break;
|
||||
case 'r': c = '\r'; break;
|
||||
case 't': c = '\t'; break;
|
||||
case 'v': c = '\v'; break;
|
||||
default: break;
|
||||
};
|
||||
}
|
||||
escaped = false;
|
||||
if ( fputc((unsigned char) c, fp) == EOF )
|
||||
{
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( singly || doubly || escaped )
|
||||
{
|
||||
fclose(fp);
|
||||
free(token);
|
||||
invalid = true;
|
||||
break;
|
||||
}
|
||||
if ( fflush(fp) == EOF )
|
||||
{
|
||||
fclose(fp);
|
||||
free(token);
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
if ( !array_add((void***) &tokens, &tokens_used, &tokens_length,
|
||||
token) )
|
||||
{
|
||||
free(token);
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( failed || invalid )
|
||||
{
|
||||
for ( size_t i = 0; i < tokens_used; i++ )
|
||||
free(tokens[i]);
|
||||
free(tokens);
|
||||
if ( invalid )
|
||||
errno = 0;
|
||||
return NULL;
|
||||
}
|
||||
char** new_tokens = reallocarray(tokens, tokens_used, sizeof(char*));
|
||||
if ( new_tokens )
|
||||
tokens = new_tokens;
|
||||
*out_tokens_used = tokens_used;
|
||||
return tokens;
|
||||
}
|
||||
|
||||
static int open_local_client_socket(const char* path, int flags)
|
||||
{
|
||||
size_t path_length = strlen(path);
|
||||
|
@ -47,17 +185,115 @@ static int open_local_client_socket(const char* path, int flags)
|
|||
return fd;
|
||||
}
|
||||
|
||||
static void rewrite(const char* path, const char* daemon, const char* flags)
|
||||
{
|
||||
FILE* fp = fopen(path, "r");
|
||||
if ( !fp )
|
||||
{
|
||||
if ( errno != ENOENT )
|
||||
err(1, "%s", path);
|
||||
}
|
||||
char* out_path;
|
||||
if ( asprintf(&out_path, "%s.XXXXXX", path) < 0 )
|
||||
err(1, "malloc");
|
||||
int out_fd = mkstemp(out_path);
|
||||
if ( out_fd < 0 )
|
||||
err(1, "mkstemp: %s.XXXXXX", path);
|
||||
FILE* out = fdopen(out_fd, "w");
|
||||
if ( !out )
|
||||
{
|
||||
unlink(out_path);
|
||||
err(1, "fdopen");
|
||||
}
|
||||
bool found = false;
|
||||
char* line = NULL;
|
||||
size_t line_size = 0;
|
||||
ssize_t line_length;
|
||||
off_t line_number = 0;
|
||||
while ( fp && 0 < (line_length = getline(&line, &line_size, fp)) )
|
||||
{
|
||||
line_number++;
|
||||
size_t tokenc;
|
||||
char** tokens = tokenize(&tokenc, line);
|
||||
if ( !tokens )
|
||||
{
|
||||
unlink(out_path);
|
||||
if ( errno )
|
||||
err(1, "%s", path);
|
||||
else
|
||||
errx(1, "%s:%ji: Syntax error", path, (intmax_t) line_number);
|
||||
}
|
||||
if ( 2 <= tokenc &&
|
||||
!strcmp(tokens[0], "require") && !strcmp(tokens[1], daemon) )
|
||||
{
|
||||
found = true;
|
||||
if ( flags )
|
||||
fprintf(out, "require %s%s\n", daemon, flags);
|
||||
}
|
||||
else
|
||||
fputs(line, out);
|
||||
}
|
||||
free(line);
|
||||
if ( !found && flags )
|
||||
fprintf(out, "require %s%s\n", daemon, flags);
|
||||
if ( (fp && ferror(fp)) || ferror(out) || fflush(out) == EOF )
|
||||
{
|
||||
unlink(out_path);
|
||||
err(1, "%s", path);
|
||||
}
|
||||
if ( fp )
|
||||
{
|
||||
struct stat st;
|
||||
fstat(fileno(fp), &st);
|
||||
fchmod(out_fd, st.st_mode & 07777);
|
||||
fchown(out_fd, st.st_uid, st.st_gid);
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
fchmod(out_fd, 0666 & ~getumask());
|
||||
if ( rename(out_path, path) < 0 )
|
||||
{
|
||||
unlink(out_path);
|
||||
err(1, "rename: %s -> %s", out_path, path);
|
||||
}
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
static bool check_daemon_exists_in_dir(const char* dir, const char* daemon)
|
||||
{
|
||||
char* path;
|
||||
if ( asprintf(&path, "%s/%s", dir, daemon) < 0 )
|
||||
err(1, "malloc");
|
||||
bool result = !access(path, F_OK);
|
||||
free(path);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void check_daemon_exists(const char* daemon)
|
||||
{
|
||||
if ( !check_daemon_exists_in_dir("/etc/init", daemon) &&
|
||||
!check_daemon_exists_in_dir("/share/init", daemon) )
|
||||
errx(1, "%s: Daemon does not exist", daemon);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const char* init_socket = getenv("INIT_SOCKET");
|
||||
if ( !init_socket )
|
||||
init_socket = "/var/run/init";
|
||||
|
||||
bool exit_code = false;
|
||||
bool no_await = false;
|
||||
bool optional = true;
|
||||
bool raw = false;
|
||||
const char* source = "local";
|
||||
|
||||
const struct option longopts[] =
|
||||
{
|
||||
{"exit-code", no_argument, NULL, 256},
|
||||
{"no-await", no_argument, NULL, 257},
|
||||
{"no-optional", no_argument, NULL, 258},
|
||||
{"source", required_argument, NULL, 't'},
|
||||
{"raw", no_argument, NULL, 'r'},
|
||||
{0, 0, 0, 0}
|
||||
|
@ -70,6 +306,9 @@ int main(int argc, char* argv[])
|
|||
{
|
||||
case 'r': raw = true; break;
|
||||
case 's': source = optarg; break;
|
||||
case 256: exit_code = true; break;
|
||||
case 257: no_await = true; break;
|
||||
case 258: optional = false; break;
|
||||
default: return 2;
|
||||
}
|
||||
}
|
||||
|
@ -94,15 +333,25 @@ int main(int argc, char* argv[])
|
|||
const char* daemon = argv[optind++];
|
||||
const char* command = argv[optind++];
|
||||
|
||||
char flags[sizeof(" optional no-await exit-code")];
|
||||
snprintf(flags, sizeof(flags), "%s%s%s",
|
||||
optional ? " optional" : "",
|
||||
no_await ? " no-await" : "",
|
||||
exit_code ? " exit-code" : "");
|
||||
char* source_path;
|
||||
if ( asprintf(&source_path, "/etc/init/%s", source) < 0 )
|
||||
err(1, "malloc");
|
||||
|
||||
if ( !strcmp(command, "enable") )
|
||||
{
|
||||
// TODO: Write out new config.
|
||||
check_daemon_exists(daemon);
|
||||
rewrite(source_path, daemon, flags);
|
||||
if ( dprintf(fd, "require %s %s start\n", source, daemon) < 0 )
|
||||
err(1, "%s", init_socket);
|
||||
}
|
||||
else if ( !strcmp(command, "disable") )
|
||||
{
|
||||
// TODO: Write out new config.
|
||||
rewrite(source_path, daemon, NULL);
|
||||
if ( dprintf(fd, "unrequire %s %s\n", source, daemon) < 0 )
|
||||
err(1, "%s", init_socket);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue