perform.c revision 16549
1#ifndef lint 2static const char *rcsid = "$Id: perform.c,v 1.34 1996/06/03 04:40:43 jkh Exp $"; 3#endif 4 5/* 6 * FreeBSD install - a package for the installation and maintainance 7 * of non-core utilities. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Jordan K. Hubbard 19 * 18 July 1993 20 * 21 * This is the main body of the add module. 22 * 23 */ 24 25#include "lib.h" 26#include "add.h" 27 28#include <signal.h> 29#include <sys/wait.h> 30 31static int pkg_do(char *); 32static int sanity_check(char *); 33static char LogDir[FILENAME_MAX]; 34 35int 36pkg_perform(char **pkgs) 37{ 38 int i, err_cnt = 0; 39 40 signal(SIGINT, cleanup); 41 signal(SIGHUP, cleanup); 42 43 if (AddMode == SLAVE) 44 err_cnt = pkg_do(NULL); 45 else { 46 for (i = 0; pkgs[i]; i++) 47 err_cnt += pkg_do(pkgs[i]); 48 } 49 return err_cnt; 50} 51 52static Package Plist; 53static char *Home; 54 55/* 56 * This is seriously ugly code following. Written very fast! 57 * [And subsequently made even worse.. Sigh! This code was just born 58 * to be hacked, I guess.. :) -jkh] 59 */ 60static int 61pkg_do(char *pkg) 62{ 63 char pkg_fullname[FILENAME_MAX]; 64 char playpen[FILENAME_MAX]; 65 char extract_contents[FILENAME_MAX]; 66 char *where_to, *tmp, *extract; 67 FILE *cfile; 68 int code; 69 PackingList p; 70 struct stat sb; 71 int inPlace; 72 73 code = 0; 74 LogDir[0] = '\0'; 75 strcpy(playpen, FirstPen); 76 inPlace = 0; 77 78 /* Are we coming in for a second pass, everything already extracted? */ 79 if (!pkg) { 80 fgets(playpen, FILENAME_MAX, stdin); 81 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */ 82 if (chdir(playpen) == FAIL) { 83 whinge("pkg_add in SLAVE mode can't chdir to %s.", playpen); 84 return 1; 85 } 86 read_plist(&Plist, stdin); 87 where_to = playpen; 88 } 89 /* Nope - do it now */ 90 else { 91 /* Is it an ftp://foo.bar.baz/file.tgz specification? */ 92 if (isURL(pkg)) { 93 if (!(Home = fileGetURL(NULL, pkg))) { 94 whinge("Unable to fetch `%s' by URL.", pkg); 95 return 1; 96 } 97 where_to = Home; 98 strcpy(pkg_fullname, pkg); 99 cfile = fopen(CONTENTS_FNAME, "r"); 100 if (!cfile) { 101 whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME); 102 goto bomb; 103 } 104 read_plist(&Plist, cfile); 105 fclose(cfile); 106 } 107 else { 108 strcpy(pkg_fullname, pkg); /* copy for sanity's sake, could remove pkg_fullname */ 109 if (strcmp(pkg, "-")) { 110 if (stat(pkg_fullname, &sb) == FAIL) { 111 whinge("Can't stat package file '%s'.", pkg_fullname); 112 goto bomb; 113 } 114 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME); 115 extract = extract_contents; 116 } 117 else 118 extract = NULL; 119 Home = make_playpen(playpen, sb.st_size * 4); 120 if (!Home) 121 whinge("Unable to make playpen for %d bytes.\n", sb.st_size * 4); 122 where_to = Home; 123 if (unpack(pkg_fullname, extract)) { 124 whinge("Unable to extract table of contents file from `%s' - not a package?.", pkg_fullname); 125 goto bomb; 126 } 127 cfile = fopen(CONTENTS_FNAME, "r"); 128 if (!cfile) { 129 whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME); 130 goto bomb; 131 } 132 read_plist(&Plist, cfile); 133 fclose(cfile); 134 135 /* Extract directly rather than moving? Oh goodie! */ 136 if (find_plist_option(&Plist, "extract-in-place")) { 137 if (Verbose) 138 printf("Doing in-place extraction for %s\n", pkg_fullname); 139 p = find_plist(&Plist, PLIST_CWD); 140 if (p) { 141 if (!isdir(p->name) && !Fake) { 142 if (Verbose) 143 printf("Desired prefix of %s does not exist, creating..\n", p->name); 144 vsystem("mkdir -p %s", p->name); 145 if (chdir(p->name) == -1) { 146 whinge("Unable to change directory to `%s' - no permission?", p->name); 147 perror("chdir"); 148 goto bomb; 149 } 150 } 151 where_to = p->name; 152 inPlace = 1; 153 } 154 else { 155 whinge("No prefix specified in `%s' - this is a bad package!", pkg_fullname); 156 goto bomb; 157 } 158 } 159 160 /* 161 * Apply a crude heuristic to see how much space the package will 162 * take up once it's unpacked. I've noticed that most packages 163 * compress an average of 75%, so multiply by 4 for good measure. 164 */ 165 166 if (!inPlace && min_free(playpen) < sb.st_size * 4) { 167 whinge("Projected size of %d exceeds available free space.\n" 168 "Please set your PKG_TMPDIR variable to point to a location with more\n" 169 "free space and try again.", sb.st_size * 4); 170 whinge("Not extracting %s\ninto %s, sorry!", pkg_fullname, where_to); 171 goto bomb; 172 } 173 174 /* If this is a direct extract and we didn't want it, stop now */ 175 if (inPlace && Fake) 176 goto success; 177 178 /* Finally unpack the whole mess */ 179 if (unpack(pkg_fullname, NULL)) { 180 whinge("Unable to extract `%s'!", pkg_fullname); 181 goto bomb; 182 } 183 } 184 185 /* Check for sanity and dependencies */ 186 if (sanity_check(pkg)) 187 goto bomb; 188 189 /* If we're running in MASTER mode, just output the plist and return */ 190 if (AddMode == MASTER) { 191 printf("%s\n", where_playpen()); 192 write_plist(&Plist, stdout); 193 return 0; 194 } 195 } 196 197 /* 198 * If we have a prefix, delete the first one we see and add this 199 * one in place of it. 200 */ 201 if (Prefix) { 202 delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 203 add_plist_top(&Plist, PLIST_CWD, Prefix); 204 } 205 206 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1); 207 /* Protect against old packages with bogus @name fields */ 208 PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 209 210 /* See if we're already registered */ 211 sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 212 if (isdir(LogDir)) { 213 whinge("Package `%s' already recorded as installed.\n", PkgName); 214 code = 1; 215 goto success; /* close enough for government work */ 216 } 217 218 /* Now check the packing list for dependencies */ 219 for (p = Plist.head; p ; p = p->next) { 220 if (p->type != PLIST_PKGDEP) 221 continue; 222 if (Verbose) 223 printf("Package `%s' depends on `%s'.\n", PkgName, p->name); 224 if (!Fake && vsystem("pkg_info -e %s", p->name)) { 225 char path[FILENAME_MAX], *cp = NULL; 226 227 if (!Fake && !isURL(pkg) && !getenv("PKG_ADD_BASE")) { 228 snprintf(path, FILENAME_MAX, "%s/%s.tgz", Home, p->name); 229 if (fexists(path)) 230 cp = path; 231 else 232 cp = fileFindByPath(pkg, p->name); 233 if (cp) { 234 if (Verbose) 235 printf("Loading it from %s.\n", cp); 236 if (vsystem("pkg_add %s", cp)) { 237 whinge("Autoload of dependency `%s' failed%s", cp, Force ? " (proceeding anyway)" : "!"); 238 if (!Force) 239 ++code; 240 } 241 } 242 } 243 else if (!Fake && (cp = fileGetURL(pkg, p->name)) != NULL) { 244 if (Verbose) 245 printf("Finished loading %s over FTP.\n", p->name); 246 if (!Fake) { 247 if (!fexists("+CONTENTS")) 248 whinge("Autoloaded package %s has no +CONTENTS file?", p->name); 249 else 250 if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) { 251 whinge("pkg_add of dependency `%s' failed%s", 252 p->name, Force ? " (proceeding anyway)" : "!"); 253 if (!Force) 254 ++code; 255 } 256 else if (Verbose) 257 printf("\t`%s' loaded successfully.\n", p->name); 258 } 259 /* Nuke the temporary playpen */ 260 leave_playpen(cp); 261 } 262 else { 263 if (Verbose) 264 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : ""); 265 else 266 printf("Package dependency %s for %s not found%s\n", p->name, pkg, 267 Force ? " (proceeding anyway)" : "!"); 268 if (!Force) 269 ++code; 270 } 271 } 272 else if (Verbose) 273 printf(" - already installed.\n"); 274 } 275 276 if (code != 0) 277 goto bomb; 278 279 /* Look for the requirements file */ 280 if (fexists(REQUIRE_FNAME)) { 281 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 282 if (Verbose) 283 printf("Running requirements file first for %s..\n", PkgName); 284 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 285 whinge("Package %s fails requirements %s", pkg_fullname, 286 Force ? "installing anyway" : "- not installed."); 287 if (!Force) { 288 code = 1; 289 goto success; /* close enough for government work */ 290 } 291 } 292 } 293 294 /* If we're really installing, and have an installation file, run it */ 295 if (!NoInstall && fexists(INSTALL_FNAME)) { 296 vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 297 if (Verbose) 298 printf("Running install with PRE-INSTALL for %s..\n", PkgName); 299 if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 300 whinge("Install script returned error status."); 301 unlink(INSTALL_FNAME); 302 code = 1; 303 goto success; /* nothing to uninstall yet */ 304 } 305 } 306 307 /* Now finally extract the entire show if we're not going direct */ 308 if (!inPlace && !Fake) 309 extract_plist(".", &Plist); 310 311 if (!Fake && fexists(MTREE_FNAME)) { 312 if (Verbose) 313 printf("Running mtree for %s..\n", PkgName); 314 p = find_plist(&Plist, PLIST_CWD); 315 if (Verbose) 316 printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME, p ? p->name : "/"); 317 if (!Fake) { 318 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s", MTREE_FNAME, p ? p->name : "/")) 319 whinge("mtree returned a non-zero status - continuing."); 320 } 321 unlink(MTREE_FNAME); 322 } 323 324 /* Run the installation script one last time? */ 325 if (!NoInstall && fexists(INSTALL_FNAME)) { 326 if (Verbose) 327 printf("Running install with POST-INSTALL for %s..\n", PkgName); 328 if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 329 whinge("Install script returned error status."); 330 unlink(INSTALL_FNAME); 331 code = 1; 332 goto fail; 333 } 334 unlink(INSTALL_FNAME); 335 } 336 337 /* Time to record the deed? */ 338 if (!NoRecord && !Fake) { 339 char contents[FILENAME_MAX]; 340 FILE *cfile; 341 342 umask(022); 343 if (getuid() != 0) 344 whinge("Not running as root - trying to record install anyway."); 345 if (!PkgName) { 346 whinge("No package name! Can't record package, sorry."); 347 code = 1; 348 goto success; /* well, partial anyway */ 349 } 350 sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 351 if (Verbose) 352 printf("Attempting to record package into %s..\n", LogDir); 353 if (make_hierarchy(LogDir)) { 354 whinge("Can't record package into '%s', you're on your own!", 355 LogDir); 356 bzero(LogDir, FILENAME_MAX); 357 code = 1; 358 goto success; /* close enough for government work */ 359 } 360 /* Make sure pkg_info can read the entry */ 361 vsystem("chmod a+rx %s", LogDir); 362 if (fexists(DEINSTALL_FNAME)) 363 move_file(".", DEINSTALL_FNAME, LogDir); 364 if (fexists(REQUIRE_FNAME)) 365 move_file(".", REQUIRE_FNAME, LogDir); 366 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 367 cfile = fopen(contents, "w"); 368 if (!cfile) { 369 whinge("Can't open new contents file '%s'! Can't register pkg.", contents); 370 goto success; /* can't log, but still keep pkg */ 371 } 372 write_plist(&Plist, cfile); 373 fclose(cfile); 374 move_file(".", DESC_FNAME, LogDir); 375 move_file(".", COMMENT_FNAME, LogDir); 376 if (fexists(DISPLAY_FNAME)) 377 move_file(".", DISPLAY_FNAME, LogDir); 378 for (p = Plist.head; p ; p = p->next) { 379 if (p->type != PLIST_PKGDEP) 380 continue; 381 if (Verbose) 382 printf("Attempting to record dependency on package `%s'\n", p->name); 383 sprintf(contents, "%s/%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 384 basename_of(p->name), REQUIRED_BY_FNAME); 385 cfile = fopen(contents, "a"); 386 if (!cfile) 387 whinge("Warning: Can't open dependency file '%s'!\n" 388 "\tDependency registration is incomplete.", contents); 389 else { 390 fprintf(cfile, "%s\n", PkgName); 391 if (fclose(cfile) == EOF) 392 whinge("Cannot properly close file %s", contents); 393 } 394 } 395 if (Verbose) 396 printf("Package %s registered in %s\n", PkgName, LogDir); 397 } 398 399 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) { 400 FILE *fp; 401 char buf[BUFSIZ]; 402 403 fp = fopen(p->name, "r"); 404 if (fp) { 405 putc('\n', stdout); 406 while (fgets(buf, sizeof(buf), fp)) 407 fputs(buf, stdout); 408 putc('\n', stdout); 409 (void) fclose(fp); 410 } else 411 whinge("Cannot open display file `%s'.", p->name); 412 } 413 414 goto success; 415 416 bomb: 417 code = 1; 418 goto success; 419 420 fail: 421 /* Nuke the whole (installed) show, XXX but don't clean directories */ 422 if (!Fake) 423 delete_package(FALSE, FALSE, &Plist); 424 425 success: 426 /* delete the packing list contents */ 427 free_plist(&Plist); 428 leave_playpen(Home); 429 return code; 430} 431 432static int 433sanity_check(char *pkg) 434{ 435 int code = 0; 436 437 if (!fexists(CONTENTS_FNAME)) { 438 whinge("Package %s has no CONTENTS file!", pkg); 439 code = 1; 440 } 441 else if (!fexists(COMMENT_FNAME)) { 442 whinge("Package %s has no COMMENT file!", pkg); 443 code = 1; 444 } 445 else if (!fexists(DESC_FNAME)) { 446 whinge("Package %s has no DESC file!", pkg); 447 code = 1; 448 } 449 return code; 450} 451 452void 453cleanup(int signo) 454{ 455 if (signo) 456 printf("Signal %d received, cleaning up..\n", signo); 457 if (Plist.head) { 458 if (!Fake) 459 delete_package(FALSE, FALSE, &Plist); 460 free_plist(&Plist); 461 } 462 if (!Fake && LogDir[0]) 463 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 464 leave_playpen(Home); 465} 466