1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* FreeBSD install - a package for the installation and maintainance
|
|
|
|
* of non-core utilities.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Jordan K. Hubbard
|
|
|
|
* 18 July 1993
|
|
|
|
*
|
|
|
|
* Routines for managing the "play pen".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-04-01 09:39:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2010-04-23 11:07:43 +00:00
|
|
|
#include "pkg.h"
|
2001-03-23 18:45:24 +00:00
|
|
|
#include <err.h>
|
2008-08-07 14:48:35 +00:00
|
|
|
#include <libutil.h>
|
2001-10-11 11:29:12 +00:00
|
|
|
#include <libgen.h>
|
1995-04-26 07:43:35 +00:00
|
|
|
#include <sys/signal.h>
|
1994-10-04 16:07:50 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
/* For keeping track of where we are */
|
1998-02-16 17:16:51 +00:00
|
|
|
static char PenLocation[FILENAME_MAX];
|
1995-10-25 15:38:37 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
where_playpen(void)
|
|
|
|
{
|
1998-02-16 17:16:51 +00:00
|
|
|
return PenLocation;
|
1995-10-25 15:38:37 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
|
1995-08-17 00:36:06 +00:00
|
|
|
/* Find a good place to play. */
|
|
|
|
static char *
|
2001-10-10 06:58:42 +00:00
|
|
|
find_play_pen(char *pen, off_t sz)
|
1995-08-17 00:36:06 +00:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
struct stat sb;
|
2008-08-07 14:48:35 +00:00
|
|
|
char humbuf[6];
|
1995-08-17 00:36:06 +00:00
|
|
|
|
2001-11-15 15:13:24 +00:00
|
|
|
if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
|
1995-10-25 15:38:37 +00:00
|
|
|
return pen;
|
|
|
|
else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
|
|
|
|
sprintf(pen, "%s/instmp.XXXXXX", cp);
|
1995-08-17 00:36:06 +00:00
|
|
|
else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
|
1995-10-25 15:38:37 +00:00
|
|
|
sprintf(pen, "%s/instmp.XXXXXX", cp);
|
1995-08-17 00:36:06 +00:00
|
|
|
else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
|
1995-10-25 15:38:37 +00:00
|
|
|
strcpy(pen, "/var/tmp/instmp.XXXXXX");
|
1995-08-17 00:36:06 +00:00
|
|
|
else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
|
1995-10-25 15:38:37 +00:00
|
|
|
strcpy(pen, "/tmp/instmp.XXXXXX");
|
|
|
|
else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
|
|
|
|
strcpy(pen, "/usr/tmp/instmp.XXXXXX");
|
|
|
|
else {
|
1997-10-08 07:48:21 +00:00
|
|
|
cleanup(0);
|
2008-08-07 14:48:35 +00:00
|
|
|
humanize_number(humbuf, sizeof humbuf, sz, "", HN_AUTOSCALE,
|
|
|
|
HN_NOSPACE);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2,
|
|
|
|
"%s: can't find enough temporary space to extract the files, please set your\n"
|
2008-08-07 14:48:35 +00:00
|
|
|
"PKG_TMPDIR environment variable to a location with at least %s bytes\n"
|
|
|
|
"free", __func__, humbuf);
|
1995-10-25 15:38:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pen;
|
1995-08-17 00:36:06 +00:00
|
|
|
}
|
|
|
|
|
1998-04-12 16:01:10 +00:00
|
|
|
#define MAX_STACK 20
|
|
|
|
static char *pstack[MAX_STACK];
|
|
|
|
static int pdepth = -1;
|
|
|
|
|
2009-06-19 17:07:38 +00:00
|
|
|
static const char *
|
2001-10-10 06:58:42 +00:00
|
|
|
pushPen(const char *pen)
|
1998-04-12 16:01:10 +00:00
|
|
|
{
|
|
|
|
if (++pdepth == MAX_STACK)
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: stack overflow.\n", __func__);
|
1998-04-12 16:01:10 +00:00
|
|
|
pstack[pdepth] = strdup(pen);
|
2009-06-19 17:07:38 +00:00
|
|
|
|
|
|
|
return pstack[pdepth];
|
1998-04-12 16:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popPen(char *pen)
|
|
|
|
{
|
|
|
|
if (pdepth == -1) {
|
|
|
|
pen[0] = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy(pen, pstack[pdepth]);
|
|
|
|
free(pstack[pdepth--]);
|
|
|
|
}
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* Make a temporary directory to play in and chdir() to it, returning
|
|
|
|
* pathname of previous working directory.
|
|
|
|
*/
|
2009-06-19 17:07:38 +00:00
|
|
|
const char *
|
2001-10-10 06:58:42 +00:00
|
|
|
make_playpen(char *pen, off_t sz)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
2010-04-23 11:07:43 +00:00
|
|
|
char humbuf[6];
|
2009-06-19 17:07:38 +00:00
|
|
|
char cwd[FILENAME_MAX];
|
2008-08-07 14:48:35 +00:00
|
|
|
|
1995-10-25 15:38:37 +00:00
|
|
|
if (!find_play_pen(pen, sz))
|
|
|
|
return NULL;
|
|
|
|
|
1998-07-28 01:18:02 +00:00
|
|
|
if (!mkdtemp(pen)) {
|
1997-10-08 07:48:21 +00:00
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: can't mktemp '%s'", __func__, pen);
|
1994-10-04 16:07:50 +00:00
|
|
|
}
|
1998-04-12 16:01:10 +00:00
|
|
|
|
2010-04-23 11:07:43 +00:00
|
|
|
humanize_number(humbuf, sizeof humbuf, sz, "", HN_AUTOSCALE, HN_NOSPACE);
|
1998-04-12 16:01:10 +00:00
|
|
|
|
1995-10-25 15:38:37 +00:00
|
|
|
if (min_free(pen) < sz) {
|
|
|
|
rmdir(pen);
|
1997-10-08 07:48:21 +00:00
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: not enough free space to create '%s'.\n"
|
1995-10-25 15:38:37 +00:00
|
|
|
"Please set your PKG_TMPDIR environment variable to a location\n"
|
2010-04-23 11:07:43 +00:00
|
|
|
"with at least %s and try the command again",
|
|
|
|
__func__, humbuf, pen);
|
1994-10-04 16:07:50 +00:00
|
|
|
}
|
1998-04-12 16:01:10 +00:00
|
|
|
|
2009-06-19 17:07:38 +00:00
|
|
|
if (!getcwd(cwd, FILENAME_MAX)) {
|
1995-10-25 15:38:37 +00:00
|
|
|
upchuck("getcwd");
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-04-12 16:01:10 +00:00
|
|
|
|
1998-09-11 07:26:58 +00:00
|
|
|
if (chdir(pen) == FAIL) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: can't chdir to '%s'", __func__, pen);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1998-04-12 16:01:10 +00:00
|
|
|
|
1998-02-16 17:16:51 +00:00
|
|
|
strcpy(PenLocation, pen);
|
2009-06-19 17:07:38 +00:00
|
|
|
return pushPen(cwd);
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience routine for getting out of playpen */
|
2009-06-19 17:07:38 +00:00
|
|
|
int
|
1998-02-16 17:16:51 +00:00
|
|
|
leave_playpen()
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
2009-06-19 17:07:38 +00:00
|
|
|
static char left[FILENAME_MAX];
|
1995-04-26 07:43:35 +00:00
|
|
|
void (*oldsig)(int);
|
|
|
|
|
2009-06-19 17:07:38 +00:00
|
|
|
if (!PenLocation[0])
|
|
|
|
return 0;
|
|
|
|
|
1995-04-26 07:43:35 +00:00
|
|
|
/* Don't interrupt while we're cleaning up */
|
|
|
|
oldsig = signal(SIGINT, SIG_IGN);
|
2009-06-19 17:07:38 +00:00
|
|
|
strcpy(left, PenLocation);
|
|
|
|
popPen(PenLocation);
|
|
|
|
|
|
|
|
if (chdir(PenLocation) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "%s: can't chdir back to '%s'", __func__, PenLocation);
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
2009-06-19 17:07:38 +00:00
|
|
|
|
|
|
|
if (left[0] == '/' && vsystem("/bin/rm -rf %s", left))
|
|
|
|
warnx("couldn't remove temporary dir '%s'", left);
|
1995-10-25 15:38:37 +00:00
|
|
|
signal(SIGINT, oldsig);
|
2009-06-19 17:07:38 +00:00
|
|
|
|
|
|
|
return 1;
|
1993-09-05 04:54:24 +00:00
|
|
|
}
|
1994-10-04 16:07:50 +00:00
|
|
|
|
1996-06-08 00:46:33 +00:00
|
|
|
off_t
|
2001-10-10 06:58:42 +00:00
|
|
|
min_free(const char *tmpdir)
|
1994-10-04 16:07:50 +00:00
|
|
|
{
|
|
|
|
struct statfs buf;
|
|
|
|
|
|
|
|
if (statfs(tmpdir, &buf) != 0) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warn("statfs");
|
1994-10-04 16:07:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1996-06-08 00:46:33 +00:00
|
|
|
return (off_t)buf.f_bavail * (off_t)buf.f_bsize;
|
1994-10-04 16:07:50 +00:00
|
|
|
}
|