~ubuntu-branches/ubuntu/trusty/pgagent/trusty

« back to all changes in this revision

Viewing changes to unix.cpp

  • Committer: Package Import Robot
  • Author(s): Gerfried Fuchs
  • Date: 2012-04-30 20:04:50 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120430200450-y05reghn6ces1kd8
Tags: 3.2.1-1
* New upstream release.
* Relicense packaging under WTFPLv2.
* Add recommended targets build-{arch,indep} to debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//////////////////////////////////////////////////////////////////////////
2
2
//
3
3
// pgAgent - PostgreSQL Tools
4
 
// $Id$
5
 
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
6
 
// This software is released under the BSD Licence
 
4
//
 
5
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
 
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// unix.cpp - pgAgent unix specific functions
9
9
//
20
20
 
21
21
void usage(const wxString &executable)
22
22
{
23
 
    wxFileName *fn = new wxFileName(executable);
 
23
        wxFileName *fn = new wxFileName(executable);
24
24
 
25
 
    wxPrintf(_("Usage:\n"));
26
 
    wxPrintf(fn->GetName() + _(" [options] <connect-string>\n"));
27
 
    wxPrintf(_("options:\n"));
28
 
    wxPrintf(_("-f run in the foreground (do not detach from the terminal)\n"));
29
 
    wxPrintf(_("-t <poll time interval in seconds (default 10)>\n"));
30
 
    wxPrintf(_("-r <retry period after connection abort in seconds (>=10, default 30)>\n"));
31
 
    wxPrintf(_("-s <log file (messages are logged to STDOUT if not specified>\n"));
32
 
    wxPrintf(_("-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"));
 
25
        wxPrintf(_("Usage:\n"));
 
26
        wxPrintf(fn->GetName() + _(" [options] <connect-string>\n"));
 
27
        wxPrintf(_("options:\n"));
 
28
        wxPrintf(_("-f run in the foreground (do not detach from the terminal)\n"));
 
29
        wxPrintf(_("-t <poll time interval in seconds (default 10)>\n"));
 
30
        wxPrintf(_("-r <retry period after connection abort in seconds (>=10, default 30)>\n"));
 
31
        wxPrintf(_("-s <log file (messages are logged to STDOUT if not specified>\n"));
 
32
        wxPrintf(_("-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"));
33
33
}
34
34
 
35
35
void LogMessage(wxString msg, int level)
36
36
{
37
 
    wxFFile file;
38
 
    if (logFile.IsEmpty()) 
39
 
    {
40
 
        file.Attach(stdout);
41
 
    }
42
 
    else 
43
 
    {
44
 
        file.Open(logFile.c_str(), wxT("a"));
45
 
    }
46
 
 
47
 
    if (!file.IsOpened()) 
48
 
    {
49
 
        wxFprintf(stderr, _("Can not open the logfile!"));
50
 
        return;
51
 
    }
52
 
 
53
 
    switch (level)
54
 
    {
55
 
    case LOG_DEBUG:
56
 
        if (minLogLevel >= LOG_DEBUG)
57
 
            file.Write(_("DEBUG: ") + msg + wxT("\n"));
58
 
        break;
59
 
    case LOG_WARNING:
60
 
        if (minLogLevel >= LOG_WARNING)
61
 
            file.Write(_("WARNING: ") + msg + wxT("\n"));
62
 
        break;
63
 
    case LOG_ERROR:
64
 
        file.Write(_("ERROR: ") + msg + wxT("\n"));
65
 
        exit(1);
66
 
        break;
67
 
    case LOG_STARTUP:
68
 
        file.Write(_("WARNING: ") + msg + wxT("\n"));
69
 
        break;
70
 
    }
71
 
 
72
 
    if (logFile.IsEmpty())
73
 
    {
74
 
        file.Detach();
75
 
    }
76
 
    else
77
 
    {
78
 
        file.Close();
79
 
    }
 
37
        wxFFile file;
 
38
        if (logFile.IsEmpty())
 
39
        {
 
40
                file.Attach(stdout);
 
41
        }
 
42
        else
 
43
        {
 
44
                file.Open(logFile.c_str(), wxT("a"));
 
45
        }
 
46
 
 
47
        if (!file.IsOpened())
 
48
        {
 
49
                wxFprintf(stderr, _("Can not open the logfile!"));
 
50
                return;
 
51
        }
 
52
 
 
53
        switch (level)
 
54
        {
 
55
                case LOG_DEBUG:
 
56
                        if (minLogLevel >= LOG_DEBUG)
 
57
                                file.Write(_("DEBUG: ") + msg + wxT("\n"));
 
58
                        break;
 
59
                case LOG_WARNING:
 
60
                        if (minLogLevel >= LOG_WARNING)
 
61
                                file.Write(_("WARNING: ") + msg + wxT("\n"));
 
62
                        break;
 
63
                case LOG_ERROR:
 
64
                        file.Write(_("ERROR: ") + msg + wxT("\n"));
 
65
                        exit(1);
 
66
                        break;
 
67
                case LOG_STARTUP:
 
68
                        file.Write(_("WARNING: ") + msg + wxT("\n"));
 
69
                        break;
 
70
        }
 
71
 
 
72
        if (logFile.IsEmpty())
 
73
        {
 
74
                file.Detach();
 
75
        }
 
76
        else
 
77
        {
 
78
                file.Close();
 
79
        }
80
80
}
81
81
 
82
82
// Shamelessly lifted from pg_autovacuum...
83
83
static void daemonize(void)
84
84
{
85
 
    pid_t pid;
 
85
        pid_t pid;
86
86
 
87
 
    pid = fork();
88
 
    if (pid == (pid_t) -1)
89
 
    {
90
 
        LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
91
 
        exit(1);
92
 
    }
93
 
    else if (pid)
94
 
        exit(0);
 
87
        pid = fork();
 
88
        if (pid == (pid_t) - 1)
 
89
        {
 
90
                LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
 
91
                exit(1);
 
92
        }
 
93
        else if (pid)
 
94
                exit(0);
95
95
 
96
96
#ifdef HAVE_SETSID
97
 
    if (setsid() < 0)
98
 
    {
99
 
        LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
100
 
        exit(1);
101
 
    }
 
97
        if (setsid() < 0)
 
98
        {
 
99
                LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
 
100
                exit(1);
 
101
        }
102
102
#endif
103
103
 
104
104
}
105
105
 
106
106
int main(int argc, char **argv)
107
107
{
108
 
    // Statup wx
109
 
    wxInitialize();
110
 
 
111
 
    wxString executable;
112
 
    executable = wxString::FromAscii(argv[0]);
113
 
 
114
 
    if (argc < 2)
115
 
    {
116
 
        usage(executable);
117
 
        return 1;
118
 
    }
119
 
 
120
 
    argc--;
121
 
    argv++;
122
 
 
123
 
    setOptions(argc, argv, executable);
124
 
 
125
 
    if (!runInForeground)
126
 
        daemonize();
127
 
 
128
 
    MainLoop();
129
 
 
130
 
    return 0;
 
108
        // Statup wx
 
109
        wxInitialize();
 
110
 
 
111
        wxString executable;
 
112
        executable = wxString::FromAscii(argv[0]);
 
113
 
 
114
        if (argc < 2)
 
115
        {
 
116
                usage(executable);
 
117
                return 1;
 
118
        }
 
119
 
 
120
        argc--;
 
121
        argv++;
 
122
 
 
123
        setOptions(argc, argv, executable);
 
124
 
 
125
        if (!runInForeground)
 
126
                daemonize();
 
127
 
 
128
        MainLoop();
 
129
 
 
130
        return 0;
131
131
}
132
132
 
133
133
#endif // !WIN32