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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAgent - PostgreSQL Tools
//
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// unix.cpp - pgAgent unix specific functions
//
//////////////////////////////////////////////////////////////////////////
#include "pgAgent.h"
// *nix only!!
#ifndef WIN32
#include <wx/filename.h>
#include <wx/ffile.h>
#include <fcntl.h>
void usage(const wxString &executable)
{
wxFileName *fn = new wxFileName(executable);
wxPrintf(_("Usage:\n"));
wxPrintf(fn->GetName() + _(" [options] <connect-string>\n"));
wxPrintf(_("options:\n"));
wxPrintf(_("-f run in the foreground (do not detach from the terminal)\n"));
wxPrintf(_("-t <poll time interval in seconds (default 10)>\n"));
wxPrintf(_("-r <retry period after connection abort in seconds (>=10, default 30)>\n"));
wxPrintf(_("-s <log file (messages are logged to STDOUT if not specified>\n"));
wxPrintf(_("-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"));
}
void LogMessage(wxString msg, int level)
{
wxFFile file;
if (logFile.IsEmpty())
{
file.Attach(stdout);
}
else
{
file.Open(logFile.c_str(), wxT("a"));
}
if (!file.IsOpened())
{
wxFprintf(stderr, _("Can not open the logfile!"));
return;
}
switch (level)
{
case LOG_DEBUG:
if (minLogLevel >= LOG_DEBUG)
file.Write(_("DEBUG: ") + msg + wxT("\n"));
break;
case LOG_WARNING:
if (minLogLevel >= LOG_WARNING)
file.Write(_("WARNING: ") + msg + wxT("\n"));
break;
case LOG_ERROR:
file.Write(_("ERROR: ") + msg + wxT("\n"));
exit(1);
break;
case LOG_STARTUP:
file.Write(_("WARNING: ") + msg + wxT("\n"));
break;
}
if (logFile.IsEmpty())
{
file.Detach();
}
else
{
file.Close();
}
}
// Shamelessly lifted from pg_autovacuum...
static void daemonize(void)
{
pid_t pid;
pid = fork();
if (pid == (pid_t) - 1)
{
LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
exit(1);
}
else if (pid)
exit(0);
#ifdef HAVE_SETSID
if (setsid() < 0)
{
LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
exit(1);
}
#endif
}
int main(int argc, char **argv)
{
// Statup wx
wxInitialize();
wxString executable;
executable = wxString::FromAscii(argv[0]);
if (argc < 2)
{
usage(executable);
return 1;
}
argc--;
argv++;
setOptions(argc, argv, executable);
if (!runInForeground)
daemonize();
MainLoop();
return 0;
}
#endif // !WIN32
|