1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 11:19:45 +01:00

Remove some no longer needed cruft

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-11-23 14:13:52 +01:00
parent c14c3c0173
commit d9a5736bdf
11 changed files with 41 additions and 160 deletions

View file

@ -2,7 +2,6 @@ shutdown_SOURCES = cmd/shutdown.c
shutdown_CPPFLAGS = $(AM_CPPFLAGS) shutdown_CPPFLAGS = $(AM_CPPFLAGS)
shutdown_CFLAGS = $(AM_CFLAGS) shutdown_CFLAGS = $(AM_CFLAGS)
shutdown_LDFLAGS = $(AM_LDFLAGS) shutdown_LDFLAGS = $(AM_LDFLAGS)
shutdown_LDADD = libutil.a
runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
runsvc_CPPFLAGS = $(AM_CPPFLAGS) runsvc_CPPFLAGS = $(AM_CPPFLAGS)

View file

@ -17,7 +17,6 @@
static const struct option options[] = { static const struct option options[] = {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "poweroff", no_argument, NULL, 'p' }, { "poweroff", no_argument, NULL, 'p' },
{ "reboot", no_argument, NULL, 'r' }, { "reboot", no_argument, NULL, 'r' },
{ "force", no_argument, NULL, 'f' }, { "force", no_argument, NULL, 'f' },
@ -25,7 +24,7 @@ static const struct option options[] = {
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
static const char *shortopt = "hVprfn"; static const char *shortopt = "hprfn";
static const char *defact_str = "power-off"; static const char *defact_str = "power-off";
static int defact = RB_POWER_OFF; static int defact = RB_POWER_OFF;
@ -78,8 +77,6 @@ int main(int argc, char **argv)
case 'r': case 'r':
defact = RB_AUTOBOOT; defact = RB_AUTOBOOT;
break; break;
case 'V':
print_version(ptr);
case 'h': case 'h':
usage(ptr, EXIT_SUCCESS); usage(ptr, EXIT_SUCCESS);
default: default:

View file

@ -4,9 +4,7 @@ libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h
libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CPPFLAGS = $(AM_CPPFLAGS)
libinit_a_CFLAGS = $(AM_CFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS)
libutil_a_SOURCES = lib/util/argv_exec.c lib/util/enum_by_name.c libutil_a_SOURCES = lib/util/argv_exec.c lib/include/util.h
libutil_a_SOURCES += lib/util/enum_to_name.c lib/util/print_version.c
libutil_a_SOURCES += lib/util/fopenat.c lib/include/util.h
libutil_a_CPPFLAGS = $(AM_CPPFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS)
libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CFLAGS = $(AM_CFLAGS)

View file

@ -19,6 +19,8 @@ enum {
it terminates. it terminates.
*/ */
SVC_RESPAWN, SVC_RESPAWN,
SVC_MAX
}; };
enum { enum {

View file

@ -15,11 +15,6 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
typedef struct {
const char *name;
int value;
} enum_map_t;
typedef struct exec_t { typedef struct exec_t {
struct exec_t *next; struct exec_t *next;
int argc; /* number of elements in argument vector */ int argc; /* number of elements in argument vector */
@ -37,31 +32,8 @@ enum {
SOCK_FLAG_DGRAM = 0x04, SOCK_FLAG_DGRAM = 0x04,
}; };
/*
Search through an array of enum_map_t entries to resolve a string to
a numeric value. The end of the map is indicated by a sentinel entry
with the name set to NULL.
*/
const enum_map_t *enum_by_name(const enum_map_t *map, const char *name);
/*
Search through an array of enum_map_t entries to resolve a numeric
value to a string name. The end of the map is indicated by a sentinel
entry with the name set to NULL.
*/
const char *enum_to_name(const enum_map_t *map, int value);
/* print a default version info and license string */
NORETURN void print_version(const char *program);
int setup_tty(const char *tty, bool truncate); int setup_tty(const char *tty, bool truncate);
NORETURN void argv_exec(exec_t *e); NORETURN void argv_exec(exec_t *e);
/*
Similar to openat: opens a file relative to a dirfd, but returns
a FILE pointer instead of an fd.
*/
FILE *fopenat(int fd, const char *filename, const char *mode);
#endif /* UTIL_H */ #endif /* UTIL_H */

View file

@ -1,41 +1,49 @@
/* SPDX-License-Identifier: ISC */ /* SPDX-License-Identifier: ISC */
#include <string.h>
#include "service.h" #include "service.h"
#include "util.h"
static const enum_map_t type_map[] = { static const char *type_map[] = {
{ "once", SVC_ONCE }, "once",
{ "wait", SVC_WAIT }, "wait",
{ "respawn", SVC_RESPAWN }, "respawn",
{ NULL, 0 },
}; };
static const enum_map_t target_map[] = { static const char *target_map[] = {
{ "boot", TGT_BOOT }, "boot",
{ "shutdown", TGT_SHUTDOWN }, "shutdown",
{ "reboot", TGT_REBOOT }, "reboot",
{ NULL, 0 },
}; };
const char *svc_type_to_string(int type) const char *svc_type_to_string(int type)
{ {
return enum_to_name(type_map, type); return type >= 0 && type < SVC_MAX ? type_map[type] : NULL;
} }
int svc_type_from_string(const char *type) int svc_type_from_string(const char *type)
{ {
const enum_map_t *ent = enum_by_name(type_map, type); size_t i;
return ent == NULL ? -1 : ent->value; for (i = 0; i < ARRAY_SIZE(type_map); ++i) {
if (strcmp(type_map[i], type) == 0)
return i;
}
return -1;
} }
const char *svc_target_to_string(int target) const char *svc_target_to_string(int target)
{ {
return enum_to_name(target_map, target); return target >= 0 && target < TGT_MAX ? target_map[target] : NULL;
} }
int svc_target_from_string(const char *target) int svc_target_from_string(const char *target)
{ {
const enum_map_t *ent = enum_by_name(target_map, target); size_t i;
return ent == NULL ? -1 : ent->value; for (i = 0; i < ARRAY_SIZE(target_map); ++i) {
if (strcmp(target_map[i], target) == 0)
return i;
}
return -1;
} }

View file

@ -5,6 +5,7 @@
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#include "libcfg.h" #include "libcfg.h"
#include "util.h" #include "util.h"
@ -12,18 +13,26 @@
int rdline_init(rdline_t *t, int dirfd, const char *filename, int rdline_init(rdline_t *t, int dirfd, const char *filename,
int argc, const char *const *argv) int argc, const char *const *argv)
{ {
int fd = openat(dirfd, filename, O_RDONLY);
if (fd == -1)
goto fail_open;
memset(t, 0, sizeof(*t)); memset(t, 0, sizeof(*t));
t->fp = fopenat(dirfd, filename, "r"); t->fp = fdopen(fd, "r");
if (t->fp == NULL) { if (t->fp == NULL) {
perror(filename); close(fd);
return -1; goto fail_open;
} }
t->filename = filename; t->filename = filename;
t->argc = argc; t->argc = argc;
t->argv = argv; t->argv = argv;
return 0; return 0;
fail_open:
perror(filename);
return -1;
} }
void rdline_cleanup(rdline_t *t) void rdline_cleanup(rdline_t *t)

View file

@ -1,16 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include <string.h>
#include "util.h"
const enum_map_t *enum_by_name(const enum_map_t *map, const char *name)
{
size_t i;
for (i = 0; map[i].name != NULL; ++i) {
if (!strcmp(map[i].name, name))
return map + i;
}
return NULL;
}

View file

@ -1,16 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include <string.h>
#include "util.h"
const char *enum_to_name(const enum_map_t *map, int value)
{
size_t i;
for (i = 0; map[i].name != NULL; ++i) {
if (map[i].value == value)
return map[i].name;
}
return NULL;
}

View file

@ -1,54 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include "util.h"
FILE *fopenat(int dirfd, const char *filename, const char *mode)
{
const char *ptr = mode;
int fd, flags = 0;
FILE *fp;
switch (*(ptr++)) {
case 'r':
flags = O_RDONLY;
break;
case 'w':
flags = O_WRONLY | O_CREAT | O_TRUNC;
break;
case 'a':
flags = O_WRONLY | O_CREAT | O_APPEND;
break;
default:
errno = EINVAL;
return NULL;
}
if (*ptr == '+') {
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
++ptr;
}
if (*ptr == 'b')
++ptr;
if (*ptr != '\0') {
errno = EINVAL;
return NULL;
}
fd = openat(dirfd, filename, flags, 0644);
if (fd == -1)
return NULL;
fp = fdopen(fd, mode);
if (fp == NULL)
close(fd);
return fp;
}

View file

@ -1,18 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "util.h"
static const char *version_string =
"%s (pygos init) " PACKAGE_VERSION "\n"
"Copyright (C) 2018 David Oberhollenzer\n\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n";
void print_version(const char *program)
{
fprintf(stdout, version_string, program);
exit(EXIT_SUCCESS);
}