Michal Sekletár
Lukáš Nykrýn
<msekleta@redhat.com>
<lnykryn@redhat.com>
Praha 7.4.2017
process running as a pid 1
starts other processes during boot
parent of orphaned processes
needs to handle SIGCHLDS
default handler of Ctrl+Alt+Delete
SIGINT
can't be SIGKILLed
Linux kernel will panic if init dies
0 | Halt | shuts down the system |
1 | Single-user mode | mode for administrative tasks |
2 | Multi-user mode | no network + network services |
3 | Multi-user mode with network | starts the system normally |
4 | Not used/user-definable | |
5 | Start the system normally with appropriate display manager (with GUI) | same as runlevel 3 + display manager |
6 | Reboot | reboots the system |
id:3:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
co:2345:respawn:/sbin/agetty ttyS0 115200 vt100-nav
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
typedef struct _child_ {
int flags; /* Status of this entry */
int exstat; /* Exit status of process */
int pid; /* Pid of this process */
time_t tm; /* When respawned last */
int count; /* Times respawned in the last 2 minutes */
char id[8]; /* Inittab id (must be unique) */
char rlevel[12]; /* run levels */
int action; /* what to do (see list below) */
char process[128]; /* The command line */
struct _child_ *new; /* New entry (after inittab re-read) */
struct _child_ *next; /* For the linked list */
} CHILD;
handling of service directly with sysvinit would not be practical
minimal set of options
can't manually start single service
...
standardized scripts wrapping deamon and service
described in LSB
but in reality they differs between distributions
written in shell
/lib/lsb/init-functions - "library" of shared functions
strictly synchronous execution
for i in /etc/rcX.d/K* ; do $i stop ; done
for i in /etc/rcX.d/S* ; do $i start ; done
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: apache2
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop apache2 web server
### END INIT INFO
#
# apache2 This init.d script is used to start apache2.
# It basically just calls apache2ctl.
ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
#[ $(ls -1 /etc/apache2/sites-enabled/ | wc -l | sed -e 's/ *//;') -eq 0 ] && \
#echo "You haven't enabled any sites yet, so I'm not starting apache2." && \
#echo "To add and enable a host, use addhost and enhost." && exit 0
#edit /etc/default/apache2 to change this.
HTCACHECLEAN_RUN=auto
HTCACHECLEAN_MODE=daemon
HTCACHECLEAN_SIZE=300M
HTCACHECLEAN_DAEMON_INTERVAL=120
HTCACHECLEAN_PATH=/var/cache/apache2/mod_disk_cache
HTCACHECLEAN_OPTIONS=""
set -e
if [ -x /usr/sbin/apache2 ] ; then
HAVE_APACHE2=1
else
echo "No apache MPM package installed"
exit 0
fi
. /lib/lsb/init-functions
test -f /etc/default/rcS && . /etc/default/rcS
test -f /etc/default/apache2 && . /etc/default/apache2
APACHE2CTL="$ENV /usr/sbin/apache2ctl"
HTCACHECLEAN="$ENV /usr/sbin/htcacheclean"
PIDFILE=$(. /etc/apache2/envvars && echo $APACHE_PID_FILE)
if [ -z "$PIDFILE" ] ; then
echo ERROR: APACHE_PID_FILE needs to be defined in /etc/apache2/envvars >&2
exit 2
fi
check_htcacheclean() {
[ "$HTCACHECLEAN_MODE" = "daemon" ] || return 1
[ "$HTCACHECLEAN_RUN" = "yes" ] && return 0
[ "$HTCACHECLEAN_RUN" = "auto" \
-a -e /etc/apache2/mods-enabled/disk_cache.load ] && return 0
return 1
}
start_htcacheclean() {
$HTCACHECLEAN $HTCACHECLEAN_OPTIONS -d$HTCACHECLEAN_DAEMON_INTERVAL \
-i -p$HTCACHECLEAN_PATH -l$HTCACHECLEAN_SIZE
}
stop_htcacheclean() {
pkill htcacheclean 2> /dev/null || echo ...not running
}
pidof_apache() {
# if there is actually an apache2 process whose pid is in PIDFILE,
# print it and return 0.
if [ -e "$PIDFILE" ]; then
if pidof apache2 | tr ' ' '\n' | grep $(cat $PIDFILE); then
return 0
fi
fi
return 1
}
apache_stop() {
if $APACHE2CTL configtest > /dev/null 2>&1; then
# if the config is ok than we just stop normaly
$APACHE2CTL stop 2>&1 | grep -v 'not running' >&2 || true
else
# if we are here something is broken and we need to try
# to exit as nice and clean as possible
PID=$(pidof_apache) || true
if [ "${PID}" ]; then
# in this case it is everything nice and dandy
# and we kill apache2
log_warning_msg "We failed to correctly shutdown apache, so we're now killing all running apache processes. This is almost certainly suboptimal, so please make sure your system is working as you'd expect now!"
kill $PID
elif [ "$(pidof apache2)" ]; then
if [ "$VERBOSE" != no ]; then
echo " ... failed!"
echo "You may still have some apache2 processes running. There are"
echo "processes named 'apache2' which do not match your pid file,"
echo "and in the name of safety, we've left them alone. Please review"
echo "the situation by hand."
fi
return 1
fi
fi
}
apache_wait_stop() {
# running ?
PIDTMP=$(pidof_apache) || true
if kill -0 "${PIDTMP:-}" 2> /dev/null; then
PID=$PIDTMP
fi
apache_stop
# wait until really stopped
if [ -n "${PID:-}" ]; then
i=0
while kill -0 "${PID:-}" 2> /dev/null; do
if [ $i = '60' ]; then
break;
else
if [ $i = '0' ]; then
echo -n " ... waiting "
else
echo -n "."
fi
i=$(($i+1))
sleep 1
fi
done
fi
}
case $1 in
start)
log_daemon_msg "Starting web server" "apache2"
if $APACHE2CTL start; then
if check_htcacheclean ; then
log_progress_msg htcacheclean
start_htcacheclean || log_end_msg 1
fi
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
if check_htcacheclean ; then
log_daemon_msg "Stopping web server" "htcacheclean"
stop_htcacheclean
log_progress_msg "apache2"
else
log_daemon_msg "Stopping web server" "apache2"
fi
if apache_wait_stop; then
log_end_msg 0
else
log_end_msg 1
fi
;;
graceful | reload | force-reload)
if ! $APACHE2CTL configtest > /dev/null 2>&1; then
$APACHE2CTL configtest || true
log_end_msg 1
exit 1
fi
log_daemon_msg "Reloading web server config" "apache2"
if pidof_apache > /dev/null ; then
if $APACHE2CTL graceful $2 ; then
log_end_msg 0
else
log_end_msg 1
fi
fi
;;
restart)
if check_htcacheclean ; then
log_daemon_msg "Restarting web server" "htcacheclean"
stop_htcacheclean
log_progress_msg apache2
else
log_daemon_msg "Restarting web server" "apache2"
fi
PID=$(pidof_apache) || true
if ! apache_wait_stop; then
log_end_msg 1 || true
fi
if $APACHE2CTL start; then
if check_htcacheclean ; then
start_htcacheclean || log_end_msg 1
fi
log_end_msg 0
else
log_end_msg 1
fi
;;
start-htcacheclean)
log_daemon_msg "Starting htcacheclean"
start_htcacheclean || log_end_msg 1
log_end_msg 0
;;
stop-htcacheclean)
log_daemon_msg "Stopping htcacheclean"
stop_htcacheclean
log_end_msg 0
;;
status)
PID=$(pidof_apache) || true
if [ -n "$PID" ]; then
echo "Apache is running (pid $PID)."
exit 0
else
echo "Apache is NOT running."
exit 1
fi
;;
*)
log_success_msg "Usage: /etc/init.d/apache2 {start|stop|restart|reload|force-reload|start-htcacheclean|stop-htcacheclean|status}"
exit 1
;;
esac
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
extra_commands="configtest modules virtualhosts"
extra_started_commands="configdump fullstatus graceful gracefulstop reload"
description_configdump="Dumps the configuration of the runing apache server. Requires server-info to be enabled and www-client/lynx."
description_configtest="Run syntax tests for configuration files."
description_fullstatus="Gives the full status of the server. Requires lynx and server-status to be enabled."
description_graceful="A graceful restart advises the children to exit after the current request and reloads the configuration."
description_gracefulstop="A graceful stop advises the children to exit after the current request and stops the server."
description_modules="Dump a list of loaded Static and Shared Modules."
description_reload="Kills all children and reloads the configuration."
description_virtualhosts="Show the settings as parsed from the config file (currently only shows the virtualhost settings)."
description_stop="Kills all children and stops the server."
# Apply default values for some conf.d variables.
PIDFILE="${PIDFILE:-/var/run/apache2.pid}"
TIMEOUT=${TIMEOUT:-15}
SERVERROOT="${SERVERROOT:-/usr/lib64/apache2}"
CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}"
LYNX="${LYNX:-lynx -dump}"
STATUSURL="${STATUSURL:-http://localhost/server-status}"
RELOAD_TYPE="${RELOAD_TYPE:-graceful}"
# Append the server root and configuration file parameters to the
# user's APACHE2_OPTS.
APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}"
APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}"
# The path to the apache2 binary.
APACHE2="/usr/sbin/apache2"
depend() {
need net
use mysql dns logger netmount postgresql
after sshd
}
configtest() {
ebegin "Checking ${SVCNAME} configuration"
checkconfig
eend $?
}
checkconfd() {
if [ ! -d ${SERVERROOT} ]; then
eerror "SERVERROOT does not exist: ${SERVERROOT}"
return 1
fi
}
checkconfig() {
checkpath --directory /run/apache_ssl_mutex
checkconfd || return 1
OUTPUT=$( ${APACHE2} ${APACHE2_OPTS} -t 2>&1 )
ret=$?
if [ $ret -ne 0 ]; then
eerror "${SVCNAME} has detected an error in your setup:"
printf "%s\n" "${OUTPUT}"
fi
return $ret
}
start() {
checkconfig || return 1
if [ -n "${STARTUPERRORLOG}" ] ; then
# We must make sure that we only append to APACHE2_OPTS
# in start() and not in stop() or anywhere else that may
# be executed along with start(), see bug #566726.
APACHE2_OPTS="${APACHE2_OPTS} -E ${STARTUPERRORLOG}"
fi
ebegin "Starting ${SVCNAME}"
# Use start stop daemon to apply system limits #347301
start-stop-daemon --start -- ${APACHE2} ${APACHE2_OPTS} -k start
local i=0 retval=1
while [ $i -lt ${TIMEOUT} ] ; do
if [ -e "${PIDFILE}" ] ; then
retval=0
break
fi
sleep 1 && i=$(expr $i + 1)
done
eend ${retval}
}
stop() {
if [ "${RC_CMD}" = "restart" ]; then
checkconfig || return 1
fi
PID=$(cat "${PIDFILE}" 2>/dev/null)
if [ -z "${PID}" ]; then
einfo "${SVCNAME} not running (no pid file)"
return 0
fi
ebegin "Stopping ${SVCNAME}"
${APACHE2} ${APACHE2_OPTS} -k stop
local i=0 retval=0
while ( test -f "${PIDFILE}" || pgrep -P ${PID} apache2 >/dev/null ) \
&& [ $i -lt ${TIMEOUT} ]; do
sleep 1 && i=$(expr $i + 1)
done
[ -e "${PIDFILE}" ] && retval=1
eend ${retval}
}
reload() {
checkconfig || return 1
if [ "${RELOAD_TYPE}" = "restart" ]; then
ebegin "Restarting ${SVCNAME}"
${APACHE2} ${APACHE2_OPTS} -k restart
eend $?
elif [ "${RELOAD_TYPE}" = "graceful" ]; then
ebegin "Gracefully restarting ${SVCNAME}"
${APACHE2} ${APACHE2_OPTS} -k graceful
eend $?
else
eerror "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit /etc/conf.d/${SVCNAME}"
fi
}
graceful() {
checkconfig || return 1
ebegin "Gracefully restarting ${SVCNAME}"
${APACHE2} ${APACHE2_OPTS} -k graceful
eend $?
}
gracefulstop() {
checkconfig || return 1
ebegin "Gracefully stopping ${SVCNAME}"
${APACHE2} ${APACHE2_OPTS} -k graceful-stop
eend $?
}
modules() {
checkconfig || return 1
${APACHE2} ${APACHE2_OPTS} -M 2>&1
}
fullstatus() {
if ! type -p $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
eerror "lynx not found! you need to emerge www-client/lynx"
else
${LYNX} ${STATUSURL}
fi
}
virtualhosts() {
checkconfig || return 1
${APACHE2} ${APACHE2_OPTS} -S
}
configdump() {
INFOURL="${INFOURL:-http://localhost/server-info}"
checkconfd || return 1
if ! type -p $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
eerror "lynx not found! you need to emerge www-client/lynx"
else
echo "${APACHE2} started with '${APACHE2_OPTS}'"
for i in config server list; do
${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server Information/d;/^[[:space:]]\+[_]\+$/Q'
done
fi
}
# vim: ts=4 filetype=gentoo-init-d
description "apache2 http server"
start on runlevel [2345]
stop on runlevel [!2345]
pre-start script
mkdir -p /var/run/apache2 || true
install -d -o www-data /var/lock/apache2 || true
# ssl_scache shouldn't be here if we're just starting up.
# (this is bad if there are several apache2 instances running)
rm -f /var/run/apache2/*ssl_scache* || true
end script
limit cpu 300 300
env APACHE_RUN_USER=www-data
env APACHE_RUN_GROUP=www-data
env APACHE_PID_FILE=/var/run/apache2.pid
# Give up if restart occurs 10 times in 30 seconds.
respawn limit 10 30
exec /usr/sbin/apache2 -D NO_DETACH
respawn
systemctl
device manager
loginctl
systemd-inhibit
journalctl
tracks VMs and containers
work with images
machinectl
hostnamectl
locale settings
keymaps
localectl
timedatectl
downloads containers images
machinectl pull-*
creates volatiles files and directories
networkctl
provides network name resolution
DNSSEC
systemd-resolve
$ cat /usr/lib/systemd/system/cups.service
[Unit]
Description=CUPS Printing Service
[Service]
ExecStart=/usr/sbin/cupsd -f
PrivateTmp=true
[Install]
Also=cups.socket cups.path
Utility | Description |
---|---|
svcs | Report service status |
svcadm | Used for service management: e.g. starting, stopping and restoring services |
svccfg | Used to list properties of a service (can be used as interactive shell) |
svcprop | Used to list properties of a service |
inetadm | Used to manage inetd services |
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='export'>
<service name='system/sysding' type='service' version='0'>
<dependency name='fs' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/minimal:default'/>
</dependency>
<dependency name='identity' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/identity:node'/>
<service_fmri value='svc:/system/identity:domain'/>
</dependency>
<property_group name='startd' type='framework'>
<propval name='duration' type='astring' value='transient'/>
</property_group>
<instance name='system' enabled='true'>
<dependency name='single-user' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/milestone/single-user:default'/>
</dependency>
<dependency name='filesystem_local' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/local:default'/>
</dependency>
<dependency name='rpcbind' grouping='optional_all' restart_on='none' type='service'>
<service_fmri value='svc:/network/rpc/bind:default'/>
</dependency>
<dependent name='sysidtoolsystem_sysconfig' restart_on='none' grouping='optional_all'>
<service_fmri value='svc:/milestone/sysconfig'/>
</dependent>
<exec_method name='start' type='method' exec='/lib/svc/method/sysding' timeout_seconds='0'/>
<exec_method name='stop' type='method' exec=':true' timeout_seconds='0'/>
<property_group name='config' type='application'>
<propval name='finished' type='boolean' value='false'/>
</property_group>
</instance>
<stability value='Unstable'/>
<template>
<common_name>
<loctext xml:lang='C'>sysding</loctext>
</common_name>
<documentation>
<manpage title='sysding' section='1M' manpath='/usr/man'/>
</documentation>
</template>
</service>
</service_bundle>
SC_HANDLE WINAPI CreateService(
_In_ SC_HANDLE hSCManager,
_In_ LPCTSTR lpServiceName,
_In_opt_ LPCTSTR lpDisplayName,
_In_ DWORD dwDesiredAccess,
_In_ DWORD dwServiceType,
_In_ DWORD dwStartType,
_In_ DWORD dwErrorControl,
_In_opt_ LPCTSTR lpBinaryPathName,
_In_opt_ LPCTSTR lpLoadOrderGroup,
_Out_opt_ LPDWORD lpdwTagId,
_In_opt_ LPCTSTR lpDependencies,
_In_opt_ LPCTSTR lpServiceStartName,
_In_opt_ LPCTSTR lpPassword
);