summaryrefslogtreecommitdiffstats
path: root/pki/base/tps/src/main/LogFile.cpp
blob: d908ca0c54f3e002b12bc41cb6600afda9460e5f (plain)
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
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA 
// 
// Copyright (C) 2010 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#include <stdio.h>

#ifdef XP_WIN32
#define TPS_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define TPS_PUBLIC
#endif /* !XP_WIN32 */

#ifdef __cplusplus
extern "C"
{
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "prmem.h"
#include "prsystem.h"
#include "plstr.h"
#include "prio.h"

#ifdef __cplusplus
}   
#endif

#include "main/ConfigStore.h"
#include "engine/RA.h"
#include "main/LogFile.h"
#include "main/RA_Context.h"
#include "main/Util.h"

//default constructor
LogFile::LogFile():
    m_fd(NULL), 
    m_fname(NULL), 
    m_signed_log(false),
    m_bytes_written(0),
    m_signed(false), 
    m_monitor(NULL),
    m_ctx(NULL) { }

int LogFile::startup(RA_Context *ctx, const char* prefix, const char *fname, bool signed_audit) 
{
    if (ctx == NULL) {
        return PR_FAILURE;
    }

    if (fname == NULL) {
        ctx->LogError("LogFile::startup",
                      __LINE__,
                      "startup error, fname is  NULL");
        return PR_FAILURE;
    }

    m_ctx = ctx;
    m_signed_log = signed_audit;
    m_fname = PL_strdup(fname);
    m_bytes_written =0;
    m_signed = false;
    m_fd = (PRFileDesc*) NULL;
    m_monitor = PR_NewMonitor();

    m_ctx->LogInfo( "LogFile::startup",
                     __LINE__,
                     "thread = 0x%lx: Logfile %s startup complete",
                     PR_GetCurrentThread(), m_fname);
    return PR_SUCCESS;
}

bool LogFile::isOpen()
{
    if (m_fd != NULL) return true;
    return false;
}

void LogFile::shutdown() 
{
    m_ctx->LogInfo( "LogFile::shutdown",
                      __LINE__,
                      "thread = 0x%lx: Logfile %s shutting down pid: %d",
                      PR_GetCurrentThread(), m_fname,getpid());

    PR_EnterMonitor(m_monitor);
    if (m_fd != NULL) {
        close();
        m_fd = (PRFileDesc *) NULL;
    }

    if (m_fname != NULL) {
        PR_Free(m_fname);
        m_fname = NULL;
    }

    PR_ExitMonitor(m_monitor);
    
    if (m_monitor != NULL) {
       PR_DestroyMonitor(m_monitor);
       m_monitor = (PRMonitor *) NULL;
    }
}

int LogFile::open()
{
    PRFileInfo info;
    PR_EnterMonitor(m_monitor);

    m_ctx->LogInfo( "LogFile::open",
                      __LINE__,
                      "Opening Log File: %s pid: %d",
                      m_fname,getpid());

    if (m_fd == NULL) {
        m_fd = PR_Open(m_fname,  PR_RDWR | PR_CREATE_FILE | PR_APPEND, 440|200);
        if (m_fd == NULL) {
            m_ctx->LogError( "LogFile::open",
                      __LINE__,
                      "Unable to open log file %s error no: %d",
                      m_fname,PR_GetError());


            goto loser;
        }
        PRStatus status = PR_GetOpenFileInfo(m_fd, &info);
        if (status != PR_SUCCESS) { 
            m_ctx->LogError( "LogFile::open",
                      __LINE__,
                      "Unable to get file information for log file %s",
                      m_fname);
            goto loser;
        }

        set_bytes_written(info.size);
    }
    PR_ExitMonitor(m_monitor);
    return PR_SUCCESS;

    loser: 
        if (m_fd != NULL) {
            PR_Close(m_fd);
            m_fd = (PRFileDesc *)NULL;
        }
        set_bytes_written(0);
        PR_ExitMonitor(m_monitor);
        return PR_FAILURE;
}

int LogFile::close() 
{
   PRStatus status;
   PR_EnterMonitor(m_monitor);
   status = PR_Close(m_fd);
   if (status != PR_SUCCESS) {
        m_ctx->LogError( "LogFile::close",
                      __LINE__,
                      "Failed to close log file %s",
                      m_fname);
   }
   PR_ExitMonitor(m_monitor);
   return status;
}

int LogFile::ReadLine(char *buf, int buf_len, int *removed_return)
{
    return Util::ReadLine(m_fd, buf,buf_len, removed_return);
}

int LogFile::printf(const char* fmt, ...)
{
    PRInt32 status;
    char msg[4096];
    va_list ap;
    va_start(ap, fmt);
    PR_vsnprintf((char *) msg, 4096, fmt, ap);
    status = this->write(msg);
    va_end(ap);
    return status;
}

int LogFile::write(char *msg_in, size_t n)
{
    char msg[4096];
    PRInt32 status;

    if (n > 4096) {
        m_ctx->LogError("LogFile::write", 
            __LINE__,
            "Trying to write more than 4096 bytes in one write to log file %s. Truncating ...",
            m_fname);
        n=4096;
    }

    PR_snprintf(msg, n, "%s", msg_in);
    status = this->write(msg);
    return status;
}

int LogFile::vfprintf(const char* fmt, va_list ap)
{
    char msg[4096];
    PRInt32 status;

    PR_vsnprintf((char *) msg, 4096, fmt, ap);
    status = this->write(msg);
    return status;
}

int LogFile::write(const char * msg)
{
    PRErrorCode error;
    PRInt32 status;
    int len;

    if (msg == NULL) {
        return PR_SUCCESS;
    }

    PR_EnterMonitor(m_monitor);
    len = PL_strlen(msg);
    if (m_fd != NULL) {
        status = PR_Write(m_fd, msg, len);
        if (status != len) {
            m_ctx->LogError( "LogFile::write",
                          __LINE__,
                          "Too few or too many bytes written to log file  %s",
                          m_fname);
            goto loser;
        } else if (status < 0) {
            // write failed
            error = PR_GetError();
            m_ctx->LogError( "LogFile::write",
                          __LINE__,
                          "Write to log file %s failed: code %d",
                          m_fname, error);
            goto loser;
        } else {
            set_bytes_written(get_bytes_written() + len);
        }
    }
    PR_ExitMonitor(m_monitor);
    return PR_SUCCESS; 
    loser: 
        PR_ExitMonitor(m_monitor);
        return PR_FAILURE;
}   

void LogFile::setSigned(bool val) {
    m_signed = val;
}

bool LogFile::getSigned() {
   return m_signed;
}

int LogFile::get_bytes_written() {
    return m_bytes_written;
}

void LogFile::set_bytes_written(int val) {
    if (val >=0) { 
        m_bytes_written = val;
    } else {
        m_ctx->LogError("LogFile::set_bytes_written", 
                        __LINE__, 
                        "Attempt to set m_bytes_written to a negative value. Ignoring");
    }
}

RA_Context * LogFile::get_context() {
    return m_ctx;
}

void LogFile::set_context(RA_Context *ctx) {
    m_ctx = ctx;
}