########################################################################
# Description : Gnu Makefile to control the services in the specified
# runlevel. It will run the required services, and log
# the output of the services to the file
# /var/log/initd.start (for service startup) and
# /var/log/initd.stop (for service shutdown).
#
# This controlling program is designed to be invoked by
# the "/etc/rc.d/rc" script.
#
# Author : jameshunt@uk.ibm.com
# Modified by : Mark Tiefenbruck
#
# Notes :
#
# - Run as,
#
# make [-n] -j -k -f runlevel.mk \
# RUNLEVEL={0|1|2|3|4|5|6} \
# JOB={start|stop|restart|status}
#
# - $(JOB) is not validated - that is left to the service program.
# - $(RUNLEVEL) is not validated - that is left to the calling program
# (usually /etc/rc.d/rc).
#
########################################################################
# passed as a parameter
RUNLEVEL =
# passed as a parameter (start, stop, status, etc)
JOB =
########################################################################
# START CONFIGURATION
# system commands used by this facility
CAT = /bin/cat
RM = /bin/rm
ECHO = /bin/echo
DATE = /bin/date
# Directory containing scripts/programs to run.
INITD_DIR = /etc/rc.d/rc$(RUNLEVEL).d
# Directory into which a lock file is created when a service starts.
# (Note that the lock file is created by the service).
SUBSYS_FILE_DIR := /var/lock/subsys
# Used to create temporary files, before collating them all into
# $(FINAL_OUTPUT_FILE).
TMP_DIR := /tmp
TMPFILE_PREFIX := .runlevel
TMP_FILE = $(TMP_DIR)/$(TMPFILE_PREFIX).$(JOB).$@
# File that contains all output of programs/scripts run.
FINAL_OUTPUT_FILE = /var/log/initd.$(JOB)
start = S
stop = K
ALL = $(patsubst $(INITD_DIR)/%,%,$(shell ls $(INITD_DIR)/$($(JOB))*))
# END CONFIGURATION
########################################################################
# Check command-line parameters
ifndef RUNLEVEL
$(error must specify RUNLEVEL, so I know what to run)
endif
ifndef JOB
$(error must specify JOB, so I know what to do)
endif
default : $(ALL) create_final_output_file
# Generic rule to control a service.
$(ALL) : $(SUBSYS_FILE_DIR)/$@
@$(ECHO) "Begin \"$(JOB) $@\" at `$(DATE)`" > $(TMP_FILE)
@$(INITD_DIR)/$@ $(JOB) 2>&1 >> $(TMP_FILE)
@$(ECHO) "End \"$(JOB) $@\" at `$(DATE)`" >> $(TMP_FILE)
# Include the relevant dependencies. Note that the dependencies for stopping
# services are usually opposite those for starting services.
#
# WARNING: including a file that does not exist will cause make to exit. This
# may cause your system to boot in an unfamiliar way.
include /etc/rc.d/$(JOB)$(RUNLEVEL).mk
# Lastly, merge all the service output files into a single file.
# Note that the order of the service output in the merged file is not
# chronological.
create_final_output_file : $(ALL)
$(CAT) $(TMP_DIR)/$(TMPFILE_PREFIX).$(JOB).* \
> $(FINAL_OUTPUT_FILE)
$(RM) -f $(TMP_DIR)/$(TMPFILE_PREFIX).$(JOB).*
# EOF