summaryrefslogtreecommitdiff
path: root/usr/src/cmd/make/bin/pmake.cc
blob: 660d8c07326b8bc8ffd6f8e7b0166463cfeccb83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */


/*
 * Included files
 */
#include <arpa/inet.h>
#include <mk/defs.h>
#include <mksh/misc.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <rpc/rpc.h>		/* host2netname(), netname2host() */
#include <libintl.h>

/*
 * Defined macros
 */

/*
 * typedefs & structs
 */

/*
 * Static variables
 */

/*
 * File table of contents
 */
static int		get_max(wchar_t **ms_address, wchar_t *hostname);
static Boolean		pskip_comment(wchar_t **cp_address);
static void		pskip_till_next_word(wchar_t **cp);
static Boolean		pskip_white_space(wchar_t **cp_address);


/*
 *	read_make_machines(Name make_machines_name)
 *
 *	For backwards compatibility w/ PMake 1.x, when DMake 2.x is
 *	being run in parallel mode, DMake should parse the PMake startup
 *	file $(HOME)/.make.machines to get the PMake max jobs.
 *
 *	Return value:
 *		int of PMake max jobs
 *
 *	Parameters:
 *		make_machines_name	Name of .make.machines file
 *
 */
int
read_make_machines(Name make_machines_name)
{
	wchar_t 		c;
	Boolean			default_make_machines;
	struct hostent		*hp;
	wchar_t			local_host[MAX_HOSTNAMELEN + 1];
	char			local_host_mb[MAX_HOSTNAMELEN + 1] = "";
	int			local_host_wslen;
	wchar_t			full_host[MAXNETNAMELEN + 1];
	int			full_host_wslen = 0;
	char			*homedir;
	Name			MAKE_MACHINES;
	struct stat		make_machines_buf;
	FILE			*make_machines_file;
	wchar_t			*make_machines_list = NULL;
	char			*make_machines_list_mb = NULL;
	wchar_t			make_machines_path[MAXPATHLEN];
	char			mb_make_machines_path[MAXPATHLEN];
	wchar_t			*mp;
	wchar_t			*ms;
	int			pmake_max_jobs = 0;
	struct utsname		uts_info;


	MBSTOWCS(wcs_buffer, "MAKE_MACHINES");
	MAKE_MACHINES = GETNAME(wcs_buffer, FIND_LENGTH);
	/* Did the user specify a .make.machines file on the command line? */
	default_make_machines = false;
	if (make_machines_name == NULL) {
		/* Try reading the default .make.machines file, in $(HOME). */
		homedir = getenv("HOME");
		if ((homedir != NULL) && (strlen(homedir) < (sizeof(mb_make_machines_path) - 16))) {
			sprintf(mb_make_machines_path,
			 "%s/.make.machines", homedir);
			MBSTOWCS(make_machines_path, mb_make_machines_path);
			make_machines_name = GETNAME(make_machines_path, FIND_LENGTH);
			default_make_machines = true;
		}
		if (make_machines_name == NULL) {
			/*
			 * No $(HOME)/.make.machines file.
			 * Return 0 for PMake max jobs.
			 */
			return(0);
		}
	}
/*
	make_machines_list_mb = getenv(MAKE_MACHINES->string_mb);
 */
	/* Open the .make.machines file. */
	if ((make_machines_file = fopen(make_machines_name->string_mb, "r")) == NULL) {
		if (!default_make_machines) {
			/* Error opening .make.machines file. */
			fatal(gettext("Open of %s failed: %s"),
			      make_machines_name->string_mb,
			      errmsg(errno));
		} else {
			/*
			 * No $(HOME)/.make.machines file.
			 * Return 0 for PMake max jobs.
			 */
			return(0);
		}
	/* Stat the .make.machines file to get the size of the file.  */
	} else if (fstat(fileno(make_machines_file), &make_machines_buf) < 0) {
		/* Error stat'ing .make.machines file. */
		fatal(gettext("Stat of %s failed: %s"),
		      make_machines_name->string_mb,
		      errmsg(errno));
	} else {
		/* Allocate memory for "MAKE_MACHINES=<contents of .m.m>" */
		make_machines_list_mb =
		  (char *) getmem((int) (strlen(MAKE_MACHINES->string_mb) +
		                         2 +
		                         make_machines_buf.st_size));
		sprintf(make_machines_list_mb,
			"%s=",
			MAKE_MACHINES->string_mb);
		/* Read in the .make.machines file. */
		if (fread(make_machines_list_mb + strlen(MAKE_MACHINES->string_mb) + 1,
			  sizeof(char),
			  (int) make_machines_buf.st_size,
			  make_machines_file) != make_machines_buf.st_size) {
			/*
			 * Error reading .make.machines file.
			 * Return 0 for PMake max jobs.
			 */
			warning(gettext("Unable to read %s"),
				make_machines_name->string_mb);
			(void) fclose(make_machines_file);
			retmem_mb((caddr_t) make_machines_list_mb);
			return(0);
		} else {
			(void) fclose(make_machines_file);
			/* putenv "MAKE_MACHINES=<contents of .m.m>" */
			*(make_machines_list_mb +
			  strlen(MAKE_MACHINES->string_mb) +
			  1 +
			  make_machines_buf.st_size) = (int) nul_char;
			if (putenv(make_machines_list_mb) != 0) {
				warning(gettext("Couldn't put contents of %s in environment"),
					make_machines_name->string_mb);
			} else {
				make_machines_list_mb += strlen(MAKE_MACHINES->string_mb) + 1;
				make_machines_list = ALLOC_WC(strlen(make_machines_list_mb) + 1);
				(void) mbstowcs(make_machines_list,
				                make_machines_list_mb,
				                (strlen(make_machines_list_mb) + 1));
			}
		}
	}

	uname(&uts_info);
	strcpy(local_host_mb, &uts_info.nodename[0]);
	MBSTOWCS(local_host, local_host_mb);
	local_host_wslen = wcslen(local_host);

	// There is no getdomainname() function on Solaris.
	// And netname2host() function does not work on Linux.
	// So we have to use different APIs.
	if (host2netname(mbs_buffer, NULL, NULL) &&
	    netname2host(mbs_buffer, mbs_buffer2, MAXNETNAMELEN+1)) {
		MBSTOWCS(full_host, mbs_buffer2);
		full_host_wslen = wcslen(full_host);
	}

	for (ms = make_machines_list;
	     (ms) && (*ms );
	     ) {
		/*
		 * Skip white space and comments till you reach
		 * a machine name.
		 */
		pskip_till_next_word(&ms);

		/*
		 * If we haven't reached the end of file, process the
		 * machine name.
		 */
		if (*ms) {
			/* 
			 * If invalid machine name decrement counter 
			 * and skip line.
			 */
			mp = ms;
			SKIPWORD(ms);
			c = *ms;
			*ms++ = '\0'; /* Append null to machine name. */
			/*
			 * If this was the beginning of a comment
			 * (we overwrote a # sign) and it's not
			 * end of line yet, shift the # sign.
			 */
			if ((c == '#') && (*ms != '\n') && (*ms)) {
				*ms = '#';
			}
			WCSTOMBS(mbs_buffer, mp);
			/*
			 * Print "Ignoring unknown host" if:
			 * 1) hostname is longer than MAX_HOSTNAMELEN, or
			 * 2) hostname is unknown
			 */
			if ((wcslen(mp) > MAX_HOSTNAMELEN) ||
			    ((hp = gethostbyname(mbs_buffer)) == NULL)) {
				warning(gettext("Ignoring unknown host %s"),
					mbs_buffer);
				SKIPTOEND(ms);
				/* Increment ptr if not end of file. */
				if (*ms) {
					ms++;
				}
			} else {
				/* Compare current hostname with local_host. */
				if (wcslen(mp) == local_host_wslen &&
				    IS_WEQUALN(mp, local_host, local_host_wslen)) {
					/*
					 * Bingo, local_host is in .make.machines.
					 * Continue reading.
					 */
					pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
				/* Compare current hostname with full_host. */
				} else if (wcslen(mp) == full_host_wslen &&
					   IS_WEQUALN(mp, full_host, full_host_wslen)) {
					/*
					 * Bingo, full_host is in .make.machines.
					 * Continue reading.
					 */
					pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
				} else {
					if (c != '\n') {
					    SKIPTOEND(ms);
					    if (*ms) {
						ms++;
					    }
					}
					continue;
				}
				/* If we get here, local_host is in .make.machines. */
				if (c != '\n')  {
					/* Now look for keyword 'max'. */
					MBSTOWCS(wcs_buffer, "max");
					SKIPSPACE(ms);
					while ((*ms != '\n') && (*ms)) {
						if (*ms == '#') {
							pskip_comment(&ms);
						} else if (IS_WEQUALN(ms, wcs_buffer, 3)) {
							/* Skip "max". */
							ms += 3; 
							pmake_max_jobs = get_max(&ms, mp); 
							SKIPSPACE(ms);
						} else {
							warning(gettext("unknown option for host %s"), mbs_buffer);
							SKIPTOEND(ms);
							break;
						}
					}
				}
				break; /* out of outermost for() loop. */
			}
		}
	}
	retmem(make_machines_list);
	return(pmake_max_jobs);
}

/*
 *	pskip_till_next_word(cp)
 *
 *	Parameters:
 *		cp		the address of the string pointer.
 *
 *	On return:
 *		cp		points to beginning of machine name.
 *
 */
static void
pskip_till_next_word(wchar_t **cp)
{
	/*
	 * Keep recursing until all combinations of white spaces
	 * and comments have been skipped.
	 */
	if (pskip_white_space(cp) || pskip_comment(cp)) {
		pskip_till_next_word(cp);
	}
}

/*
 *	pskip_white_space(cp_address)
 *
 *	Advances the string pointer so that it points to the first
 *	non white character (space/tab/linefeed).
 *
 *	Parameters:
 *		cp_address	the address of the string pointer.
 *
 *	Return Value:
 *				True if the pointer was changed.
 *
 */
static Boolean
pskip_white_space(wchar_t **cp_address)
{
	wchar_t		*cp = *cp_address;

	while (*cp && iswspace(*cp)) {
		cp++;
	}
	/* Have we skipped any characters? */
	if (cp != *cp_address) {
		*cp_address = cp;
		return(true);
	} else {
		return(false);
	}
}

/*
 *	pskip_comment(cp_address)
 *
 *	If cp_address is pointing to '#' (the beginning of a comment),
 *	increment the pointer till you reach end of line.
 *
 *	Parameters:
 *		cp_address	the address of the string pointer.
 *
 *	Return Value:
 *				True if the pointer was changed.
 *
 */
static Boolean
pskip_comment(wchar_t **cp_address)
{
	wchar_t		*cp = *cp_address;

	/* Is this the beginning of a comment? Skip till end of line. */
	if (*cp == '#') {
		SKIPTOEND(cp);
	}
	/* Have we skipped a comment line? */
	if (cp != *cp_address)	{
		*cp_address = cp;
		return(true);
	} else {
		return(false);
	}
}

static int
get_max(wchar_t **ms_address, wchar_t *hostname)
{
	wchar_t 	*ms = *ms_address;
	int		limit = PMAKE_DEF_MAX_JOBS; /* Default setting. */

	WCSTOMBS(mbs_buffer, hostname);
	/* Look for `='. */
	SKIPSPACE(ms);
	if ((!*ms) || (*ms == '\n') || (*ms != '=')) {
		SKIPTOEND(ms);
		warning(gettext("expected `=' after max, ignoring rest of line for host %s"),
			mbs_buffer);
		*ms_address = ms;
		return((int) limit);
	} else {
		ms++;
		SKIPSPACE(ms);
		if ((*ms != '\n') && (*ms != '\0')) {
			/* We've found, hopefully, a valid "max" value. */
			limit = (int) wcstol(ms, &ms, 10);
			if (limit < 1) {
				limit = PMAKE_DEF_MAX_JOBS;
				warning(gettext("max value cannot be less than or equal to zero for host %s"), mbs_buffer);
			}
		} else {
			/* No "max" value after "max=". */
			warning(gettext("no max value specified for host %s"), mbs_buffer);
		}
		*ms_address = ms;
		return(limit);
	}
}