root/syslogwrappers.cpp
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- dmtcp_Syslog_EventHook
- _ident
- SyslogCheckpointer_StopService
- SyslogCheckpointer_RestoreService
- SyslogCheckpointer_ResetOnFork
- openlog
- closelog
1 /****************************************************************************
2 * Copyright (C) 2006-2013 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu *
4 * *
5 * This file is part of DMTCP. *
6 * *
7 * DMTCP is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #include <string>
23 #include <syslog.h>
24 #include "syscallwrappers.h"
25 #include "dmtcpalloc.h"
26 #include "../jalib/jassert.h"
27
28 using namespace dmtcp;
29
30 static bool _isSuspended = false;
31 static bool _syslogEnabled = false;
32 static bool _identIsNotNULL = false;
33 static int _option = -1;
34 static int _facility = -1;
35
36 static void SyslogCheckpointer_StopService();
37 static void SyslogCheckpointer_RestoreService();
38 static void SyslogCheckpointer_ResetOnFork();
39
40 void dmtcp_Syslog_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data)
41 {
42 switch (event) {
43 case DMTCP_EVENT_THREADS_SUSPEND:
44 SyslogCheckpointer_StopService();
45
46 break;
47
48 case DMTCP_EVENT_REFILL:
49 SyslogCheckpointer_RestoreService();
50 break;
51
52 case DMTCP_EVENT_ATFORK_CHILD:
53 SyslogCheckpointer_ResetOnFork();
54 break;
55
56 default:
57 break;
58 }
59 }
60
61 static string& _ident()
62 {
63 static string t;
64 return t;
65 }
66
67 void SyslogCheckpointer_StopService()
68 {
69 JASSERT ( !_isSuspended );
70 if ( _syslogEnabled )
71 {
72 closelog();
73 _isSuspended = true;
74 }
75 }
76
77 void SyslogCheckpointer_RestoreService()
78 {
79 if ( _isSuspended )
80 {
81 _isSuspended = false;
82 JASSERT ( _option>=0 && _facility>=0 ) ( _option ) ( _facility );
83 openlog ( ( _identIsNotNULL ? _ident().c_str() : NULL),
84 _option, _facility );
85 }
86 }
87
88 void SyslogCheckpointer_ResetOnFork()
89 {
90 _syslogEnabled = false;
91 }
92
93 extern "C" void openlog ( const char *ident, int option, int facility )
94 {
95 JASSERT ( !_isSuspended );
96 JTRACE ( "openlog" ) ( ident );
97 _real_openlog ( ident, option, facility );
98 _syslogEnabled = true;
99
100 _identIsNotNULL = (ident != NULL);
101 if (ident != NULL) {
102 _ident() = ident;
103 }
104 _option = option;
105 _facility = facility;
106 }
107
108 extern "C" void closelog ( void )
109 {
110 JASSERT ( !_isSuspended );
111 JTRACE ( "closelog" );
112 _real_closelog();
113 _syslogEnabled = false;
114 }
115
116 // FIXME: Need to add wrappers for vsyslog() and setlogmask()
117 // NOTE: openlog() is optional. Its purpose is primarily to set default
118 // parameters. If syslog() or vsyslog() is called without it,
119 // it will still open the log. Hence, we need a wrapper for them
120 // that will set _syslogEnabled = true.
121 // NOTE: We also need to save and restore the mask of setlogmask()
122 // NOTE: Need a test/syslog.c to test this code. How can we verify that
123 // it continues to log on restart in an automatic fashion?