]> git.saurik.com Git - apple/launchd.git/blame - launchd/src/service
launchd-106.10.tar.gz
[apple/launchd.git] / launchd / src / service
CommitLineData
e91b9f68
A
1#!/bin/sh
2
3set -e
4
5# don't let people kill us. We shouldn't be long, so this isn't a big deal.
6
7trap "" TSTP
8trap "" HUP
9trap "" INT
10trap "" QUIT
11trap "" TERM
12
13function restart_xinetd ()
14{
15 lockfile -r-1 /var/run/.xinetd-restart.lock
16 kill -HUP $(cat /var/run/xinetd.pid 2>/dev/null) 2>/dev/null || /usr/sbin/xinetd -pidfile /var/run/xinetd.pid
17 rm -f /var/run/.xinetd-restart.lock
18}
19
20function restart_xinetd_hard ()
21{
22 lockfile -r-1 /var/run/.xinetd-restart.lock
23 kill -TERM $(cat /var/run/xinetd.pid 2>/dev/null) 2>/dev/null || echo >/dev/null
24 kill -0 $(cat /var/run/xinetd.pid 2>/dev/null) 2>/dev/null && sleep 1
25 /usr/sbin/xinetd -pidfile /var/run/xinetd.pid
26 rm -f /var/run/.xinetd-restart.lock
27}
28
29if [ $# -eq 0 ]
30then
31 echo "Usage: $(basename $0) --list | <service-name> <command>" >&2
32 exit 1
33fi
34
35if [ "$1" == "--list" ]
36then
37 echo smtp
38 echo fax-receive
39 cd /etc/xinetd.d 2>/dev/null
40 ls -1
41 cd /System/Library/LaunchDaemons 2>/dev/null
42 ls -1 | egrep '.plist$' | sed 's,.plist$,,g'
43 exit 0
44elif [ "$1" == "--test-if-configured-on" ]
45then
46 if [ -f "/etc/xinetd.d/$2" ]
47 then
48 egrep "disable.*=.*no" /etc/xinetd.d/$2 >/dev/null 2>&1
49 exit $?
50 fi
51 if [ -f /System/Library/LaunchDaemons/$2.plist ]
52 then
53 IS_OFF=$(defaults read /System/Library/LaunchDaemons/$2 Disabled 2>/dev/null || true)
54 if [ "$IS_OFF" = 1 ]
55 then
56 exit 1
57 else
58 exit 0
59 fi
60 fi
61 if [ "$2" = "smtp" ]
62 then
63 egrep '^MAILSERVER.*-YES-' /etc/hostconfig >/dev/null 2>&1
64 exit $?
65 fi
66 if [ "$2" = "fax-receive" ]
67 then
68 egrep '^fax.*unknown.*on$' /etc/ttys >/dev/null 2>&1
69 exit $?
70 fi
71 exit 1
72elif [ "$1" == "--test-if-available" ]
73then
74 [ -f /System/Library/LaunchDaemons/$2.plist ] && exit 0
75 [ "$2" = "smtp" ] && exit 0
76 [ "$2" = "fax-receive" ] && exit 0
77 [ ! -f "/etc/xinetd.d/$2" ] && exit 1
78 SERVER_FILE=$(egrep 'server[ ]' "/etc/xinetd.d/$2" | sed 's,.*server[ ]*=[ ]*\(.*\),\1,g')
79 [ ! -f "$SERVER_FILE" ] && exit 1
80 exit 0
81elif [ -f "/etc/xinetd.d/$1" ]
82then
83 if [ $UID != 0 ]
84 then
85 echo "You must be root to run this option" >&2
86 exit 1
87 fi
88 # yes, /var/run is gross, but it is tmp directory cleaned up a boot,
89 # writable only by root (so i don't need to worry about the security
90 # implications of mktemp)
91 TMPFILE=$(mktemp /var/run/xinetd.tmp.$$.XXXXXX)
92 cp -f "/etc/xinetd.d/$1" $TMPFILE
93 if [ "$2" == start ]
94 then
95 sed 's/disable.*=.*/disable = no/g' < $TMPFILE > "/etc/xinetd.d/$1"
96 restart_xinetd
97 elif [ "$2" == stop ]
98 then
99 sed 's/disable.*=.*/disable = yes/g' < $TMPFILE > "/etc/xinetd.d/$1"
100 if [ "$1" == "nmbd" ]
101 then
102 restart_xinetd_hard
103 kill -TERM $(cat /var/run/nmbd.pid) || echo >/dev/null
104 else
105 restart_xinetd
106 fi
107 else
108 echo "No such service command" >&2
109 fi
110 rm -f $TMPFILE
111elif [ -f "/System/Library/LaunchDaemons/$1.plist" ]
112then
113 [ "$2" == start ] && launchctl load -w /System/Library/LaunchDaemons/$1.plist
114 [ "$2" == stop ] && launchctl unload -w /System/Library/LaunchDaemons/$1.plist
115elif [ "$1" = "smtp" ]
116then
117 if [ $UID != 0 ]
118 then
119 echo "You must be root to run this option" >&2
120 exit 1
121 fi
122 TMPFILE=$(mktemp /var/run/xinetd.tmp.$$.XXXXXX)
123 cp -f /etc/hostconfig $TMPFILE
124 if [ "$2" == start ]
125 then
126 if grep -q MAILSERVER=-NO- /etc/hostconfig ; then
127 sed 's,^MAILSERVER=-NO-,MAILSERVER=-YES-,g' < $TMPFILE > /etc/hostconfig
128 fi
129 postfix start
130 postfix flush
131 elif [ "$2" == stop ]
132 then
133 if grep -q MAILSERVER=-YES- /etc/hostconfig ; then
134 sed 's,^MAILSERVER=-YES-,MAILSERVER=-NO-,g' < $TMPFILE > /etc/hostconfig
135 fi
136 postfix stop
137 else
138 echo "No such service command" >&2
139 fi
140 rm -f $TMPFILE
141elif [ "$1" = "fax-receive" ]
142then
143 if [ $UID != 0 ]
144 then
145 echo "You must be root to run this option" >&2
146 exit 1
147 fi
148 TMPFILE=$(mktemp /var/run/xinetd.tmp.$$.XXXXXX)
149 cp -f /etc/ttys $TMPFILE
150 if [ "$2" == start ]
151 then
152 sed 's,^fax\(.*\)off$,fax\1on,g' < $TMPFILE > /etc/ttys
153 kill -HUP 1
154 elif [ "$2" == stop ]
155 then
156 sed 's,^fax\(.*\)on$,fax\1off,g' < $TMPFILE > /etc/ttys
157 kill -HUP 1
158 else
159 echo "No such service command" >&2
160 fi
161 rm -f $TMPFILE
162else
163 echo "No such service $1" >&2
164 exit 1
165fi