1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-06-29 14:30:15 +02:00

Minor cleanup

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-04-07 15:35:19 +02:00
parent 43274e3910
commit b81668e045
3 changed files with 20 additions and 52 deletions

View file

@ -38,17 +38,14 @@ static void handle_exited(service_t *svc)
{ {
switch (svc->type) { switch (svc->type) {
case SVC_RESPAWN: case SVC_RESPAWN:
if (target == TGT_REBOOT || target == TGT_SHUTDOWN) { if (target == TGT_REBOOT || target == TGT_SHUTDOWN)
delsvc(svc);
break; break;
}
if (svc->rspwn_limit > 0) { if (svc->rspwn_limit > 0) {
svc->rspwn_limit -= 1; svc->rspwn_limit -= 1;
if (svc->rspwn_limit == 0) { if (svc->rspwn_limit == 0) {
print_status(svc->desc, STATUS_FAIL, false); print_status(svc->desc, STATUS_FAIL, false);
delsvc(svc);
break; break;
} }
} }
@ -56,20 +53,18 @@ static void handle_exited(service_t *svc)
svc->pid = runlst(svc->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); break;
} }
svclist_add(svc); svclist_add(svc);
break; return;
case SVC_ONCE: case SVC_ONCE:
print_status(svc->desc, print_status(svc->desc,
svc->status == EXIT_SUCCESS ? svc->status == EXIT_SUCCESS ?
STATUS_OK : STATUS_FAIL, false); STATUS_OK : STATUS_FAIL, false);
/* fall-through */
default:
delsvc(svc);
break; break;
} }
delsvc(svc);
} }
static void handle_signal(int sigfd) static void handle_signal(int sigfd)
@ -86,11 +81,7 @@ static void handle_signal(int sigfd)
switch (info.ssi_signo) { switch (info.ssi_signo) {
case SIGCHLD: case SIGCHLD:
for (;;) { while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0)
break;
status = WIFEXITED(status) ? WEXITSTATUS(status) : status = WIFEXITED(status) ? WEXITSTATUS(status) :
EXIT_FAILURE; EXIT_FAILURE;
@ -132,9 +123,6 @@ static void start_runlevel(int level)
true); true);
delsvc(svc); delsvc(svc);
} else { } else {
if (svc->type == SVC_RESPAWN)
print_status(svc->desc, STATUS_STARTED, false);
svc->pid = runlst(svc->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);
@ -142,6 +130,9 @@ static void start_runlevel(int level)
continue; continue;
} }
if (svc->type == SVC_RESPAWN)
print_status(svc->desc, STATUS_STARTED, false);
svclist_add(svc); svclist_add(svc);
} }
} }

View file

@ -30,25 +30,14 @@
int mksock(void) int mksock(void)
{ {
struct sockaddr_un un; struct sockaddr_un un;
int fd, flags; int fd;
fd = socket(AF_UNIX, SOCK_STREAM, 0); fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) { if (fd < 0) {
perror("socket"); perror("socket");
return -1; return -1;
} }
flags = fcntl(fd, F_GETFD);
if (flags == -1) {
perror("socket F_GETFD");
goto fail;
}
if (fcntl(fd, F_SETFD, flags | O_CLOEXEC)) {
perror("socket F_SETFD");
goto fail;
}
memset(&un, 0, sizeof(un)); memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX; un.sun_family = AF_UNIX;

View file

@ -16,26 +16,15 @@
* 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 <sys/wait.h> #include <sys/wait.h>
#include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h> #include <fcntl.h>
#include "init.h" #include "init.h"
extern char **environ; extern char **environ;
static NORETURN void split_and_exec(exec_t *cmd)
{
execve(cmd->argv[0], cmd->argv, environ);
perror(cmd->argv[0]);
exit(EXIT_FAILURE);
}
static int child_setup(const char *ctty) static int child_setup(const char *ctty)
{ {
sigset_t mask; sigset_t mask;
@ -77,7 +66,9 @@ int runlst_wait(exec_t *list, const char *ctty)
if (pid == 0) { if (pid == 0) {
if (child_setup(ctty)) if (child_setup(ctty))
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
split_and_exec(list); execve(list->argv[0], list->argv, environ);
perror(list->argv[0]);
exit(EXIT_FAILURE);
} }
if (pid == -1) { if (pid == -1) {
@ -101,21 +92,18 @@ int runlst_wait(exec_t *list, const char *ctty)
pid_t runlst(exec_t *list, const char *ctty) pid_t runlst(exec_t *list, const char *ctty)
{ {
int status; pid_t pid = fork();
pid_t pid;
pid = fork();
if (pid == 0) { if (pid == 0) {
if (child_setup(ctty)) if (child_setup(ctty))
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (list->next != NULL) { if (list->next != NULL)
status = runlst_wait(list, NULL); exit(runlst_wait(list, NULL));
exit(status);
} else { execve(list->argv[0], list->argv, environ);
split_and_exec(list); perror(list->argv[0]);
} exit(EXIT_FAILURE);
} }
if (pid == -1) if (pid == -1)