Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance: Add dedicated /etc/hosts manager #1810

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa8cccc
Add libcfg.la
yadij Nov 3, 2021
f3b0e44
Replace ConfigParser::CfgFile I/O implementation with Cfg::File
yadij Nov 4, 2021
3377bc3
Replace ConfigParser::strtokFile() I/O implementation with Cfg::File
yadij Nov 4, 2021
9c10697
Replace parseOneConfigFile() I/O implementation with Cfg::File
yadij Nov 4, 2021
48d8b24
Make SemaphoreCI old compiler happier with std::unique_ptr
yadij Nov 5, 2021
eeac0d9
Rename Cfg::File::tryLoadFile() per reviewer request
yadij Jan 20, 2022
9c06252
Revert optimization to fix dangling pointers
yadij Jan 20, 2022
8fcfb08
Drop stat::st_size usage per reviewer request
yadij Jan 20, 2022
175896e
Make File::nextLine() ignore file truncation
yadij Jan 20, 2022
52b7cd5
Update src/cfg/File.h
yadij Jan 20, 2022
355332a
Update src/cfg/File.h
yadij Jan 20, 2022
cfdecbd
Fix typos in documentation
yadij Jan 20, 2022
a9ac7f0
Remove pipe auto-detection per reviewer request.
yadij Jan 20, 2022
2fe941f
Remove documentation per reviewer request
yadij Jan 20, 2022
6672412
Move filePath processing to constructor per reviewer request
yadij Jan 20, 2022
7a284f0
Enable #line syntax in all loaded files, per reviewer request
yadij Jan 20, 2022
713d406
Update ConfigParser::CfgFile documentation
yadij Jan 21, 2022
a0bd6c7
Update doxygen for Cfg::File
yadij Jan 21, 2022
30cc912
Switch to existing namespace 'Configuration'
yadij Mar 2, 2022
7c88f43
Revert ConfigParser::destruct polish
yadij Mar 3, 2022
5e5f668
Reviewer requested change
yadij Mar 4, 2023
c1a3e5a
polish error message
yadij Mar 4, 2023
c795391
Update src/ConfigParser.cc
yadij Apr 30, 2024
6038816
FIx bootstrap after latest master changes
yadij May 7, 2024
f3f6c63
Reviewer requested change
yadij May 7, 2024
97252e7
Clarify CfgFile description using American
yadij May 7, 2024
8f03367
Add dedicated /etc/hosts manager
yadij May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,7 @@ AC_CONFIG_FILES([
src/base/Makefile
src/clients/Makefile
src/comm/Makefile
src/configuration/Makefile
src/debug/Makefile
src/dns/Makefile
src/DiskIO/Makefile
Expand Down
113 changes: 33 additions & 80 deletions src/ConfigParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
#include "fatal.h"
#include "globals.h"
#include "neighbors.h"
#include "parser/Tokenizer.h"
#include "sbuf/Stream.h"

#include <memory>

bool ConfigParser::RecognizeQuotedValues = true;
bool ConfigParser::StrictMode = true;
std::stack<ConfigParser::CfgFile *> ConfigParser::CfgFiles;
Expand All @@ -41,14 +44,12 @@ ConfigParser::destruct()
if (!CfgFiles.empty()) {
std::ostringstream message;
CfgFile *f = CfgFiles.top();
message << "Bungled " << f->filePath << " line " << f->lineNo <<
": " << f->currentLine << std::endl;
message << "Bungled " << f->lineInfo() << std::endl;
CfgFiles.pop();
delete f;
while (!CfgFiles.empty()) {
f = CfgFiles.top();
message << " included from " << f->filePath << " line " <<
f->lineNo << ": " << f->currentLine << std::endl;
message << " included from " << f->lineInfo() << std::endl;
CfgFiles.pop();
delete f;
}
Expand All @@ -68,7 +69,7 @@ ConfigParser::strtokFile()
return ConfigParser::NextToken();

static int fromFile = 0;
static FILE *wordFile = nullptr;
static std::unique_ptr<Configuration::File> wordFile;

char *t;
static char buf[CONFIG_LINE_LIMIT];
Expand All @@ -90,42 +91,25 @@ ConfigParser::strtokFile()

*t = '\0';

if ((wordFile = fopen(fn, "r")) == nullptr) {
debugs(3, DBG_CRITICAL, "ERROR: Can not open file " << fn << " for reading");
return nullptr;
}

#if _SQUID_WINDOWS_
setmode(fileno(wordFile), O_TEXT);
#endif

wordFile.reset(new Configuration::File(fn));
wordFile->load();
fromFile = 1;
} else {
return t;
}
}

/* fromFile */
if (fgets(buf, sizeof(buf), wordFile) == nullptr) {
static SBuf line;
line = wordFile->nextLine();
if (line.isEmpty()) {
/* stop reading from file */
fclose(wordFile);
wordFile = nullptr;
fromFile = 0;
return nullptr;
} else {
char *t2, *t3;
t = buf;
/* skip leading and trailing white space */
t += strspn(buf, w_space);
t2 = t + strcspn(t, w_space);
t3 = t2 + strspn(t2, w_space);

while (*t3 && *t3 != '#') {
t2 = t3 + strcspn(t3, w_space);
t3 = t2 + strspn(t2, w_space);
}

*t2 = '\0';
assert(line.length() < CONFIG_LINE_LIMIT);
t = const_cast<char *>(line.c_str());
}

/* skip comments */
Expand Down Expand Up @@ -341,7 +325,7 @@ ConfigParser::NextToken()
if (!token) {
assert(!wordfile->isOpen());
CfgFiles.pop();
debugs(3, 4, "CfgFiles.pop " << wordfile->filePath);
debugs(3, 4, "CfgFiles.pop " << wordfile->lineInfo());
delete wordfile;
}
}
Expand All @@ -361,6 +345,8 @@ ConfigParser::NextToken()
return nullptr;
}

assert(path); // a QuotedToken cannot be nil

// The next token in current cfg file line must be a ")"
char *end = NextToken();
ConfigParser::PreviewMode_ = savePreview;
Expand All @@ -376,14 +362,9 @@ ConfigParser::NextToken()
return nullptr;
}

ConfigParser::CfgFile *wordfile = new ConfigParser::CfgFile();
if (!path || !wordfile->startParse(path)) {
debugs(3, DBG_CRITICAL, "FATAL: Error opening config file: " << token);
delete wordfile;
self_destruct();
return nullptr;
}
CfgFiles.push(wordfile);
std::unique_ptr<ConfigParser::CfgFile> wordfile(new ConfigParser::CfgFile(path));
wordfile->startParse(); // throws on error
CfgFiles.push(wordfile.release());
token = nullptr;
}
} while (token == nullptr && !CfgFiles.empty());
Expand Down Expand Up @@ -608,54 +589,32 @@ ConfigParser::optionalAclList()
return acls;
}

bool
ConfigParser::CfgFile::startParse(char *path)
{
assert(wordFile == nullptr);
debugs(3, 3, "Parsing from " << path);
if ((wordFile = fopen(path, "r")) == nullptr) {
debugs(3, DBG_CRITICAL, "WARNING: file :" << path << " not found");
return false;
}

#if _SQUID_WINDOWS_
setmode(fileno(wordFile), O_TEXT);
#endif

filePath = path;
return getFileLine();
}

bool
ConfigParser::CfgFile::getFileLine()
void
ConfigParser::CfgFile::startParse()
{
// Else get the next line
if (fgets(parseBuffer, CONFIG_LINE_LIMIT, wordFile) == nullptr) {
/* stop reading from file */
fclose(wordFile);
wordFile = nullptr;
parseBuffer[0] = '\0';
return false;
}
parsePos = parseBuffer;
currentLine = parseBuffer;
lineNo++;
return true;
assert(isOpen());
confFileData.load();
debugs(3, 3, "Parsing from " << confFileData.lineInfo());
}

char *
ConfigParser::CfgFile::parse(ConfigParser::TokenType &type)
{
if (!wordFile)
return nullptr;

if (!*parseBuffer)
return nullptr;

char *token;
while (!(token = nextElement(type))) {
if (!getFileLine())
auto line = confFileData.nextLine();
if (line.isEmpty()) {
*parseBuffer = 0;
return nullptr;
}

assert(line.length() < CONFIG_LINE_LIMIT);

SBufToCstring(parseBuffer, line);
parsePos = parseBuffer;
}
return token;
}
Expand All @@ -671,9 +630,3 @@ ConfigParser::CfgFile::nextElement(ConfigParser::TokenType &type)
return token;
}

ConfigParser::CfgFile::~CfgFile()
{
if (wordFile)
fclose(wordFile);
}

42 changes: 15 additions & 27 deletions src/ConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "acl/forward.h"
#include "base/forward.h"
#include "configuration/File.h"
#include "sbuf/forward.h"
#include "SquidString.h"

Expand All @@ -22,16 +23,6 @@
class CachePeer;
class wordlist;

/**
* Limit to how long any given config line may be.
* This affects squid.conf and all included files.
*
* Behaviour when setting larger than 2KB is unknown.
* The config parser read mechanism can cope, but the other systems
* receiving the data from its buffers on such lines may not.
*/
#define CONFIG_LINE_LIMIT 2048

/**
* A configuration file Parser. Instances of this class track
* parsing state and perform tokenisation. Syntax is currently
Expand Down Expand Up @@ -161,22 +152,21 @@ class ConfigParser

protected:
/**
* Class used to store required information for the current
* configuration file.
* Interprets file content as a series of configuration tokens/elements instead of lines.
*/
class CfgFile
{
public:
CfgFile(): wordFile(nullptr), parsePos(nullptr), lineNo(0) { parseBuffer[0] = '\0';}
~CfgFile();
CfgFile(const char *path) : confFileData(path) { *parseBuffer = 0; }

/// True if the configuration file is open
bool isOpen() {return wordFile != nullptr;}
bool isOpen() const { return confFileData.isOpen(); }

/**
* Open the file given by 'path' and initializes the CfgFile object
* to start parsing
*/
bool startParse(char *path);
// filename and line number for debug display
SourceLocation lineInfo() const { return confFileData.lineInfo(); }

/// Initializes the CfgFile object to start parsing
void startParse();

/**
* Do the next parsing step:
Expand All @@ -187,19 +177,17 @@ class ConfigParser
char *parse(TokenType &type);

private:
bool getFileLine(); ///< Read the next line from the file
/**
* Return the body of the next element. If the wasQuoted is given
* set to true if the element was quoted.
*/
char *nextElement(TokenType &type);
FILE *wordFile; ///< Pointer to the file.

private:
Configuration::File confFileData; ///< actual file manager holding unparsed file content

char parseBuffer[CONFIG_LINE_LIMIT]; ///< Temporary buffer to store data to parse
const char *parsePos; ///< The next element position in parseBuffer string
public:
std::string filePath; ///< The file path
std::string currentLine; ///< The current line to parse
int lineNo; ///< Current line number
const char *parsePos = nullptr; ///< The next element position in parseBuffer string
};

/**
Expand Down
16 changes: 14 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LOADABLE_MODULES_SOURCES = \
LoadableModules.cc \
LoadableModules.h

SUBDIRS = mem time debug base anyp helper dns html ftp parser comm error eui acl format clients sbuf servers fs repl store DiskIO proxyp
SUBDIRS = mem time debug base configuration anyp helper dns html ftp parser comm error eui acl format clients sbuf servers fs repl store DiskIO proxyp

if ENABLE_AUTH
SUBDIRS += auth
Expand Down Expand Up @@ -521,6 +521,7 @@ squid_LDADD = \
mem/libmem.la \
store/libstore.la \
time/libtime.la \
configuration/libconfiguration.la \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
$(top_builddir)/lib/libmiscutil.la \
Expand Down Expand Up @@ -1150,6 +1151,7 @@ tests_testRock_LDADD = \
mem/libmem.la \
store/libstore.la \
$(ADAPTATION_LIBS) \
configuration/libconfiguration.la \
sbuf/libsbuf.la \
time/libtime.la \
$(top_builddir)/lib/libmisccontainers.la \
Expand Down Expand Up @@ -1327,6 +1329,7 @@ tests_testUfs_LDADD = \
mem/libmem.la \
store/libstore.la \
$(ADAPTATION_LIBS) \
configuration/libconfiguration.la \
sbuf/libsbuf.la \
time/libtime.la \
$(top_builddir)/lib/libmisccontainers.la \
Expand Down Expand Up @@ -1492,6 +1495,7 @@ tests_testStore_LDADD= \
sbuf/libsbuf.la \
DiskIO/libdiskio.la \
ipc/libipc.la \
configuration/libconfiguration.la \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
$(top_builddir)/lib/libmiscutil.la \
Expand Down Expand Up @@ -1668,6 +1672,7 @@ tests_testDiskIO_LDADD = \
dns/libdns.la \
base/libbase.la \
mem/libmem.la \
configuration/libconfiguration.la \
sbuf/libsbuf.la \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
Expand Down Expand Up @@ -1720,6 +1725,7 @@ tests_testACLMaxUserIP_LDADD = \
acl/libacls.la \
SquidConfig.o \
ip/libip.la \
configuration/libconfiguration.la \
parser/libparser.la \
sbuf/libsbuf.la \
base/libbase.la \
Expand Down Expand Up @@ -1973,6 +1979,7 @@ tests_test_http_range_LDADD = \
sbuf/libsbuf.la \
debug/libdebug.la \
store/libstore.la \
configuration/libconfiguration.la \
$(SNMP_LIBS) \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
Expand Down Expand Up @@ -2131,6 +2138,7 @@ tests_testHttpReply_LDADD=\
ip/libip.la \
base/libbase.la \
ipc/libipc.la \
configuration/libconfiguration.la \
sbuf/libsbuf.la \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
Expand Down Expand Up @@ -2363,6 +2371,7 @@ tests_testHttpRequest_LDADD = \
$(REPL_OBJS) \
$(ADAPTATION_LIBS) \
$(ESI_LIBS) \
configuration/libconfiguration.la \
$(top_builddir)/lib/libmisccontainers.la \
$(top_builddir)/lib/libmiscencoding.la \
$(top_builddir)/lib/libmiscutil.la \
Expand Down Expand Up @@ -2635,7 +2644,6 @@ tests_testCacheManager_LDADD = \
helper/libhelper.la \
http/libhttp.la \
proxyp/libproxyp.la \
parser/libparser.la \
ident/libident.la \
acl/libacls.la \
acl/libstate.la \
Expand All @@ -2660,6 +2668,8 @@ tests_testCacheManager_LDADD = \
$(SNMP_LIBS) \
mem/libmem.la \
store/libstore.la \
configuration/libconfiguration.la \
parser/libparser.la \
sbuf/libsbuf.la \
time/libtime.la \
debug/libdebug.la \
Expand Down Expand Up @@ -2736,6 +2746,8 @@ nodist_tests_testConfigParser_SOURCES = \
tests/stub_libmem.cc \
tests/stub_neighbors.cc
tests_testConfigParser_LDADD = \
configuration/libconfiguration.la \
parser/libparser.la \
base/libbase.la \
$(LIBCPPUNIT_LIBS) \
$(COMPAT_LIB) \
Expand Down
Loading
Loading