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