1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-05-19 04:06:13 +02:00

Merge preprocessing of command lines

- Common function for splitting string into argument vector
 - Preprocess & split command lines while parsing the service file
 - Specify "before" and "after" dependencies in a single line

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-04-04 14:58:01 +02:00
parent aa1356cb19
commit 04a23330e4
17 changed files with 207 additions and 143 deletions

View file

@ -48,7 +48,7 @@ enum {
does not exit with EXIT_SUCCESS, processing of the list is aborted
and the function returns the exit status of the failed process.
*/
int runlst_wait(char **exec, size_t num, const char *ctty);
int runlst_wait(exec_t *list, const char *ctty);
/*
Does basically the same as runlst_wait, but asynchronously.
@ -60,7 +60,7 @@ int runlst_wait(char **exec, size_t num, const char *ctty);
Alternatively, if num is 1, the child process directly exec()s the
given command.
*/
pid_t runlst(char **exec, size_t num, const char *ctty);
pid_t runlst(exec_t *list, const char *ctty);
/********** setup_tty.c **********/

View file

@ -53,7 +53,7 @@ static void handle_exited(service_t *svc)
}
}
svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty);
svc->pid = runlst(svc->exec, svc->ctty);
if (svc->pid == -1) {
print_status(svc->desc, STATUS_FAIL, false);
delsvc(svc);
@ -115,7 +115,7 @@ static void start_runlevel(int level)
svc = cfg.targets[level];
cfg.targets[level] = svc->next;
if (!svc->num_exec) {
if (svc->exec == NULL) {
print_status(svc->desc, STATUS_OK, false);
delsvc(svc);
continue;
@ -124,8 +124,7 @@ static void start_runlevel(int level)
if (svc->type == SVC_WAIT) {
print_status(svc->desc, STATUS_WAIT, false);
status = runlst_wait(svc->exec, svc->num_exec,
svc->ctty);
status = runlst_wait(svc->exec, svc->ctty);
print_status(svc->desc,
status == EXIT_SUCCESS ?
@ -136,7 +135,7 @@ static void start_runlevel(int level)
if (svc->type == SVC_RESPAWN)
print_status(svc->desc, STATUS_STARTED, false);
svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty);
svc->pid = runlst(svc->exec, svc->ctty);
if (svc->pid == -1) {
print_status(svc->desc, STATUS_FAIL, false);
delsvc(svc);

View file

@ -29,42 +29,10 @@
extern char **environ;
static NORETURN void split_and_exec(char *cmd)
static NORETURN void split_and_exec(exec_t *cmd)
{
char *argv[128];
size_t i = 0;
while (*cmd != '\0') {
argv[i++] = cmd; /* FIXME: buffer overflow!! */
if (*cmd == '"') {
while (*cmd != '\0' && *cmd != '"') {
if (cmd[0] == '\\' && cmd[1] != '\0')
++cmd;
++cmd;
}
if (*cmd == '"')
*(cmd++) = '\0';
unescape(argv[i - 1]);
} else {
while (*cmd != '\0' && *cmd != ' ')
++cmd;
if (*cmd == ' ')
*(cmd++) = '\0';
}
while (*cmd == ' ')
++cmd;
}
argv[i] = NULL;
execve(argv[0], argv, environ);
perror(argv[0]);
execve(cmd->argv[0], cmd->argv, environ);
perror(cmd->argv[0]);
exit(EXIT_FAILURE);
}
@ -98,19 +66,18 @@ static int child_setup(const char *ctty)
return 0;
}
int runlst_wait(char **exec, size_t num, const char *ctty)
int runlst_wait(exec_t *list, const char *ctty)
{
pid_t ret, pid;
int status;
size_t i;
for (i = 0; i < num; ++i) {
for (; list != NULL; list = list->next) {
pid = fork();
if (pid == 0) {
if (child_setup(ctty))
exit(EXIT_FAILURE);
split_and_exec(exec[i]);
split_and_exec(list);
}
if (pid == -1) {
@ -132,7 +99,7 @@ int runlst_wait(char **exec, size_t num, const char *ctty)
return EXIT_SUCCESS;
}
pid_t runlst(char **exec, size_t num, const char *ctty)
pid_t runlst(exec_t *list, const char *ctty)
{
int status;
pid_t pid;
@ -143,11 +110,11 @@ pid_t runlst(char **exec, size_t num, const char *ctty)
if (child_setup(ctty))
exit(EXIT_FAILURE);
if (num > 1) {
status = runlst_wait(exec, num, NULL);
if (list->next != NULL) {
status = runlst_wait(list, NULL);
exit(status);
} else {
split_and_exec(exec[0]);
split_and_exec(list);
}
}

View file

@ -44,21 +44,27 @@ enum {
TGT_MAX
};
typedef struct exec_t {
char **argv;
char *raw_argv;
struct exec_t *next;
} exec_t;
typedef struct service_t {
int type; /* SVC_* service type */
int target; /* TGT_* service target */
char *name; /* canonical service name */
char *desc; /* description string */
char **exec; /* command lines to execute */
size_t num_exec; /* number of command lines */
exec_t *exec; /* command lines to execute */
char *ctty; /* controlling tty or log file */
int rspwn_limit; /* maximum respawn count */
char **before; /* services that must be executed later */
size_t num_before;
char **after; /* services that must be executed first */
size_t num_after;
char *raw_after;
char *raw_before;
pid_t pid;
int status; /* process exit status */

View file

@ -73,6 +73,8 @@ char *rdline(int fd, int argc, const char *const *argv);
*/
int unescape(char *src);
char **split_argv(char *str);
/*
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

View file

@ -21,16 +21,19 @@
void delsvc(service_t *svc)
{
size_t i;
exec_t *e;
for (i = 0; i < svc->num_exec; ++i)
free(svc->exec[i]);
while (svc->exec != NULL) {
e = svc->exec;
svc->exec = e->next;
for (i = 0; i < svc->num_before; ++i)
free(svc->before[i]);
free(e->argv);
free(e->raw_argv);
free(e);
}
for (i = 0; i < svc->num_after; ++i)
free(svc->after[i]);
free(svc->raw_before);
free(svc->raw_after);
free(svc->before);
free(svc->after);

View file

@ -38,6 +38,26 @@ static int try_unescape(char *arg, const char *filename, size_t lineno)
return 0;
}
static char **try_split_argv(char *str, const char *filename, size_t lineno)
{
char **argv = split_argv(str);
if (argv == NULL) {
switch (errno) {
case EINVAL:
fprintf(stderr, "%s: %zu: malformed string constant\n",
filename, lineno);
break;
default:
fprintf(stderr, "%s: %zu: %s\n", filename, lineno,
strerror(errno));
break;
}
}
return argv;
}
static int svc_desc(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
@ -59,107 +79,108 @@ static int svc_tty(service_t *svc, char *arg,
static int svc_exec(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1));
exec_t *e, *end;
char **argv;
if (new == NULL) {
argv = try_split_argv(arg, filename, lineno);
if (argv == NULL)
return -1;
e = calloc(1, sizeof(*e));
if (e == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
free(arg);
free(argv);
return -1;
}
svc->exec = new;
svc->exec[svc->num_exec++] = arg;
e->argv = argv;
e->raw_argv = arg;
if (svc->exec == NULL) {
svc->exec = e;
} else {
for (end = svc->exec; end->next != NULL; end = end->next)
;
end->next = e;
}
return 0;
}
static int svc_before(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
char **new;
if (try_unescape(arg, filename, lineno))
return -1;
new = realloc(svc->before, sizeof(char*) * (svc->num_before + 1));
if (new == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
free(arg);
if (svc->before != NULL) {
fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
filename, lineno);
return -1;
}
svc->before = new;
svc->before[svc->num_before++] = arg;
svc->before = try_split_argv(arg, filename, lineno);
if (svc->before == NULL)
return -1;
svc->raw_before = arg;
return 0;
}
static int svc_after(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
char **new;
if (try_unescape(arg, filename, lineno))
return -1;
new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1));
if (new == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
free(arg);
if (svc->after != NULL) {
fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
filename, lineno);
return -1;
}
svc->after = new;
svc->after[svc->num_after++] = arg;
svc->after = try_split_argv(arg, filename, lineno);
if (svc->after == NULL)
return -1;
svc->raw_after = arg;
return 0;
}
static int svc_type(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
char *extra;
int type;
char **args;
int i, type;
if (try_unescape(arg, filename, lineno))
args = try_split_argv(arg, filename, lineno);
if (args == NULL)
return -1;
for (extra = arg; *extra != ' ' && *extra != '\0'; ++extra)
;
if (*extra == ' ') {
*(extra++) = '\0';
} else {
extra = NULL;
}
type = svc_type_from_string(arg);
type = svc_type_from_string(args[0]);
if (type == -1) {
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
filename, lineno, arg);
filename, lineno, args[0]);
free(args);
return -1;
}
if (extra != NULL) {
if (args[1] != NULL) {
switch (type) {
case SVC_RESPAWN:
if (strncmp(extra, "limit ", 6) != 0)
if (strcmp(args[1], "limit") != 0)
goto fail_limit;
extra += 6;
svc->rspwn_limit = 0;
if (!isdigit(*extra))
if (!isdigit(args[2][0]))
goto fail_limit;
while (isdigit(*extra)) {
for (i = 0; isdigit(args[2][i]); ++i) {
svc->rspwn_limit *= 10;
svc->rspwn_limit += *(extra++) - '0';
svc->rspwn_limit += args[2][i] - '0';
}
if (*extra != '\0')
if (args[2][i] != '\0')
goto fail_limit;
break;
if (args[3] == NULL)
break;
/* fall-through */
default:
fprintf(stderr, "%s: %zu: unexpected extra arguments "
"for type '%s'\n", filename, lineno, arg);
@ -168,11 +189,13 @@ static int svc_type(service_t *svc, char *arg,
}
svc->type = type;
free(args);
free(arg);
return 0;
fail_limit:
fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n",
filename, lineno);
free(args);
return -1;
}

View file

@ -15,7 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "util.h"
@ -86,3 +88,74 @@ int unescape(char *src)
*(dst++) = '\0';
return 0;
}
char **split_argv(char *str)
{
size_t i = 0, cap = 0, new_cap;
char **argv = NULL, **new;
char *ptr;
ptr = str;
for (;;) {
if (*ptr == ' ') {
++ptr;
continue;
}
if (i == cap) {
new_cap = cap ? cap * 2 : 16;
new = realloc(argv, sizeof(argv[0]) * new_cap);
if (new == NULL) {
free(argv);
errno = ENOMEM;
return NULL;
}
cap = new_cap;
argv = new;
}
if (*ptr == '\0') {
argv[i++] = NULL;
break;
}
argv[i++] = ptr;
if (*ptr == '"') {
++ptr;
while (*ptr != '\0' && *ptr != '"') {
if (ptr[0] == '\\' && ptr[1] != '\0')
++ptr;
++ptr;
}
if (*ptr == '"')
++ptr;
if (*ptr == ' ') {
*(ptr++) = '\0';
} else if (*ptr != '\0') {
goto fail_str;
}
if (unescape(argv[i - 1]))
goto fail_str;
} else {
while (*ptr != '\0' && *ptr != ' ')
++ptr;
if (*ptr == ' ')
*(ptr++) = '\0';
}
}
return argv;
fail_str:
free(argv);
errno = EINVAL;
return NULL;
}

View file

@ -27,14 +27,18 @@ static bool has_dependencies(service_t *list, service_t *svc)
size_t i;
while (list != NULL) {
for (i = 0; i < svc->num_after; ++i) {
if (!strcmp(svc->after[i], list->name))
return true;
if (svc->after != NULL) {
for (i = 0; svc->after[i] != NULL; ++i) {
if (!strcmp(svc->after[i], list->name))
return true;
}
}
for (i = 0; i < list->num_before; ++i) {
if (!strcmp(list->before[i], svc->name))
return true;
if (list->before != NULL) {
for (i = 0; list->before[i] != NULL; ++i) {
if (!strcmp(list->before[i], svc->name))
return true;
}
}
list = list->next;

View file

@ -32,17 +32,17 @@ static void print_services(service_t *svc)
if (svc->type == SVC_RESPAWN && svc->rspwn_limit > 0)
printf("\tRespawn limit: %d\n", svc->rspwn_limit);
if (svc->num_before) {
if (svc->before != NULL) {
fputs("\tMust be run before:\n", stdout);
for (i = 0; i < svc->num_before; ++i)
for (i = 0; svc->before[i] != NULL; ++i)
printf("\t\t%s\n", svc->before[i]);
}
if (svc->num_after) {
if (svc->after != NULL) {
fputs("\tMust be run after:\n", stdout);
for (i = 0; i < svc->num_after; ++i)
for (i = 0; svc->after[i] != NULL; ++i)
printf("\t\t%s\n", svc->after[i]);
}
}

View file

@ -2,8 +2,7 @@ description configure network loopback device
type wait
target boot
before sysinit
after hwclock
after hostname
after hwclock hostname
exec "@SBINPATH@/ip" addr add 127.0.0.1/8 dev lo brd +
exec "@SBINPATH@/ip" link set lo up

View file

@ -2,6 +2,4 @@ description system reboot
exec "@SBINPATH@/shutdown" -nrf
type wait
target reboot
after sync
after sigkill
after sigterm
after sync sigkill sigterm

View file

@ -2,6 +2,4 @@ description system shutdown
exec "@SBINPATH@/shutdown" -npf
type wait
target shutdown
after sync
after sigkill
after sigterm
after sync sigkill sigterm

View file

@ -3,6 +3,4 @@ exec "@SCRIPTDIR@/killall5" 9
type wait
target %0
after sigterm
before sync
before shutdown
before reboot
before sync shutdown reboot

View file

@ -3,7 +3,4 @@ exec "@SCRIPTDIR@/killall5" 15
exec "@BINPATH@/sleep" 5
type wait
target %0
before sigkill
before sync
before reboot
before shutdown
before sigkill sync reboot shutdown

View file

@ -2,7 +2,5 @@ description sync
exec "@BINPATH@/sync"
type wait
target %0
after sigkill
after sigterm
before reboot
before shutdown
after sigkill sigterm
before reboot shutdown

View file

@ -3,5 +3,4 @@ exec "@SBINPATH@/sysctl" --system
type wait
target boot
before sysinit
after hwclock
after hostname
after hwclock hostname