1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-05 04:07:10 +01: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 does not exit with EXIT_SUCCESS, processing of the list is aborted
and the function returns the exit status of the failed process. 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. 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 Alternatively, if num is 1, the child process directly exec()s the
given command. 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 **********/ /********** 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) { if (svc->pid == -1) {
print_status(svc->desc, STATUS_FAIL, false); print_status(svc->desc, STATUS_FAIL, false);
delsvc(svc); delsvc(svc);
@ -115,7 +115,7 @@ static void start_runlevel(int level)
svc = cfg.targets[level]; svc = cfg.targets[level];
cfg.targets[level] = svc->next; cfg.targets[level] = svc->next;
if (!svc->num_exec) { if (svc->exec == NULL) {
print_status(svc->desc, STATUS_OK, false); print_status(svc->desc, STATUS_OK, false);
delsvc(svc); delsvc(svc);
continue; continue;
@ -124,8 +124,7 @@ static void start_runlevel(int level)
if (svc->type == SVC_WAIT) { if (svc->type == SVC_WAIT) {
print_status(svc->desc, STATUS_WAIT, false); print_status(svc->desc, STATUS_WAIT, false);
status = runlst_wait(svc->exec, svc->num_exec, status = runlst_wait(svc->exec, svc->ctty);
svc->ctty);
print_status(svc->desc, print_status(svc->desc,
status == EXIT_SUCCESS ? status == EXIT_SUCCESS ?
@ -136,7 +135,7 @@ static void start_runlevel(int level)
if (svc->type == SVC_RESPAWN) if (svc->type == SVC_RESPAWN)
print_status(svc->desc, STATUS_STARTED, false); 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) { if (svc->pid == -1) {
print_status(svc->desc, STATUS_FAIL, false); print_status(svc->desc, STATUS_FAIL, false);
delsvc(svc); delsvc(svc);

View file

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

View file

@ -44,21 +44,27 @@ enum {
TGT_MAX TGT_MAX
}; };
typedef struct exec_t {
char **argv;
char *raw_argv;
struct exec_t *next;
} exec_t;
typedef struct service_t { typedef struct service_t {
int type; /* SVC_* service type */ int type; /* SVC_* service type */
int target; /* TGT_* service target */ int target; /* TGT_* service target */
char *name; /* canonical service name */ char *name; /* canonical service name */
char *desc; /* description string */ char *desc; /* description string */
char **exec; /* command lines to execute */ exec_t *exec; /* command lines to execute */
size_t num_exec; /* number of command lines */
char *ctty; /* controlling tty or log file */ char *ctty; /* controlling tty or log file */
int rspwn_limit; /* maximum respawn count */ int rspwn_limit; /* maximum respawn count */
char **before; /* services that must be executed later */ char **before; /* services that must be executed later */
size_t num_before;
char **after; /* services that must be executed first */ char **after; /* services that must be executed first */
size_t num_after; char *raw_after;
char *raw_before;
pid_t pid; pid_t pid;
int status; /* process exit status */ 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); int unescape(char *src);
char **split_argv(char *str);
/* /*
Search through an array of enum_map_t entries to resolve a string to 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 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) void delsvc(service_t *svc)
{ {
size_t i; exec_t *e;
for (i = 0; i < svc->num_exec; ++i) while (svc->exec != NULL) {
free(svc->exec[i]); e = svc->exec;
svc->exec = e->next;
for (i = 0; i < svc->num_before; ++i) free(e->argv);
free(svc->before[i]); free(e->raw_argv);
free(e);
}
for (i = 0; i < svc->num_after; ++i) free(svc->raw_before);
free(svc->after[i]); free(svc->raw_after);
free(svc->before); free(svc->before);
free(svc->after); free(svc->after);

View file

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

View file

@ -15,7 +15,9 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include "util.h" #include "util.h"
@ -86,3 +88,74 @@ int unescape(char *src)
*(dst++) = '\0'; *(dst++) = '\0';
return 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,15 +27,19 @@ static bool has_dependencies(service_t *list, service_t *svc)
size_t i; size_t i;
while (list != NULL) { while (list != NULL) {
for (i = 0; i < svc->num_after; ++i) { if (svc->after != NULL) {
for (i = 0; svc->after[i] != NULL; ++i) {
if (!strcmp(svc->after[i], list->name)) if (!strcmp(svc->after[i], list->name))
return true; return true;
} }
}
for (i = 0; i < list->num_before; ++i) { if (list->before != NULL) {
for (i = 0; list->before[i] != NULL; ++i) {
if (!strcmp(list->before[i], svc->name)) if (!strcmp(list->before[i], svc->name))
return true; return true;
} }
}
list = list->next; 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) if (svc->type == SVC_RESPAWN && svc->rspwn_limit > 0)
printf("\tRespawn limit: %d\n", svc->rspwn_limit); printf("\tRespawn limit: %d\n", svc->rspwn_limit);
if (svc->num_before) { if (svc->before != NULL) {
fputs("\tMust be run before:\n", stdout); 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]); printf("\t\t%s\n", svc->before[i]);
} }
if (svc->num_after) { if (svc->after != NULL) {
fputs("\tMust be run after:\n", stdout); 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]); printf("\t\t%s\n", svc->after[i]);
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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