Print this page
XXX Remove nawk(1)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/svc/milestone/net-svc
+++ new/usr/src/cmd/svc/milestone/net-svc
1 1 #!/sbin/sh
2 2 #
3 3 # CDDL HEADER START
4 4 #
5 5 # The contents of this file are subject to the terms of the
6 6 # Common Development and Distribution License (the "License").
7 7 # You may not use this file except in compliance with the License.
8 8 #
9 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 # or http://www.opensolaris.org/os/licensing.
11 11 # See the License for the specific language governing permissions
12 12 # and limitations under the License.
13 13 #
14 14 # When distributing Covered Code, include this CDDL HEADER in each
15 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 # If applicable, add the following below this CDDL HEADER, with the
17 17 # fields enclosed by brackets "[]" replaced with your own identifying
18 18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 19 #
20 20 # CDDL HEADER END
21 21 #
22 22 #
23 23 # Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 # Use is subject to license terms.
25 25 #
26 26
27 27 #
28 28 # This is third phase of TCP/IP startup/configuration. This script
29 29 # runs after the NIS startup script. We run things here that may
30 30 # depend on NIS maps.
31 31 #
32 32
33 33 . /lib/svc/share/smf_include.sh
34 34 . /lib/svc/share/net_include.sh
35 35
36 36 NWAM_FMRI="svc:/network/physical:nwam"
37 37
38 38 case "$1" in
39 39 'start')
40 40 #
41 41 # In a shared-IP zone we need this service to be up, but all of the
42 42 # work it tries to do is irrelevant (and will actually lead to the
43 43 # service failing if we try to do it), so just bail out.
44 44 # In the global zone and exclusive-IP zones we proceed.
45 45 #
46 46 smf_configure_ip || exit $SMF_EXIT_OK
47 47
48 48 #
49 49 # If nwam is enabled, the nwam service will handle the tasks performed
50 50 # by this service, so just bail out.
51 51 #
52 52 service_is_enabled $NWAM_FMRI && exit $SMF_EXIT_OK
53 53 ;; # fall through -- rest of script is the initialization code
54 54
55 55 'stop')
56 56 exit $SMF_EXIT_OK
57 57 ;;
58 58
59 59 *)
60 60 echo "Usage: $0 { start | stop }"
61 61 exit 1
62 62 ;;
63 63 esac
64 64
65 65 interface=$2
66 66
67 67 # If boot variables are not set, set variables we use
68 68 [ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
69 69
70 70 #
71 71 # This function takes two file names and the file mode as input. The two
72 72 # files are compared for differences (using cmp(1)) and if different, the
73 73 # second file is over written with the first. A chmod is done with the file
74 74 # mode passed in. If the files are equal, the first file passed
75 75 # in (the /tmp file) is deleted.
76 76 #
77 77 mv_file ()
78 78 {
79 79 /usr/bin/cmp -s $1 $2
80 80 if [ $? -eq 1 ]; then
81 81 /usr/bin/mv $1 $2
82 82 #
83 83 # The umask during boot is configurable, which requires
84 84 # explicit setting of file permission modes when we
85 85 # create files.
86 86 #
87 87 /usr/bin/chmod $3 $2
88 88 else
89 89 /usr/bin/rm $1
90 90 fi
91 91 }
92 92
93 93 #
94 94 # This function takes a DHCP parameter (as defined in /etc/dhcp/inittab)
95 95 # and returns the value for that parameter returned by the DHCP server.
96 96 # If the global 'interface' is defined, it will request the value learned
97 97 # on that interface, else it will request the value learned on the primary
98 98 # interface.
99 99 #
100 100 get_dhcp_var ()
101 101 {
102 102 if [ -n "$interface" ]; then
103 103 /sbin/dhcpinfo -i $interface $1
104 104 else
105 105 /sbin/dhcpinfo $1
106 106 fi
107 107 }
108 108
109 109 #
110 110 # This function returns true if the string "# Added by DHCP$" occurs in
111 111 # the passed-in file, false otherwise.
112 112 #
113 113 dhcp_edits ()
114 114 {
115 115 /usr/bin/grep '# Added by DHCP$' $1 >/dev/null 2>&1
116 116 return $?
117 117 }
118 118
119 119 #
120 120 # update_resolv()
121 121 # Go through /etc/resolv.conf and replace any existing domain or
122 122 # nameserver entries with new ones derived from DHCP. Note that
123 123 # it is important to preserve order of domain entries vs. search
124 124 # entries; the search entries are reserved for administrator
125 125 # customization and if placed after the domain entry will override
126 126 # it. See resolv.conf(4).
127 127 #
128 128 # The first arg should be the dns servers string, the second
129 129 # should be the dns domain.
↓ open down ↓ |
129 lines elided |
↑ open up ↑ |
130 130 #
131 131 update_resolv ()
132 132 {
133 133 dnsservers=$1
134 134 dnsdomain=$2
135 135
136 136 if [ ! -f /etc/resolv.conf ]; then
137 137 /usr/bin/touch /etc/resolv.conf
138 138 fi
139 139 export dnsservers dnsdomain
140 - /usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
140 + /usr/xpg4/bin/awk </etc/resolv.conf >/tmp/resolv.conf.$$ '
141 141 function writedomain() {
142 142 if (updated == 0) {
143 143 # Use only first domain, not a search list
144 144 split(ENVIRON["dnsdomain"], d)
145 145 if(length(d[1]) != 0)
146 146 printf("domain %s\n", d[1])
147 147 }
148 148 ++updated
149 149 }
150 150 $1 == "domain" { writedomain(); next }
151 151 $1 != "nameserver" { print $0 }
152 152 END {
153 153 writedomain()
154 154 n = split(ENVIRON["dnsservers"], s)
155 155 for (i = 1; i <= n; ++i)
156 156 printf("nameserver %s\n", s[i])
157 157 }'
158 158 unset dnsservers dnsdomain
159 159 mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
160 160 }
161 161
162 162 #
163 163 # update_nss()
164 164 # This routine takes as a parameter, the name of the respective policy
165 165 # to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
166 166 #
167 167 update_nss ()
168 168 {
169 169 policy=$1;
170 170 # Add dns to the nsswitch file, if it isn't already there.
171 171 /usr/bin/awk ' $1 ~ /^'${policy}':/ {
172 172 n = split($0, a);
173 173 newl = a[1];
174 174 if ($0 !~ /dns/) {
175 175 printf("#%s # Commented out by DHCP\n", $0);
176 176 updated = 0;
177 177 for (i = 2; i <= n; i++) {
178 178 if (updated == 0 && index(a[i], "[") == 1) {
179 179 newl = newl" dns";
180 180 updated++;
181 181 }
182 182 newl = newl" "a[i];
183 183 }
184 184 if (updated == 0) {
185 185 newl = newl" dns";
186 186 updated++;
187 187 }
188 188 if (updated != 0)
189 189 newl = newl" # Added by DHCP";
190 190 else
191 191 newl = $0;
192 192 printf("%s\n", newl);
193 193 } else
194 194 printf("%s\n", $0);
195 195 } $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
196 196 >/tmp/nsswitch.conf.$$
197 197
198 198 mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
199 199 }
200 200
201 201 #
202 202 # Remove any lines with the "# Added by DHCP" tag from /etc/nsswitch.conf;
203 203 # also uncomment hosts and ipnodes entries which were previously commented
204 204 # out by this script.
205 205 #
206 206 cleanup_nss ()
207 207 {
208 208 /usr/bin/sed \
209 209 -e '/# Added by DHCP$/d' \
210 210 -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
211 211 -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
↓ open down ↓ |
61 lines elided |
↑ open up ↑ |
212 212 /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
213 213
214 214 mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
215 215 }
216 216
217 217 #
218 218 # Remove any lines with the "# Added by DHCP" tag from /etc/inet/hosts.
219 219 #
220 220 cleanup_hosts ()
221 221 {
222 - /usr/bin/nawk '{
222 + /usr/xpg4/bin/awk '{
223 223 if (index($0, "# Added by DHCP") == 0 ||
224 224 $1 == "127.0.0.1" || $1 == "::1") {
225 225 print $0
226 226 }
227 227 }' /etc/inet/hosts > /tmp/hosts.$$
228 228 mv_file /tmp/hosts.$$ /etc/inet/hosts 444
229 229 }
230 230
231 231 #
232 232 # If our network configuration strategy is DHCP, check for DNS
233 233 # configuration parameters obtained from the DHCP server.
234 234 #
235 235 # Script execution starts here.
236 236 #
237 237 smf_netstrategy
238 238
239 239 if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
240 240 dnsservers=`get_dhcp_var DNSserv`
241 241 dnsdomain=`get_dhcp_var DNSdmain`
242 242 else
243 243 dnsservers=""
244 244 dnsdomain=""
245 245 fi
246 246
247 247 if [ -n "$dnsservers" ]; then
248 248 #
249 249 # add settings retrieved from dhcp server to /etc/resolv.conf
250 250 #
251 251 update_resolv "$dnsservers" "$dnsdomain"
252 252
253 253 #
254 254 # Add dns to the nsswitch file, if it isn't already there.
255 255 #
256 256 update_nss hosts
257 257 update_nss ipnodes
258 258
259 259 elif dhcp_edits /etc/nsswitch.conf; then
260 260 # If we added DNS to the hosts and ipnodes
261 261 # policy in the nsswitch, remove it.
262 262 cleanup_nss
263 263 fi
264 264
265 265 if dhcp_edits /etc/inet/hosts; then
266 266 # Clean up any old DHCP-added entries
267 267 # (except loopback) in the hosts file.
268 268 cleanup_hosts
269 269 fi
270 270
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX