1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
// fgfilelist.cc
#include "fgfilelist.h"
#ifndef _FGFILEGRAB_H
#include "fgfilegrab.h"
#endif
#ifndef __SGI_STL_VECTOR_H
#include <vector.h>
#endif
#ifndef _FGSTRING_H
#define "fgstring.h"
#endif
#ifndef _FGLOGGER_H
#include "fglogger.h"
#endif
#ifndef _FGEXC_H
#include "fgexc.h"
#endif
#ifndef _FGGLOB_H
#include "fgglob.h"
#endif
#include <stdio.h>
#include <string.h>
#include <assert.h>
// Threading. Cool.
#include <pthread.h>
// Class scope variables
int FGFileList::msThreadsActive = 0;
struct FGFileList::Internal_FGFileList {
// The individual rules one by one
vector<FGFileGrab> mFiles;
// Threading synchronization object
// Children flag this when they exit to alert
// master process to spawn more
static pthread_cond_t msChildExitCondition;
// Mutex to go with the above
static pthread_mutex_t msChildExitMutex;
// Mutex to ensure no children run before the parent
// has acknowledged the broadcast
static pthread_mutex_t msBroadcastSerializer;
};
pthread_cond_t FGFileList::Internal_FGFileList::msChildExitCondition;
pthread_mutex_t FGFileList::Internal_FGFileList::msChildExitMutex;
pthread_mutex_t FGFileList::Internal_FGFileList::msBroadcastSerializer;
FGFileList::FGFileList()
{
// Allocate the implementation
mpInternals = new Internal_FGFileList;
// Set up the threading objects
pthread_cond_init(&Internal_FGFileList::msChildExitCondition, NULL);
pthread_mutex_init(&Internal_FGFileList::msChildExitMutex, NULL);
pthread_mutex_init(&Internal_FGFileList::msBroadcastSerializer, NULL);
}
FGFileList::~FGFileList()
{
if (mpInternals) {
// Free allocated resources of each FGFileGrab object
vector<FGFileGrab>::iterator iFiles;
for (iFiles = mpInternals->mFiles.begin();
iFiles != mpInternals->mFiles.end(); iFiles++) {
iFiles->FreeResources();
}
pthread_mutex_destroy(&Internal_FGFileList::msChildExitMutex);
pthread_mutex_destroy(&Internal_FGFileList::msBroadcastSerializer);
pthread_cond_destroy(&Internal_FGFileList::msChildExitCondition);
delete mpInternals;
}
// Bug if we've still got threads hanging around
assert(msThreadsActive == 0);
}
FGString
FGFileList::GetLineString(void) const
{
FGString ret("at line ");
char intBuf[10];
sprintf(intBuf, "%d", mLine);
ret += intBuf;
return ret;
}
void
FGFileList::LoadConfig(const FGString& configFile)
{
FILE* pFile = fopen(configFile, "r");
if (pFile == NULL) {
FGString details("`");
details += configFile;
details += '\'';
throw FGException(FGException::kConfigFileNotFound, details);
}
// Catch then rethrow exceptions so we can clean up and close file
try {
// Temp. strings as we build up a file entry
FGString ruleName;
FGString host;
FGString remDir;
FGString localDir;
FGString file;
// This indicates how many bits we have to go before we have a
// complete file entry
int componentsToFind = 5;
mLine = 0;
char buf[1024];
while (fgets(buf, sizeof(buf), pFile) != NULL) {
mLine++;
// Nail carrige returns into end of lines
char* pCR = strchr(buf, '\n');
if (pCR != NULL) {
*pCR = '\0';
}
// Skip whitespace
char* pPos = buf;
while (*pPos == ' ' || *pPos == '\t') {
pPos++;
}
// 09-Dec-99 - Also ignore whitespace at end of line!
char* pStart = pPos;
while (*pStart != '\0')
{
pStart++;
}
while (pStart > pPos)
{
pStart--;
if (*pStart == ' ' || *pStart == '\t')
{
*pStart = '\0';
}
else
{
break;
}
}
// Ignore comments or blank lines
if (*pPos == '#' || *pPos == '\0') {
continue;
}
// First ":" is separator between token and value
char* pSep = strchr(pPos, ':');
if (pSep == NULL) {
// Must have a :
FGString details = GetLineString();
throw FGException(FGException::kConfigMissingColon, details);
}
*pSep = '\0';
pSep++;
while (*pSep == ' ' || *pSep == '\t') {
pSep++;
}
if (!strcmp(pPos, "RemoteDir")) {
if (!remDir.IsEmpty()) {
FGString details("RemoteDir, ");
details += GetLineString();
throw FGException(FGException::kDuplicateToken, details);
}
remDir = pSep;
componentsToFind--;
} else if (!strcmp(pPos, "LocalDir")) {
if (!localDir.IsEmpty()) {
FGString details("LocalDir, ");
details += GetLineString();
throw FGException(FGException::kDuplicateToken, details);
}
localDir = pSep;
componentsToFind--;
} else if (!strcmp(pPos, "Name")) {
if (!ruleName.IsEmpty()) {
FGString details("Name, ");
details += GetLineString();
throw FGException(FGException::kDuplicateToken, details);
}
ruleName = pSep;
componentsToFind--;
} else if (!strcmp(pPos, "Host")) {
if (!host.IsEmpty()) {
FGString details("Host, ");
details += GetLineString();
throw FGException(FGException::kDuplicateToken, details);
}
host = pSep;
componentsToFind--;
} else if (!strcmp(pPos, "File")) {
if (!file.IsEmpty()) {
FGString details("File, ");
details += GetLineString();
throw FGException(FGException::kDuplicateToken, details);
}
file = pSep;
componentsToFind--;
} else {
// Unknown tag
FGString details(pPos);
details += ", ";
details += GetLineString();
}
if (!componentsToFind) {
// Oooh ready to make a FileGrab
FGFileGrab theGrab(ruleName, host, remDir, localDir, file);
mpInternals->mFiles.push_back(theGrab);
componentsToFind = 5;
ruleName.MakeEmpty();
host.MakeEmpty();
file.MakeEmpty();
localDir.MakeEmpty();
remDir.MakeEmpty();
}
} // end: while(more lines)
// Ended halfway through a rule?
if (componentsToFind != 5) {
throw FGException(FGException::kUnexpectedEndOfConfig);
}
} // end try() block
catch (FGException&) {
fclose(pFile);
throw;
}
fclose(pFile);
}
// This function spawns lots of threads to attempt to complete several
// rules simultaneously. The semantics are this: This call does not
// complete until all spawned threads complete. At any given time the
// number of threads active is limited to a maximum value
void
FGFileList::GetAllFiles(void) const
{
assert(msThreadsActive == 0);
// Grab the mutex for child exit notification
int lockRet = pthread_mutex_trylock(&Internal_FGFileList::msChildExitMutex);
assert(lockRet == 0);
vector<FGFileGrab>::const_iterator iFiles;
for (iFiles = mpInternals->mFiles.begin(); iFiles != mpInternals->mFiles.end();
iFiles++) {
// If we are operating with max. children, must wait for some to finish
assert(msThreadsActive >=0 && msThreadsActive <= FGGlob::gMaxThreads);
if (msThreadsActive == FGGlob::gMaxThreads) {
// Wait for a single thread to complete
pthread_cond_wait(&Internal_FGFileList::msChildExitCondition,
&Internal_FGFileList::msChildExitMutex);
msThreadsActive--;
pthread_mutex_unlock(&Internal_FGFileList::msBroadcastSerializer);
}
// OK so we are ready to launch a thread
// We never sync on a thread's exit so make them detached
pthread_attr_t threadAttr;
pthread_attr_init(&threadAttr);
int detachState = PTHREAD_CREATE_DETACHED;
pthread_attr_setdetachstate(&threadAttr, detachState);
pthread_t newThread;
void* pArg = (void*)(&(*iFiles));
// And they're off..
msThreadsActive++;
pthread_create(&newThread, &threadAttr,
&ThreadStartPoint, pArg);
pthread_attr_destroy(&threadAttr);
}
// OK so all grabs are scheduled. We might have some still active
// child threads (in fact that's almost certain). Hence we must
// wait for them to finish
while (msThreadsActive > 0) {
pthread_cond_wait(&Internal_FGFileList::msChildExitCondition,
&Internal_FGFileList::msChildExitMutex);
msThreadsActive--;
pthread_mutex_unlock(&Internal_FGFileList::msBroadcastSerializer);
}
}
// ALERT: When this function executes, it is in the context of the
// child thread.
void*
ThreadStartPoint(void* pArg)
{
// Cast the arg, which is actually a pointer to the FGFileGrab object
FGFileGrab* pGrab = (FGFileGrab*)pArg;
FGString vmsg("About to run rule \"");
vmsg += pGrab->GetRuleName();
vmsg += '"';
FGLogger::GetLogger().LogMsg(vmsg, FGLogger::kFGLLVerbose);
try {
pGrab->GrabIt();
}
catch (FGException& e) {
FGString msg("Failed to complete rule \"");
msg += pGrab->GetRuleName();
msg += "\", reason follows";
// To guarantee reason is line _immediately_ after "reason follows"
FGLogger::Lock();
FGLogger::GetLogger().LogMsg(msg, FGLogger::kFGLLWarn, true);
FGLogger::GetLogger().LogException(e, true);
FGLogger::Unlock();
}
// Prevent other threads broadcasting before parent has seen
// the broadcast
pthread_mutex_lock(&FGFileList::Internal_FGFileList::msBroadcastSerializer);
// Thread about to die; signal master parent thread
// Only signal when parent is waiting for it (parent unlocks mutex when
// it starts to wait for children to exit)
pthread_mutex_lock(&FGFileList::Internal_FGFileList::msChildExitMutex);
pthread_cond_broadcast(&FGFileList::Internal_FGFileList::msChildExitCondition);
// Parent is trying to lock the mutex now - so it is blocked until we
// unlock the mutex
pthread_mutex_unlock(&FGFileList::Internal_FGFileList::msChildExitMutex);
// However! There is a race condition. When we unlock, other children
// are competing with the parent for the processor. If another child
// runs first it will broadcast the condition before the parent has
// acknowledged it and a broadcast will be lost
// Hence the broadcast serializing mutex
}
|