mirror of
https://github.com/troglobit/finit.git
synced 2026-10-12 10:12:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65c9e7ebc6 | ||
|
|
c002b5301f | ||
|
|
3f1c2f6211 | ||
|
|
5116cd66ef | ||
|
|
f55f68d032 | ||
|
|
5e99b5e735 | ||
|
|
d26852e07a | ||
|
|
a90a7c04e4 | ||
|
|
14a1fb9cde | ||
|
|
b373e199a8 | ||
|
|
149ed0e81c | ||
|
|
e32ca1fc83 | ||
|
|
9aa90763b0 | ||
|
|
06759d3544 | ||
|
|
ff73dab447 | ||
|
|
0d70fa4cf0 | ||
|
|
103bf33b3c |
@@ -1,3 +1,31 @@
|
||||
1.4 (20121002)
|
||||
- Fix I/O plugin watcher and load plugins earlier for a new hook
|
||||
- Start refactoring helpers.c into a libite.so (-lite). This
|
||||
means other user space applications/daemons can make use of
|
||||
the neat toolbox available in finit
|
||||
- Use short-form -s/-w -u to work with BusyBox hwclock as well
|
||||
- Use RTLD_GLOBAL flag to tell dynamic loader to load dependent
|
||||
.so files as well. Lets other plugins use global symbols.
|
||||
- Greatly simplify svc hook for external plugins and cleanup
|
||||
the plugin API.
|
||||
- And more... see the GIT log for more details.
|
||||
|
||||
1.3 (20120928)
|
||||
- Cleanup public plugin API a bit and add new pid/pidfile funcs
|
||||
- Add plugin hook to end of service startup
|
||||
- Remove finit.h from svc.h, plugins should not need this.
|
||||
- Move utility macros etc. to helpers.h
|
||||
- Make finit.h daemon internal, only
|
||||
- Move defines of FIFO, conf and rcS.d to Makefile => correct paths
|
||||
- Add support for installing required headers in system include dir
|
||||
- Better support for distributions and packagers with install-exec,
|
||||
install-data, and install-dev targets in Makefile. Useful if you
|
||||
want to call targets with different $DESTDIR!
|
||||
- Makefile fixes for installation, paths encoded wrong
|
||||
- Strip binaries + .so files, support for $(CROSS) toolchain strip
|
||||
- Default install is now to /sbin/finit and /usr/
|
||||
- Note change in $PLUGIN_DIR environemnt variable to $plugindir
|
||||
|
||||
1.2 (20120927)
|
||||
- Fix installation paths encoded in finit binary
|
||||
- Update README with section on building and enviroment variables
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# finit - The Improved fast init
|
||||
# Finit - Extremely fast /sbin/init replacement w/ I/O, hook & service plugins
|
||||
#
|
||||
# Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
# Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -21,73 +21,127 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
.PHONY: all romfs clean
|
||||
.PHONY: all install clean distclean dist \
|
||||
install-exec install-data install-dev \
|
||||
uninstall-exec uninstall-data uninstall-dev
|
||||
|
||||
# Top directory for building complete system, fall back to this directory
|
||||
ROOTDIR ?= $(shell pwd)
|
||||
|
||||
# Installation paths, always prepended with DESTDIR if set
|
||||
prefix ?= /usr/local
|
||||
sysconfdir ?= /etc
|
||||
datadir = $(prefix)/share/doc/finit
|
||||
mandir = $(prefix)/share/man/man8
|
||||
|
||||
# Plugin directory, fall back to this directory if unset in environment
|
||||
PLUGIN_DIR ?= $(prefix)/lib/finit/plugins
|
||||
ROOTDIR = $(shell pwd)
|
||||
|
||||
#VERSION ?= $(shell git tag -l | tail -1)
|
||||
VERSION ?= 1.2
|
||||
EXEC = finit
|
||||
PKG = $(EXEC)-$(VERSION)
|
||||
VERSION ?= 1.4
|
||||
NAME = finit
|
||||
PKG = $(NAME)-$(VERSION)
|
||||
DEV = $(NAME)-dev
|
||||
ARCHIVE = $(PKG).tar.xz
|
||||
EXEC = finit reboot
|
||||
HEADERS = plugin.h svc.h helpers.h
|
||||
DISTFILES = LICENSE README ChangeLog finit.conf services
|
||||
OBJS = finit.o conf.o helpers.o sig.o svc.o plugin.o
|
||||
OBJS += strlcpy.o
|
||||
SRCS = $(OBJS:.o=.c)
|
||||
DEPS = $(addprefix .,$(SRCS:.c=.d))
|
||||
|
||||
# Installation paths, always prepended with DESTDIR if set
|
||||
prefix ?= /usr
|
||||
sysconfdir ?= /etc
|
||||
sbindir ?= /sbin
|
||||
libdir ?= /lib
|
||||
plugindir ?= $(libdir)/finit/plugins
|
||||
incdir = $(prefix)/include/finit
|
||||
datadir = $(prefix)/share/doc/finit
|
||||
mandir = $(prefix)/share/man/man8
|
||||
|
||||
# The initctl FIFO, should probably be in /run, not /dev
|
||||
FINIT_FIFO ?= /dev/initctl
|
||||
FINIT_CONF ?= $(sysconfdir)/finit.conf
|
||||
FINIT_RCSD ?= $(sysconfdir)/finit.d
|
||||
|
||||
CFLAGS += -W -Wall -Werror -Os
|
||||
# Disable annoying gcc warning for "warn_unused_result", see GIT 37af997
|
||||
CPPFLAGS += -U_FORTIFY_SOURCE
|
||||
CPPFLAGS += -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE
|
||||
CPPFLAGS += -Ilibite -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE
|
||||
CPPFLAGS += -DVERSION=\"$(VERSION)\" -DWHOAMI=\"`whoami`@`hostname`\"
|
||||
CPPFLAGS += -DPLUGIN_PATH=\"$(PLUGIN_DIR)\"
|
||||
LDFLAGS += -rdynamic
|
||||
LDLIBS += -ldl
|
||||
CPPFLAGS += -DFINIT_FIFO=\"$(FINIT_FIFO)\" -DFINIT_CONF=\"$(FINIT_CONF)\"
|
||||
CPPFLAGS += -DFINIT_RCSD=\"$(FINIT_RCSD)\" -DPLUGIN_PATH=\"$(plugindir)\"
|
||||
LDFLAGS += -rdynamic -L$(ROOTDIR)/libite
|
||||
DEPLIBS = libite/libite.so
|
||||
LDLIBS += -ldl -lite
|
||||
|
||||
include common.mk
|
||||
export PLUGIN_DIR ROOTDIR CPPFLAGS
|
||||
export libdir plugindir incdir ROOTDIR CPPFLAGS LDFLAGS LDLIBS
|
||||
|
||||
all: Makefile $(EXEC)
|
||||
all: $(DEPLIBS) $(EXEC)
|
||||
$(MAKE) -C plugins $@
|
||||
|
||||
$(DEPLIBS): Makefile
|
||||
$(MAKE) -C libite all
|
||||
|
||||
$(OBJS): Makefile
|
||||
|
||||
$(EXEC): $(OBJS)
|
||||
finit: $(OBJS) $(DEPLIBS)
|
||||
|
||||
# @ln -sf /sbin/finit /sbin/init
|
||||
install: all
|
||||
@install -d $(DESTDIR)$(prefix)/sbin
|
||||
@install -d $(DESTDIR)$(sysconfdir)
|
||||
@install -d $(DESTDIR)$(datadir)
|
||||
@install -d $(DESTDIR)$(mandir)
|
||||
reboot: reboot.o $(DEPLIBS)
|
||||
|
||||
install-exec: all
|
||||
@$(INSTALL) -d $(DESTDIR)$(FINIT_RCSD)
|
||||
@$(INSTALL) -d $(DESTDIR)$(sbindir)
|
||||
@for file in $(EXEC); do \
|
||||
printf " INSTALL $(DESTDIR)$(prefix)/sbin/$$file\n"; \
|
||||
install -m 0755 $$file $(DESTDIR)$(prefix)/sbin/$$file; \
|
||||
printf " INSTALL $(DESTDIR)$(sbindir)/$$file\n"; \
|
||||
$(STRIPINST) $$file $(DESTDIR)$(sbindir)/$$file; \
|
||||
done
|
||||
$(MAKE) -C plugins $@
|
||||
$(MAKE) -C libite install-exec
|
||||
$(MAKE) -C plugins install
|
||||
|
||||
uninstall:
|
||||
-@for file in $(EXEC); do \
|
||||
printf " REMOVE $(DESTDIR)$(prefix)/sbin/$$file\n"; \
|
||||
rm $(DESTDIR)$(prefix)/sbin/$$file 2>/dev/null; \
|
||||
install-data:
|
||||
@$(INSTALL) -d $(DESTDIR)$(datadir)
|
||||
@$(INSTALL) -d $(DESTDIR)$(mandir)
|
||||
@for file in $(DISTFILES); do \
|
||||
printf " INSTALL $(DESTDIR)$(datadir)/$$file\n"; \
|
||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(datadir)/$$file; \
|
||||
done
|
||||
$(MAKE) -C plugins $@
|
||||
|
||||
install-dev:
|
||||
@$(INSTALL) -d $(DESTDIR)$(incdir)
|
||||
@for file in $(HEADERS); do \
|
||||
printf " INSTALL $(DESTDIR)$(incdir)/$$file\n"; \
|
||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(incdir)/$$file; \
|
||||
done
|
||||
$(MAKE) -C libite install-dev install-exec
|
||||
|
||||
install: install-exec install-data install-dev
|
||||
|
||||
uninstall-exec:
|
||||
-@for file in $(EXEC); do \
|
||||
printf " REMOVE $(DESTDIR)$(sbindir)/$$file\n"; \
|
||||
rm $(DESTDIR)$(sbindir)/$$file 2>/dev/null; \
|
||||
done
|
||||
-@rmdir $(DESTDIR)$(sbindir) 2>/dev/null
|
||||
-@rmdir $(DESTDIR)$(FINIT_RCSD) 2>/dev/null
|
||||
$(MAKE) -C libite uninstall
|
||||
$(MAKE) -C plugins uninstall
|
||||
|
||||
uninstall-data:
|
||||
@for file in $(DISTFILES); do \
|
||||
printf " REMOVE $(DESTDIR)$(datadir)/$$file\n"; \
|
||||
rm $(DESTDIR)$(datadir)/$$file 2>/dev/null; \
|
||||
done
|
||||
-@rmdir $(DESTDIR)$(mandir)
|
||||
-@rmdir $(DESTDIR)$(datadir)
|
||||
|
||||
uninstall-dev:
|
||||
-@$(RM) -rf $(DESTDIR)$(incdir)
|
||||
|
||||
uninstall: uninstall-exec uninstall-data uninstall-dev
|
||||
|
||||
clean:
|
||||
-@$(RM) $(OBJS) $(DEPS) $(EXEC)
|
||||
$(MAKE) -C libite $@
|
||||
$(MAKE) -C plugins $@
|
||||
|
||||
distclean: clean
|
||||
-@$(RM) $(JUNK) unittest *.o
|
||||
-@$(RM) $(JUNK) unittest *.o .*.d
|
||||
$(MAKE) -C libite $@
|
||||
$(MAKE) -C plugins $@
|
||||
|
||||
dist:
|
||||
@@ -95,6 +149,15 @@ dist:
|
||||
git archive --format=tar --prefix=$(PKG)/ $(VERSION) | xz >../$(ARCHIVE)
|
||||
@(cd ..; md5sum $(ARCHIVE) | tee $(ARCHIVE).md5)
|
||||
|
||||
dev: distclean
|
||||
@echo "Building unstable xz $(DEV) in parent dir..."
|
||||
-@$(RM) -f ../$(DEV).tar.xz*
|
||||
@(dir=`mktemp -d`; mkdir $$dir/$(DEV); cp -a . $$dir/$(DEV); \
|
||||
cd $$dir; tar --exclude=.git --exclude=contrib \
|
||||
-c -J -f $(DEV).tar.xz $(DEV); \
|
||||
cd - >/dev/null; mv $$dir/$(DEV).tar.xz ../; cd ..; \
|
||||
rm -rf $$dir; md5sum $(DEV).tar.xz | tee $(DEV).tar.xz.md5)
|
||||
|
||||
# Include automatically generated rules, such as:
|
||||
# uncgi.o: .../some/dir/uncgi.c /usr/include/stdio.h
|
||||
# but don't bother during clean!
|
||||
|
||||
+74
-48
@@ -4,19 +4,17 @@
|
||||
|
||||
Welcome to finit!
|
||||
|
||||
This is a heavily extended version of the original /sbin/init replacement
|
||||
finit_. The original was a reimplementation of the EeePC "fastinit" daemon
|
||||
based on its system calls with gaps filled with frog DNA.
|
||||
This is an extremely fast /sbin/init replacement with focus on small
|
||||
embedded GNU/Linux systems. Based on Claudio Matsuoka's original finit_
|
||||
with I/O, service and hook plugin extensions. The original was a
|
||||
reimplementation of the EeePC fastinit_ daemon based on its system
|
||||
calls with gaps filled with frog DNA.
|
||||
|
||||
Focus of this extended version is on small embedded GNU/Linux systems and
|
||||
headless miniature servers with a need for extremely fast boot. In such
|
||||
environments configurations rarely change and setup is rather static and
|
||||
straight forward.
|
||||
In the world of finit we write C plugins that hook into and extend the
|
||||
``main()`` loop of ``/sbin/init``, this is what gives the system its raw
|
||||
performance and the user full control.
|
||||
|
||||
In the world of finit we write C "scripts" to boot our system, this is what
|
||||
gives the system its raw performance and the user full control.
|
||||
|
||||
If you chose finit you know what you want, a fast boot with no hassle.
|
||||
If you chose finit you know what you want, a fast no hassles boot!
|
||||
|
||||
|
||||
Features
|
||||
@@ -41,31 +39,35 @@ plugins with either hooks or I/O demands.
|
||||
/etc/finit.conf
|
||||
---------------
|
||||
|
||||
Contrary to most script based init alternatives (SysV init_, upstart_, OpenRC_
|
||||
and the likes) finit reads its configuration from /etc/finit.conf, see the
|
||||
source code for available options.
|
||||
Contrary to most script based init alternatives (`SysV init`_, upstart_,
|
||||
OpenRC_ and the likes) finit instead reads its configuration from
|
||||
``/etc/finit.conf``, see the source code for available options.
|
||||
|
||||
Try out the examples/finit.conf for a Debian 6.0 example configuration
|
||||
capable of service monitoring SSH, sysklogd, gdm and a console getty!
|
||||
When running ``make install`` no default ``/etc/finit.conf`` will be
|
||||
provided since the system requirements differ too much. Try out the
|
||||
Debian 6.0 example ``/usr/share/doc/finit/finit.conf`` configuration
|
||||
that is capable of service monitoring SSH, sysklogd, gdm and a console
|
||||
getty!
|
||||
|
||||
|
||||
/etc/finit.d
|
||||
------------
|
||||
|
||||
At the end of the boot, when networking and all services are up, finit calls
|
||||
its built-in run-parts(8) on the /etc/finit.d/ directory, akin to how the
|
||||
/ec/rc.local file works in most other inits, only finit runs a directory of
|
||||
scripts. This replaces the earlier /usr/sbin/services.sh support of the
|
||||
original finit_.
|
||||
At the end of the boot, when networking and all services are up, finit
|
||||
calls its built-in run-parts(8) on the ``/etc/finit.d/`` directory, akin
|
||||
to how the ``/ec/rc.local`` file works in most other inits, only finit
|
||||
runs a directory of scripts. This replaces the earlier
|
||||
``/usr/sbin/services.sh`` support of the original finit_.
|
||||
|
||||
|
||||
Rebooting and Halting
|
||||
---------------------
|
||||
|
||||
Finit handles SIGUSR1 and SIGUSR2 for reboot and halt, and listens to
|
||||
/dev/initctl so standard Linux reboot and halt commands should also
|
||||
``/dev/initctl`` so standard Linux reboot and halt commands should also
|
||||
work.
|
||||
|
||||
|
||||
Building
|
||||
--------
|
||||
|
||||
@@ -80,15 +82,8 @@ what is built and where resulting binaries are installed.
|
||||
**ROOTDIR=**
|
||||
Top directory for building complete system, used in pretty printing
|
||||
|
||||
**PLUGINS=**
|
||||
List of stock finit plugins to build and install.
|
||||
|
||||
**PLUGIN_DIR=**
|
||||
Absolute path to where finit should look for dynamically loadable plugins
|
||||
at runtime. At installation prepended by ''DESTDIR'' and ''prefix''-
|
||||
|
||||
**VERSION=**
|
||||
Defaults to the currently released version of finit, e.g., 1.2 but can
|
||||
Defaults to the currently released version of finit, e.g., 1.3 but can
|
||||
be overridden by packages to add a suffix or completely alter the version.
|
||||
|
||||
**CFLAGS=**
|
||||
@@ -104,24 +99,37 @@ what is built and where resulting binaries are installed.
|
||||
Standard LIBLIBS are inherited from the build enviornmant.
|
||||
|
||||
**prefix=**
|
||||
Base prefix path for all files, except ``sbinbdir`` and ``sysconfdir``.
|
||||
Used in concert with the ``DESTDIR`` variable. Defaults to ``/usr``
|
||||
|
||||
**sbindir=**
|
||||
Path to where resulting binaries should install to. Used in concert
|
||||
with the ''DESTDIR'' variable. Defaults to /usr/local
|
||||
with the ``DESTDIR`` variable. Defaults to ``/sbin``
|
||||
|
||||
**sysconfdir=**
|
||||
Used after ''prefix'' to create the path to finit.conf configuration
|
||||
file. Defaults to /etc
|
||||
Path to where finit configuration files should install to. Used in
|
||||
concert with the ``DESTDIR`` variable. Defaults to ``/etc``, but is
|
||||
currently unused.
|
||||
|
||||
**PLUGINS=**
|
||||
List of stock finit plugins to build and install.
|
||||
|
||||
**plugindir=**
|
||||
Absolute path to where finit should look for dynamically loadable plugins
|
||||
at runtime. At installation prepended by ``DESTDIR`` and ``prefix``.
|
||||
Defaults to ``/lib/finit/plugins``
|
||||
|
||||
**DESTDIR=**
|
||||
Used by packagers and distributions when building a relocatable
|
||||
bundle of files. Alwawys prepended to the ''prefix'' destination
|
||||
bundle of files. Alwawys prepended to the ``prefix`` destination
|
||||
directory.
|
||||
|
||||
**Example**::
|
||||
|
||||
$ tar xfJ finit-1.2.tar.xz
|
||||
$ PLUGINS="initctl.so hwclock.so" prefix= DESTDIR=/tmp/finit/dst \
|
||||
make -C finit-1.2/ clean install
|
||||
make: Entering directory `/home/troglobit/finit-1.2'
|
||||
$ tar xfJ finit-1.3.tar.xz
|
||||
$ PLUGINS="initctl.so hwclock.so" DESTDIR=/tmp/finit/dst \
|
||||
make -C finit-1.3/ clean install
|
||||
make: Entering directory `/home/troglobit/finit-1.3'
|
||||
CC finit.o
|
||||
CC conf.o
|
||||
CC helpers.o
|
||||
@@ -137,25 +145,42 @@ what is built and where resulting binaries are installed.
|
||||
INSTALL /tmp/finit/dst/sbin/finit
|
||||
INSTALL /tmp/finit/dst/lib/finit/plugins/initctl.so
|
||||
INSTALL /tmp/finit/dst/lib/finit/plugins/hwclock.so
|
||||
make: Leaving directory `/home/troglobit/finit-1.2'
|
||||
make: Leaving directory `/home/troglobit/finit-1.3'
|
||||
|
||||
In this example the finit-1.2.tar.xz archive is unpacked to the user's
|
||||
home directory, built and installed to a temporary staging directory.
|
||||
The enviroment variables ''prefix'', ''DESTDIR'' and ''PLUGINS'' are all
|
||||
In this example the `finit-1.3.tar.xz`_ archive is unpacked to the
|
||||
user's home directory, built and installed to a temporary staging
|
||||
directory. The enviroment variables ``DESTDIR`` and ``PLUGINS`` are
|
||||
changed to suit this particular build.
|
||||
|
||||
|
||||
Running
|
||||
-------
|
||||
|
||||
The default install does not setup finit as the system default
|
||||
``/sbin/init``, neither does it setup an initial ``/etc/finit.conf``.
|
||||
|
||||
It is assumed that users of finit are competent enough to either setup
|
||||
finit as their default ``/sbin/init`` or alter their respective Grub,
|
||||
LOADLIN, LILO, U-Boot/Barebox or RedBoot boot loader configuration to
|
||||
give the kernel the following extra command line::
|
||||
|
||||
init=/sbin/finit
|
||||
|
||||
|
||||
Debugging
|
||||
---------
|
||||
|
||||
Add finit_debug, or --debug, to the kernel command line to enable trace
|
||||
messages. A console getty is always started, see the file finit.h for
|
||||
more useful comile-time tweaks.
|
||||
Add ``finit_debug``, or ``--debug``, to the kernel command line to
|
||||
enable trace messages. A console getty is always started, see the file
|
||||
``finit.h`` for more useful comile-time tweaks::
|
||||
|
||||
init=/sbin/finit --debug
|
||||
|
||||
|
||||
Contact
|
||||
-------
|
||||
|
||||
Finit is maintained collaborativly at https://github.com/troglobit/finit —
|
||||
Finit is maintained collaborativly at http://github.com/troglobit/finit —
|
||||
please file a bug report, clone it, or send pull requests for bug fixes and
|
||||
proposed extensions, or become a co-maintainer by contacting the main author.
|
||||
|
||||
@@ -163,7 +188,8 @@ Regards
|
||||
/Joachim Nilsson <troglobit@gmail.com>
|
||||
|
||||
.. _finit: http://helllabs.org/finit/
|
||||
.. _init: http://savannah.nongnu.org/projects/sysvinit
|
||||
.. _fastinit: http://wiki.eeeuser.com/boot_process:the_boot_process
|
||||
.. _`SysV init`: http://savannah.nongnu.org/projects/sysvinit
|
||||
.. _upstart: http://upstart.ubuntu.com/
|
||||
.. _openrc: http://www.gentoo.org/proj/en/base/openrc/
|
||||
|
||||
.. _`finit-1.3.tar.xz`: http://github.com/downloads/troglobit/finit/finit-1.3.tar.xz
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
* Look at how to support a standard SysV init runlevel mechanism
|
||||
* Arguments to finit, like to SysV init "init q" to reload finit.conf at runtime, etc.
|
||||
* Implement initctl stop|start|reload <SVC> and service <SVC> stop|start|reload on top
|
||||
* Write man pages for finit and finit.conf, steal from pimd man pages...
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# 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.*
|
||||
MAKE := @$(MAKE)
|
||||
MAKEFLAGS = --no-print-directory
|
||||
MAKEFLAGS = --no-print-directory --silent
|
||||
INSTALL := install --backup=off
|
||||
STRIPINST := $(INSTALL) -s --strip-program=$(CROSS)strip -m 0755
|
||||
|
||||
# Smart autodependecy generation via GCC -M.
|
||||
.%.d: %.c
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Improved fast init
|
||||
/* Finit - Extremely fast /sbin/init replacement w/ I/O, hook & service plugins
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -49,6 +49,7 @@ static void parse_kernel_cmdline(void)
|
||||
|
||||
if ((fp = fopen("/proc/cmdline", "r")) != NULL) {
|
||||
fgets(line, sizeof(line), fp);
|
||||
_d("Kernel command line: %s", line);
|
||||
|
||||
if (strstr(line, "finit_debug") || strstr(line, "--debug"))
|
||||
debug = 1;
|
||||
@@ -64,7 +65,7 @@ static int run_loop(void)
|
||||
{
|
||||
while (1) {
|
||||
svc_monitor();
|
||||
io_monitor();
|
||||
plugin_monitor();
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -91,15 +92,6 @@ int main(int UNUSED(args), char *argv[])
|
||||
mount("none", "/dev/shm", "tmpfs", 0, NULL);
|
||||
umask(022);
|
||||
|
||||
/*
|
||||
* Populate /dev and prepare for runtime events from kernel.
|
||||
*/
|
||||
#if defined(USE_UDEV)
|
||||
run_interactive("udevd --daemon", "Populating device tree");
|
||||
#elif defined (MDEV)
|
||||
run_interactive(MDEV "-s", "Populating device tree");
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Parse kernel parameters
|
||||
*/
|
||||
@@ -108,11 +100,28 @@ int main(int UNUSED(args), char *argv[])
|
||||
cls();
|
||||
echo("finit " VERSION " (built " __DATE__ " " __TIME__ " by " WHOAMI ")");
|
||||
|
||||
/*
|
||||
* Populate /dev and prepare for runtime events from kernel.
|
||||
*/
|
||||
#if defined(USE_UDEV)
|
||||
run_interactive("udevd --daemon", "Populating device tree");
|
||||
#elif defined (MDEV)
|
||||
run_interactive(MDEV " -s", "Populating device tree");
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Parse configuration file
|
||||
*/
|
||||
parse_finit_conf(FINIT_CONF);
|
||||
|
||||
/*
|
||||
* Load plugins. Must run after finit.conf has registered
|
||||
* all services, or service plugins won't have anything to
|
||||
* hook on to.
|
||||
*/
|
||||
print_desc("", "Loading plugins");
|
||||
print_result(plugin_load_all(PLUGIN_PATH));
|
||||
|
||||
/*
|
||||
* Mount filesystems
|
||||
*/
|
||||
@@ -124,6 +133,9 @@ int main(int UNUSED(args), char *argv[])
|
||||
#ifdef SYSROOT
|
||||
run(SYSROOT, "/", NULL, MS_MOVE, NULL);
|
||||
#endif
|
||||
_d("Root FS up, calling hooks ...");
|
||||
plugin_run_hooks(HOOK_ROOTFS_UP);
|
||||
|
||||
umask(0);
|
||||
run("/bin/mount -na");
|
||||
run("/sbin/swapon -ea");
|
||||
@@ -137,14 +149,8 @@ int main(int UNUSED(args), char *argv[])
|
||||
*/
|
||||
sig_setup();
|
||||
|
||||
/*
|
||||
* Load plugins and run first level hooks.
|
||||
*/
|
||||
_d("Loading plugins ...");
|
||||
load_plugins(PLUGIN_PATH);
|
||||
|
||||
_d("Running first level hooks ...");
|
||||
run_hooks(HOOK_BASEFS_UP);
|
||||
_d("Base FS up, calling hooks ...");
|
||||
plugin_run_hooks(HOOK_BASEFS_UP);
|
||||
|
||||
/*
|
||||
* Network stuff
|
||||
@@ -164,7 +170,7 @@ int main(int UNUSED(args), char *argv[])
|
||||
/*
|
||||
* Hooks that rely on loopback, or basic networking being up.
|
||||
*/
|
||||
run_hooks(HOOK_NETWORK_UP);
|
||||
plugin_run_hooks(HOOK_NETWORK_UP);
|
||||
|
||||
/*
|
||||
* Start service monitor framework
|
||||
@@ -181,7 +187,7 @@ int main(int UNUSED(args), char *argv[])
|
||||
/*
|
||||
* Hooks that should run at the very end
|
||||
*/
|
||||
run_hooks(HOOK_SYSTEM_UP);
|
||||
plugin_run_hooks(HOOK_SYSTEM_UP);
|
||||
|
||||
/* Start GETTY on console */
|
||||
_d("Starting getty on console ...");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Improved fast init
|
||||
/* Finit - Extremely fast /sbin/init replacement w/ I/O, hook & service plugins
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -33,35 +33,18 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.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)
|
||||
|
||||
#define FINIT_FIFO "/dev/initctl"
|
||||
#define FINIT_CONF "/etc/finit.conf"
|
||||
#define FINIT_RCSD "/etc/finit.d"
|
||||
|
||||
/* Distribution configuration */
|
||||
|
||||
#if defined EMBEDDED_SYSTEM
|
||||
# define CONSOLE "/dev/console"
|
||||
# define MDEV "/sbin/mdev"
|
||||
# define GETTY "/sbin/getty -L 115200 ttyS0 vt100"
|
||||
# define LISTEN_INITCTL
|
||||
#else /* Debian/Ubuntu based distributions */
|
||||
# define RANDOMSEED "/var/lib/urandom/random-seed"
|
||||
# define CONSOLE "/dev/tty1"
|
||||
# define GETTY "/sbin/getty -8 38400 tty1"
|
||||
# define REMOUNT_ROOTFS_RW
|
||||
# define RUNLEVEL 2
|
||||
# define LISTEN_INITCTL
|
||||
# define USE_UDEV
|
||||
# define USE_MESSAGE_BUS
|
||||
#endif
|
||||
@@ -79,42 +62,14 @@
|
||||
#define USERNAME_SIZE 16
|
||||
#define HOSTNAME_SIZE 32
|
||||
|
||||
#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
|
||||
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||
#endif
|
||||
|
||||
#define echo(fmt, args...) do { if (1) { fprintf(stderr, fmt "\n", ##args); } } while (0)
|
||||
#define _d(fmt, args...) do { if (debug) { fprintf(stderr, "finit:%s() - " fmt "\n", __func__, ##args); } } while (0)
|
||||
#define _e(fmt, args...) do { fprintf(stderr, "finit:%s() - " fmt "\n", __func__, ##args); } while (0)
|
||||
#define _pe(fmt, args...) do { fprintf(stderr, "finit:%s() - " fmt ". Error %d: %s\n", __func__, ##args, errno, strerror(errno)); } while (0)
|
||||
|
||||
extern int debug;
|
||||
extern int verbose;
|
||||
extern char *sdown;
|
||||
extern char *network;
|
||||
extern char *hostname;
|
||||
extern char *username;
|
||||
|
||||
/* strlcpy.c */
|
||||
size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||
|
||||
/* conf.c */
|
||||
void parse_finit_conf(char *file);
|
||||
|
||||
|
||||
#endif /* FINIT_H_ */
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Misc. utility functions and C-library extensions to simplify finit system setup.
|
||||
/* Misc. utility functions and C-library extensions for finit and its plugins
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -48,6 +48,7 @@
|
||||
#include "helpers.h"
|
||||
#include "private.h"
|
||||
#include "sig.h"
|
||||
#include "lite.h"
|
||||
|
||||
#define NUM_ARGS 16
|
||||
#define NUM_SCRIPTS 128 /* ought to be enough for anyone */
|
||||
@@ -56,23 +57,30 @@
|
||||
* Helpers to replace system() calls
|
||||
*/
|
||||
|
||||
int makepath(char *p)
|
||||
int makepath(char *path)
|
||||
{
|
||||
char *x, path[PATH_MAX];
|
||||
char buf[PATH_MAX];
|
||||
char *x = buf;
|
||||
int ret;
|
||||
|
||||
x = path;
|
||||
|
||||
if (!path) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
do { *x++ = *p++; } while (*p && *p != '/');
|
||||
do {
|
||||
*x++ = *path++;
|
||||
} while (*path && *path != '/');
|
||||
|
||||
*x = 0;
|
||||
ret = mkdir(path, 0777);
|
||||
} while (*p && (*p != '/' || *(p + 1))); /* ignore trailing slash */
|
||||
ret = mkdir(buf, 0777);
|
||||
} while (*path && (*path != '/' || *(path + 1))); /* ignore trailing slash */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ifconfig(char *name, char *inet, char *mask, int up)
|
||||
void ifconfig(char *ifname, char *addr, char *mask, int up)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
struct sockaddr_in *a = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
@@ -82,14 +90,18 @@ void ifconfig(char *name, char *inet, char *mask, int up)
|
||||
return;
|
||||
|
||||
memset(&ifr, 0, sizeof (ifr));
|
||||
strlcpy(ifr.ifr_name, name, IFNAMSIZ);
|
||||
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
|
||||
if (up) {
|
||||
inet_aton(inet, &a->sin_addr);
|
||||
ioctl(sock, SIOCSIFADDR, &ifr);
|
||||
inet_aton(mask, &a->sin_addr);
|
||||
ioctl(sock, SIOCSIFNETMASK, &ifr);
|
||||
if (addr) {
|
||||
inet_aton(addr, &a->sin_addr);
|
||||
ioctl(sock, SIOCSIFADDR, &ifr);
|
||||
}
|
||||
if (mask) {
|
||||
inet_aton(mask, &a->sin_addr);
|
||||
ioctl(sock, SIOCSIFNETMASK, &ifr);
|
||||
}
|
||||
}
|
||||
|
||||
ioctl(sock, SIOCGIFFLAGS, &ifr);
|
||||
@@ -105,30 +117,213 @@ void ifconfig(char *name, char *inet, char *mask, int up)
|
||||
}
|
||||
|
||||
|
||||
void copyfile(char *src, char *dst, int size)
|
||||
/**
|
||||
* pidfile_read - Reads a PID value from a pidfile.
|
||||
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||
*
|
||||
* This function takes a @pidfile and returns the PID found therein.
|
||||
*
|
||||
* Returns:
|
||||
* On invalid @pidfile -1 and @errno set to %EINVAL, when @pidfile does not exist -1
|
||||
* and errno set to %ENOENT. When the pidfile is empty or when its contents cannot
|
||||
* be translated this function returns zero (0), on success this function returns
|
||||
* a PID value greater than one. PID 1 is reserved for the system init process.
|
||||
*/
|
||||
pid_t pidfile_read(char *pidfile)
|
||||
{
|
||||
char buffer[BUF_SIZE];
|
||||
int s, d, n;
|
||||
pid_t pid = 0;
|
||||
char buf[16];
|
||||
FILE *fp;
|
||||
|
||||
/* Size == 0 means copy entire file */
|
||||
if (size == 0)
|
||||
size = INT_MAX;
|
||||
if (!pidfile) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((s = open(src, O_RDONLY)) >= 0) {
|
||||
if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) {
|
||||
do {
|
||||
int csize = size > BUF_SIZE ? BUF_SIZE : size;
|
||||
|
||||
if ((n = read(s, buffer, csize)) > 0)
|
||||
write(d, buffer, n);
|
||||
size -= csize;
|
||||
} while (size > 0 && n == BUF_SIZE);
|
||||
close(d);
|
||||
}
|
||||
close(s);
|
||||
}
|
||||
if (!fexist(pidfile))
|
||||
return -1;
|
||||
|
||||
fp = fopen(pidfile, "r");
|
||||
if (!fp)
|
||||
return -1;
|
||||
|
||||
if (fgets(buf, sizeof(buf), fp)) {
|
||||
errno = 0;
|
||||
pid = strtoul(buf, NULL, 0);
|
||||
if (errno)
|
||||
pid = 0; /* Failed conversion. */
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* pidfile_poll - Poll for the existence of a pidfile and return PID
|
||||
* @cmd: Process name, or command, called to expect a pidfile
|
||||
* @path: Path to pidfile to poll for
|
||||
*
|
||||
* This function polls for the pidfile at @path for at most 5 seconds
|
||||
* before timing out. If the file is created within that time span the
|
||||
* file is read and its PID contents returned.
|
||||
*
|
||||
* Returns:
|
||||
* The PID read from @path, or zero on timeout.
|
||||
*/
|
||||
pid_t pidfile_poll(char *cmd, char *path)
|
||||
{
|
||||
pid_t pid = 0;
|
||||
int tries = 0;
|
||||
|
||||
/* Timeout = 100 * 50ms = 5s */
|
||||
while (!fexist(path) && tries++ < 100) {
|
||||
/* Wait 50ms between retries */
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
if (!fexist(path)) {
|
||||
_e("Timeout! No PID found for %s, pidfile %s does not exist?", cmd, path);
|
||||
pid = 0;
|
||||
} else {
|
||||
pid = pidfile_read(path);
|
||||
}
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pid_alive - Check if a given process ID is running
|
||||
* @pid: Process ID to check for.
|
||||
*
|
||||
* Returns:
|
||||
* %TRUE(1) if pid is alive (/proc/pid exists), otherwise %FALSE(0)
|
||||
*/
|
||||
int pid_alive(pid_t pid)
|
||||
{
|
||||
char name[24]; /* Enough for max pid_t */
|
||||
|
||||
snprintf(name, sizeof(name), "/proc/%d", pid);
|
||||
|
||||
return fexist(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pid_get_name - Find name of a process
|
||||
* @pid: PID of process to find.
|
||||
* @name: Pointer to buffer where to return process name, may be %NULL
|
||||
* @len: Length in bytes of @name buffer.
|
||||
*
|
||||
* If @name is %NULL, or @len is zero the function will return
|
||||
* a static string. This may be useful to one-off calls.
|
||||
*
|
||||
* Returns:
|
||||
* %NULL on error, otherwise a va
|
||||
*/
|
||||
char *pid_get_name(pid_t pid, char *name, size_t len)
|
||||
{
|
||||
int ret = 1;
|
||||
FILE *fp;
|
||||
char path[32];
|
||||
static char line[64];
|
||||
|
||||
if (!name || len == 0)
|
||||
name = line;
|
||||
|
||||
snprintf(path, sizeof(path), "/proc/%d/status", pid);
|
||||
if ((fp = fopen(path, "r")) != NULL) {
|
||||
if (fgets(line, sizeof (line), fp)) {
|
||||
char *pname = line + 6; /* Skip first part of line --> "Name:\t" */
|
||||
|
||||
chomp(pname);
|
||||
strlcpy(name, pname, len);
|
||||
ret = 0; /* Found it! */
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if (ret)
|
||||
return NULL;
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* procname_set - Change process name, as seen in process listnings
|
||||
* @name: New name of process
|
||||
* @args: The process' argv[] vector.
|
||||
*/
|
||||
void procname_set(char *name, char *args[])
|
||||
{
|
||||
size_t len = strlen(args[0]) + 1; /* Include terminating '\0' */
|
||||
|
||||
prctl(PR_SET_NAME, name, 0, 0, 0);
|
||||
memset(args[0], 0, len);
|
||||
strlcpy(args[0], name, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* procname_kill - Send a signal to a process group by name.
|
||||
* @name: Name of process to send signal to.
|
||||
* @signo: Signal to send.
|
||||
*
|
||||
* Send a signal to a running process (group). This function searches
|
||||
* for a given process @name and sends a signal, @signo, to each
|
||||
* process ID matching that @name.
|
||||
*
|
||||
* Returns:
|
||||
* Number of signals sent, i.e. number of processes who have received the signal.
|
||||
*/
|
||||
int procname_kill (char *name, int signo)
|
||||
{
|
||||
int result = 0;
|
||||
char path[32], line[64];
|
||||
FILE *fp;
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
|
||||
dir = opendir("/proc");
|
||||
if (!dir || !name) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((entry = readdir (dir)) != NULL) {
|
||||
/* Skip non-process entries in /proc */
|
||||
if (!isdigit (*entry->d_name))
|
||||
continue;
|
||||
|
||||
snprintf (path, sizeof(path), "/proc/%s/status", entry->d_name);
|
||||
/* Skip non-readable files (protected?) */
|
||||
if ((fp = fopen (path, "r")) == NULL)
|
||||
continue;
|
||||
|
||||
if (fgets (line, sizeof (line), fp)) {
|
||||
char *pname = line + 6; /* Skip first part of line --> "Name:\t" */
|
||||
|
||||
if (strncmp(pname, name, strlen(pname) - 1) == 0) {
|
||||
int error = errno;
|
||||
|
||||
if (kill(atoi(entry->d_name), signo)) {
|
||||
FLOG_ERROR("Failed signalling(%d) %s: %s!", signo, name, strerror(error));
|
||||
} else {
|
||||
result++; /* Track number of processes we deliver the signal to. */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static int print_uptime(void)
|
||||
{
|
||||
#if defined(CONFIG_PRINTK_TIME)
|
||||
@@ -150,7 +345,7 @@ static int print_uptime(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_descr(char *action, char *descr)
|
||||
void print_desc(char *action, char *desc)
|
||||
{
|
||||
const char home[] = "\r\e[K";
|
||||
const char dots[] = " .....................................................................";
|
||||
@@ -160,8 +355,8 @@ void print_descr(char *action, char *descr)
|
||||
print_uptime();
|
||||
|
||||
write(STDERR_FILENO, action, strlen(action));
|
||||
write(STDERR_FILENO, descr, strlen(descr));
|
||||
write(STDERR_FILENO, dots, 60 - strlen(descr) - strlen(action)); /* pad with dots. */
|
||||
write(STDERR_FILENO, desc, strlen(desc));
|
||||
write(STDERR_FILENO, dots, 60 - strlen(desc) - strlen(action)); /* pad with dots. */
|
||||
}
|
||||
|
||||
int print_result(int fail)
|
||||
@@ -285,7 +480,7 @@ int run_interactive(char *cmd, char *fmt, ...)
|
||||
vsnprintf(line, sizeof(line), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
print_descr("", line);
|
||||
print_desc("", line);
|
||||
if (fp && !debug) {
|
||||
oldout = dup(STDOUT_FILENO);
|
||||
olderr = dup(STDERR_FILENO);
|
||||
@@ -333,21 +528,19 @@ pid_t run_getty(char *cmd, char *argv[])
|
||||
if (open(CONSOLE, O_RDWR) != 0)
|
||||
exit(1);
|
||||
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_handler = SIG_DFL;
|
||||
|
||||
sigemptyset(&nmask);
|
||||
sigaddset(&nmask, SIGCHLD);
|
||||
sigprocmask(SIG_UNBLOCK, &nmask, NULL);
|
||||
|
||||
for (i = 1; i < NSIG; i++)
|
||||
sigaction(i, &sa, NULL);
|
||||
/* Reset signal handlers that were set by the parent process */
|
||||
for (i = 1; i < NSIG; i++)
|
||||
DFLSIG(sa, i, 0);
|
||||
|
||||
dup2(0, STDIN_FILENO);
|
||||
dup2(0, STDOUT_FILENO);
|
||||
dup2(0, STDERR_FILENO);
|
||||
|
||||
set_procname(argv, "console");
|
||||
procname_set("console", argv);
|
||||
|
||||
while (!fexist(SYNC_SHUTDOWN)) {
|
||||
static const char msg[] = "\nPlease press Enter to activate this console. ";
|
||||
@@ -446,21 +639,21 @@ int run_parts(char *dir, ...)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getuser(char *s)
|
||||
int getuser(char *username)
|
||||
{
|
||||
struct passwd *usr;
|
||||
|
||||
if (!s || (usr = getpwnam(s)) == NULL)
|
||||
if (!username || (usr = getpwnam(username)) == NULL)
|
||||
return -1;
|
||||
|
||||
return usr->pw_uid;
|
||||
}
|
||||
|
||||
int getgroup(char *s)
|
||||
int getgroup(char *group)
|
||||
{
|
||||
struct group *grp;
|
||||
|
||||
if ((grp = getgrnam(s)) == NULL)
|
||||
if ((grp = getgrnam(group)) == NULL)
|
||||
return -1;
|
||||
|
||||
return grp->gr_gid;
|
||||
@@ -478,122 +671,14 @@ void cls(void)
|
||||
fprintf (stderr, "%s", cls);
|
||||
}
|
||||
|
||||
void chomp(char *s)
|
||||
void chomp(char *str)
|
||||
{
|
||||
char *x;
|
||||
|
||||
if ((x = strchr((s), 0x0a)) != NULL)
|
||||
if ((x = strchr((str), 0x0a)) != NULL)
|
||||
*x = 0;
|
||||
}
|
||||
|
||||
void set_procname(char *args[], char *name)
|
||||
{
|
||||
size_t len = strlen(args[0]) + 1; /* Include terminating '\0' */
|
||||
|
||||
prctl(PR_SET_NAME, name, 0, 0, 0);
|
||||
memset(args[0], 0, len);
|
||||
strlcpy(args[0], name, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* get_pidname - Find name of a process
|
||||
* @pid: PID of process to find.
|
||||
* @name: Pointer to buffer where to return process name, may be %NULL
|
||||
* @len: Length in bytes of @name buffer.
|
||||
*
|
||||
* If @name is %NULL, or @len is zero the function will return
|
||||
* a static string. This may be useful to one-off calls.
|
||||
*
|
||||
* Returns:
|
||||
* %NULL on error, otherwise a va
|
||||
*/
|
||||
char *get_pidname(pid_t pid, char *name, size_t len)
|
||||
{
|
||||
int ret = 1;
|
||||
FILE *fp;
|
||||
char path[32];
|
||||
static char line[64];
|
||||
|
||||
if (!name || len == 0)
|
||||
name = line;
|
||||
|
||||
snprintf(path, sizeof(path), "/proc/%d/status", pid);
|
||||
if ((fp = fopen(path, "r")) != NULL) {
|
||||
if (fgets(line, sizeof (line), fp)) {
|
||||
char *pname = line + 6; /* Skip first part of line --> "Name:\t" */
|
||||
|
||||
chomp(pname);
|
||||
strlcpy(name, pname, len);
|
||||
ret = 0; /* Found it! */
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if (ret)
|
||||
return NULL;
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* kill_procname - Send a signal to a process group by name.
|
||||
* @name: Name of process to send signal to.
|
||||
* @signo: Signal to send.
|
||||
*
|
||||
* Send a signal to a running process (group). This function searches
|
||||
* for a given process @name and sends a signal, @signo, to each
|
||||
* process ID matching that @name.
|
||||
*
|
||||
* Returns:
|
||||
* Number of signals sent, i.e. number of processes who have received the signal.
|
||||
*/
|
||||
int kill_procname (const char *name, int signo)
|
||||
{
|
||||
int result = 0;
|
||||
char path[32], line[64];
|
||||
FILE *fp;
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
|
||||
dir = opendir("/proc");
|
||||
if (!dir || !name) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((entry = readdir (dir)) != NULL) {
|
||||
/* Skip non-process entries in /proc */
|
||||
if (!isdigit (*entry->d_name))
|
||||
continue;
|
||||
|
||||
snprintf (path, sizeof(path), "/proc/%s/status", entry->d_name);
|
||||
/* Skip non-readable files (protected?) */
|
||||
if ((fp = fopen (path, "r")) == NULL)
|
||||
continue;
|
||||
|
||||
if (fgets (line, sizeof (line), fp)) {
|
||||
char *pname = line + 6; /* Skip first part of line --> "Name:\t" */
|
||||
|
||||
if (strncmp(pname, name, strlen(pname) - 1) == 0) {
|
||||
int error = errno;
|
||||
|
||||
if (kill(atoi(entry->d_name), signo)) {
|
||||
ERROR("Failed signalling(%d) %s: %s!", signo, name, strerror(error));
|
||||
} else {
|
||||
result++; /* Track number of processes we deliver the signal to. */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void set_hostname(char *hostname)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Misc. utility functions and C-library extensions to simplify finit system setup.
|
||||
/* Misc. utility functions and C-library extensions for finit and its plugins
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -25,19 +25,62 @@
|
||||
#ifndef FINIT_HELPERS_H_
|
||||
#define FINIT_HELPERS_H_
|
||||
|
||||
int makepath (char *);
|
||||
void ifconfig (char *, char *, char *, int);
|
||||
void copyfile (char *, char *, int);
|
||||
void print_descr (char *action, char *descr);
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#define DO_LOG(level, fmt, args...) \
|
||||
{ \
|
||||
openlog("finit", LOG_CONS | LOG_PID, LOG_DAEMON); \
|
||||
syslog(level, fmt, ##args); \
|
||||
closelog(); \
|
||||
}
|
||||
|
||||
#define FLOG_DEBUG(fmt, args...) DO_LOG(LOG_DEBUG, fmt, ##args)
|
||||
#define FLOG_ERROR(fmt, args...) DO_LOG(LOG_CRIT, fmt, ##args)
|
||||
#define FLOG_PERROR(fmt, args...) DO_LOG(LOG_CRIT, fmt ". Error %d: %s", ##args, errno, strerror(errno))
|
||||
|
||||
|
||||
#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 UNUSED
|
||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||
#endif
|
||||
|
||||
#define echo(fmt, args...) do { if (verbose) { fprintf(stderr, fmt "\n", ##args); } } while (0)
|
||||
#define _d(fmt, args...) do { if (debug) { fprintf(stderr, "finit:%s() - " fmt "\n", __func__, ##args); } } while (0)
|
||||
#define _e(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } while (0)
|
||||
#define _pe(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt ". Error %d: %s\n", __FILE__, __func__, ##args, errno, strerror(errno)); } while (0)
|
||||
|
||||
extern int debug;
|
||||
extern int verbose;
|
||||
|
||||
int makepath (char *path);
|
||||
void ifconfig (char *ifname, char *addr, char *mask, int up);
|
||||
|
||||
pid_t pidfile_read (char *pidfile);
|
||||
pid_t pidfile_poll (char *cmd, char *path);
|
||||
|
||||
int pid_alive (pid_t pid);
|
||||
char *pid_get_name (pid_t pid, char *name, size_t len);
|
||||
|
||||
void procname_set (char *name, char *args[]);
|
||||
int procname_kill (char *name, int signo);
|
||||
|
||||
void print_desc (char *action, char *desc);
|
||||
int print_result (int fail);
|
||||
int start_process (char *cmd, char *args[], int console);
|
||||
void cls (void);
|
||||
void chomp (char *);
|
||||
int getuser (char *);
|
||||
int getgroup (char *);
|
||||
void set_procname (char *args[], char *name);
|
||||
char *get_pidname (pid_t pid, char *name, size_t len);
|
||||
int kill_procname (const char *name, int signo);
|
||||
void chomp (char *str);
|
||||
int getuser (char *username);
|
||||
int getgroup (char *group);
|
||||
void set_hostname (char *hostname);
|
||||
|
||||
int run (char *cmd);
|
||||
@@ -45,6 +88,9 @@ int run_interactive (char *cmd, char *fmt, ...);
|
||||
pid_t run_getty (char *cmd, char *argv[]);
|
||||
int run_parts (char *dir, ...);
|
||||
|
||||
/* strlcpy.c */
|
||||
size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||
|
||||
#endif /* FINIT_HELPERS_H_ */
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
# Finit libite -- Light-weight utility functions and C-library extensions
|
||||
#
|
||||
# Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
#
|
||||
# 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.
|
||||
|
||||
.EXPORT_ALL_VARIABLES:
|
||||
.PHONY: all install clean
|
||||
|
||||
include ../common.mk
|
||||
|
||||
CFLAGS += -fPIC
|
||||
HEADERS = lite.h
|
||||
OBJS := copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o
|
||||
DEPS := $(addprefix ., $(OBJS:.o=.d))
|
||||
|
||||
VER = 0
|
||||
LIBNAME = libite
|
||||
TARGET = $(LIBNAME).so
|
||||
VERLIB = $(TARGET).$(VER)
|
||||
STATICLIB = $(LIBNAME).a
|
||||
|
||||
CHECK_FLAGS = $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(OBJS): Makefile
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
@printf " LINK %s\n" $@
|
||||
# @$(AR) $(ARFLAGS) $(STATICLIB) $(OBJS)
|
||||
@$(CC) $(LDFLAGS) -shared $(OBJS) -lrt -lcrypt -Wl,-soname,$@ -o $@
|
||||
@ln -sf $@ $(VERLIB)
|
||||
|
||||
install-exec: all
|
||||
@install -d $(DESTDIR)$(libdir)
|
||||
@echo " INSTALL $(DESTDIR)$(libdir)/$(TARGET)"
|
||||
@$(STRIPINST) $(TARGET) $(DESTDIR)$(libdir)/$(TARGET)
|
||||
@ln -sf $(TARGET) $(DESTDIR)$(libdir)/$(VERLIB)
|
||||
|
||||
install-dev:
|
||||
@for file in $(HEADERS); do \
|
||||
printf " INSTALL $(DESTDIR)$(incdir)/$$file\n"; \
|
||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(incdir)/$$file; \
|
||||
done
|
||||
|
||||
install: install-dev install-exec
|
||||
|
||||
uninstall:
|
||||
-@$(RM) $(DESTDIR)$(libdir)/$(VERLIB)
|
||||
-@$(RM) $(DESTDIR)$(libdir)/$(TARGET)
|
||||
|
||||
clean: uninstall
|
||||
-@$(RM) $(OBJS) $(DEPS) $(TARGET) $(VERLIB) $(STATICLIB)
|
||||
|
||||
distclean: clean
|
||||
-@$(RM) $(JUNK) unittest *.o .*.d *.unittest
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
ifneq ($(MAKECMDGOALS),distclean)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
endif
|
||||
@@ -0,0 +1,317 @@
|
||||
/* Fastinit (finit) copyfile() implementation.
|
||||
*
|
||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "lite.h"
|
||||
|
||||
static int __isdir_and_append_filename(char *src, char **dst);
|
||||
|
||||
/**
|
||||
* copyfile - Copy a file to another.
|
||||
* @src: Source file.
|
||||
* @dst: Target file.
|
||||
* @len: Number of bytes to copy, zero (0) for entire file.
|
||||
*
|
||||
* This is a C implementation of the command line cp(1) utility. It is one
|
||||
* of the classic missing links in the UNIX C library. This version is from
|
||||
* the finit project, http://helllabs.org/finit/, which is a reimplementation
|
||||
* of fastinit for the Asus EeePC.
|
||||
*
|
||||
* Only some small changes to save stack space have been introduced.
|
||||
*
|
||||
* Returns:
|
||||
* The number of bytes copied, zero may be error (check errno!), but it
|
||||
* may also indicate that @src was empty. If @src is a directory errno
|
||||
* will be set to %EISDIR since copyfile() is not recursive.
|
||||
*/
|
||||
ssize_t copyfile(char *src, char *dst, int len)
|
||||
{
|
||||
char *buffer;
|
||||
int s, d, n, isdir = 0, saved_errno = 0;
|
||||
ssize_t size = 0;
|
||||
|
||||
errno = 0; /* Reset before leaving this function. */
|
||||
// fprintf (stderr, "%s() src: %s, dst: %s, len: %d\n", __func__, src, dst, len);
|
||||
|
||||
buffer = malloc(BUFSIZ);
|
||||
if (!buffer)
|
||||
return 0;
|
||||
|
||||
if (fisdir(src)) {
|
||||
errno = EISDIR; /* Error: source is a directory */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check if target is a directory, then append the src base filename. */
|
||||
isdir = __isdir_and_append_filename(src, &dst);
|
||||
|
||||
/* Len == 0 means copy entire file */
|
||||
if (len == 0) {
|
||||
/* XXX: This feels a bit unsafe, but 2047 MiB may be sufficient for us.
|
||||
* --Jocke 2008-08-11 */
|
||||
len = INT_MAX;
|
||||
}
|
||||
|
||||
if ((s = open(src, O_RDONLY)) >= 0) {
|
||||
if ((d =
|
||||
open(dst, O_WRONLY | O_CREAT | O_TRUNC,
|
||||
fmode(src))) >= 0) {
|
||||
do {
|
||||
int clen = len > BUFSIZ ? BUFSIZ : len;
|
||||
|
||||
if ((n = read(s, buffer, clen)) > 0) {
|
||||
size += write(d, buffer, n);
|
||||
}
|
||||
len -= clen;
|
||||
}
|
||||
while (len > 0 && n == BUFSIZ);
|
||||
|
||||
close(d);
|
||||
} else {
|
||||
saved_errno = errno;
|
||||
}
|
||||
|
||||
close(s);
|
||||
} else {
|
||||
saved_errno = errno;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
if (isdir)
|
||||
free(dst);
|
||||
errno = saved_errno;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* movefile - Move a file to another location
|
||||
* @src: Source file.
|
||||
* @dst: Target file, or location.
|
||||
*
|
||||
* This is a C implementation of the command line mv(1) utility.
|
||||
* Usually the rename() API is sufficient, but not when moving across
|
||||
* file system boundaries.
|
||||
*
|
||||
* The @src argument must include the full path to the source file,
|
||||
* whereas the @dst argument may only be a directory, in which case the
|
||||
* same file name from @src is used.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with errno set.
|
||||
*/
|
||||
int movefile(char *src, char *dst)
|
||||
{
|
||||
int isdir, result = 0;
|
||||
|
||||
/* Check if target is a directory, then append the src base filename. */
|
||||
isdir = __isdir_and_append_filename(src, &dst);
|
||||
|
||||
if (rename(src, dst)) {
|
||||
if (errno == EXDEV) {
|
||||
errno = 0;
|
||||
copyfile(src, dst, 0);
|
||||
if (errno)
|
||||
result = 1;
|
||||
else
|
||||
result = remove(src);
|
||||
} else {
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (isdir)
|
||||
free(dst);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* copy_filep - Copy between FILE *fp.
|
||||
* @src: Source FILE.
|
||||
* @dst: Destination FILE.
|
||||
*
|
||||
* Function takes signals into account and will restart the syscalls as long
|
||||
* as error is %EINTR.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with errno set on error.
|
||||
*/
|
||||
int copy_filep(FILE *src, FILE *dst)
|
||||
{
|
||||
int result = 0;
|
||||
char *buf = (char *)malloc(BUFSIZ);
|
||||
|
||||
if (!src || !dst)
|
||||
return EINVAL;
|
||||
|
||||
if (!buf)
|
||||
return errno;
|
||||
|
||||
while (1) {
|
||||
if (!fgets(buf, BUFSIZ, src)) {
|
||||
if (errno == EINTR) {
|
||||
clearerr(src);
|
||||
continue;
|
||||
}
|
||||
if (feof(src))
|
||||
break;
|
||||
|
||||
result = errno;
|
||||
goto exit;
|
||||
}
|
||||
while (EOF == fputs(buf, dst)) {
|
||||
if (errno == EINTR) {
|
||||
clearerr(dst);
|
||||
continue;
|
||||
}
|
||||
|
||||
result = errno;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
free(buf);
|
||||
return errno = result;
|
||||
}
|
||||
|
||||
/* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */
|
||||
static int __isdir_and_append_filename(char *src, char **dst)
|
||||
{
|
||||
int isdir = 0;
|
||||
|
||||
if (fisdir(*dst)) {
|
||||
int slash = 0;
|
||||
char *tmp, *ptr = strrchr(src, '/');
|
||||
|
||||
if (!ptr)
|
||||
ptr = src;
|
||||
else
|
||||
ptr++;
|
||||
|
||||
// fprintf (stderr, "Yes %s is a dir --> add %s\n", *dst, ptr);
|
||||
tmp = malloc(strlen(*dst) + strlen(ptr) + 2);
|
||||
if (!tmp) {
|
||||
errno = EISDIR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
isdir = 1; /* Free dst before exit! */
|
||||
slash = fisslashdir(*dst);
|
||||
|
||||
sprintf(tmp, "%s%s%s", *dst, slash ? "" : "/", ptr);
|
||||
*dst = tmp;
|
||||
// fprintf (stderr, "NEW DST:%s\n", *dst);
|
||||
}
|
||||
|
||||
return isdir;
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
#include <stdio.h>
|
||||
/* XXX: Make a real set of unit tests ... */
|
||||
#if 0
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 3) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s <src> <dst>\nCopies the <src> file to the <dst> file.\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Copying file %s to %s\n", argv[1], argv[2]);
|
||||
|
||||
return copyfile(argv[1], argv[2], 0);
|
||||
}
|
||||
#else
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
char *files[] = {
|
||||
"/etc/passwd", "/tmp/tok", "/tmp/tok",
|
||||
"/etc/passwd", "/tmp/", "/tmp/passwd",
|
||||
"/etc/passwd", "/tmp", "/tmp/passwd",
|
||||
NULL
|
||||
};
|
||||
FILE *src, *dst;
|
||||
|
||||
fprintf(stderr, "\n=>Start testing copy_filep()\n");
|
||||
while (files[i]) {
|
||||
fprintf(stderr, "copy_filep(%s, %s)\t", files[i],
|
||||
files[i + 1]);
|
||||
src = fopen(files[i], "r");
|
||||
dst = fopen(files[i + 1], "w");
|
||||
if (copy_filep(src, dst))
|
||||
perror("Failed");
|
||||
|
||||
if (src)
|
||||
fclose(src);
|
||||
if (dst)
|
||||
fclose(dst);
|
||||
|
||||
if (fexist(files[i + 2]))
|
||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
||||
|
||||
remove(files[i + 2]);
|
||||
i += 3;
|
||||
}
|
||||
|
||||
printf("\n\n=>Start testing copyfile()\n");
|
||||
i = 0;
|
||||
while (files[i]) {
|
||||
fprintf(stderr, "copyfile(%s, %s)\t", files[i], files[i + 1]);
|
||||
if (!copyfile(files[i], files[i + 1], 0))
|
||||
perror("Failed");
|
||||
|
||||
if (fexist(files[i + 2]))
|
||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
||||
|
||||
remove(files[i + 2]);
|
||||
i += 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif /* UNITTEST */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* compile-command: "gcc -g -I../include -o unittest -DUNITTEST copyfile.c && ./unittest"
|
||||
* version-control: t
|
||||
* kept-new-versions: 2
|
||||
* c-file-style: "ellemtel"
|
||||
* End:
|
||||
*/
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
/* Functions for operating on files in directories.
|
||||
*
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *matcher_type = NULL;
|
||||
static int (*matcher_filter) (const char *file) = NULL;
|
||||
static int matcher(const struct dirent *entry)
|
||||
{
|
||||
char *pos = strrchr(entry->d_name, '.');
|
||||
|
||||
if (matcher_filter && !matcher_filter(entry->d_name))
|
||||
/* User matcher overrides the rest. */
|
||||
return 0;
|
||||
|
||||
/* Skip current dir "." from list of files. */
|
||||
if ((1 == strlen(entry->d_name) && entry->d_name[0] == '.') ||
|
||||
(2 == strlen(entry->d_name) && !strcmp(entry->d_name, "..")))
|
||||
return 0;
|
||||
|
||||
/* filetype == "" */
|
||||
if (matcher_type[0] == 0)
|
||||
return 1;
|
||||
|
||||
/* Entry has no "." */
|
||||
if (!pos)
|
||||
return 0;
|
||||
|
||||
return !strcmp(pos, matcher_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* dir - List all files of a certain type in the given directory.
|
||||
* @dir: Base directory for dir operation.
|
||||
* @type: File type suffix, e.g. ".cfg".
|
||||
* @filter: Optional file name filter.
|
||||
* @list: Pointer to an array of file names.
|
||||
* @strip: Flag, if set dir() strips the file type.
|
||||
*
|
||||
* This function returns a @list of files, matching the @type suffix,
|
||||
* in the given directory @dir.
|
||||
*
|
||||
* The @list argument is a pointer to where to store the dynamically
|
||||
* allocated list of file names. This list should be free'd by first
|
||||
* calling free() on each file name and then on the list itself.
|
||||
*
|
||||
* If @filter is not %NULL it will be called for each file found. If
|
||||
* @filter returns non-zero the @file argument will be included in the
|
||||
* resulting @list. If @filter returns zero for given @file it will
|
||||
* be discarded.
|
||||
*
|
||||
* If the @strip flag is set the resulting @list of files has their
|
||||
* file type stripped, including the dot. So a match "config0.cfg"
|
||||
* would be returned as "config0".
|
||||
*
|
||||
* Returns:
|
||||
* Number of files in @list, zero if no matching files of @type.
|
||||
*/
|
||||
int dir(const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip)
|
||||
{
|
||||
int i, n, num = 0;
|
||||
char **files;
|
||||
struct dirent **namelist;
|
||||
|
||||
assert(list);
|
||||
|
||||
if (!dir)
|
||||
/* Assuming current directory */
|
||||
dir = ".";
|
||||
if (!type)
|
||||
/* Assuming all files. */
|
||||
type = "";
|
||||
|
||||
matcher_type = type;
|
||||
matcher_filter = filter;
|
||||
n = scandir(dir, &namelist, matcher, alphasort);
|
||||
if (n < 0) {
|
||||
perror("scandir");
|
||||
} else {
|
||||
files = (char **)malloc(n * sizeof(char *));
|
||||
for (i = 0; i < n; i++) {
|
||||
if (files) {
|
||||
char *name = namelist[i]->d_name;
|
||||
char *type = strrchr(name, '.');
|
||||
|
||||
if (type && strip)
|
||||
*type = 0;
|
||||
|
||||
files[i] = strdup(name);
|
||||
num++;
|
||||
}
|
||||
free(namelist[i]);
|
||||
}
|
||||
free(namelist);
|
||||
}
|
||||
|
||||
if (num)
|
||||
*list = files;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "otn/c.h"
|
||||
|
||||
#define DIR_TYPE_IMAGE ".img"
|
||||
#define DIR_TYPE_SYSLOG ""
|
||||
#define DIR_TYPE_CONFIG ".cfg"
|
||||
#define STARTUP_CONFIG "startup-config.cfg"
|
||||
|
||||
int simulate_files(int creat)
|
||||
{
|
||||
int i;
|
||||
char *files[] =
|
||||
{ "config0.cfg", "config1.cfg", "config2.cfg", "config3.cfg",
|
||||
"rr109.img", "rx100.img", "rx107.img", "rm957.img", "messages"
|
||||
};
|
||||
|
||||
for (i = 0; i < ARRAY_ELEMENTS(files); i++) {
|
||||
if (creat)
|
||||
touch(files[i]);
|
||||
else
|
||||
remove(files[i]);
|
||||
}
|
||||
|
||||
if (creat)
|
||||
symlink("config2.cfg", STARTUP_CONFIG);
|
||||
else
|
||||
remove(STARTUP_CONFIG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cfg_dir_filter(const char *file)
|
||||
{
|
||||
/* Skip the STARTUP_CONFIG file, it is a symbolic link to the
|
||||
* current startup configuration. */
|
||||
return ! !strcmp(file, STARTUP_CONFIG);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, num;
|
||||
char *type = DIR_TYPE_CONFIG;
|
||||
char **files;
|
||||
int once = 1;
|
||||
|
||||
int is_startup_config(const char *entry) {
|
||||
static char file[80];
|
||||
|
||||
if (once) {
|
||||
int len = readlink(STARTUP_CONFIG, file, sizeof(file));
|
||||
|
||||
if (len == -1)
|
||||
return 0;
|
||||
|
||||
file[len] = 0;
|
||||
once = 0; /* Only once per call to dir() */
|
||||
}
|
||||
//printf ("Comparing link %s with entry %s\n", file, entry);
|
||||
return !strcmp(file, entry);
|
||||
}
|
||||
|
||||
simulate_files(1);
|
||||
|
||||
if (argc >= 2) {
|
||||
if (!strcasecmp("CONFIG", argv[1])) {
|
||||
type = DIR_TYPE_CONFIG;
|
||||
system("ls -l *" DIR_TYPE_CONFIG);
|
||||
}
|
||||
if (!strcasecmp("IMAGE", argv[1])) {
|
||||
type = DIR_TYPE_IMAGE;
|
||||
system("ls -l *" DIR_TYPE_IMAGE);
|
||||
}
|
||||
if (!strcasecmp("SYSLOG", argv[1])) {
|
||||
type = DIR_TYPE_SYSLOG;
|
||||
system("ls -l *");
|
||||
}
|
||||
}
|
||||
|
||||
num = dir(NULL, type, cfg_dir_filter, &files, 0);
|
||||
if (num) {
|
||||
for (i = 0; i < num; i++) {
|
||||
printf("%s", files[i]);
|
||||
if (is_startup_config(files[i]))
|
||||
printf(" --> startup-config");
|
||||
printf("\n");
|
||||
|
||||
free(files[i]);
|
||||
}
|
||||
free(files);
|
||||
}
|
||||
|
||||
simulate_files(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* UNITTEST */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* compile-command: "gcc -g -DUNITTEST -o unittest dir.c"
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -0,0 +1,53 @@
|
||||
/* Check if file exists
|
||||
*
|
||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* fexist - Check if a file exists in the file system.
|
||||
* @file: File to look for, with full path.
|
||||
*
|
||||
* Returns:
|
||||
* %TRUE(1) if the file exists, otherwise %FALSE(0).
|
||||
*/
|
||||
int fexist(char *file)
|
||||
{
|
||||
if (!file) {
|
||||
errno = EINVAL;
|
||||
return 0; /* Doesn't exist ... */
|
||||
}
|
||||
|
||||
if (-1 == access(file, F_OK))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -0,0 +1,51 @@
|
||||
/* Check if directory exists
|
||||
*
|
||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* fexist - Check if a file exists and is a directory.
|
||||
* @file: File to look for, with full path.
|
||||
*
|
||||
* Returns:
|
||||
* %TRUE(1) if the file exists and is a directory, otherwise %FALSE(0).
|
||||
*/
|
||||
int fisdir(char *file)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
if (!stat(file, &sb) && S_ISDIR(sb.st_mode))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -0,0 +1,57 @@
|
||||
/* File mode bit operations
|
||||
*
|
||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* fmode - Return a file's mode bits.
|
||||
* @file: File to look for, with full path.
|
||||
*
|
||||
* Returns:
|
||||
* The mode bits or zero when @file does not exist and errno set to %ENOENT.
|
||||
*/
|
||||
mode_t fmode(char *file)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
if (!file) {
|
||||
errno = EINVAL;
|
||||
return 0; /* Doesn't exist ... */
|
||||
}
|
||||
|
||||
if (!stat(file, &sb))
|
||||
return sb.st_mode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -0,0 +1,61 @@
|
||||
/* Collection of utility funcs and C library extensions for finit & its plugins
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* 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_LITE_H_
|
||||
#define FINIT_LITE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int fexist (char *file);
|
||||
int fisdir (char *file);
|
||||
mode_t fmode (char *file);
|
||||
|
||||
ssize_t copyfile (char *src, char *dst, int len);
|
||||
int movefile (char *src, char *dst);
|
||||
int copy_filep (FILE *src, FILE *dst);
|
||||
|
||||
int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
|
||||
int rsync (char *src, char *dst, int delete, int (*filter) (const char *file));
|
||||
|
||||
static inline int fisslashdir(char *dir)
|
||||
{
|
||||
if (!dir) return 0;
|
||||
if (strlen (dir) > 0) return dir[strlen (dir) - 1] == '/';
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* FINIT_LITE_H_ */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
/* Micro "rsync" implementation.
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h> /* NULL, free() */
|
||||
#include <string.h> /* strlen() */
|
||||
#include <strings.h> /* rindex() */
|
||||
#include <stdio.h>
|
||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "lite.h"
|
||||
#include "../helpers.h"
|
||||
|
||||
static int makedir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode);
|
||||
static int prune(char *dst, char **new_files, int new_num);
|
||||
|
||||
|
||||
/**
|
||||
* rsync - Synchronize contents and optionally remove non-existing backups
|
||||
* @src: Source directory
|
||||
* @dst: Destination directory
|
||||
* @delete: Prune files from @dst that no longer exist in @src.
|
||||
* @filter: Optional filtering function for source directory.
|
||||
*
|
||||
* This is a miniature implementation of the famous rsync for local use only.
|
||||
* In fact, it is not even a true rsync since it copies all files from @src
|
||||
* to @dst. The @delete option is useful for creating backups, when set all
|
||||
* files removed from src since last backup are pruned from the destination
|
||||
* (backup) directory.
|
||||
*
|
||||
* The filter callback, @filter, if provided, is used to determine what files to
|
||||
* include from the source directory when backing up. If a file is to be skipped
|
||||
* the callback should simply return zero.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with @errno set on error.
|
||||
*/
|
||||
int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
||||
{
|
||||
char source[256];
|
||||
char dest[256];
|
||||
int i = 0, num = 0, result = 0;
|
||||
char **files; /* Array of file names. */
|
||||
|
||||
void copy(char *src, char *dst)
|
||||
{
|
||||
errno = 0;
|
||||
copyfile(src, dst, 0);
|
||||
if (errno) result++;
|
||||
}
|
||||
|
||||
if (!fisdir(dst))
|
||||
mkdir(dst, 0755);
|
||||
|
||||
if (!fisdir(src)) {
|
||||
if (!fexist(src))
|
||||
return 1;
|
||||
|
||||
copy(src, dst);
|
||||
return errno;
|
||||
}
|
||||
|
||||
/* Copy dir as well? */
|
||||
if (!fisslashdir(src)) {
|
||||
char *ptr = rindex(src, '/');
|
||||
|
||||
if (!ptr)
|
||||
ptr = src;
|
||||
else
|
||||
ptr++;
|
||||
|
||||
if (makedir(dest, sizeof(dest), dst, ptr, fmode(src)))
|
||||
return 1;
|
||||
dst = dest;
|
||||
}
|
||||
|
||||
num = dir(src, "", filter, &files, 0);
|
||||
for (i = 0; i < num; i++) {
|
||||
/* Recursively copy sub-directries */
|
||||
snprintf(source, sizeof(source), "%s%s%s", src, fisslashdir(src) ? "" : "/", files[i]);
|
||||
if (fisdir(source)) {
|
||||
char dst2[256];
|
||||
|
||||
strcat(source, "/");
|
||||
if (makedir (dst2, sizeof(dst2), dst, files[i], fmode(source))) {
|
||||
result++;
|
||||
continue;
|
||||
}
|
||||
|
||||
rsync(source, dst2, delete, filter);
|
||||
continue; /* Next file/dir in @src to copy... */
|
||||
}
|
||||
copy(source, dst);
|
||||
}
|
||||
|
||||
/* We ignore any errors from the pruning, that phase albeit useful is only
|
||||
* cosmetic. --Jocke 2011-03-24 */
|
||||
if (delete)
|
||||
prune(dst, files, num);
|
||||
|
||||
if (num) {
|
||||
for (i = 0; i < num; i++)
|
||||
free(files[i]);
|
||||
free(files);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Creates dir/name @mode ... skipping / if dir already ends so. */
|
||||
static int makedir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode)
|
||||
{
|
||||
snprintf(buf, buf_len, "%s%s%s/", dir, fisslashdir(dir) ? "" : "/", name);
|
||||
if (mkdir(buf, mode))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Prune old files, no longer existing on source, from destination directory. */
|
||||
static int prune(char *dst, char **new_files, int new_num)
|
||||
{
|
||||
int i, num, result = 0;
|
||||
char **files;
|
||||
|
||||
int find(char *file) {
|
||||
int n;
|
||||
|
||||
for (n = 0; n < new_num; n++)
|
||||
if (!strncmp (new_files[n], file, MAX(strlen(new_files[n]), strlen(file))))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
num = dir(dst, "", NULL, &files, 0);
|
||||
if (num) {
|
||||
for (i = 0; i < num; i++) {
|
||||
if (!find(files[i])) {
|
||||
char *name;
|
||||
size_t len = strlen(files[i]) + 2 + strlen(dst);
|
||||
|
||||
name = malloc(len);
|
||||
if (name) {
|
||||
snprintf(name, len, "%s%s%s", dst, fisslashdir(dst) ? "" : "/", files[i]);
|
||||
if (remove(name))
|
||||
result++;
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
free(files[i]);
|
||||
}
|
||||
free(files);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
#define BASE "/tmp/.unittest/"
|
||||
#define SRC BASE "src/"
|
||||
#define DST BASE "dst/"
|
||||
|
||||
char *files[] = {
|
||||
SRC "sub1/1.tst",
|
||||
SRC "sub1/2.tst",
|
||||
SRC "sub1/3.tst",
|
||||
SRC "sub2/4.tst",
|
||||
SRC "sub2/5.tst",
|
||||
SRC "sub2/6.tst",
|
||||
SRC "sub3/7.tst",
|
||||
SRC "sub3/8.tst",
|
||||
SRC "sub3/9.tst",
|
||||
NULL
|
||||
};
|
||||
|
||||
void cleanup_test(void)
|
||||
{
|
||||
system("rm -rf " BASE);
|
||||
}
|
||||
|
||||
void setup_test(void)
|
||||
{
|
||||
int i;
|
||||
char cmd[256];
|
||||
mode_t dir_modes[] = { 755, 700 };
|
||||
mode_t file_modes[] = { 644, 600 };
|
||||
|
||||
cleanup_test();
|
||||
|
||||
mkdir(BASE, 0755);
|
||||
mkdir(SRC, 0755);
|
||||
mkdir(DST, 0755);
|
||||
|
||||
for (i = 0; files[i]; i++) {
|
||||
snprintf(cmd, sizeof(cmd), "mkdir -m %d -p `dirname %s`",
|
||||
dir_modes[i % 2], files[i]);
|
||||
system(cmd);
|
||||
|
||||
snprintf(cmd, sizeof(cmd), "touch %s; chmod %d %s", files[i],
|
||||
file_modes[i % 2], files[i]);
|
||||
system(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
int run_test()
|
||||
{
|
||||
#if 0
|
||||
setup_test();
|
||||
puts("Before:");
|
||||
system("tree -np " BASE);
|
||||
|
||||
rsync(SRC, DST, 0, NULL);
|
||||
puts("After:");
|
||||
system("tree -np " BASE);
|
||||
cleanup_test();
|
||||
#endif
|
||||
|
||||
setup_test();
|
||||
rsync(BASE "src", DST, 0, NULL);
|
||||
puts("Only partial rsync of src <-- No slash!");
|
||||
system("tree -np " BASE);
|
||||
#if 0
|
||||
cleanup_test();
|
||||
setup_test();
|
||||
rsync(BASE "src/sub1", BASE "dst", 0, NULL);
|
||||
puts("Only partial rsync of src/sub1 <-- No slashes!!");
|
||||
system("tree -np " BASE);
|
||||
|
||||
cleanup_test();
|
||||
setup_test();
|
||||
rsync(BASE "src/sub1/", DST, 0, NULL);
|
||||
puts("Only partial rsync of src/sub1/");
|
||||
system("tree -np " BASE);
|
||||
|
||||
cleanup_test();
|
||||
setup_test();
|
||||
rsync(BASE "src/sub1", DST, 0, NULL);
|
||||
puts("Only partial rsync of src/sub1 <-- No slash!");
|
||||
system("tree -np " BASE);
|
||||
|
||||
rsync("/etc", "/var/tmp", 0, NULL);
|
||||
puts("Real life test:");
|
||||
system("tree -np /var/tmp");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
atexit(cleanup_test);
|
||||
|
||||
return run_test();
|
||||
}
|
||||
#endif /* UNITTEST */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* compile-command: "make -f rsync.mk"
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -0,0 +1,22 @@
|
||||
# Unittest makefile for rsync.c
|
||||
.PHONY: clean
|
||||
|
||||
SRC = rsync.c
|
||||
OBJS = dir.o copyfile.o fexist.o fisdir.o fmode.o
|
||||
UNIT = rsync.unittest
|
||||
#CPPFLAGS = -DDEBUG -I../../.. -I../../include
|
||||
|
||||
all: build run
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS) $(UNIT)
|
||||
|
||||
# This rule also forces a cleanup of built objects to not leave any object files
|
||||
# lingering in the tree to interfere with cross builds.
|
||||
build: clean $(OBJS)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $(UNIT) -DUNITTEST $(SRC) $(OBJS)
|
||||
@rm -f $(OBJS)
|
||||
|
||||
run:
|
||||
@./$(UNIT)
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include "private.h"
|
||||
#include "helpers.h"
|
||||
#include "plugin.h"
|
||||
#include "svc.h"
|
||||
|
||||
#define is_io_plugin(p) ((p)->io.cb && (p)->io.fd >= 0)
|
||||
|
||||
@@ -42,7 +41,7 @@ static LIST_HEAD(, plugin) plugins = LIST_HEAD_INITIALIZER();
|
||||
|
||||
int plugin_register(plugin_t *plugin)
|
||||
{
|
||||
int inuse = 0;
|
||||
int i, inuse = 0;
|
||||
|
||||
if (!plugin) {
|
||||
errno = EINVAL;
|
||||
@@ -69,17 +68,23 @@ int plugin_register(plugin_t *plugin)
|
||||
}
|
||||
|
||||
if (plugin->svc.cb) {
|
||||
svc_t *svc = svc_find_by_name(plugin->name);
|
||||
svc_t *svc = svc_find(plugin->name);
|
||||
|
||||
if (svc) {
|
||||
plugin->svc.id = svc_id(svc);
|
||||
svc->plugin = &plugin->svc;
|
||||
inuse++;
|
||||
svc->cb = plugin->svc.cb;
|
||||
svc->dynamic = plugin->svc.dynamic;
|
||||
svc->dynamic_stop = plugin->svc.dynamic_stop;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < HOOK_MAX_NUM; i++) {
|
||||
if (plugin->hook[i].cb)
|
||||
inuse++;
|
||||
}
|
||||
|
||||
if (!inuse) {
|
||||
_e("No service \"%s\" loaded, skipping plugin.", basename(plugin->name));
|
||||
_d("No service \"%s\" loaded, and no I/O or finit hooks, skipping plugin.", basename(plugin->name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -92,13 +97,22 @@ int plugin_unregister(plugin_t *plugin)
|
||||
{
|
||||
LIST_REMOVE(plugin, link);
|
||||
|
||||
if (plugin->svc.cb) {
|
||||
svc_t *svc = svc_find(plugin->name);
|
||||
|
||||
if (svc) {
|
||||
svc->cb = NULL;
|
||||
svc->dynamic = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* XXX: Unfinished, add cleanup code here! */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Private daemon API *******************************************************/
|
||||
void run_hooks(hook_point_t no)
|
||||
void plugin_run_hooks(hook_point_t no)
|
||||
{
|
||||
plugin_t *p;
|
||||
|
||||
@@ -110,18 +124,6 @@ void run_hooks(hook_point_t no)
|
||||
}
|
||||
}
|
||||
|
||||
void run_services(void)
|
||||
{
|
||||
plugin_t *p;
|
||||
|
||||
PLUGIN_ITERATOR(p) {
|
||||
if (p->svc.cb) {
|
||||
_d("Calling svc %s from runloop...", basename(p->name));
|
||||
p->svc.cb(p->svc.arg, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Generic libev I/O callback, looks up correct plugin and calls its callback */
|
||||
static void generic_io_cb(int fd, int events)
|
||||
{
|
||||
@@ -137,7 +139,7 @@ static void generic_io_cb(int fd, int events)
|
||||
}
|
||||
}
|
||||
|
||||
void io_monitor(void)
|
||||
void plugin_monitor(void)
|
||||
{
|
||||
int ret;
|
||||
size_t i;
|
||||
@@ -171,15 +173,16 @@ static void init_plugins(void)
|
||||
PLUGIN_ITERATOR(p) {
|
||||
if (is_io_plugin(p)) {
|
||||
_d("Initializing plugin %s for I/O", basename(p->name));
|
||||
fds[i].revents = 0;
|
||||
fds[i].events = p->io.flags;
|
||||
fds[num_fds++].fd = p->io.fd;
|
||||
fds[i].revents = 0;
|
||||
fds[i].events = p->io.flags;
|
||||
fds[i++].fd = p->io.fd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int load_plugins(char *path)
|
||||
int plugin_load_all(char *path)
|
||||
{
|
||||
int fail = 0;
|
||||
DIR *dp = opendir(path);
|
||||
struct dirent *entry;
|
||||
|
||||
@@ -192,25 +195,28 @@ int load_plugins(char *path)
|
||||
char *ext = entry->d_name + strlen(entry->d_name) - 3;
|
||||
|
||||
if (!strcmp(ext, ".so")) {
|
||||
int result = 0;
|
||||
void *handle;
|
||||
char filename[1024];
|
||||
char plugin[1024];
|
||||
|
||||
snprintf(filename, sizeof(filename), "%s/%s", path, entry->d_name);
|
||||
print_descr(" Loading plugin ", basename(filename));
|
||||
handle = dlopen(filename, RTLD_LAZY);
|
||||
snprintf(plugin, sizeof(plugin), "%s/%s", path, entry->d_name);
|
||||
// print_desc(" Loading plugin ", basename(plugin));
|
||||
handle = dlopen(plugin, RTLD_LAZY | RTLD_GLOBAL);
|
||||
if (!handle) {
|
||||
_d("Failed loading plugin %s: %s", filename, dlerror());
|
||||
print_result(1);
|
||||
return 1;
|
||||
_d("Failed loading plugin %s: %s", plugin, dlerror());
|
||||
result = 1;
|
||||
}
|
||||
print_result(0);
|
||||
if (result)
|
||||
fail++;
|
||||
|
||||
// print_result(result);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dp);
|
||||
init_plugins();
|
||||
|
||||
return 0;
|
||||
return fail;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <poll.h>
|
||||
#include <sys/queue.h> /* BSD sys/queue.h API */
|
||||
#include "svc.h"
|
||||
|
||||
#define PLUGIN_IO_READ POLLIN
|
||||
#define PLUGIN_IO_WRITE POLLOUT
|
||||
@@ -39,9 +40,11 @@
|
||||
* Predefined hook points for easier plugin debugging
|
||||
*/
|
||||
typedef enum {
|
||||
HOOK_BASEFS_UP = 0,
|
||||
HOOK_ROOTFS_UP = 0,
|
||||
HOOK_BASEFS_UP,
|
||||
HOOK_NETWORK_UP,
|
||||
HOOK_SYSTEM_UP,
|
||||
HOOK_SVC_UP,
|
||||
HOOK_SHUTDOWN,
|
||||
HOOK_MAX_NUM
|
||||
} hook_point_t;
|
||||
@@ -64,7 +67,8 @@ typedef enum {
|
||||
* The "dynamic events" discussed in the svc callback is for external
|
||||
* service plugins to implement. However, it can be anything that
|
||||
* can cause a service to need to SIGHUP at runtime. E.g., acquiring
|
||||
* a DHCP lease, or an interface going UP/DOWN.
|
||||
* a DHCP lease, or an interface going UP/DOWN. The event itself can
|
||||
* be passed as an integer @event and any optional argument @event_arg
|
||||
*
|
||||
* It is up to the external service plugin to track these events and
|
||||
* relay them to each @dynamic service plugins' callback. I.e., to
|
||||
@@ -79,14 +83,10 @@ typedef struct plugin {
|
||||
char *name;
|
||||
|
||||
/* Service callback to be called once per lap of runloop. */
|
||||
struct plugin_svc {
|
||||
/* Private */
|
||||
int id; /* Service ID# to match this service plugin against, set on installation. */
|
||||
/* Public */
|
||||
void *arg; /* Optional argument to callback func, set by plugin. */
|
||||
int dynamic; /* Reload (SIGHUP) on dynamic event? Set by plugin. */
|
||||
int private; /* For callbacks to use freely, possibly to store "states", set by plugin. */
|
||||
int (*cb)(void *arg, int event);
|
||||
struct {
|
||||
int dynamic; /* Callback wants external events as well. */
|
||||
int dynamic_stop; /* Callback allows dynamic events to stop. */
|
||||
svc_cmd_t (*cb)(svc_t *svc, int event, void *event_arg);
|
||||
} svc;
|
||||
|
||||
/* List of hook callbacks. */
|
||||
@@ -103,9 +103,6 @@ typedef struct plugin {
|
||||
} io;
|
||||
} plugin_t;
|
||||
|
||||
/* Used by svc.h */
|
||||
typedef struct plugin_svc plugin_svc_t;
|
||||
|
||||
/* Public plugin API */
|
||||
int plugin_register (plugin_t *plugin);
|
||||
int plugin_unregister (plugin_t *plugin);
|
||||
|
||||
+9
-8
@@ -1,7 +1,8 @@
|
||||
# Plugin based services architecture for finit -*-Makefile-*-
|
||||
CFLAGS = -fPIC -g -Os
|
||||
CFLAGS += -fPIC -Os
|
||||
CPPFLAGS += -I..
|
||||
LDFLAGS = -shared
|
||||
LDFLAGS := $(filter-out -rdynamic, $(LDFLAGS))
|
||||
LDFLAGS += -shared
|
||||
PLUGINS ?= initctl.so alsa-utils.so bootmisc.so dbus.so hwclock.so \
|
||||
resolvconf.so urandom.so x11-common.so
|
||||
DEPS = $(addprefix .,$(PLUGINS:.so=.d))
|
||||
@@ -17,18 +18,18 @@ distclean: clean
|
||||
-@$(RM) $(JUNK) $(DEPS)
|
||||
|
||||
install: all
|
||||
@install -d $(DESTDIR)$(prefix)$(PLUGIN_DIR)
|
||||
@install -d $(DESTDIR)$(plugindir)
|
||||
@for plugin in $(PLUGINS); do \
|
||||
echo " INSTALL $(DESTDIR)$(prefix)$(PLUGIN_DIR)/$$plugin"; \
|
||||
cp $$plugin $(DESTDIR)$(prefix)$(PLUGIN_DIR); \
|
||||
echo " INSTALL $(DESTDIR)$(plugindir)/$$plugin"; \
|
||||
$(STRIPINST) $$plugin $(DESTDIR)$(plugindir)/$$plugin; \
|
||||
done
|
||||
|
||||
uninstall:
|
||||
-@for plugin in $(PLUGINS); do \
|
||||
echo " RM $(PLUGIN_DIR)/$$plugin"; \
|
||||
rm $(DESTDIR)$(prefix)$(PLUGIN_DIR)/$$plugin 2>/dev/null; \
|
||||
echo " RM $(DESTDIR)$(plugindir)/$$plugin"; \
|
||||
rm $(DESTDIR)$(plugindir)/$$plugin 2>/dev/null; \
|
||||
done
|
||||
-@rmdir $(DESTDIR)$(prefix)$(PLUGIN_DIR) 2>/dev/null
|
||||
-@rmdir $(DESTDIR)$(plugindir) 2>/dev/null
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(DEPS)
|
||||
|
||||
+4
-2
@@ -28,13 +28,15 @@
|
||||
static void save(void *UNUSED(arg))
|
||||
{
|
||||
_d("Saving system clock to RTC ...");
|
||||
run_interactive("/sbin/hwclock --systohc --utc", "Saving system time (UTC) to RTC");
|
||||
/* -w,--systohc, -u,--utc */
|
||||
run_interactive("/sbin/hwclock -w -u", "Saving system time (UTC) to RTC");
|
||||
}
|
||||
|
||||
static void restore(void *UNUSED(arg))
|
||||
{
|
||||
_d("Restoring system clock from RTC ...");
|
||||
run_interactive("/sbin/hwclock --hctosys --utc", "Restoring system clock (UTC) from RTC");
|
||||
/* -s,--hctosys, -u,--utc */
|
||||
run_interactive("/sbin/hwclock -s -u", "Restoring system clock (UTC) from RTC");
|
||||
}
|
||||
|
||||
static plugin_t plugin = {
|
||||
|
||||
+4
-4
@@ -23,11 +23,14 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h> /* O_RDONLY et al */
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h> /* read() */
|
||||
|
||||
#include "finit.h"
|
||||
#include "helpers.h"
|
||||
#include "plugin.h"
|
||||
#include "sig.h"
|
||||
|
||||
#define INIT_MAGIC 0x03091969
|
||||
#define INIT_CMD_RUNLVL 1
|
||||
@@ -40,7 +43,6 @@ struct init_request {
|
||||
char data[368];
|
||||
};
|
||||
|
||||
#ifdef LISTEN_INITCTL
|
||||
/* Standard reboot/shutdown utilities talk to init using /dev/initctl.
|
||||
* We should check if the fifo was recreated and reopen it.
|
||||
*/
|
||||
@@ -109,8 +111,6 @@ PLUGIN_EXIT(plugin_exit)
|
||||
plugin_unregister(&plugin);
|
||||
}
|
||||
|
||||
#endif /* LISTEN_INITCTL */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
|
||||
@@ -25,13 +25,15 @@
|
||||
#ifndef FINIT_PRIVATE_H_
|
||||
#define FINIT_PRIVATE_H_
|
||||
|
||||
#include "svc.h"
|
||||
#include "plugin.h"
|
||||
|
||||
/* plugin.c */
|
||||
void run_hooks (hook_point_t no);
|
||||
void run_services (void);
|
||||
int load_plugins (char *path);
|
||||
void io_monitor (void);
|
||||
void svc_start_all (void);
|
||||
void svc_monitor (void);
|
||||
|
||||
void plugin_run_hooks (hook_point_t no);
|
||||
int plugin_load_all (char *path);
|
||||
void plugin_monitor (void);
|
||||
|
||||
#endif /* FINIT_PRIVATE_H_ */
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/* reboot for finit
|
||||
*
|
||||
* Copyright (c) 2009-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
return !!kill(1, SIGINT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -62,7 +62,7 @@ void do_shutdown (int sig)
|
||||
|
||||
sleep(1);
|
||||
/* Call all shutdown hooks, this is the last call before rebooting... */
|
||||
run_hooks(HOOK_SHUTDOWN);
|
||||
plugin_run_hooks(HOOK_SHUTDOWN);
|
||||
|
||||
_d("Sending SIGKILL to remaining processes.");
|
||||
kill(-1, SIGKILL);
|
||||
@@ -87,7 +87,7 @@ void do_shutdown (int sig)
|
||||
static void shutdown_handler(int sig, siginfo_t *info, void *UNUSED(ctx))
|
||||
{
|
||||
_d("Rebooting on signal %d from %s (PID %d)",
|
||||
sig, get_pidname(info->si_pid, NULL, 0), info->si_pid);
|
||||
sig, pid_get_name(info->si_pid, NULL, 0), info->si_pid);
|
||||
|
||||
do_shutdown(sig);
|
||||
}
|
||||
@@ -106,14 +106,14 @@ static void chld_handler(int UNUSED(sig), siginfo_t *UNUSED(info), void *UNUSED(
|
||||
static void sigstop_handler(int sig, siginfo_t *info, void *UNUSED(ctx))
|
||||
{
|
||||
_d("Received SIGSTOP(%d) from %s (PID %d)",
|
||||
sig, get_pidname(info->si_pid, NULL, 0), info->si_pid);
|
||||
sig, pid_get_name(info->si_pid, NULL, 0), info->si_pid);
|
||||
touch(SYNC_STOPPED);
|
||||
stopped ++;
|
||||
}
|
||||
static void sigcont_handler(int sig, siginfo_t *info, void *UNUSED(ctx))
|
||||
{
|
||||
_d("Received SIGCONT(%d) from %s (PID %d)",
|
||||
sig, get_pidname(info->si_pid, NULL, 0), info->si_pid);
|
||||
sig, pid_get_name(info->si_pid, NULL, 0), info->si_pid);
|
||||
stopped = 0;
|
||||
remove(SYNC_STOPPED);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Generic API for managing svc_t structures
|
||||
/* Finit service monitor and heneric API for managing svc_t structures
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "helpers.h"
|
||||
#include "private.h"
|
||||
#include "sig.h"
|
||||
#include "lite.h"
|
||||
#include "svc.h"
|
||||
|
||||
/* The registered services are kept in shared memory for easy read-only access
|
||||
@@ -46,7 +47,7 @@ static void __connect_shm(void)
|
||||
if (!services) {
|
||||
/* This should never happen, but if it does we're probably
|
||||
* knee-deep in more serious problems already... */
|
||||
_e("Failed setting up shared memory for service monitor.\n");
|
||||
_e("Failed allocating shared memory, error %d: %s", errno, strerror (errno));
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@@ -64,9 +65,8 @@ svc_t *svc_new(void)
|
||||
|
||||
__connect_shm();
|
||||
if (svc_counter < MAX_NUM_SVC) {
|
||||
svc = &services[svc_counter];
|
||||
svc = &services[svc_counter++];
|
||||
memset(svc, 0, sizeof(*svc));
|
||||
svc->id = svc_counter++;
|
||||
} else {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
@@ -76,29 +76,13 @@ svc_t *svc_new(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* svc_find_by_id - Find a service object by its logical ID#
|
||||
* @id: Logical ID#
|
||||
*
|
||||
* Returns:
|
||||
* A pointer to an &svc_t object, or %NULL if not found.
|
||||
*/
|
||||
svc_t *svc_find_by_id(int id)
|
||||
{
|
||||
__connect_shm();
|
||||
if (id >= svc_counter)
|
||||
return NULL;
|
||||
|
||||
return &services[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* svc_find_by_name - Find a service object by its full path name
|
||||
* svc_find - Find a service object by its full path name
|
||||
* @name: Full path name, e.g., /sbin/syslogd
|
||||
*
|
||||
* Returns:
|
||||
* A pointer to an &svc_t object, or %NULL if not found.
|
||||
*/
|
||||
svc_t *svc_find_by_name(char *name)
|
||||
svc_t *svc_find(char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -125,16 +109,16 @@ svc_t *svc_find_by_name(char *name)
|
||||
*/
|
||||
svc_t *svc_iterator(int restart)
|
||||
{
|
||||
static int id = 0;
|
||||
static int i = 0;
|
||||
|
||||
__connect_shm();
|
||||
if (restart)
|
||||
id = 0;
|
||||
i = 0;
|
||||
|
||||
if (id >= svc_counter)
|
||||
if (i >= svc_counter)
|
||||
return NULL;
|
||||
|
||||
return &services[id++];
|
||||
return &services[i++];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +140,7 @@ svc_t *svc_iterator(int restart)
|
||||
int svc_register(char *line, char *username)
|
||||
{
|
||||
int i = 0;
|
||||
char *cmd, *descr;
|
||||
char *cmd, *desc;
|
||||
svc_t *svc;
|
||||
|
||||
if (!line) {
|
||||
@@ -164,9 +148,9 @@ int svc_register(char *line, char *username)
|
||||
return errno = EINVAL;
|
||||
}
|
||||
|
||||
descr = strstr(line, "-- ");
|
||||
if (descr)
|
||||
*descr = 0;
|
||||
desc = strstr(line, "-- ");
|
||||
if (desc)
|
||||
*desc = 0;
|
||||
|
||||
cmd = strtok(line, " ");
|
||||
if (!cmd) {
|
||||
@@ -190,8 +174,8 @@ int svc_register(char *line, char *username)
|
||||
return errno = ENOMEM;
|
||||
}
|
||||
|
||||
if (descr)
|
||||
strlcpy(svc->descr, descr + 3, sizeof(svc->descr));
|
||||
if (desc)
|
||||
strlcpy(svc->desc, desc + 3, sizeof(svc->desc));
|
||||
if (username)
|
||||
strlcpy(svc->username, username, sizeof(svc->username));
|
||||
strlcpy(svc->cmd, cmd, sizeof(svc->cmd));
|
||||
@@ -207,8 +191,9 @@ int svc_register(char *line, char *username)
|
||||
|
||||
/**
|
||||
* svc_enabled - Should the service run?
|
||||
* @svc: Pointer to &svc_t object
|
||||
* @dynamic: Dynamic event, opaque flag passed to callback
|
||||
* @svc: Pointer to &svc_t object
|
||||
* @event: Dynamic event, opaque flag passed to callback
|
||||
* @arg: Event argument, used only by external service plugins.
|
||||
*
|
||||
* This method calls an associated service callback, if registered by a
|
||||
* plugin, and returns the &svc_cmd_t status. If no plugin is registered
|
||||
@@ -218,19 +203,19 @@ int svc_register(char *line, char *username)
|
||||
* Returns:
|
||||
* Either one of %SVC_START, %SVC_STOP, %SVC_RELOAD.
|
||||
*/
|
||||
svc_cmd_t svc_enabled(svc_t *svc, int dynamic)
|
||||
svc_cmd_t svc_enabled(svc_t *svc, int event, void *arg)
|
||||
{
|
||||
if (!svc) {
|
||||
errno = EINVAL;
|
||||
return SVC_STOP;
|
||||
}
|
||||
|
||||
if (!svc->plugin) {
|
||||
/* Unknown service, default to enabled. */
|
||||
return SVC_START;
|
||||
}
|
||||
/* Is there a service plugin registered? */
|
||||
if (svc->cb)
|
||||
return svc->cb(svc, event, arg);
|
||||
|
||||
return svc->plugin->cb(svc, dynamic);
|
||||
/* No service plugin, default to start, since listed in finit.conf */
|
||||
return SVC_START;
|
||||
}
|
||||
|
||||
static int is_norespawn(void)
|
||||
@@ -270,20 +255,22 @@ void svc_monitor(void)
|
||||
|
||||
/* Cleanup any lingering or semi-restarting tasks before respawning */
|
||||
_d("Sending SIGTERM to service group %s", name);
|
||||
kill_procname(name, SIGTERM);
|
||||
procname_kill(name, SIGTERM);
|
||||
|
||||
/* Restarting lost service. */
|
||||
if (svc_enabled(svc, 0))
|
||||
if (svc_enabled(svc, 0, NULL))
|
||||
svc_start(svc);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Remember: svc_enabled() must be called before calling svc_start() */
|
||||
int svc_start(svc_t *svc)
|
||||
{
|
||||
int respawn = svc->pid != 0;
|
||||
pid_t pid;
|
||||
sigset_t nmask, omask;
|
||||
char *args[MAX_NUM_SVC_ARGS];
|
||||
|
||||
/* Ignore if finit is SIGSTOP'ed */
|
||||
@@ -291,14 +278,24 @@ int svc_start(svc_t *svc)
|
||||
return 0;
|
||||
|
||||
if (!respawn)
|
||||
print_descr("Starting ", svc->descr);
|
||||
print_desc("Starting ", svc->desc);
|
||||
|
||||
/* Block sigchild while forking. */
|
||||
sigemptyset(&nmask);
|
||||
sigaddset(&nmask, SIGCHLD);
|
||||
sigprocmask(SIG_BLOCK, &nmask, &omask);
|
||||
|
||||
pid = fork();
|
||||
sigprocmask(SIG_SETMASK, &omask, NULL);
|
||||
if (pid == 0) {
|
||||
int i = 0;
|
||||
int uid = getuser(svc->username);
|
||||
struct sigaction sa;
|
||||
|
||||
sigemptyset(&nmask);
|
||||
sigaddset(&nmask, SIGCHLD);
|
||||
sigprocmask(SIG_UNBLOCK, &nmask, NULL);
|
||||
|
||||
/* Reset signal handlers that were set by the parent process */
|
||||
for (i = 1; i < NSIG; i++)
|
||||
DFLSIG(sa, i, 0);
|
||||
@@ -313,16 +310,23 @@ int svc_start(svc_t *svc)
|
||||
args[i] = NULL;
|
||||
|
||||
if (debug) {
|
||||
int fd = open (CONSOLE, O_WRONLY | O_APPEND);
|
||||
int fd;
|
||||
char buf[256] = "";
|
||||
|
||||
fd = open (CONSOLE, O_WRONLY | O_APPEND);
|
||||
if (-1 != fd) {
|
||||
dup2(STDOUT_FILENO, fd);
|
||||
dup2(STDERR_FILENO, fd);
|
||||
}
|
||||
|
||||
_e("%starting %s ", respawn ? "Res" : "S", svc->cmd);
|
||||
for (i = 0; args[i] && i < MAX_NUM_SVC_ARGS; i++)
|
||||
_e("%s ", args[i]);
|
||||
_e("");
|
||||
for (i = 0; args[i] && i < MAX_NUM_SVC_ARGS; i++) {
|
||||
char arg[MAX_ARG_LEN];
|
||||
|
||||
snprintf(arg, sizeof(arg), "%s ", args[i]);
|
||||
if ((sizeof(buf) - strlen(buf)) < strlen(arg))
|
||||
strcat(buf, arg);
|
||||
}
|
||||
_e("%starting %s: %s", respawn ? "Res" : "S", svc->cmd, buf);
|
||||
}
|
||||
|
||||
execvp(svc->cmd, args);
|
||||
@@ -338,9 +342,9 @@ int svc_start(svc_t *svc)
|
||||
|
||||
int svc_start_by_name(char *name)
|
||||
{
|
||||
svc_t *svc = svc_find_by_name(name);
|
||||
svc_t *svc = svc_find(name);
|
||||
|
||||
if (svc && svc_enabled(svc, 0))
|
||||
if (svc && svc_enabled(svc, 0, NULL))
|
||||
return svc_start(svc);
|
||||
|
||||
return 1;
|
||||
@@ -356,13 +360,13 @@ int svc_stop(svc_t *svc)
|
||||
}
|
||||
|
||||
if (svc->pid <= 1) {
|
||||
_d("Bad PID %d for %s, SIGTERM", svc->pid, svc->descr);
|
||||
_d("Bad PID %d for %s, SIGTERM", svc->pid, svc->desc);
|
||||
svc->pid = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
print_descr("Stopping ", svc->descr);
|
||||
_d("Sending SIGTERM to pid:%d name:'%s'", svc->pid, get_pidname(svc->pid, NULL, 0));
|
||||
print_desc("Stopping ", svc->desc);
|
||||
_d("Sending SIGTERM to pid:%d name:'%s'", svc->pid, pid_get_name(svc->pid, NULL, 0));
|
||||
res = kill(svc->pid, SIGTERM);
|
||||
print_result(res);
|
||||
svc->pid = 0;
|
||||
@@ -372,6 +376,10 @@ int svc_stop(svc_t *svc)
|
||||
|
||||
int svc_reload(svc_t *svc)
|
||||
{
|
||||
/* Ignore if finit is SIGSTOP'ed */
|
||||
if (is_norespawn())
|
||||
return 0;
|
||||
|
||||
if (!svc) {
|
||||
_e("Failed, no svc pointer.");
|
||||
return 1;
|
||||
@@ -393,14 +401,15 @@ void svc_start_all(void)
|
||||
svc_cmd_t cmd;
|
||||
|
||||
for (svc = svc_iterator(1); svc; svc = svc_iterator(0)) {
|
||||
cmd = svc_enabled(svc, 0);
|
||||
if (cmd == SVC_START || (cmd == SVC_RELOAD && svc->pid == 0))
|
||||
cmd = svc_enabled(svc, 0, NULL);
|
||||
if (SVC_START == cmd || (SVC_RELOAD == cmd && svc->pid == 0))
|
||||
svc_start(svc);
|
||||
else if (cmd == SVC_RELOAD)
|
||||
else if (SVC_RELOAD == cmd)
|
||||
svc_reload(svc);
|
||||
}
|
||||
|
||||
/* XXX: Hook point for plugins. */
|
||||
_d("Running svc up hooks ...");
|
||||
plugin_run_hooks(HOOK_SVC_UP);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Generic API for managing svc_t structures
|
||||
/* Finit service monitor and heneric API for managing svc_t structures
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
||||
@@ -30,9 +30,6 @@
|
||||
#include <sys/shm.h> /* shmat() */
|
||||
#include <sys/types.h> /* pid_t */
|
||||
|
||||
#include "finit.h"
|
||||
#include "plugin.h"
|
||||
|
||||
typedef enum {
|
||||
SVC_STOP = 0, /* Disabled */
|
||||
SVC_START, /* Enabled */
|
||||
@@ -51,16 +48,22 @@ typedef enum {
|
||||
* of issuing an initctl call. E.g.
|
||||
* initctl <stop|start|restart> service */
|
||||
typedef struct svc {
|
||||
/* Private */
|
||||
int id; /* Service ID#, set by svc_new() */
|
||||
pid_t pid;
|
||||
int reload;
|
||||
char cmd[MAX_ARG_LEN];
|
||||
char args[MAX_NUM_SVC_ARGS][MAX_ARG_LEN];
|
||||
char descr[MAX_STR_LEN];
|
||||
char desc[MAX_STR_LEN];
|
||||
char username[MAX_USER_LEN];
|
||||
/* Public */
|
||||
int delayed_reload; /* For external service plugin. */
|
||||
plugin_svc_t *plugin;
|
||||
|
||||
/* For external plugins. If @cb is set a plugin is loaded.
|
||||
* @dynamic: Set by plugins that want dynamic events.
|
||||
* @dynamic_stop: Set by plugins that allow dyn. events to stop it as well.
|
||||
* @private: Can be used freely by plugin, e.g., to store "states".
|
||||
*/
|
||||
int dynamic;
|
||||
int dynamic_stop;
|
||||
int private;
|
||||
svc_cmd_t (*cb)(struct svc *svc, int event, void *event_arg);
|
||||
} svc_t;
|
||||
|
||||
typedef struct svc_map svc_map_t;
|
||||
@@ -73,39 +76,24 @@ static inline svc_t *finit_svc_connect(void)
|
||||
|
||||
if ((void *)-1 == ptr) {
|
||||
ptr = shmat (shmget (ID, sizeof(svc_t) * MAX_NUM_SVC, 0600 | IPC_CREAT), NULL, 0);
|
||||
if ((void *)-1 == ptr) {
|
||||
_d("Failed allocating shared memory, error %d: %s", errno, strerror (errno));
|
||||
if ((void *)-1 == ptr)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return (svc_t *)ptr;
|
||||
}
|
||||
|
||||
static inline int svc_id(svc_t *svc)
|
||||
{
|
||||
if (!svc) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return svc->id;
|
||||
}
|
||||
|
||||
svc_t *svc_new (void);
|
||||
svc_t *svc_find_by_id (int id);
|
||||
svc_t *svc_find_by_name (char *name);
|
||||
svc_t *svc_find (char *name);
|
||||
svc_t *svc_iterator (int restart);
|
||||
|
||||
int svc_register (char *line, char *username);
|
||||
int svc_id_by_name (char *name);
|
||||
svc_cmd_t svc_enabled (svc_t *svc, int dynamic);
|
||||
svc_cmd_t svc_enabled (svc_t *svc, int event, void *arg);
|
||||
int svc_start (svc_t *svc);
|
||||
int svc_start_by_name (char *name);
|
||||
int svc_stop (svc_t *svc);
|
||||
int svc_reload (svc_t *svc);
|
||||
void svc_start_all (void);
|
||||
void svc_monitor (void);
|
||||
|
||||
#endif /* FINIT_SVC_H_ */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user