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