1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /*
26 * @(#)pmake.cc 1.9 06/12/12
27 */
28
29 #pragma ident "@(#)pmake.cc 1.9 06/12/12"
30
31 #ifdef TEAMWARE_MAKE_CMN
32
33 /*
34 * Included files
35 */
36 #include <arpa/inet.h>
37 #include <mk/defs.h>
38 #include <mksh/misc.h>
39 #include <netdb.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/utsname.h>
45 #include <rpc/rpc.h> /* host2netname(), netname2host() */
46 #ifdef linux
47 # include <unistd.h> /* getdomainname() */
48 #endif
49
50 /*
51 * Defined macros
52 */
53
54 /*
55 * typedefs & structs
56 */
57
58 /*
59 * Static variables
60 */
61
62 /*
63 * File table of contents
64 */
65 static int get_max(wchar_t **ms_address, wchar_t *hostname);
66 static Boolean pskip_comment(wchar_t **cp_address);
67 static void pskip_till_next_word(wchar_t **cp);
68 static Boolean pskip_white_space(wchar_t **cp_address);
69
70
71 /*
72 * read_make_machines(Name make_machines_name)
73 *
74 * For backwards compatibility w/ PMake 1.x, when DMake 2.x is
75 * being run in parallel mode, DMake should parse the PMake startup
76 * file $(HOME)/.make.machines to get the PMake max jobs.
77 *
78 * Return value:
79 * int of PMake max jobs
80 *
81 * Parameters:
82 * make_machines_name Name of .make.machines file
83 *
84 */
85 int
86 read_make_machines(Name make_machines_name)
87 {
88 wchar_t c;
89 Boolean default_make_machines;
90 struct hostent *hp;
91 wchar_t local_host[MAX_HOSTNAMELEN + 1];
92 char local_host_mb[MAX_HOSTNAMELEN + 1] = "";
93 int local_host_wslen;
94 wchar_t full_host[MAXNETNAMELEN + 1];
95 int full_host_wslen = 0;
96 char *homedir;
97 Name MAKE_MACHINES;
98 struct stat make_machines_buf;
99 FILE *make_machines_file;
100 wchar_t *make_machines_list = NULL;
101 char *make_machines_list_mb = NULL;
102 wchar_t make_machines_path[MAXPATHLEN];
103 char mb_make_machines_path[MAXPATHLEN];
104 wchar_t *mp;
105 wchar_t *ms;
106 int pmake_max_jobs = 0;
107 struct utsname uts_info;
108
109
110 MBSTOWCS(wcs_buffer, NOCATGETS("MAKE_MACHINES"));
111 MAKE_MACHINES = GETNAME(wcs_buffer, FIND_LENGTH);
112 /* Did the user specify a .make.machines file on the command line? */
113 default_make_machines = false;
114 if (make_machines_name == NULL) {
115 /* Try reading the default .make.machines file, in $(HOME). */
116 homedir = getenv(NOCATGETS("HOME"));
117 if ((homedir != NULL) && (strlen(homedir) < (sizeof(mb_make_machines_path) - 16))) {
118 sprintf(mb_make_machines_path,
119 NOCATGETS("%s/.make.machines"), homedir);
120 MBSTOWCS(make_machines_path, mb_make_machines_path);
121 make_machines_name = GETNAME(make_machines_path, FIND_LENGTH);
122 default_make_machines = true;
123 }
124 if (make_machines_name == NULL) {
125 /*
126 * No $(HOME)/.make.machines file.
127 * Return 0 for PMake max jobs.
128 */
129 return(0);
130 }
131 }
132 /*
133 make_machines_list_mb = getenv(MAKE_MACHINES->string_mb);
134 */
135 /* Open the .make.machines file. */
136 if ((make_machines_file = fopen(make_machines_name->string_mb, "r")) == NULL) {
137 if (!default_make_machines) {
138 /* Error opening .make.machines file. */
139 fatal(catgets(catd, 1, 314, "Open of %s failed: %s"),
140 make_machines_name->string_mb,
141 errmsg(errno));
142 } else {
143 /*
144 * No $(HOME)/.make.machines file.
145 * Return 0 for PMake max jobs.
146 */
147 return(0);
148 }
149 /* Stat the .make.machines file to get the size of the file. */
150 } else if (fstat(fileno(make_machines_file), &make_machines_buf) < 0) {
151 /* Error stat'ing .make.machines file. */
152 fatal(catgets(catd, 1, 315, "Stat of %s failed: %s"),
153 make_machines_name->string_mb,
154 errmsg(errno));
155 } else {
156 /* Allocate memory for "MAKE_MACHINES=<contents of .m.m>" */
157 make_machines_list_mb =
158 (char *) getmem((int) (strlen(MAKE_MACHINES->string_mb) +
159 2 +
160 make_machines_buf.st_size));
161 sprintf(make_machines_list_mb,
162 "%s=",
163 MAKE_MACHINES->string_mb);
164 /* Read in the .make.machines file. */
165 if (fread(make_machines_list_mb + strlen(MAKE_MACHINES->string_mb) + 1,
166 sizeof(char),
167 (int) make_machines_buf.st_size,
168 make_machines_file) != make_machines_buf.st_size) {
169 /*
170 * Error reading .make.machines file.
171 * Return 0 for PMake max jobs.
172 */
173 warning(catgets(catd, 1, 316, "Unable to read %s"),
174 make_machines_name->string_mb);
175 (void) fclose(make_machines_file);
176 retmem_mb((caddr_t) make_machines_list_mb);
177 return(0);
178 } else {
179 (void) fclose(make_machines_file);
180 /* putenv "MAKE_MACHINES=<contents of .m.m>" */
181 *(make_machines_list_mb +
182 strlen(MAKE_MACHINES->string_mb) +
183 1 +
184 make_machines_buf.st_size) = (int) nul_char;
185 if (putenv(make_machines_list_mb) != 0) {
186 warning(catgets(catd, 1, 317, "Couldn't put contents of %s in environment"),
187 make_machines_name->string_mb);
188 } else {
189 make_machines_list_mb += strlen(MAKE_MACHINES->string_mb) + 1;
190 make_machines_list = ALLOC_WC(strlen(make_machines_list_mb) + 1);
191 (void) mbstowcs(make_machines_list,
192 make_machines_list_mb,
193 (strlen(make_machines_list_mb) + 1));
194 }
195 }
196 }
197
198 uname(&uts_info);
199 strcpy(local_host_mb, &uts_info.nodename[0]);
200 MBSTOWCS(local_host, local_host_mb);
201 local_host_wslen = wslen(local_host);
202
203 // There is no getdomainname() function on Solaris.
204 // And netname2host() function does not work on Linux.
205 // So we have to use different APIs.
206 #ifdef linux
207 if (getdomainname(mbs_buffer, MAXNETNAMELEN+1) == 0) {
208 sprintf(mbs_buffer2, "%s.%s", local_host_mb, mbs_buffer);
209 #else
210 if (host2netname(mbs_buffer, NULL, NULL) &&
211 netname2host(mbs_buffer, mbs_buffer2, MAXNETNAMELEN+1)) {
212 #endif
213 MBSTOWCS(full_host, mbs_buffer2);
214 full_host_wslen = wslen(full_host);
215 }
216
217 for (ms = make_machines_list;
218 (ms) && (*ms );
219 ) {
220 /*
221 * Skip white space and comments till you reach
222 * a machine name.
223 */
224 pskip_till_next_word(&ms);
225
226 /*
227 * If we haven't reached the end of file, process the
228 * machine name.
229 */
230 if (*ms) {
231 /*
232 * If invalid machine name decrement counter
233 * and skip line.
234 */
235 mp = ms;
236 SKIPWORD(ms);
237 c = *ms;
238 *ms++ = '\0'; /* Append null to machine name. */
239 /*
240 * If this was the beginning of a comment
241 * (we overwrote a # sign) and it's not
242 * end of line yet, shift the # sign.
243 */
244 if ((c == '#') && (*ms != '\n') && (*ms)) {
245 *ms = '#';
246 }
247 WCSTOMBS(mbs_buffer, mp);
248 /*
249 * Print "Ignoring unknown host" if:
250 * 1) hostname is longer than MAX_HOSTNAMELEN, or
251 * 2) hostname is unknown
252 */
253 if ((wslen(mp) > MAX_HOSTNAMELEN) ||
254 ((hp = gethostbyname(mbs_buffer)) == NULL)) {
255 warning(catgets(catd, 1, 318, "Ignoring unknown host %s"),
256 mbs_buffer);
257 SKIPTOEND(ms);
258 /* Increment ptr if not end of file. */
259 if (*ms) {
260 ms++;
261 }
262 } else {
263 /* Compare current hostname with local_host. */
264 if (wslen(mp) == local_host_wslen &&
265 IS_WEQUALN(mp, local_host, local_host_wslen)) {
266 /*
267 * Bingo, local_host is in .make.machines.
268 * Continue reading.
269 */
270 pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
271 /* Compare current hostname with full_host. */
272 } else if (wslen(mp) == full_host_wslen &&
273 IS_WEQUALN(mp, full_host, full_host_wslen)) {
274 /*
275 * Bingo, full_host is in .make.machines.
276 * Continue reading.
277 */
278 pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
279 } else {
280 if (c != '\n') {
281 SKIPTOEND(ms);
282 if (*ms) {
283 ms++;
284 }
285 }
286 continue;
287 }
288 /* If we get here, local_host is in .make.machines. */
289 if (c != '\n') {
290 /* Now look for keyword 'max'. */
291 MBSTOWCS(wcs_buffer, NOCATGETS("max"));
292 SKIPSPACE(ms);
293 while ((*ms != '\n') && (*ms)) {
294 if (*ms == '#') {
295 pskip_comment(&ms);
296 } else if (IS_WEQUALN(ms, wcs_buffer, 3)) {
297 /* Skip "max". */
298 ms += 3;
299 pmake_max_jobs = get_max(&ms, mp);
300 SKIPSPACE(ms);
301 } else {
302 warning(catgets(catd, 1, 322, "unknown option for host %s"), mbs_buffer);
303 SKIPTOEND(ms);
304 break;
305 }
306 }
307 }
308 break; /* out of outermost for() loop. */
309 }
310 }
311 }
312 retmem(make_machines_list);
313 return(pmake_max_jobs);
314 }
315
316 /*
317 * pskip_till_next_word(cp)
318 *
319 * Parameters:
320 * cp the address of the string pointer.
321 *
322 * On return:
323 * cp points to beginning of machine name.
324 *
325 */
326 static void
327 pskip_till_next_word(wchar_t **cp)
328 {
329 /*
330 * Keep recursing until all combinations of white spaces
331 * and comments have been skipped.
332 */
333 if (pskip_white_space(cp) || pskip_comment(cp)) {
334 pskip_till_next_word(cp);
335 }
336 }
337
338 /*
339 * pskip_white_space(cp_address)
340 *
341 * Advances the string pointer so that it points to the first
342 * non white character (space/tab/linefeed).
343 *
344 * Parameters:
345 * cp_address the address of the string pointer.
346 *
347 * Return Value:
348 * True if the pointer was changed.
349 *
350 */
351 static Boolean
352 pskip_white_space(wchar_t **cp_address)
353 {
354 wchar_t *cp = *cp_address;
355
356 while (*cp && iswspace(*cp)) {
357 cp++;
358 }
359 /* Have we skipped any characters? */
360 if (cp != *cp_address) {
361 *cp_address = cp;
362 return(true);
363 } else {
364 return(false);
365 }
366 }
367
368 /*
369 * pskip_comment(cp_address)
370 *
371 * If cp_address is pointing to '#' (the beginning of a comment),
372 * increment the pointer till you reach end of line.
373 *
374 * Parameters:
375 * cp_address the address of the string pointer.
376 *
377 * Return Value:
378 * True if the pointer was changed.
379 *
380 */
381 static Boolean
382 pskip_comment(wchar_t **cp_address)
383 {
384 wchar_t *cp = *cp_address;
385
386 /* Is this the beginning of a comment? Skip till end of line. */
387 if (*cp == '#') {
388 SKIPTOEND(cp);
389 }
390 /* Have we skipped a comment line? */
391 if (cp != *cp_address) {
392 *cp_address = cp;
393 return(true);
394 } else {
395 return(false);
396 }
397 }
398
399 static int
400 get_max(wchar_t **ms_address, wchar_t *hostname)
401 {
402 wchar_t *ms = *ms_address;
403 int limit = PMAKE_DEF_MAX_JOBS; /* Default setting. */
404
405 WCSTOMBS(mbs_buffer, hostname);
406 /* Look for `='. */
407 SKIPSPACE(ms);
408 if ((!*ms) || (*ms == '\n') || (*ms != '=')) {
409 SKIPTOEND(ms);
410 warning(catgets(catd, 1, 319, "expected `=' after max, ignoring rest of line for host %s"),
411 mbs_buffer);
412 *ms_address = ms;
413 return((int) limit);
414 } else {
415 ms++;
416 SKIPSPACE(ms);
417 if ((*ms != '\n') && (*ms != '\0')) {
418 /* We've found, hopefully, a valid "max" value. */
419 limit = (int) wcstol(ms, &ms, 10);
420 if (limit < 1) {
421 limit = PMAKE_DEF_MAX_JOBS;
422 warning(catgets(catd, 1, 320, "max value cannot be less than or equal to zero for host %s"), mbs_buffer);
423 }
424 } else {
425 /* No "max" value after "max=". */
426 warning(catgets(catd, 1, 321, "no max value specified for host %s"), mbs_buffer);
427 }
428 *ms_address = ms;
429 return(limit);
430 }
431 }
432
433 #endif
434