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

Move initd signal setup to platform specific file

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-04-15 13:21:34 +02:00
parent 6e839a4ad5
commit 2cb64e48fc
5 changed files with 81 additions and 21 deletions

View file

@ -1,4 +1,4 @@
init_SOURCES = initd/main.c initd/runlst.c initd/init.h init_SOURCES = initd/main.c initd/runlst.c initd/init.h initd/signal_linux.c
init_SOURCES += initd/status.c initd/mksock.c initd/svclist.c initd/env.c init_SOURCES += initd/status.c initd/mksock.c initd/svclist.c initd/env.c
init_CPPFLAGS = $(AM_CPPFLAGS) init_CPPFLAGS = $(AM_CPPFLAGS)
init_CFLAGS = $(AM_CFLAGS) init_CFLAGS = $(AM_CFLAGS)

View file

@ -19,7 +19,9 @@
#define INIT_H #define INIT_H
#include <linux/reboot.h> #include <linux/reboot.h>
#include <sys/signalfd.h>
#include <sys/reboot.h> #include <sys/reboot.h>
#include <signal.h>
#include "service.h" #include "service.h"
#include "telinit.h" #include "telinit.h"
@ -117,5 +119,28 @@ service_t *svclist_remove(pid_t pid);
*/ */
int initenv(void); int initenv(void);
/********** signal_<platform>.c **********/
/*
Setup signal handling. Returns -1 on error, a file descriptor on
success.
The returned file descriptor can be polled and becomes readable
when a signal arrives. Reading from it returns a signalfd_siginfo
structure.
The returned file descriptor has the close on exec flag set.
The kernel is also told to send us SIGINT signals if a user presses
the local equivalent of CTRL+ALT+DEL.
*/
int sigsetup(void);
/*
Undo everything that sigsetup() changed about signal handling and
restore the default.
*/
void sigreset(void);
#endif /* INIT_H */ #endif /* INIT_H */

View file

@ -25,8 +25,6 @@
#include <errno.h> #include <errno.h>
#include <poll.h> #include <poll.h>
#include <sys/signalfd.h>
#include "init.h" #include "init.h"
static service_list_t cfg; static service_list_t cfg;
@ -189,32 +187,20 @@ int main(void)
{ {
int ti_sock = -1, sfd, ret, count; int ti_sock = -1, sfd, ret, count;
struct pollfd pfd[2]; struct pollfd pfd[2];
sigset_t mask;
if (getpid() != 1) { if (getpid() != 1) {
fputs("init does not have pid 1, terminating!\n", stderr); fputs("init does not have pid 1, terminating!\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (reboot(LINUX_REBOOT_CMD_CAD_OFF))
perror("cannot disable CTRL+ALT+DEL");
if (svcscan(SVCDIR, &cfg)) { if (svcscan(SVCDIR, &cfg)) {
fputs("Error reading service list from " SVCDIR "\n" fputs("Error reading service list from " SVCDIR "\n"
"Trying to continue anyway\n", stderr); "Trying to continue anyway\n", stderr);
} }
sigfillset(&mask); sfd = sigsetup();
if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) { if (sfd < 0)
perror("sigprocmask"); return -1;
return EXIT_FAILURE;
}
sfd = signalfd(-1, &mask, SFD_CLOEXEC);
if (sfd == -1) {
perror("signalfd");
return EXIT_FAILURE;
}
memset(pfd, 0, sizeof(pfd)); memset(pfd, 0, sizeof(pfd));
pfd[0].fd = sfd; pfd[0].fd = sfd;

View file

@ -26,11 +26,9 @@
static int child_setup(const char *ctty) static int child_setup(const char *ctty)
{ {
sigset_t mask;
int fd; int fd;
sigemptyset(&mask); sigreset();
sigprocmask(SIG_SETMASK, &mask, NULL);
if (ctty != NULL) { if (ctty != NULL) {
fd = open(ctty, O_RDWR); fd = open(ctty, O_RDWR);

51
initd/signal_linux.c Normal file
View file

@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* Copyright (C) 2018 - David Oberhollenzer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 <stdio.h>
#include "init.h"
int sigsetup(void)
{
sigset_t mask;
int sfd;
sigfillset(&mask);
if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) {
perror("sigprocmask");
return -1;
}
sfd = signalfd(-1, &mask, SFD_CLOEXEC);
if (sfd == -1) {
perror("signalfd");
return -1;
}
if (reboot(LINUX_REBOOT_CMD_CAD_OFF))
perror("cannot disable CTRL+ALT+DEL");
return sfd;
}
void sigreset(void)
{
sigset_t mask;
sigemptyset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);
}