1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 19:19:47 +01:00

Add init socket to initd

Create a socket if boot target is done. Close and reopen socket
if SIGUSR1 is received.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-03-17 18:33:12 +01:00
parent c78bbd2f73
commit 08f72865b2
2 changed files with 38 additions and 7 deletions

View file

@ -5,6 +5,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -17,6 +18,7 @@
#include <sys/reboot.h> #include <sys/reboot.h>
#include <signal.h> #include <signal.h>
#include "initsock.h"
#include "service.h" #include "service.h"
#include "util.h" #include "util.h"

View file

@ -2,6 +2,7 @@
#include "init.h" #include "init.h"
static int sigfd = -1; static int sigfd = -1;
static int sockfd = -1;
static void handle_signal(void) static void handle_signal(void)
{ {
@ -29,13 +30,27 @@ static void handle_signal(void)
case SIGINT: case SIGINT:
supervisor_set_target(TGT_REBOOT); supervisor_set_target(TGT_REBOOT);
break; break;
case SIGUSR1:
if (sockfd >= 0) {
close(sockfd);
unlink(INIT_SOCK_PATH);
sockfd = -1;
} }
sockfd = init_socket_create();
break;
}
}
static void handle_request(void)
{
} }
void target_completed(int target) void target_completed(int target)
{ {
switch (target) { switch (target) {
case TGT_BOOT: case TGT_BOOT:
if (sockfd < 0)
sockfd = init_socket_create();
break; break;
case TGT_SHUTDOWN: case TGT_SHUTDOWN:
for (;;) for (;;)
@ -50,7 +65,7 @@ void target_completed(int target)
int main(void) int main(void)
{ {
int ret, count; int i, ret, count;
struct pollfd pfd[2]; struct pollfd pfd[2];
if (getpid() != 1) { if (getpid() != 1) {
@ -69,15 +84,29 @@ int main(void)
; ;
memset(pfd, 0, sizeof(pfd)); memset(pfd, 0, sizeof(pfd));
pfd[0].fd = sigfd; count = 0;
pfd[0].events = POLLIN;
count = 1; pfd[count].fd = sigfd;
pfd[count].events = POLLIN;
++count;
if (sockfd >= 0) {
pfd[count].fd = sockfd;
pfd[count].events = POLLIN;
++count;
}
ret = poll(pfd, count, -1); ret = poll(pfd, count, -1);
if (ret <= 0)
continue;
if (ret > 0) { for (i = 0; i < count; ++i) {
if (pfd[0].revents & POLLIN) if (pfd[i].revents & POLLIN) {
if (pfd[i].fd == sigfd)
handle_signal(); handle_signal();
if (pfd[i].fd == sockfd)
handle_request();
}
} }
} }