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