/*
- * $Id: main.cc,v 1.202 1998/01/05 20:49:11 wessels Exp $
+ * $Id: main.cc,v 1.203 1998/01/06 07:11:53 wessels Exp $
*
* DEBUG: section 1 Startup and Main Loop
* AUTHOR: Harvest Derived
static void sendSignal(void);
static void serverConnectionsOpen(void);
static void watch_child(char **);
+static void setEffectiveUser(void);
+
static void
usage(void)
{
" -s Enable logging to syslog.\n"
" -u port Specify ICP port number (default: %d), disable with 0.\n"
" -v Print version.\n"
- " -z Zap disk storage -- deletes all objects in disk cache.\n"
+ " -z Create swap directories\n"
" -C Do not catch fatal signals.\n"
" -D Disable initial DNS tests.\n"
" -F Foreground fast store rebuild.\n"
exit(0);
/* NOTREACHED */
case 'z':
- opt_zap_disk_store = 1;
+ opt_create_swap_dirs = 1;
break;
case '?':
default:
debug(1, 0) ("Ready to serve requests.\n");
}
+static void
+setEffectiveUser(void)
+{
+ leave_suid(); /* Run as non privilegied user */
+ if (geteuid() == 0) {
+ debug(0, 0) ("Squid is not safe to run as root! If you must\n");
+ debug(0, 0) ("start Squid as root, then you must configure\n");
+ debug(0, 0) ("it to run as a non-priveledged user with the\n");
+ debug(0, 0) ("'cache_effective_user' option in the config file.\n");
+ fatal("Don't run Squid as root, set 'cache_effective_user'!");
+ }
+}
+
static void
mainInitialize(void)
{
ConfigFile = xstrdup(DefaultConfigFile);
parseConfigFile(ConfigFile);
- leave_suid(); /* Run as non privilegied user */
- if (geteuid() == 0) {
- debug(0, 0) ("Squid is not safe to run as root! If you must\n");
- debug(0, 0) ("start Squid as root, then you must configure\n");
- debug(0, 0) ("it to run as a non-priveledged user with the\n");
- debug(0, 0) ("'cache_effective_user' option in the config file.\n");
- fatal("Don't run Squid as root, set 'cache_effective_user'!");
- }
+ setEffectiveUser();
assert(Config.Port.http);
if (httpPortNumOverride != 1)
Config.Port.http->i = (u_short) httpPortNumOverride;
sendSignal();
/* NOTREACHED */
}
+ if (opt_create_swap_dirs) {
+ if (ConfigFile == NULL)
+ ConfigFile = xstrdup(DefaultConfigFile);
+ cbdataInit();
+ parseConfigFile(ConfigFile);
+ setEffectiveUser();
+ debug(0, 0)("Creating Swap Directories\n");
+ storeCreateSwapDirectories();
+ return 0;
+ }
if (!opt_no_daemon)
watch_child(argv);
setMaxFD();
/*
- * $Id: store.cc,v 1.363 1998/01/06 00:42:12 wessels Exp $
+ * $Id: store.cc,v 1.364 1998/01/06 07:11:55 wessels Exp $
*
* DEBUG: section 20 Storeage Manager
* AUTHOR: Harvest Derived
storelog_fd = file_open(fname, O_WRONLY | O_CREAT, NULL, NULL);
if (storelog_fd < 0)
debug(20, 1) ("Store logging disabled\n");
- storeVerifySwapDirs();
+ if (storeVerifyCacheDirs() < 0) {
+ xstrncpy(tmp_error_buf,
+ "\tFailed to verify one of the swap directories, Check cache.log\n"
+ "\tfor details. Run 'squid -z' to create swap directories\n"
+ "\tif needed, or if running Squid for the first time.",
+ ERROR_BUF_SZ);
+ fatal(tmp_error_buf);
+ }
storeDirOpenSwapLogs();
- if (!opt_zap_disk_store)
- storeStartRebuildFromDisk();
- else
- store_rebuilding = 0;
+ storeStartRebuildFromDisk();
all_list.head = all_list.tail = NULL;
inmem_list.head = inmem_list.tail = NULL;
}
/*
- * $Id: store_dir.cc,v 1.38 1997/11/14 04:55:09 wessels Exp $
+ * $Id: store_dir.cc,v 1.39 1998/01/06 07:11:56 wessels Exp $
*
* DEBUG: section 47 Store Directory Routines
* AUTHOR: Duane Wessels
#define DefaultLevelOneDirs 16
#define DefaultLevelTwoDirs 256
+static char *storeSwapSubDir(int dirn, int subdirn);
+static int storeMostFreeSwapDir(void);
+static int storeVerifyDirectory(const char *path);
+static void storeCreateDirectory(const char *path, int lvl);
+static void storeCreateSwapSubDirs(int j);
+
/* return full name to swapfile */
char *
storeSwapFullPath(int fn, char *fullpath)
return fullpath;
}
-/* return full name to swapfile */
+static char *
+storeSwapSubDir(int dirn, int subdirn)
+{
+ LOCAL_ARRAY(char, fullfilename, SQUID_MAXPATHLEN);
+ SwapDir *SD;
+ assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
+ SD = &Config.cacheSwap.swapDirs[dirn];
+ assert(0 <= subdirn && subdirn < SD->l1);
+ snprintf(fullfilename, SQUID_MAXPATHLEN, "%s/%02X",
+ Config.cacheSwap.swapDirs[dirn].path,
+ subdirn);
+ return fullfilename;
+}
+
char *
storeSwapSubSubDir(int fn, char *fullpath)
{
return fullpath;
}
+static void
+storeCreateDirectory(const char *path, int lvl)
+{
+ struct stat st;
+ if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+ debug(20, lvl) ("%s exists\n", path);
+ } else if (mkdir(path, 0755) == 0) {
+ debug(20, lvl) ("%s created\n", path);
+ } else if (errno == EEXIST) {
+ debug(20, lvl) ("%s exists\n", path);
+ } else {
+ snprintf(tmp_error_buf, ERROR_BUF_SZ,
+ "Failed to make swap directory %s: %s",
+ path, xstrerror());
+ fatal(tmp_error_buf);
+ }
+}
+
static int
-storeVerifyOrCreateDir(const char *path)
+storeVerifyDirectory(const char *path)
{
struct stat sb;
- if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
- debug(47, 3) ("%s exists\n", path);
- return 0;
+ if (stat(path, &sb) < 0) {
+ debug(20, 0) ("%s: %s\n", path, xstrerror());
+ return -1;
}
- safeunlink(path, 1);
- if (mkdir(path, 0777) < 0) {
- if (errno != EEXIST) {
- snprintf(tmp_error_buf, ERROR_BUF_SZ,
- "Failed to create swap directory %s: %s",
- path,
- xstrerror());
- fatal(tmp_error_buf);
- }
+ if (S_ISDIR(sb.st_mode) == 0) {
+ debug(20, 0) ("%s is not a directory\n", path);
+ return -1;
}
- debug(47, 1) ("Created directory %s\n", path);
- if (stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) {
- snprintf(tmp_error_buf, ERROR_BUF_SZ,
- "Failed to create directory %s: %s", path, xstrerror());
- fatal(tmp_error_buf);
- }
- return 1;
+ return 0;
}
+/*
+ * This function is called by storeInit(). If this returns < 0,
+ * then Squid exits, complains about swap directories not
+ * existing, and instructs the admin to run 'squid -z'
+ */
int
-storeVerifySwapDirs(void)
+storeVerifyCacheDirs(void)
+{
+ int i;
+ int j;
+ const char *path;
+ for (i = 0; i < Config.cacheSwap.n_configured; i++) {
+ path = Config.cacheSwap.swapDirs[i].path;
+ if (storeVerifyDirectory(path) < 0)
+ return -1;
+ for (j = 0; j < Config.cacheSwap.swapDirs[i].l1; j++) {
+ path = storeSwapSubDir(i, j);
+ if (storeVerifyDirectory(path) < 0)
+ return -1;
+ }
+ }
+ return 0;
+}
+
+void
+storeCreateSwapDirectories(void)
{
int i;
const char *path = NULL;
- int directory_created = 0;
for (i = 0; i < Config.cacheSwap.n_configured; i++) {
path = Config.cacheSwap.swapDirs[i].path;
- debug(47, 3) ("storeVerifySwapDirs: Creating swap space in %s\n", path);
- storeVerifyOrCreateDir(path);
+ debug(47, 3) ("Creating swap space in %s\n", path);
+ storeCreateDirectory(path, 0);
storeCreateSwapSubDirs(i);
}
- return directory_created;
}
-void
+static void
storeCreateSwapSubDirs(int j)
{
int i, k;
LOCAL_ARRAY(char, name, MAXPATHLEN);
for (i = 0; i < SD->l1; i++) {
snprintf(name, MAXPATHLEN, "%s/%02X", SD->path, i);
- if (storeVerifyOrCreateDir(name) == 0)
- continue;
+ storeCreateDirectory(name, 0);
debug(47, 1) ("Making directories in %s\n", name);
for (k = 0; k < SD->l2; k++) {
snprintf(name, MAXPATHLEN, "%s/%02X/%02X", SD->path, i, k);
- storeVerifyOrCreateDir(name);
+ storeCreateDirectory(name, 2);
}
}
}
-int
+static int
storeMostFreeSwapDir(void)
{
double least_used = 1.0;