First commit
This commit is contained in:
57
libexec/common.c
Normal file
57
libexec/common.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "config.h"
|
||||
|
||||
int len(const char *msg)
|
||||
{
|
||||
char *p;
|
||||
for (p = (char *)msg; *p; ++p)
|
||||
;
|
||||
return p - msg;
|
||||
}
|
||||
|
||||
void log(const char *msg)
|
||||
{
|
||||
write(1, "=> ", 4);
|
||||
write(1, msg, len(msg));
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
void runprgs(struct cmd *cmds, int n)
|
||||
{
|
||||
int tty, i;
|
||||
tty = open(LOGTO, O_WRONLY);
|
||||
close(1);
|
||||
close(2);
|
||||
dup2(tty, 1);
|
||||
dup2(tty, 2);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
int pid;
|
||||
|
||||
if (cmds[i].flags == RESPAWN)
|
||||
{
|
||||
if (fork())
|
||||
goto end;
|
||||
}
|
||||
|
||||
loop:
|
||||
pid = fork();
|
||||
|
||||
if (!pid)
|
||||
{
|
||||
execve(cmds[i].args[0], cmds[i].args, NULL);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
switch (cmds[i].flags)
|
||||
{
|
||||
case RESPAWN:
|
||||
waitpid(pid, NULL, 0);
|
||||
sleep(1);
|
||||
goto loop;
|
||||
case WAIT:
|
||||
wait4(pid, NULL, 0, NULL);
|
||||
}
|
||||
end:;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user