]> git.saurik.com Git - apple/launchd.git/blob - launchd/src/service
launchd-106.20.tar.gz
[apple/launchd.git] / launchd / src / service
1 #!/bin/sh
2
3 set -e
4
5 # don't let people kill us. We shouldn't be long, so this isn't a big deal.
6
7 trap "" TSTP
8 trap "" HUP
9 trap "" INT
10 trap "" QUIT
11 trap "" TERM
12
13 function 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
20 function 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
29 if [ $# -eq 0 ]
30 then
31 echo "Usage: $(basename $0) --list | <service-name> <command>" >&2
32 exit 1
33 fi
34
35 if [ "$1" == "--list" ]
36 then
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
44 elif [ "$1" == "--test-if-configured-on" ]
45 then
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
72 elif [ "$1" == "--test-if-available" ]
73 then
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
81 elif [ -f "/etc/xinetd.d/$1" ]
82 then
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
111 elif [ -f "/System/Library/LaunchDaemons/$1.plist" ]
112 then
113 [ "$2" == start ] && launchctl load -w /System/Library/LaunchDaemons/$1.plist
114 [ "$2" == stop ] && launchctl unload -w /System/Library/LaunchDaemons/$1.plist
115 elif [ "$1" = "smtp" ]
116 then
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
141 elif [ "$1" = "fax-receive" ]
142 then
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
162 else
163 echo "No such service $1" >&2
164 exit 1
165 fi