perform.c revision 7996
1#ifndef lint 2static const char *rcsid = "$Id: perform.c,v 1.17 1995/04/19 14:54:25 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 35 36int 37pkg_perform(char **pkgs) 38{ 39 int i, err_cnt = 0; 40 41 signal(SIGINT, cleanup); 42 signal(SIGHUP, cleanup); 43 44 if (AddMode == SLAVE) 45 err_cnt = pkg_do(NULL); 46 else { 47 for (i = 0; pkgs[i]; i++) 48 err_cnt += pkg_do(pkgs[i]); 49 } 50 return err_cnt; 51} 52 53static Package Plist; 54 55/* This is seriously ugly code following. Written very fast! */ 56static int 57pkg_do(char *pkg) 58{ 59 char pkg_fullname[FILENAME_MAX]; 60 char home[FILENAME_MAX]; 61 char *tmp; 62 FILE *cfile; 63 int code = 0; 64 PackingList p; 65 struct stat sb; 66 67 /* Reset some state */ 68 if (Plist.head) 69 free_plist(&Plist); 70 LogDir[0] = '\0'; 71 if (AddMode == SLAVE) { 72 char tmp_dir[FILENAME_MAX]; 73 74 fgets(tmp_dir, FILENAME_MAX, stdin); 75 tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */ 76 if (chdir(tmp_dir) == FAIL) { 77 whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir); 78 return 1; 79 } 80 read_plist(&Plist, stdin); 81 } 82 else { 83 if (!getcwd(home, FILENAME_MAX)) 84 upchuck("getcwd"); 85 86 if (pkg[0] == '/') /* full pathname? */ 87 strcpy(pkg_fullname, pkg); 88 else 89 sprintf(pkg_fullname, "%s/%s", home, pkg); 90 if (!fexists(pkg_fullname)) { 91 whinge("Can't find package '%s'.", pkg_fullname); 92 return 1; 93 } 94 /* 95 * Apply a crude heuristic to see how much space the package will 96 * take up once it's unpacked. I've noticed that most packages 97 * compress an average of 75%, so multiply by 4 for good measure. 98 */ 99 if (stat(pkg_fullname, &sb) == FAIL) { 100 whinge("Can't stat package file '%s'.", pkg_fullname); 101 return 1; 102 } 103 sb.st_size *= 4; 104 Home = make_playpen(PlayPen, sb.st_size); 105 if (unpack(pkg_fullname, NULL)) { 106 leave_playpen(); 107 return 1; 108 } 109 110 if (sanity_check(pkg_fullname)) { 111 leave_playpen(); 112 return 1; 113 } 114 115 cfile = fopen(CONTENTS_FNAME, "r"); 116 if (!cfile) { 117 whinge("Unable to open %s file.", CONTENTS_FNAME); 118 goto fail; 119 } 120 read_plist(&Plist, cfile); 121 fclose(cfile); 122 if (Prefix) { 123 /* 124 * If we have a prefix, delete the first one we see and add this 125 * one in place of it. 126 */ 127 delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 128 add_plist_top(&Plist, PLIST_CWD, Prefix); 129 } 130 /* If we're running in MASTER mode, just output the plist and return */ 131 if (AddMode == MASTER) { 132 printf("%s\n", where_playpen()); 133 write_plist(&Plist, stdout); 134 return 0; 135 } 136 } 137 setenv(PKG_PREFIX_VNAME, 138 (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1); 139 /* Protect against old packages with bogus @name fields */ 140 PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 141 sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 142 basename_of(PkgName)); 143 if (isdir(LogDir)) { 144 whinge("Package `%s' already recorded as installed.\n", PkgName); 145 code = 1; 146 goto success; /* close enough for government work */ 147 } 148 for (p = Plist.head; p ; p = p->next) { 149 if (p->type != PLIST_PKGDEP) 150 continue; 151 if (Verbose) 152 printf("Package `%s' depends on `%s'", PkgName, p->name); 153 if (!Fake && vsystem("pkg_info -e %s", p->name)) { 154 char tmp[120]; 155 156 if (Verbose) 157 printf(" which is not currently loaded"); 158 sprintf(tmp, "%s/%s.tgz", Home, p->name); 159 if (fexists(tmp)) { 160 if (Verbose) 161 printf(" but was found - loading:\n"); 162 if (vsystem("pkg_add %s", tmp)) { 163 whinge("Autoload of dependency package `%s' failed!%s", 164 p->name, Force ? " (proceeding anyway)" : ""); 165 if (!Force) 166 code++; 167 } 168 else if (Verbose) 169 printf("\t`%s' loaded successfully.\n", p->name); 170 } 171 else { 172 whinge("and was not found%s.", p->name, 173 Force ? " (proceeding anyway)" : ""); 174 if (!Force) 175 code++; 176 } 177 } 178 else if (Verbose) 179 printf(" - already installed.\n"); 180 } 181 if (code != 0) 182 goto success; /* close enough for government work */ 183 184 if (fexists(REQUIRE_FNAME)) { 185 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 186 if (Verbose) 187 printf("Running requirements file first for %s..\n", PkgName); 188 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 189 whinge("Package %s fails requirements %s", 190 pkg_fullname, 191 Force ? "installing anyway" : "- not installed."); 192 if (!Force) { 193 code = 1; 194 goto success; /* close enough for government work */ 195 } 196 } 197 } 198 if (!NoInstall && fexists(INSTALL_FNAME)) { 199 vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 200 if (Verbose) 201 printf("Running install with PRE-INSTALL for %s..\n", PkgName); 202 if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 203 whinge("Install script returned error status."); 204 code = 1; 205 goto success; /* nothing to uninstall yet */ 206 } 207 } 208 extract_plist(home, &Plist); 209 if (!NoInstall && fexists(MTREE_FNAME)) { 210 if (Verbose) 211 printf("Running mtree for %s..\n", PkgName); 212 p = find_plist(&Plist, PLIST_CWD); 213 if (Verbose) 214 printf("mtree -u -f %s -d -e -p %s\n", MTREE_FNAME, 215 p ? p->name : "/"); 216 if (!Fake) { 217 if (vsystem("/usr/sbin/mtree -u -f %s -d -e -p %s", 218 MTREE_FNAME, p ? p->name : "/")) { 219 perror("error in the execution of mtree"); 220 goto fail; 221 } 222 } 223 } 224 if (!NoInstall && fexists(INSTALL_FNAME)) { 225 if (Verbose) 226 printf("Running install with POST-INSTALL for %s..\n", PkgName); 227 if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 228 whinge("Install script returned error status."); 229 code = 1; 230 goto fail; 231 } 232 } 233 if (!NoRecord && !Fake) { 234 char contents[FILENAME_MAX]; 235 FILE *cfile; 236 237 umask(022); 238 if (getuid() != 0) 239 whinge("Not running as root - trying to record install anyway."); 240 if (!PkgName) { 241 whinge("No package name! Can't record package, sorry."); 242 code = 1; 243 goto success; /* well, partial anyway */ 244 } 245 sprintf(LogDir, "%s/%s", 246 (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 247 basename_of(PkgName)); 248 if (Verbose) 249 printf("Attempting to record package into %s..\n", LogDir); 250 if (make_hierarchy(LogDir)) { 251 whinge("Can't record package into '%s', you're on your own!", 252 LogDir); 253 bzero(LogDir, FILENAME_MAX); 254 code = 1; 255 goto success; /* close enough for government work */ 256 } 257 /* Make sure pkg_info can read the entry */ 258 vsystem("chmod a+rx %s", LogDir); 259 if (fexists(DEINSTALL_FNAME)) 260 copy_file(".", DEINSTALL_FNAME, LogDir); 261 if (fexists(REQUIRE_FNAME)) 262 copy_file(".", REQUIRE_FNAME, LogDir); 263 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 264 cfile = fopen(contents, "w"); 265 if (!cfile) { 266 whinge("Can't open new contents file '%s'! Can't register pkg.", 267 contents); 268 goto success; /* can't log, but still keep pkg */ 269 } 270 write_plist(&Plist, cfile); 271 fclose(cfile); 272 copy_file(".", DESC_FNAME, LogDir); 273 copy_file(".", COMMENT_FNAME, LogDir); 274 if (fexists(DISPLAY_FNAME)) 275 copy_file(".", DISPLAY_FNAME, LogDir); 276 for (p = Plist.head; p ; p = p->next) { 277 if (p->type != PLIST_PKGDEP) 278 continue; 279 if (Verbose) 280 printf("Attempting to record dependency on package `%s'\n", 281 p->name); 282 sprintf(contents, "%s/%s/%s", 283 (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 284 basename_of(p->name), REQUIRED_BY_FNAME); 285 cfile = fopen(contents, "a"); 286 if (!cfile) { 287 whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.", 288 contents); 289 continue; 290 } 291 fprintf(cfile, "%s\n", basename_of(PkgName)); 292 if (fclose(cfile) == EOF) 293 warn("Cannot properly close file %s", contents); 294 } 295 if (Verbose) 296 printf("Package %s registered in %s\n", PkgName, LogDir); 297 } 298 299 if (p = find_plist(&Plist, PLIST_DISPLAY)) { 300 FILE *fp; 301 char buf[BUFSIZ]; 302 fp = fopen(p->name, "r"); 303 if (fp) { 304 putc('\n', stdout); 305 while (fgets(buf, sizeof(buf), fp)) 306 fputs(buf, stdout); 307 putc('\n', stdout); 308 (void) fclose(fp); 309 } else 310 warn("Cannot open display file `%s'.", p->name); 311 } 312 313 goto success; 314 315 fail: 316 /* Nuke the whole (installed) show, XXX but don't clean directories */ 317 if (!Fake) 318 delete_package(FALSE, FALSE, &Plist); 319 320 success: 321 /* delete the packing list contents */ 322 leave_playpen(); 323 324 return code; 325} 326 327static int 328sanity_check(char *pkg) 329{ 330 if (!fexists(CONTENTS_FNAME)) { 331 whinge("Package %s has no CONTENTS file!", pkg); 332 return 1; 333 } 334 if (!fexists(COMMENT_FNAME)) { 335 whinge("Package %s has no COMMENT file!", pkg); 336 return 1; 337 } 338 if (!fexists(DESC_FNAME)) { 339 whinge("Package %s has no DESC file!", pkg); 340 return 1; 341 } 342 return 0; 343} 344 345void 346cleanup(int signo) 347{ 348 if (signo) 349 printf("Signal %d received, cleaning up..\n", signo); 350 if (Plist.head) { 351 if (!Fake) 352 delete_package(FALSE, FALSE, &Plist); 353 free_plist(&Plist); 354 } 355 if (!Fake && LogDir[0]) 356 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 357 leave_playpen(); 358} 359