mirror of
https://github.com/troglobit/finit.git
synced 2026-11-06 15:51:03 +00:00
Minor cleanup. Moved code around a bit, to functions, away from main().
This commit is in preparation for importing the Westermo code changes. Slowly refactoring the finit core into a more modularized unit to be a bit more maintainable. Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
a654d99b92
commit
4657d9713a
6 files changed
+439
-197
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
Copyright (c) 2008 Claudio Matsuoka
|
Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
|
Copyright (c) 2009-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
# finit - The Improved fast init
|
||||||
|
#
|
||||||
|
# Copyright (c) 2008 Claudio Matsuoka
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
# THE SOFTWARE.
|
||||||
|
|
||||||
|
.PHONY: all romfs clean
|
||||||
|
|
||||||
|
# Top directory for building complete system, fall back to this directory
|
||||||
|
ROOTDIR ?= $(shell pwd)
|
||||||
|
|
||||||
|
VERSION = 1.0-pre
|
||||||
|
PKG = finit-$(VERSION)
|
||||||
|
EXEC = finit
|
||||||
|
OBJS = finit.o helpers.o initctl.o
|
||||||
|
SRCS = $(OBJS:.o=.c)
|
||||||
|
DEPS = $(addprefix .,$(SRCS:.c=.d))
|
||||||
|
CPPFLAGS += -DVERSION=\"$(VERSION)\" -DWHOAMI=\"`whoami`@`hostname`\"
|
||||||
|
CPPFLAGS += -DLISTEN_INITCTL -DBUILTIN_RUNPARTS
|
||||||
|
CPPFLAGS += -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE
|
||||||
|
# Disable annoying gcc warning for "warn_unused_result", see GIT 37af997
|
||||||
|
CPPFLAGS += -U_FORTIFY_SOURCE
|
||||||
|
CPPFLAGS += -I$(ROOTDIR)/$(CONFIG_LINUXDIR)/include/
|
||||||
|
CFLAGS += -W -Wall -Werror -Os
|
||||||
|
|
||||||
|
prefix ?= /usr/local
|
||||||
|
sysconfdir ?= /etc
|
||||||
|
datadir = $(prefix)/share/doc/finit
|
||||||
|
mandir = $(prefix)/share/man/man8
|
||||||
|
|
||||||
|
include common.mk
|
||||||
|
|
||||||
|
all: Makefile $(EXEC)
|
||||||
|
|
||||||
|
$(OBJS): Makefile
|
||||||
|
|
||||||
|
finit: $(OBJS)
|
||||||
|
|
||||||
|
install: all
|
||||||
|
@install -d $(DESTDIR)$(prefix)/sbin
|
||||||
|
@install -d $(DESTDIR)$(sysconfdir)
|
||||||
|
@install -d $(DESTDIR)$(datadir)
|
||||||
|
@install -d $(DESTDIR)$(mandir)
|
||||||
|
@for file in $(EXEC); do \
|
||||||
|
install -m 0755 $$file $(DESTDIR)$(prefix)/sbin/$$file; \
|
||||||
|
done
|
||||||
|
@ln -sf /sbin/finit /sbin/init
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
-@for file in $(EXEC); do \
|
||||||
|
$(RM) $(DESTDIR)$(prefix)/sbin/$$file; \
|
||||||
|
done
|
||||||
|
-@$(RM) /sbin/finit /sbin/init
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-@$(RM) $(OBJS) $(DEPS) $(EXEC)
|
||||||
|
|
||||||
|
distclean: clean
|
||||||
|
-@$(RM) $(JUNK) unittest *.o
|
||||||
|
|
||||||
|
# Include automatically generated rules, such as:
|
||||||
|
# uncgi.o: .../some/dir/uncgi.c /usr/include/stdio.h
|
||||||
|
# but don't bother during clean!
|
||||||
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
|
ifneq ($(MAKECMDGOALS),distclean)
|
||||||
|
-include $(DEPS)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# Some junk files we always want to be removed when doing a make clean.
|
||||||
|
JUNK = *~ *.bak *.aux *.dvi *.idx *.ind *.log *.ps *.map .*.d DEADJOE semantic.cache *.gdb *.elf core core.*
|
||||||
|
RM := -@$(RM)
|
||||||
|
MAKE := @$(MAKE)
|
||||||
|
MAKEFLAGS = --no-print-directory
|
||||||
|
|
||||||
|
# Smart autodependecy generation via GCC -M.
|
||||||
|
.%.d: %.c
|
||||||
|
@$(SHELL) -ec "$(CC) -MM $(CFLAGS) $(CPPFLAGS) $< \
|
||||||
|
| sed 's,.*: ,$*.o $@ : ,g' > $@; \
|
||||||
|
[ -s $@ ] || rm -f $@"
|
||||||
|
|
||||||
|
# Override default implicit rules
|
||||||
|
%.o: %.c
|
||||||
|
@printf " CC $(subst $(ROOTDIR)/,,$(shell pwd)/$@)\n"
|
||||||
|
@$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
%: %.o
|
||||||
|
@printf " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/$@)\n"
|
||||||
|
@$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
|
||||||
@@ -1,25 +1,25 @@
|
|||||||
/*
|
/*
|
||||||
Improved fast init
|
Improved fast init
|
||||||
|
|
||||||
Copyright (c) 2008 Claudio Matsuoka
|
Copyright (c) 2008 Claudio Matsuoka
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
in the Software without restriction, including without limitation the rights
|
in the Software without restriction, including without limitation the rights
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
furnished to do so, subject to the following conditions:
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
The above copyright notice and this permission notice shall be included in
|
||||||
all copies or substantial portions of the Software.
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -39,89 +39,15 @@ THE SOFTWARE.
|
|||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <utmp.h>
|
#include <utmp.h>
|
||||||
|
|
||||||
|
#include "finit.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
|
|
||||||
#ifndef DEFUSER
|
static void chld_handler(int sig __attribute__ ((unused)));
|
||||||
#define DEFUSER "user"
|
static void sig_init(void);
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Distribution configuration */
|
|
||||||
|
|
||||||
#if defined DIST_MDV /* Mandriva */
|
|
||||||
#define RANDOMSEED "/var/lib/random-seed"
|
|
||||||
#define SYSROOT "/sysroot"
|
|
||||||
#define GETTY "/usr/bin/openvt /sbin/mingetty tty2"
|
|
||||||
#define RUNPARTS "/usr/bin/run-parts"
|
|
||||||
#define REMOUNT_ROOTFS_RW
|
|
||||||
#define MAKE_DEVICES
|
|
||||||
#define MAKE_DEV_VCS
|
|
||||||
#define PAM_CONSOLE
|
|
||||||
#define LISTEN_INITCTL
|
|
||||||
#define RUNLEVEL 5
|
|
||||||
#define USE_MESSAGE_BUS
|
|
||||||
#elif defined DIST_EEEXUBUNTU /* eeeXubuntu */
|
|
||||||
#define RANDOMSEED "/var/lib/urandom/random-seed"
|
|
||||||
#define SYSROOT "/sysroot"
|
|
||||||
#define GETTY "/usr/bin/openvt /sbin/getty 38400 tty2"
|
|
||||||
#define RUNPARTS "/bin/run-parts"
|
|
||||||
#define REMOUNT_ROOTFS_RW
|
|
||||||
#define MAKE_DEVICES
|
|
||||||
#define TOUCH_ETC_NETWORK_RUN_IFSTATE
|
|
||||||
#define LISTEN_INITCTL
|
|
||||||
#else /* original Eeepc distribution */
|
|
||||||
#define RANDOMSEED "/var/lib/urandom/random-seed"
|
|
||||||
#define SYSROOT "/mnt"
|
|
||||||
#define GETTY "/usr/bin/openvt /sbin/getty 38400 tty2"
|
|
||||||
#define RUNPARTS "/bin/run-parts"
|
|
||||||
#define TOUCH_ETC_NETWORK_RUN_IFSTATE
|
|
||||||
#define USE_ETC_RESOLVCONF_RUN
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* From sysvinit */
|
|
||||||
/* Set a signal handler. */
|
|
||||||
#define SETSIG(sa, sig, fun, flags) \
|
|
||||||
do { \
|
|
||||||
sa.sa_handler = fun; \
|
|
||||||
sa.sa_flags = flags; \
|
|
||||||
sigemptyset(&sa.sa_mask); \
|
|
||||||
sigaction(sig, &sa, NULL); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#ifdef LISTEN_INITCTL
|
|
||||||
|
|
||||||
#define INIT_MAGIC 0x03091969
|
|
||||||
#define INIT_CMD_RUNLVL 1
|
|
||||||
|
|
||||||
struct init_request {
|
|
||||||
int magic; /* Magic number */
|
|
||||||
int cmd; /* What kind of request */
|
|
||||||
int runlevel; /* Runlevel to change to */
|
|
||||||
int sleeptime; /* Time between TERM and KILL */
|
|
||||||
char data[368];
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define touch(x) mknod((x), S_IFREG|0644, 0)
|
|
||||||
#define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
|
|
||||||
#define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
|
|
||||||
|
|
||||||
#define MATCH_CMD(l,c,x) \
|
|
||||||
((!strncmp((l), (c), strlen((c)))) && ((x) = (l) + strlen((c))))
|
|
||||||
|
|
||||||
#define _d(x...) do { if (debug) { printf(x); printf("\n"); } } while (0)
|
|
||||||
|
|
||||||
#define LINE_SIZE 1024
|
|
||||||
#define CMD_SIZE 256
|
|
||||||
#define USERNAME_SIZE 16
|
|
||||||
#define HOSTNAME_SIZE 32
|
|
||||||
|
|
||||||
void shutdown(int);
|
|
||||||
void signal_handler(int);
|
|
||||||
void chld_handler(int);
|
|
||||||
|
|
||||||
static int debug = 0;
|
static int debug = 0;
|
||||||
static char sdown[CMD_SIZE];
|
static char sdown[CMD_SIZE] = "";
|
||||||
|
|
||||||
|
|
||||||
static void build_cmd(char *cmd, char *x, int len)
|
static void build_cmd(char *cmd, char *x, int len)
|
||||||
{
|
{
|
||||||
@@ -131,7 +57,7 @@ static void build_cmd(char *cmd, char *x, int len)
|
|||||||
c = cmd + strlen(cmd);
|
c = cmd + strlen(cmd);
|
||||||
|
|
||||||
/* skip spaces */
|
/* skip spaces */
|
||||||
for (; *x && (*x == ' ' || *x == '\t'); x++);
|
for (; *x && (*x == ' ' || *x == '\t'); x++) ;
|
||||||
|
|
||||||
/* copy next arg */
|
/* copy next arg */
|
||||||
for (l = 0; *x && *x != '#' && *x != '\t' && l < len; l++)
|
for (l = 0; *x && *x != '#' && *x != '\t' && l < len; l++)
|
||||||
@@ -141,55 +67,10 @@ static void build_cmd(char *cmd, char *x, int len)
|
|||||||
_d("cmd = %s", cmd);
|
_d("cmd = %s", cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
#ifdef LISTEN_INITCTL
|
|
||||||
|
|
||||||
/* Standard reboot/shutdown utilities talk to init using /dev/initctl.
|
|
||||||
* We should check if the fifo was recreated and reopen it.
|
|
||||||
*/
|
|
||||||
static void listen_initctl()
|
|
||||||
{
|
{
|
||||||
if (!fork()) {
|
|
||||||
int ctl;
|
|
||||||
fd_set fds;
|
|
||||||
struct init_request request;
|
|
||||||
|
|
||||||
mkfifo("/dev/initctl", 0600);
|
|
||||||
ctl = open("/dev/initctl", O_RDONLY);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&fds);
|
|
||||||
FD_SET(ctl, &fds);
|
|
||||||
if (select(ctl + 1, &fds, NULL, NULL, NULL) <= 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
read(ctl, &request, sizeof(request));
|
|
||||||
|
|
||||||
if (request.magic != INIT_MAGIC)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (request.cmd == INIT_CMD_RUNLVL) {
|
|
||||||
switch (request.runlevel) {
|
|
||||||
case '0':
|
|
||||||
shutdown(SIGUSR2);
|
|
||||||
break;
|
|
||||||
case '6':
|
|
||||||
shutdown(SIGUSR1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char line[LINE_SIZE];
|
char line[LINE_SIZE];
|
||||||
struct sigaction sa, act;
|
|
||||||
sigset_t nmask, nmask2;
|
|
||||||
char username[USERNAME_SIZE] = DEFUSER;
|
char username[USERNAME_SIZE] = DEFUSER;
|
||||||
char hostname[HOSTNAME_SIZE] = "eviltwin";
|
char hostname[HOSTNAME_SIZE] = "eviltwin";
|
||||||
char cmd[CMD_SIZE];
|
char cmd[CMD_SIZE];
|
||||||
@@ -207,33 +88,13 @@ int main()
|
|||||||
|
|
||||||
chdir("/");
|
chdir("/");
|
||||||
umask(022);
|
umask(022);
|
||||||
|
|
||||||
/*
|
|
||||||
* Signal management
|
|
||||||
*/
|
|
||||||
for (i = 1; i < NSIG; i++)
|
|
||||||
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
|
||||||
|
|
||||||
SETSIG(sa, SIGINT, shutdown, 0);
|
|
||||||
SETSIG(sa, SIGPWR, SIG_IGN, 0);
|
|
||||||
SETSIG(sa, SIGUSR1, shutdown, 0);
|
|
||||||
SETSIG(sa, SIGUSR2, shutdown, 0);
|
|
||||||
SETSIG(sa, SIGTERM, SIG_IGN, 0);
|
|
||||||
SETSIG(sa, SIGALRM, SIG_IGN, 0);
|
|
||||||
SETSIG(sa, SIGHUP, SIG_IGN, 0);
|
|
||||||
SETSIG(sa, SIGCONT, SIG_IGN, SA_RESTART);
|
|
||||||
SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
|
|
||||||
|
|
||||||
/* Block sigchild while forking */
|
|
||||||
sigemptyset(&nmask);
|
|
||||||
sigaddset(&nmask, SIGCHLD);
|
|
||||||
sigprocmask(SIG_BLOCK, &nmask, NULL);
|
|
||||||
|
|
||||||
reboot(RB_DISABLE_CAD);
|
|
||||||
|
|
||||||
mount("none", "/proc", "proc", 0, NULL);
|
mount("none", "/proc", "proc", 0, NULL);
|
||||||
mount("none", "/sys", "sysfs", 0, NULL);
|
mount("none", "/sys", "sysfs", 0, NULL);
|
||||||
|
|
||||||
|
/* Setup signals */
|
||||||
|
sig_init();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse kernel parameters
|
* Parse kernel parameters
|
||||||
*/
|
*/
|
||||||
@@ -252,8 +113,6 @@ int main()
|
|||||||
|
|
||||||
_d("finit " VERSION " (built " __DATE__ " " __TIME__ " by " WHOAMI ")");
|
_d("finit " VERSION " (built " __DATE__ " " __TIME__ " by " WHOAMI ")");
|
||||||
|
|
||||||
setsid();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse configuration file
|
* Parse configuration file
|
||||||
*/
|
*/
|
||||||
@@ -314,7 +173,7 @@ int main()
|
|||||||
blkdev("/dev/sda5", 0660, 8, 5);
|
blkdev("/dev/sda5", 0660, 8, 5);
|
||||||
blkdev("/dev/sda6", 0660, 8, 6);
|
blkdev("/dev/sda6", 0660, 8, 6);
|
||||||
#ifdef MAKE_DEV_VCS
|
#ifdef MAKE_DEV_VCS
|
||||||
/* Caio reports that newer X.org needs these */
|
/* Caio reports that newer X.org needs these */
|
||||||
blkdev("/dev/vcs1", 0660, 7, 1);
|
blkdev("/dev/vcs1", 0660, 7, 1);
|
||||||
blkdev("/dev/vcs2", 0660, 7, 2);
|
blkdev("/dev/vcs2", 0660, 7, 2);
|
||||||
blkdev("/dev/vcs3", 0660, 7, 3);
|
blkdev("/dev/vcs3", 0660, 7, 3);
|
||||||
@@ -370,15 +229,15 @@ int main()
|
|||||||
chardev("/dev/ptmx", 0666, 5, 2);
|
chardev("/dev/ptmx", 0666, 5, 2);
|
||||||
chardev("/dev/null", 0666, 1, 3);
|
chardev("/dev/null", 0666, 1, 3);
|
||||||
chmod("/dev/null", 0666);
|
chmod("/dev/null", 0666);
|
||||||
chardev("/dev/mem", 0640, 1, 1);
|
chardev("/dev/mem", 0640, 1, 1);
|
||||||
chardev("/dev/tty0", 0660, 4, 0);
|
chardev("/dev/tty0", 0660, 4, 0);
|
||||||
chardev("/dev/tty1", 0660, 4, 1);
|
chardev("/dev/tty1", 0660, 4, 1);
|
||||||
chardev("/dev/tty2", 0660, 4, 2);
|
chardev("/dev/tty2", 0660, 4, 2);
|
||||||
chardev("/dev/tty3", 0660, 4, 3);
|
chardev("/dev/tty3", 0660, 4, 3);
|
||||||
chardev("/dev/input/mice", 0660, 13, 63);
|
chardev("/dev/input/mice", 0660, 13, 63);
|
||||||
chardev("/dev/input/event0", 0660, 13, 64);
|
chardev("/dev/input/event0", 0660, 13, 64);
|
||||||
chardev("/dev/agpgart", 0660, 10, 175);
|
chardev("/dev/agpgart", 0660, 10, 175);
|
||||||
blkdev("/dev/loop0", 0600, 7, 0);
|
blkdev("/dev/loop0", 0600, 7, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unlink("/etc/mtab");
|
unlink("/etc/mtab");
|
||||||
@@ -407,7 +266,7 @@ int main()
|
|||||||
if (isalnum(d->d_name[0]))
|
if (isalnum(d->d_name[0]))
|
||||||
continue;
|
continue;
|
||||||
snprintf(line, LINE_SIZE,
|
snprintf(line, LINE_SIZE,
|
||||||
"/etc/resolvconf/run/interface/%s", d->d_name);
|
"/etc/resolvconf/run/interface/%s", d->d_name);
|
||||||
unlink(line);
|
unlink(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,29 +303,31 @@ int main()
|
|||||||
#endif
|
#endif
|
||||||
chdir("/");
|
chdir("/");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef TOUCH_ETC_NETWORK_RUN_IFSTATE
|
#ifdef TOUCH_ETC_NETWORK_RUN_IFSTATE
|
||||||
touch("/etc/network/run/ifstate");
|
touch("/etc/network/run/ifstate");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ((f = fopen("/etc/hostname", "r")) != NULL) {
|
if ((f = fopen("/etc/hostname", "r")) != NULL) {
|
||||||
fgets(hostname, HOSTNAME_SIZE, f);
|
fgets(hostname, HOSTNAME_SIZE, f);
|
||||||
chomp(hostname);
|
chomp(hostname);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
sethostname(hostname, strlen(hostname));
|
sethostname(hostname, strlen(hostname));
|
||||||
|
|
||||||
ifconfig("lo", "127.0.0.1", "255.0.0.0", 1);
|
ifconfig("lo", "127.0.0.1", "255.0.0.0", 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set random seed
|
* Set random seed
|
||||||
*/
|
*/
|
||||||
|
#ifdef RANDOMSEED
|
||||||
copyfile(RANDOMSEED, "/dev/urandom", 0);
|
copyfile(RANDOMSEED, "/dev/urandom", 0);
|
||||||
unlink(RANDOMSEED);
|
unlink(RANDOMSEED);
|
||||||
umask(077);
|
umask(077);
|
||||||
copyfile("/dev/urandom", RANDOMSEED, 4096);
|
copyfile("/dev/urandom", RANDOMSEED, 4096);
|
||||||
umask(0);
|
umask(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Console setup (for X)
|
* Console setup (for X)
|
||||||
@@ -490,11 +351,15 @@ int main()
|
|||||||
mkdir("/tmp/.ICE-unix", 01777);
|
mkdir("/tmp/.ICE-unix", 01777);
|
||||||
umask(022);
|
umask(022);
|
||||||
|
|
||||||
|
/* Start console login. */
|
||||||
system(GETTY "&");
|
system(GETTY "&");
|
||||||
|
|
||||||
_d("forking");
|
_d("forking");
|
||||||
if (!fork()) {
|
if (!fork()) {
|
||||||
/* child process */
|
/* child process */
|
||||||
|
int i;
|
||||||
|
sigset_t nmask;
|
||||||
|
struct sigaction sa;
|
||||||
|
|
||||||
vhangup();
|
vhangup();
|
||||||
|
|
||||||
@@ -502,15 +367,15 @@ int main()
|
|||||||
close(1);
|
close(1);
|
||||||
close(0);
|
close(0);
|
||||||
|
|
||||||
if (open("/dev/tty1", O_RDWR) != 0)
|
if (open(CONSOLE, O_RDWR) != 0)
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
sigemptyset(&act.sa_mask);
|
sigemptyset(&sa.sa_mask);
|
||||||
act.sa_handler = SIG_DFL;
|
sa.sa_handler = SIG_DFL;
|
||||||
|
|
||||||
sigemptyset(&nmask2);
|
sigemptyset(&nmask);
|
||||||
sigaddset(&nmask2, SIGCHLD);
|
sigaddset(&nmask, SIGCHLD);
|
||||||
sigprocmask(SIG_UNBLOCK, &nmask2, NULL);
|
sigprocmask(SIG_UNBLOCK, &nmask, NULL);
|
||||||
|
|
||||||
for (i = 1; i < NSIG; i++)
|
for (i = 1; i < NSIG; i++)
|
||||||
sigaction(i, &sa, NULL);
|
sigaction(i, &sa, NULL);
|
||||||
@@ -536,7 +401,7 @@ int main()
|
|||||||
/* ConsoleKit needs this */
|
/* ConsoleKit needs this */
|
||||||
setenv("DISPLAY", ":0", 1);
|
setenv("DISPLAY", ":0", 1);
|
||||||
|
|
||||||
while (access("/tmp/shutdown", F_OK) < 0) {
|
while (!fexist("/tmp/shutdown")) {
|
||||||
_d("start X as %s\n", username);
|
_d("start X as %s\n", username);
|
||||||
if (debug) {
|
if (debug) {
|
||||||
snprintf(line, LINE_SIZE,
|
snprintf(line, LINE_SIZE,
|
||||||
@@ -553,27 +418,29 @@ int main()
|
|||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parent process */
|
/* parent process */
|
||||||
|
|
||||||
|
|
||||||
/* sleep(1); */
|
/* sleep(1); */
|
||||||
system("/usr/sbin/services.sh &>/dev/null&");
|
system("/usr/sbin/services.sh &>/dev/null&");
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
sigset_t nmask;
|
||||||
|
|
||||||
sigemptyset(&nmask);
|
sigemptyset(&nmask);
|
||||||
pselect(0, NULL, NULL, NULL, NULL, &nmask);
|
pselect(0, NULL, NULL, NULL, NULL, &nmask);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Shut down on INT USR1 USR2
|
* Shut down on INT USR1 USR2
|
||||||
*/
|
*/
|
||||||
void shutdown(int sig)
|
void shutdown_handler(int sig)
|
||||||
{
|
{
|
||||||
touch("/tmp/shutdown");
|
touch("/tmp/shutdown");
|
||||||
if (sdown) {
|
if (sdown[0] != 0) {
|
||||||
system(sdown);
|
system(sdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -599,11 +466,10 @@ void shutdown(int sig)
|
|||||||
reboot(RB_POWER_OFF);
|
reboot(RB_POWER_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* SIGCHLD: one of our children has died
|
* SIGCHLD: one of our children has died
|
||||||
*/
|
*/
|
||||||
void chld_handler(int sig)
|
static void chld_handler(int sig __attribute__ ((unused)))
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
@@ -613,3 +479,58 @@ void chld_handler(int sig)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Signal management - Be conservative with what finit responds to!
|
||||||
|
*
|
||||||
|
* The standard SysV init only responds to the following signals:
|
||||||
|
* SIGHUP
|
||||||
|
* Has the same effect as telinit q.
|
||||||
|
*
|
||||||
|
* SIGUSR1
|
||||||
|
* On receipt of this signals, init closes and re-opens its control
|
||||||
|
* fifo, /dev/initctl. Useful for bootscripts when /dev is remounted.
|
||||||
|
*
|
||||||
|
* SIGINT
|
||||||
|
* Normally the kernel sends this signal to init when CTRL-ALT-DEL is
|
||||||
|
* pressed. It activates the ctrlaltdel action.
|
||||||
|
*
|
||||||
|
* SIGWINCH
|
||||||
|
* The kernel sends this signal when the KeyboardSignal key is hit.
|
||||||
|
* It activates the kbrequest action.
|
||||||
|
*/
|
||||||
|
static void sig_init(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
sigset_t nmask;
|
||||||
|
struct sigaction sa;
|
||||||
|
|
||||||
|
for (i = 1; i < NSIG; i++)
|
||||||
|
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
||||||
|
|
||||||
|
SETSIG(sa, SIGINT, shutdown_handler, 0);
|
||||||
|
SETSIG(sa, SIGPWR, SIG_IGN, 0);
|
||||||
|
SETSIG(sa, SIGUSR1, shutdown_handler, 0);
|
||||||
|
SETSIG(sa, SIGUSR2, shutdown_handler, 0);
|
||||||
|
SETSIG(sa, SIGTERM, SIG_IGN, 0);
|
||||||
|
SETSIG(sa, SIGALRM, SIG_IGN, 0);
|
||||||
|
SETSIG(sa, SIGHUP, SIG_IGN, 0);
|
||||||
|
SETSIG(sa, SIGCONT, SIG_IGN, SA_RESTART);
|
||||||
|
SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
|
||||||
|
|
||||||
|
/* Block sigchild while forking */
|
||||||
|
sigemptyset(&nmask);
|
||||||
|
sigaddset(&nmask, SIGCHLD);
|
||||||
|
sigprocmask(SIG_BLOCK, &nmask, NULL);
|
||||||
|
|
||||||
|
/* Disable CTRL-ALT-DELETE from kernel, we handle shutdown gracefully with SIGINT */
|
||||||
|
reboot(RB_DISABLE_CAD);
|
||||||
|
setsid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
Improved fast init
|
||||||
|
|
||||||
|
Copyright (c) 2008 Claudio Matsuoka
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FINIT_H__
|
||||||
|
#define FINIT_H__
|
||||||
|
|
||||||
|
#include <syslog.h>
|
||||||
|
#define DO_LOG(level, fmt, args...) \
|
||||||
|
{ \
|
||||||
|
openlog("finit", LOG_CONS | LOG_PID, LOG_DAEMON); \
|
||||||
|
syslog(LOG_DEBUG, fmt, ##args); \
|
||||||
|
closelog(); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DEBUG(fmt, args...) DO_LOG(LOG_DEBUG, fmt, ##args)
|
||||||
|
#define ERROR(fmt, args...) DO_LOG(LOG_CRIT, fmt, ##args)
|
||||||
|
|
||||||
|
/* Distribution configuration */
|
||||||
|
|
||||||
|
#if defined DIST_MDV /* Mandriva */
|
||||||
|
#define RANDOMSEED "/var/lib/random-seed"
|
||||||
|
#define SYSROOT "/sysroot"
|
||||||
|
#define CONSOLE "/dev/console"
|
||||||
|
#define GETTY "/usr/bin/openvt /sbin/mingetty tty2"
|
||||||
|
#define RUNPARTS "/usr/bin/run-parts"
|
||||||
|
#define REMOUNT_ROOTFS_RW
|
||||||
|
#define MAKE_DEVICES
|
||||||
|
#define MAKE_DEV_VCS
|
||||||
|
#define PAM_CONSOLE
|
||||||
|
#define LISTEN_INITCTL
|
||||||
|
#define RUNLEVEL 5
|
||||||
|
#define USE_MESSAGE_BUS
|
||||||
|
#elif defined DIST_EEEXUBUNTU /* eeeXubuntu */
|
||||||
|
#define RANDOMSEED "/var/lib/urandom/random-seed"
|
||||||
|
#define SYSROOT "/sysroot"
|
||||||
|
#define CONSOLE "/dev/console"
|
||||||
|
#define GETTY "/usr/bin/openvt /sbin/getty 38400 tty2"
|
||||||
|
#define RUNPARTS "/bin/run-parts"
|
||||||
|
#define REMOUNT_ROOTFS_RW
|
||||||
|
#define MAKE_DEVICES
|
||||||
|
#define TOUCH_ETC_NETWORK_RUN_IFSTATE
|
||||||
|
#define LISTEN_INITCTL
|
||||||
|
#else /* original Eeepc distribution */
|
||||||
|
#define RANDOMSEED "/var/lib/urandom/random-seed"
|
||||||
|
#define SYSROOT "/mnt"
|
||||||
|
#define CONSOLE "/dev/console"
|
||||||
|
#define GETTY "/usr/bin/openvt /sbin/getty 38400 tty2"
|
||||||
|
#define RUNPARTS "/bin/run-parts"
|
||||||
|
#define TOUCH_ETC_NETWORK_RUN_IFSTATE
|
||||||
|
#define USE_ETC_RESOLVCONF_RUN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef DEFUSER
|
||||||
|
#define DEFUSER "user"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LINE_SIZE 1024
|
||||||
|
#define CMD_SIZE 256
|
||||||
|
#define USERNAME_SIZE 16
|
||||||
|
#define HOSTNAME_SIZE 32
|
||||||
|
|
||||||
|
/* From sysvinit */
|
||||||
|
/* Set a signal handler. */
|
||||||
|
#define SETSIG(sa, sig, fun, flags) \
|
||||||
|
do { \
|
||||||
|
sa.sa_handler = fun; \
|
||||||
|
sa.sa_flags = flags; \
|
||||||
|
sigemptyset(&sa.sa_mask); \
|
||||||
|
sigaction(sig, &sa, NULL); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef touch
|
||||||
|
#define touch(x) mknod((x), S_IFREG|0644, 0)
|
||||||
|
#endif
|
||||||
|
#ifndef chardev
|
||||||
|
#define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
|
||||||
|
#endif
|
||||||
|
#ifndef blkdev
|
||||||
|
#define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
|
||||||
|
#endif
|
||||||
|
#ifndef fexist
|
||||||
|
#define fexist(x) (access(x, F_OK) != -1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Match one command. */
|
||||||
|
#define MATCH_CMD(l,c,x) \
|
||||||
|
((!strncmp((l), (c), strlen((c)))) && ((x) = (l) + strlen((c))))
|
||||||
|
|
||||||
|
#define _d(fmt, args...) do { if (debug) { fprintf(stderr, "%s(): " fmt "\n", __func__, ##args); } } while (0)
|
||||||
|
|
||||||
|
void shutdown_handler(int sig);
|
||||||
|
void listen_initctl(void);
|
||||||
|
|
||||||
|
#endif /* FINIT_H__ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
Improved fast init
|
||||||
|
|
||||||
|
Copyright (c) 2008 Claudio Matsuoka
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef LISTEN_INITCTL
|
||||||
|
#include <features.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/select.h> /* According to POSIX.1-2001 */
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "finit.h"
|
||||||
|
|
||||||
|
#define INIT_MAGIC 0x03091969
|
||||||
|
#define INIT_CMD_RUNLVL 1
|
||||||
|
|
||||||
|
struct init_request {
|
||||||
|
int magic; /* Magic number */
|
||||||
|
int cmd; /* What kind of request */
|
||||||
|
int runlevel; /* Runlevel to change to */
|
||||||
|
int sleeptime; /* Time between TERM and KILL */
|
||||||
|
char data[368];
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Standard reboot/shutdown utilities talk to init using /dev/initctl.
|
||||||
|
* We should check if the fifo was recreated and reopen it.
|
||||||
|
*/
|
||||||
|
void listen_initctl(void)
|
||||||
|
{
|
||||||
|
if (!fork()) {
|
||||||
|
int ctl;
|
||||||
|
fd_set fds;
|
||||||
|
struct init_request request;
|
||||||
|
|
||||||
|
mkfifo("/dev/initctl", 0600);
|
||||||
|
ctl = open("/dev/initctl", O_RDONLY);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
FD_ZERO(&fds);
|
||||||
|
FD_SET(ctl, &fds);
|
||||||
|
if (select(ctl + 1, &fds, NULL, NULL, NULL) <= 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
read(ctl, &request, sizeof(request));
|
||||||
|
|
||||||
|
if (request.magic != INIT_MAGIC)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (request.cmd == INIT_CMD_RUNLVL) {
|
||||||
|
switch (request.runlevel) {
|
||||||
|
case '0':
|
||||||
|
shutdown_handler(SIGUSR2);
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
shutdown_handler(SIGUSR1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LISTEN_INITCTL */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user