1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-05 12:17:10 +01:00

cleanup: delete remains of libutil

- exec_t belongs to service.h, the main place where it is used/needed
 - code for executing exec_t is moved to runsvc for the same reason
 - what is left are NORETURN and ARRAY_SIZE
   - the former can be replaced with direct attribute usage since
     the only relevant compilers all support the attribute.
   - the later is only used in 3 places and can be trivially replaced
     with direct usage of sizeof().

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2020-03-31 18:19:27 +02:00
parent 9f9807d4d3
commit 87a524d931
16 changed files with 67 additions and 104 deletions

View file

@ -7,7 +7,7 @@ runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
runsvc_CPPFLAGS = $(AM_CPPFLAGS)
runsvc_CFLAGS = $(AM_CFLAGS)
runsvc_LDFLAGS = $(AM_LDFLAGS)
runsvc_LDADD = libinit.a libcfg.a libutil.a
runsvc_LDADD = libinit.a libcfg.a
killall5_SOURCES = cmd/killall5.c
killall5_CPPFLAGS = $(AM_CPPFLAGS)
@ -30,7 +30,7 @@ service_SOURCES += $(SRVHEADERS)
service_CPPFLAGS = $(AM_CPPFLAGS)
service_CFLAGS = $(AM_CFLAGS)
service_LDFLAGS = $(AM_LDFLAGS)
service_LDADD = libinit.a libcfg.a libutil.a
service_LDADD = libinit.a libcfg.a
dist_man8_MANS += cmd/shutdown.8 cmd/service/service.8

View file

@ -9,9 +9,7 @@
#include <ctype.h>
#include <errno.h>
#include "util.h"
static NORETURN void usage_and_exit(void)
static __attribute__((noreturn)) void usage_and_exit(void)
{
fputs("Usage: killall5 SIGNAL\n", stderr);
exit(EXIT_FAILURE);

View file

@ -1,6 +1,49 @@
/* SPDX-License-Identifier: ISC */
#include "runsvc.h"
static int setup_tty(const char *tty, bool truncate)
{
int fd;
if (tty == NULL)
return 0;
fd = open(tty, O_RDWR);
if (fd < 0) {
perror(tty);
return -1;
}
if (truncate)
ftruncate(fd, 0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
setsid();
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
return 0;
}
static __attribute__((noreturn)) void argv_exec(exec_t *e)
{
char **argv = alloca(sizeof(char *) * (e->argc + 1)), *ptr;
int i;
for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1)
argv[i] = ptr;
argv[i] = NULL;
execvp(argv[0], argv);
perror(argv[0]);
exit(EXIT_FAILURE);
}
static int run_sequentially(exec_t *list)
{
pid_t ret, pid;

View file

@ -4,6 +4,8 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
@ -12,7 +14,7 @@
#include "service.h"
#include "libcfg.h"
#include "util.h"
#include "config.h"
#define ENVFILE ETCPATH "/initd.env"

View file

@ -5,7 +5,6 @@
#include "servicecmd.h"
#include "service.h"
#include "config.h"
#include "util.h"
command_t *commands;

View file

@ -2,13 +2,14 @@
#ifndef SERVICECMD_H
#define SERVICECMD_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "service.h"
#include "util.h"
#include "config.h"
/*
Describes a command that can be launched by passing its name as
@ -45,7 +46,7 @@ service_t *loadsvc(const char *directory, const char *filename, int flags);
Implemented in servicecmd.c. Prints program usage message and
terminates with the given exit status.
*/
void usage(int status) NORETURN;
void usage(int status) __attribute__((noreturn));
/*
Write a message to stderr that advises the user how to consult the

View file

@ -10,8 +10,6 @@
#include <sys/reboot.h>
#include <linux/reboot.h>
#include "util.h"
#define FL_FORCE 0x01
#define FL_NOSYNC 0x02
@ -29,7 +27,7 @@ static const char *shortopt = "hprfn";
static const char *defact_str = "power-off";
static int defact = RB_POWER_OFF;
static NORETURN void usage(const char *progname, int status)
static __attribute__((noreturn)) void usage(const char *progname, int status)
{
fprintf(status == EXIT_SUCCESS ? stdout : stderr,
"%s [OPTIONS...]\n\n"

View file

@ -3,6 +3,6 @@ init_SOURCES += initd/status.c initd/supervisor.c initd/initsock.c
init_CPPFLAGS = $(AM_CPPFLAGS)
init_CFLAGS = $(AM_CFLAGS)
init_LDFLAGS = $(AM_LDFLAGS)
init_LDADD = libinit.a libcfg.a libutil.a
init_LDADD = libinit.a libcfg.a
sbin_PROGRAMS += init

View file

@ -17,11 +17,12 @@
#include <linux/reboot.h>
#include <sys/signalfd.h>
#include <sys/reboot.h>
#include <stdbool.h>
#include <signal.h>
#include "initsock.h"
#include "service.h"
#include "util.h"
#include "config.h"
#define RUNSVCBIN SCRIPTDIR "/runsvc"

View file

@ -7,13 +7,9 @@ libinit_a_SOURCES += lib/init/init_socket_recv_status.c
libinit_a_CPPFLAGS = $(AM_CPPFLAGS)
libinit_a_CFLAGS = $(AM_CFLAGS)
libutil_a_SOURCES = lib/util/argv_exec.c lib/include/util.h
libutil_a_CPPFLAGS = $(AM_CPPFLAGS)
libutil_a_CFLAGS = $(AM_CFLAGS)
libcfg_a_SOURCES = lib/libcfg/rdline.c lib/libcfg/unescape.c lib/libcfg/rdcfg.c
libcfg_a_SOURCES += lib/libcfg/pack_argv.c lib/include/libcfg.h
libcfg_a_CPPFLAGS = $(AM_CPPFLAGS)
libcfg_a_CFLAGS = $(AM_CFLAGS)
noinst_LIBRARIES += libinit.a libcfg.a libutil.a
noinst_LIBRARIES += libinit.a libcfg.a

View file

@ -4,7 +4,11 @@
#include <sys/types.h>
#include "util.h"
typedef struct exec_t {
struct exec_t *next;
int argc; /* number of elements in argument vector */
char args[]; /* argument vectot string blob */
} exec_t;
enum {
/*

View file

@ -1,28 +0,0 @@
/* SPDX-License-Identifier: ISC */
#ifndef UTIL_H
#define UTIL_H
#include <sys/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "config.h"
#ifdef __GNUC__
#define NORETURN __attribute__((noreturn))
#endif
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
typedef struct exec_t {
struct exec_t *next;
int argc; /* number of elements in argument vector */
char args[]; /* argument vectot string blob */
} exec_t;
int setup_tty(const char *tty, bool truncate);
NORETURN void argv_exec(exec_t *e);
#endif /* UTIL_H */

View file

@ -11,7 +11,6 @@
#include "service.h"
#include "libcfg.h"
#include "util.h"
static int try_unescape(char *arg, rdline_t *rd)
{
@ -265,8 +264,10 @@ service_t *rdsvc(int dirfd, const char *filename, int flags)
memcpy(svc->name, filename, nlen);
svc->id = -1;
if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags))
if (rdcfg(svc, &rd, svc_params,
sizeof(svc_params) / sizeof(svc_params[0]), flags)) {
goto fail;
}
out:
rdline_cleanup(&rd);

View file

@ -23,7 +23,7 @@ int svc_type_from_string(const char *type)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(type_map); ++i) {
for (i = 0; i < sizeof(type_map) / sizeof(type_map[0]); ++i) {
if (strcmp(type_map[i], type) == 0)
return i;
}
@ -40,7 +40,7 @@ int svc_target_from_string(const char *target)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(target_map); ++i) {
for (i = 0; i < sizeof(target_map) / sizeof(target_map[0]); ++i) {
if (strcmp(target_map[i], target) == 0)
return i;
}

View file

@ -8,7 +8,6 @@
#include <fcntl.h>
#include "libcfg.h"
#include "util.h"
int rdline_init(rdline_t *t, int dirfd, const char *filename,
int argc, const char *const *argv)

View file

@ -1,51 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include "service.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
int setup_tty(const char *tty, bool truncate)
{
int fd;
if (tty == NULL)
return 0;
fd = open(tty, O_RDWR);
if (fd < 0) {
perror(tty);
return -1;
}
if (truncate)
ftruncate(fd, 0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
setsid();
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
return 0;
}
void argv_exec(exec_t *e)
{
char **argv = alloca(sizeof(char *) * (e->argc + 1)), *ptr;
int i;
for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1)
argv[i] = ptr;
argv[i] = NULL;
execvp(argv[0], argv);
perror(argv[0]);
exit(EXIT_FAILURE);
}