1999-08-24 04:48:37 +04:00
|
|
|
/* $NetBSD: pen.c,v 1.16 1999/08/24 00:48:39 hubertf Exp $ */
|
1997-06-05 16:59:18 +04:00
|
|
|
|
1997-10-17 18:53:18 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#ifndef lint
|
1997-06-05 16:59:18 +04:00
|
|
|
#if 0
|
1997-10-16 04:31:32 +04:00
|
|
|
static const char *rcsid = "from FreeBSD Id: pen.c,v 1.25 1997/10/08 07:48:12 charnier Exp";
|
1997-06-05 16:59:18 +04:00
|
|
|
#else
|
1999-08-24 04:48:37 +04:00
|
|
|
__RCSID("$NetBSD: pen.c,v 1.16 1999/08/24 00:48:39 hubertf Exp $");
|
1997-06-05 16:59:18 +04:00
|
|
|
#endif
|
1997-06-05 12:54:23 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1997-10-16 04:31:32 +04:00
|
|
|
#include <err.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#include "lib.h"
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
/* For keeping track of where we are */
|
|
|
|
static char Current[FILENAME_MAX];
|
|
|
|
static char Previous[FILENAME_MAX];
|
1999-08-24 04:48:37 +04:00
|
|
|
static int CurrentSet; /* rm -rf Current only if it's really set! */
|
|
|
|
/* CurrentSet is set to 0 before strcpy()s
|
|
|
|
* to prevent rm'ing of a partial string
|
|
|
|
* when interrupted by ^C */
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Backup Current and Previous into temp. strings that are later
|
1998-10-02 01:16:26 +04:00
|
|
|
* restored & freed by restore_dirs
|
|
|
|
* This is to make nested calls to makeplaypen/leave_playpen work
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
save_dirs(char **c, char **p)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
*c = strdup(Current); /* XXX */
|
|
|
|
*p = strdup(Previous);
|
1998-10-02 01:16:26 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Restore Current and Previous from temp strings that were created
|
1998-10-02 01:16:26 +04:00
|
|
|
* by safe_dirs.
|
|
|
|
* This is to make nested calls to makeplaypen/leave_playpen work
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
restore_dirs(char *c, char *p)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
CurrentSet = 0; /* prevent from deleting */
|
|
|
|
strcpy(Current, c);
|
|
|
|
CurrentSet = 1; /* rm -fr Current is safe now */
|
|
|
|
free(c);
|
|
|
|
|
|
|
|
strcpy(Previous, p);
|
|
|
|
free(p);
|
1998-10-02 01:16:26 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
where_playpen(void)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
return Current;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Find a good place to play.
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
static char *
|
1998-10-08 16:57:58 +04:00
|
|
|
find_play_pen(char *pen, size_t pensize, size_t sz)
|
1997-06-05 12:54:23 +04:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char *cp;
|
|
|
|
struct stat sb;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (pen[0] && stat(pen, &sb) != FAIL && (min_free(pen) >= sz))
|
|
|
|
return pen;
|
|
|
|
else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
|
|
|
|
(void) snprintf(pen, pensize, "%s/instmp.XXXXXX", cp);
|
|
|
|
else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
|
|
|
|
(void) snprintf(pen, pensize, "%s/instmp.XXXXXX", cp);
|
|
|
|
else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
|
|
|
|
strcpy(pen, "/var/tmp/instmp.XXXXXX");
|
|
|
|
else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
|
|
|
|
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 {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2,
|
|
|
|
"can't find enough temporary space to extract the files, please set your\n"
|
|
|
|
"PKG_TMPDIR environment variable to a location with at least %lu bytes\n"
|
|
|
|
"free", (u_long) sz);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
return pen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a temporary directory to play in and chdir() to it, returning
|
|
|
|
* pathname of previous working directory.
|
|
|
|
*/
|
1999-08-24 04:48:37 +04:00
|
|
|
char *
|
1998-10-08 16:57:58 +04:00
|
|
|
make_playpen(char *pen, size_t pensize, size_t sz)
|
1997-06-05 12:54:23 +04:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
if (!find_play_pen(pen, pensize, sz))
|
|
|
|
return NULL;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-03-02 06:04:06 +03:00
|
|
|
#if (defined(NetBSD1_3) || (NetBSD <= 199713)) && (NetBSD1_3 <9)
|
1999-08-24 04:48:37 +04:00
|
|
|
/* values from 1.3.2/1.3I */
|
|
|
|
/* mkdtemp(3) is not present on 1.3.3 and below */
|
|
|
|
if (!mktemp(pen)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't mktemp '%s'", pen);
|
|
|
|
}
|
|
|
|
if (mkdir(pen, 0755) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't mkdir '%s'", pen);
|
|
|
|
}
|
1999-01-19 20:01:56 +03:00
|
|
|
#else
|
1999-08-24 04:48:37 +04:00
|
|
|
if (!mkdtemp(pen)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't mkdtemp '%s'", pen);
|
|
|
|
}
|
1999-01-19 20:01:56 +03:00
|
|
|
#endif
|
1999-08-24 04:48:37 +04:00
|
|
|
if (Verbose) {
|
|
|
|
if (sz)
|
|
|
|
fprintf(stderr, "Requested space: %lu bytes, free space: %qd bytes in %s\n", (u_long) sz, (long long) min_free(pen), pen);
|
|
|
|
}
|
|
|
|
if (min_free(pen) < sz) {
|
|
|
|
rmdir(pen);
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "not enough free space to create '%s'.\n"
|
|
|
|
"Please set your PKG_TMPDIR environment variable to a location\n"
|
|
|
|
"with more space and\ntry the command again", pen);
|
|
|
|
}
|
|
|
|
if (Current[0])
|
|
|
|
strcpy(Previous, Current);
|
|
|
|
else if (!getcwd(Previous, FILENAME_MAX)) {
|
|
|
|
cleanup(0);
|
|
|
|
err(1, "fatal error during execution: getcwd");
|
|
|
|
}
|
|
|
|
if (chdir(pen) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't chdir to '%s'", pen);
|
|
|
|
}
|
|
|
|
strcpy(Current, pen); CurrentSet = 1;
|
|
|
|
|
|
|
|
return Previous;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Convenience routine for getting out of playpen
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
void
|
|
|
|
leave_playpen(char *save)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
void (*oldsig) (int);
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/* Make us interruptable while we're cleaning up - just in case... */
|
|
|
|
oldsig = signal(SIGINT, SIG_DFL);
|
|
|
|
if (Previous[0] && chdir(Previous) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't chdir back to '%s'", Previous);
|
|
|
|
} else if (CurrentSet && Current[0] && strcmp(Current, Previous)) {
|
|
|
|
if (strcmp(Current, "/") == 0) {
|
|
|
|
fprintf(stderr, "PANIC: About to rm -rf / (not doing so, aborting)\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (vsystem("rm -rf %s", Current))
|
|
|
|
warnx("couldn't remove temporary dir '%s'", Current);
|
|
|
|
strcpy(Current, Previous);
|
|
|
|
}
|
|
|
|
if (save)
|
|
|
|
strcpy(Previous, save);
|
|
|
|
else
|
|
|
|
Previous[0] = '\0';
|
|
|
|
signal(SIGINT, oldsig);
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Return free disk space (in bytes) on given file system
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
off_t
|
|
|
|
min_free(char *tmpdir)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
struct statfs buf;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (statfs(tmpdir, &buf) != 0) {
|
|
|
|
warn("statfs");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (off_t) buf.f_bavail * (off_t) buf.f_bsize;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|