pen.c revision 39068
1#ifndef lint 2static const char rcsid[] = 3 "$Id: pen.c,v 1.29 1998/07/28 01:18:02 nectar Exp $"; 4#endif 5 6/* 7 * FreeBSD install - a package for the installation and maintainance 8 * of non-core utilities. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Jordan K. Hubbard 20 * 18 July 1993 21 * 22 * Routines for managing the "play pen". 23 * 24 */ 25 26#include <err.h> 27#include "lib.h" 28#include <sys/signal.h> 29#include <sys/param.h> 30#include <sys/mount.h> 31 32/* For keeping track of where we are */ 33static char PenLocation[FILENAME_MAX]; 34static char Previous[FILENAME_MAX]; 35 36char * 37where_playpen(void) 38{ 39 return PenLocation; 40} 41 42/* Find a good place to play. */ 43static char * 44find_play_pen(char *pen, size_t sz) 45{ 46 char *cp; 47 struct stat sb; 48 49 if (pen[0] && stat(pen, &sb) != FAIL && (min_free(pen) >= sz)) 50 return pen; 51 else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz)) 52 sprintf(pen, "%s/instmp.XXXXXX", cp); 53 else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz)) 54 sprintf(pen, "%s/instmp.XXXXXX", cp); 55 else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz) 56 strcpy(pen, "/var/tmp/instmp.XXXXXX"); 57 else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz) 58 strcpy(pen, "/tmp/instmp.XXXXXX"); 59 else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz) 60 strcpy(pen, "/usr/tmp/instmp.XXXXXX"); 61 else { 62 cleanup(0); 63 errx(2, 64"can't find enough temporary space to extract the files, please set your\n" 65"PKG_TMPDIR environment variable to a location with at least %d bytes\n" 66"free", sz); 67 return NULL; 68 } 69 return pen; 70} 71 72#define MAX_STACK 20 73static char *pstack[MAX_STACK]; 74static int pdepth = -1; 75 76static void 77pushPen(char *pen) 78{ 79 if (++pdepth == MAX_STACK) 80 errx(2, "stack overflow in pushPen().\n"); 81 pstack[pdepth] = strdup(pen); 82} 83 84static void 85popPen(char *pen) 86{ 87 if (pdepth == -1) { 88 pen[0] = '\0'; 89 return; 90 } 91 strcpy(pen, pstack[pdepth]); 92 free(pstack[pdepth--]); 93} 94 95/* 96 * Make a temporary directory to play in and chdir() to it, returning 97 * pathname of previous working directory. 98 */ 99char * 100make_playpen(char *pen, size_t sz) 101{ 102 if (!find_play_pen(pen, sz)) 103 return NULL; 104 105 if (!mkdtemp(pen)) { 106 cleanup(0); 107 errx(2, "can't mktemp '%s'", pen); 108 } 109 if (chmod(pen, 0755) == FAIL) { 110 cleanup(0); 111 errx(2, "can't mkdir '%s'", pen); 112 } 113 114 if (Verbose) { 115 if (sz) 116 fprintf(stderr, "Requested space: %d bytes, free space: %qd bytes in %s\n", (int)sz, min_free(pen), pen); 117 } 118 119 if (min_free(pen) < sz) { 120 rmdir(pen); 121 cleanup(0); 122 errx(2, "not enough free space to create '%s'.\n" 123 "Please set your PKG_TMPDIR environment variable to a location\n" 124 "with more space and\ntry the command again", pen); 125 } 126 127 if (!getcwd(Previous, FILENAME_MAX)) { 128 upchuck("getcwd"); 129 return NULL; 130 } 131 132 if (chdir(pen) == FAIL) { 133 cleanup(0); 134 errx(2, "can't chdir to '%s'", pen); 135 } 136 137 if (PenLocation[0]) 138 pushPen(PenLocation); 139 140 strcpy(PenLocation, pen); 141 return Previous; 142} 143 144/* Convenience routine for getting out of playpen */ 145void 146leave_playpen() 147{ 148 void (*oldsig)(int); 149 150 /* Don't interrupt while we're cleaning up */ 151 oldsig = signal(SIGINT, SIG_IGN); 152 if (Previous[0]) { 153 if (chdir(Previous) == FAIL) { 154 cleanup(0); 155 errx(2, "can't chdir back to '%s'", Previous); 156 } 157 Previous[0] = '\0'; 158 } 159 if (PenLocation[0]) { 160 if (PenLocation[0] == '/' && vsystem("rm -rf %s", PenLocation)) 161 warnx("couldn't remove temporary dir '%s'", PenLocation); 162 popPen(PenLocation); 163 } 164 signal(SIGINT, oldsig); 165} 166 167off_t 168min_free(char *tmpdir) 169{ 170 struct statfs buf; 171 172 if (statfs(tmpdir, &buf) != 0) { 173 warn("statfs"); 174 return -1; 175 } 176 return (off_t)buf.f_bavail * (off_t)buf.f_bsize; 177} 178