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