Previous Page | Next Page

  1. Introduction
  2. Boot Process
  3. Init and System Initialization
  4. rc.sysinit script
  5. rc script
  6. functions script
  7. Services
  8. apm daemon
  9. network startup
  10. The network script file
  11. The network-functions file
  12. Portmap startup
  13. Random initialization
  14. Syslog initialization
  15. Gated
  16. Atd
  17. cron initialization
  18. pcmcia
  19. inetd daemon
  20. named daemon
  21. lpd daemon
  22. mars-nwe
  23. netfs startup
  24. dhcpd daemon
  25. autofs daemon
  26. keytable daemon
  27. sendmail daemon
  28. gpm daemon
  29. httpd daemon
  30. xfs startup
  31. smb startup
  32. innd startup
  33. linuxconf startup
  34. rc.local script
  35. Init, Getty, Login
  36. The shell
  37. Shutting down
  38. X
  39. Conclusions
  40. App A. rc.sysinit listing
  41. App B. functions listing
  42. App C. rc listing
  43. Credits

Appendix B

Linux /etc/rc.d/functions script program listing

1	#!/bin/sh
2	#
3	# functions	This file contains functions to be used by most or all
4	#		shell scripts in the /etc/init.d directory.
5	#
6	# Version:	@(#) /etc/init.d/functions 1.01 26-Oct-1993
7	#
8	# Author:	Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
9	# Hacked by:    Greg Galloway and Marc Ewing
10	#
11
12	# First set up a default search path.
13	export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
14
15	# Get a sane screen width
16	[ -z "$COLUMNS" ] && COLUMNS=80
17
18	# Read in our configuration
19	if [ -z "$BOOTUP" ]; then
20	  if [ -f /etc/sysconfig/init ]; then
21	      . /etc/sysconfig/init
22	  else
23	    # This all seem confusing? Look in /etc/sysconfig/init,
24	    # or in /usr/doc/initscripts-*/sysconfig.txt
25	    BOOTUP=color
26	    RES_COL=60
27	    MOVE_TO_COL="echo -en \\033[300C\\033[$[${COLUMNS}-${RES_COL}]D"
28	    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
29	    SETCOLOR_FAILURE="echo -en \\033[1;31m"
30	    SETCOLOR_WARNING="echo -en \\033[1;33m"
31	    SETCOLOR_NORMAL="echo -en \\033[0;39m"
32	    LOGLEVEL=1
33	  fi
34	fi
35	
36	if [ "$BOOTUP" != "verbose" ]; then
37	   INITLOG_ARGS="-q"
38	else
39	   INITLOG_ARGS=
40	fi
41	
42	# A function to start a program.
43	daemon() {
44		# Test syntax.
45		gotbase=
46		case $1 in
47		    '')    echo '$0: Usage: daemon [+/-nicelevel] {program}'
48		           return 1;;
49		    --check)
50		           shift
51			   base=$1
52			   gotbase="yes"
53			   shift
54			   nicelevel=0
55			   ;;
56		          
57		    -*|+*) nicelevel=$1
58		           shift;;
59		     *)    nicelevel=0;;
60		esac
61	
62	        # Save basename.
63	        [ -z $gotbase ] && base=`basename $1`
64	
65	        # See if it's already running.
66		pid=`pidofproc $base`
67		[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && return
68	
69		# make sure it doesn't core dump anywhere; while this could mask
70		# problems with the daemon, it also closes some security problems
71		ulimit -c 0
72		
73		# Echo daemon
74	        [ "$BOOTUP" = "verbose" ] && echo -n " $base"
75	
76		# And start it up.
77		nice -n $nicelevel initlog $INITLOG_ARGS -c "$*" && success "$base startup" || failure "$base 78	startup"
79	}
80	
81	# A function to stop a program.
82	killproc() {
83		# Test syntax.
84		if [ $# = 0 ]; then
85			echo "Usage: killproc {program} [signal]"
86			return 1
87		fi
88
89		notset=0
90		# check for second arg to be kill level
91		if [ "$2" != "" ] ; then
92			killlevel=$2
93		else
94			notset=1
95			killlevel="-9"
96		fi
97	
98	        # Save basename.
99	        base=`basename $1`
100	
101	        # Find pid.
102	        pid=`pidofproc $base`
103	
104	        # Kill it.
105	        if [ "$pid" != "" ] ; then
106	                [ $BOOTUP = "verbose" ] && echo -n "$base "
107			if [ "$notset" = "1" ] ; then
108			       if ps h $pid>/dev/null 2>&1; then
109				   # TERM first, then KILL if not dead
110				   kill -TERM $pid
111				   usleep 100000
112				   if ps h $pid >/dev/null 2>&1 ; then
113					sleep 1
114					if ps h $pid >/dev/null 2>&1 ; then
115					        sleep 3
116						if ps h $pid >/dev/null 2>&1 ; then
117						   kill -KILL $pid
118						fi
119					fi
120				   fi
121			        fi
122			       ps h $pid >/dev/null 2>&1 && failure "$base shutdown" || success "$base shutdown"
124			# use specified level only
125			else
126			        if ps h $pid >/dev/null 2>&1; then
127		                	kill $killlevel $pid && success "$base $killlevel" || failure "$base $killlevel"
128				fi
129			fi
130		else
131		    failure "$base shutdown"
132		fi
133	
134	        # Remove pid file if any.
135		if [ "$notset" = "1" ]; then
136	            rm -f /var/run/$base.pid
137		fi
138	}
139	
140	# A function to find the pid of a program.
141	pidofproc() {
142		# Test syntax.
143		if [ $# = 0 ] ; then
144			echo "Usage: pidofproc {program}"
145			return 1
146		fi
147	
148		# First try "/var/run/*.pid" files
149		if [ -f /var/run/$1.pid ] ; then
150		        pid=`head -1 /var/run/$1.pid`
151		        if [ "$pid" != "" ] ; then
152		                echo $pid
153		                return 0
154		        fi
155		fi
156	
157		# Next try "pidof"
158		pid=`pidof $1`
159		if [ "$pid" != "" ] ; then
160		        echo $pid
161		        return 0
162		fi
163	
164		# Finally try to extract it from ps
165		ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
166				   { if ((prog == $5) || (("(" prog ")") == $5) ||
167				     (("[" prog "]") == $5) ||
168				   ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
169	}
170	
171	status() {
172		# Test syntax.
173		if [ $# = 0 ] ; then
174			echo "Usage: status {program}"
175			return 1
176		fi
177
178		# First try "pidof"
179		pid=`pidof $1`
180		if [ "$pid" != "" ] ; then
181		        echo "$1 (pid $pid) is running..."
182		        return 0
183	        else
184	                pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
185				   { if ((prog == $5) || (("(" prog ")") == $5) ||
186				     (("[" prog "]") == $5) ||
187				   ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
188	                if [ "$pid" != "" ] ; then
189	                        echo "$1 (pid $pid) is running..."
190	                        return 0
191	                fi
192		fi
193	
194		# Next try "/var/run/*.pid" files
195		if [ -f /var/run/$1.pid ] ; then
196		        pid=`head -1 /var/run/$1.pid`
197		        if [ "$pid" != "" ] ; then
198		                echo "$1 dead but pid file exists"
199		                return 1
200		        fi
201		fi
202		# See if /var/lock/subsys/$1 exists
203		if [ -f /var/lock/subsys/$1 ]; then
204		echo "$1 dead but subsys locked"
205			return 2
206		fi
207		echo "$1 is stopped"
208		return 3
209	}
210	
211	echo_success() {
212	  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
213	  echo -n "[  "
214	  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
215	  echo -n "OK"
216	  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
217	  echo -n "  ]"
218	  return 0
219	}
220	
221	echo_failure() {
222	  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
223	  echo -n "["
224	  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
225	  echo -n "FAILED"
226	  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
227	  echo -n "]"
228	  return 1
229	}
230	
231	echo_passed() {
232	  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
233	  echo -n "["
234	  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
235	  echo -n "PASSED"
236	  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
237	  echo -n "]"
238	  return 1
239	}
240	
241	# Log that something succeeded
242	success() {
243	  if [ -z "$IN_INITLOG" ]; then
244	     initlog -n $0 -s "$1" -e 1
245	  else
246	     echo "-n $0 -s \"$1\" -e 1" >&21
247	  fi
248	  [ "$BOOTUP" != "verbose" ] && echo_success
249	  return 0
250	}
251	
252	# Log that something failed
253	failure() {
254	  rc=$?
255	  if [ -z "$IN_INITLOG" ]; then
256	     initlog -n $0 -s "$1" -e 2
257	  else
258	     echo "-n $0 -s \"$1\" -e 2" >&21
259	  fi
260	  [ "$BOOTUP" != "verbose" ] && echo_failure
261	  return $rc
262	}
263	
264	# Log that something passed, but may have had errors. Useful for fsck
265	passed() {
266	  rc=$?
267	  if [ -z "$IN_INITLOG" ]; then
268	     initlog -n $0 -s "$1" -e 1
269	  else
270	     echo "-n $0 -s \"$1\" -e 1" >&21
271	  fi
272	  [ "$BOOTUP" != "verbose" ] && echo_passed
273	  return $rc
274	}  
275	
276	# Run some action. Log its output.
277	action() {
278	  STRING=$1
279	  echo -n "$STRING "
280	  shift
281	  initlog $INITLOG_ARGS -c "$*" && success "$STRING" || failure "$STRING"
282	  rc=$?
283	  echo
284	  return $rc
285	}
286	
287	# Confirm whether we really want to run this service
288	confirm() {
289	  echo -n "Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
290	  read answer
291	  case $answer in
292	    y|Y|"")
293	      return 0
294	    ;;
295	    c|C)
296	      return 2
297	    ;;
298	    n|N)
299	      return 1
300	    ;;
301	    *)
302	      confirm $1
303	      return $?
304	    ;;
305	    esac
306	}