Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OT_API::Pid Class Reference

Public Member Functions

 Pid ()
 
 ~Pid ()
 
void OpenPid (const OTString &strPidFilePath)
 
void ClosePid ()
 
bool IsPidOpen () const
 

Detailed Description

Definition at line 385 of file OpenTransactions.cpp.

Constructor & Destructor Documentation

opentxs::OT_API::Pid::Pid ( )

Definition at line 399 of file OpenTransactions.cpp.

400  : m_bIsPidOpen(false)
401  , m_strPidFilePath("")
402 {
403 }
opentxs::OT_API::Pid::~Pid ( )

Definition at line 405 of file OpenTransactions.cpp.

406 {
407  // nothing for now
408 }

Member Function Documentation

void opentxs::OT_API::Pid::ClosePid ( )

Definition at line 513 of file OpenTransactions.cpp.

514 {
515  if (!IsPidOpen()) {
516  otErr
517  << __FUNCTION__
518  << ": strPidFilePath is CLOSED, WHY CLOSE A PID IF NONE IS OPEN!\n";
519  OT_FAIL;
520  }
521  if (!m_strPidFilePath.Exists()) {
522  otErr << __FUNCTION__ << ": m_strPidFilePath is Empty!\n";
523  OT_FAIL;
524  }
525 
526  // PID -- Set it to 0 in the lock file so the next time we run OT, it knows
527  // there isn't
528  // another copy already running (otherwise we might wind up with two copies
529  // trying to write
530  // to the same data folder simultaneously, which could corrupt the data...)
531  //
532 
533  std::ofstream pid_outfile(m_strPidFilePath.Get());
534 
535  if (pid_outfile.is_open()) {
536  uint32_t the_pid = 0;
537  pid_outfile << the_pid;
538  pid_outfile.close();
539  m_bIsPidOpen = false;
540  }
541  else {
542  otErr << "Failed trying to open data locking file (to wipe PID "
543  "back to 0): " << m_strPidFilePath << "\n";
544  m_bIsPidOpen = true;
545  }
546 }
EXPORT bool Exists() const
Definition: OTString.cpp:1035
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OT_API::Pid::IsPidOpen ( ) const

Definition at line 548 of file OpenTransactions.cpp.

549 {
550  return m_bIsPidOpen;
551 }
void opentxs::OT_API::Pid::OpenPid ( const OTString strPidFilePath)

Definition at line 410 of file OpenTransactions.cpp.

411 {
412  if (IsPidOpen()) {
413  otErr << __FUNCTION__ << ": strPidFilePath is OPEN, MUST CLOSE BEFORE "
414  "OPENING A NEW ONE!\n";
415  OT_FAIL;
416  }
417 
418  if (!strPidFilePath.Exists()) {
419  otErr << __FUNCTION__ << ": strPidFilePath is Empty!\n";
420  OT_FAIL;
421  }
422  if (3 > strPidFilePath.GetLength()) {
423  otErr << __FUNCTION__ << ": strPidFilePath is Too Short! ("
424  << strPidFilePath << ")\n";
425  OT_FAIL;
426  }
427 
428  otWarn << __FUNCTION__ << ": Using Pid File: " << strPidFilePath << "\n";
429  m_strPidFilePath = strPidFilePath;
430 
431  {
432  // 1. READ A FILE STORING THE PID. (It will already exist, if OT is
433  // already running.)
434  //
435  // We open it for reading first, to see if it already exists. If it
436  // does,
437  // we read the number. 0 is fine, since we overwrite with 0 on shutdown.
438  // But
439  // any OTHER number means OT is still running. Or it means it was killed
440  // while
441  // running and didn't shut down properly, and that you need to delete
442  // the pid file
443  // by hand before running OT again. (This is all for the purpose of
444  // preventing two
445  // copies of OT running at the same time and corrupting the data
446  // folder.)
447  //
448  std::ifstream pid_infile(m_strPidFilePath.Get());
449 
450  // 2. (IF FILE EXISTS WITH ANY PID INSIDE, THEN DIE.)
451  //
452 
453  if (pid_infile.is_open()) // it existed already
454  {
455  uint32_t old_pid = 0;
456  pid_infile >> old_pid;
457  pid_infile.close();
458 
459  // There was a real PID in there.
460  if (old_pid != 0) {
461  const uint64_t lPID = old_pid;
462  otErr
463  << "\n\n\nIS OPEN-TRANSACTIONS ALREADY RUNNING?\n\n"
464  "I found a PID (" << lPID
465  << ") in the data lock file, located at: "
466  << m_strPidFilePath << "\n\n"
467  "If the OT process with PID " << lPID
468  << " is truly not running "
469  "anymore, "
470  "then just erase that file and restart.\nThis is "
471  "normally "
472  "cleaned "
473  "up during AppCleanup / AppShutdown. (Or should be.)\n";
474 #ifndef ANDROID
475  m_bIsPidOpen = false;
476  return;
477 #endif
478  }
479  // Otherwise, though the file existed, the PID within was 0.
480  // (Meaning the previous instance of OT already set it to 0 as it
481  // was shutting down.)
482  }
483  // Next let's record our PID to the same file, so other copies of OT
484  // can't trample on US.
485 
486  // 3. GET THE CURRENT (ACTUAL) PROCESS ID.
487  //
488  uint64_t the_pid = 0;
489 
490 #ifdef _WIN32
491  the_pid = GetCurrentProcessId();
492 #else
493  the_pid = getpid();
494 #endif
495 
496  // 4. OPEN THE FILE IN WRITE MODE, AND SAVE THE PID TO IT.
497  //
498  std::ofstream pid_outfile(m_strPidFilePath.Get());
499 
500  if (pid_outfile.is_open()) {
501  pid_outfile << the_pid;
502  pid_outfile.close();
503  m_bIsPidOpen = true;
504  }
505  else {
506  otErr << "Failed trying to open data locking file (to store "
507  "PID " << the_pid << "): " << m_strPidFilePath << "\n";
508  m_bIsPidOpen = false;
509  }
510  }
511 }
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otWarn
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr

The documentation for this class was generated from the following file: