pen.c revision 37900
1#ifndef lint
2static const char rcsid[] =
3	"$Id: pen.c,v 1.28 1998/04/12 16:01:10 jkh 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), errx(2, "can't chdir to '%s'", pen);
134
135    if (PenLocation[0])
136	pushPen(PenLocation);
137
138    strcpy(PenLocation, pen);
139    return Previous;
140}
141
142/* Convenience routine for getting out of playpen */
143void
144leave_playpen()
145{
146    void (*oldsig)(int);
147
148    /* Don't interrupt while we're cleaning up */
149    oldsig = signal(SIGINT, SIG_IGN);
150    if (Previous[0]) {
151	if (chdir(Previous) == FAIL)
152	    cleanup(0), errx(2, "can't chdir back to '%s'", Previous);
153	Previous[0] = '\0';
154    }
155    if (PenLocation[0]) {
156	if (PenLocation[0] == '/' && vsystem("rm -rf %s", PenLocation))
157	    warnx("couldn't remove temporary dir '%s'", PenLocation);
158	popPen(PenLocation);
159    }
160    signal(SIGINT, oldsig);
161}
162
163off_t
164min_free(char *tmpdir)
165{
166    struct statfs buf;
167
168    if (statfs(tmpdir, &buf) != 0) {
169	warn("statfs");
170	return -1;
171    }
172    return (off_t)buf.f_bavail * (off_t)buf.f_bsize;
173}
174