Skyld AV  0.6
On access virus scanning for Linux
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Messaging.cc
Go to the documentation of this file.
1 /*
2  * File: Messaging.cc
3  *
4  * Copyright 2013 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
25 #include <errno.h>
26 #include <iostream>
27 #include <malloc.h>
28 #include <libgen.h>
29 #include <sstream>
30 #include <string>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include "config.h"
35 #include "skyldav.h"
36 #include "Messaging.h"
37 
42 
47  char *path;
48  char *filename;
49  mode_t mask;
50 
51  // Filter debug messages by default.
53  logfs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
54 
55  // Open syslog.
56  setlogmask(LOG_UPTO(LOG_NOTICE));
57  openlog(SYSLOG_ID, LOG_PID, LOG_USER);
58 
59  // Set umask = 022;
60  mask = umask(S_IWGRP | S_IWOTH);
61 
62  // Create directory for logfile.
63  filename = strdup(LOGFILE);
64  path = dirname(filename);
65  mkdir(path, 0755);
66  free(filename);
67 
68  // Open logfile for append.
69  try {
70  logfs.open(LOGFILE, std::fstream::out | std::fstream::app);
71  if (-1 == chmod(LOGFILE, 0644)) {
72  std::cerr << "Failure to set mask for logfile." << std::endl;
73  }
74  } catch (class std::ios_base::failure ex) {
75  std::cerr << "Failure to open logfile '"
76  << LOGFILE << "'" << std::endl;
77  }
78 
79  // Reset umask.
80  umask(mask);
81 }
82 
88 void Messaging::error(const std::string &label) {
89  std::stringstream text;
90  char errbuf[256];
91  text << label << ": " << strerror_r(errno, errbuf, sizeof(errbuf));
92  message(ERROR, text.str());
93 }
94 
101 void Messaging::message(const enum Level level, const std::string &message) {
102  std::string type;
103 
104  getSingleton();
105  if (level < singleton->messageLevel) {
106  return;
107  }
108  switch (level) {
109  case ERROR:
110  type = "E";
111  syslog(LOG_ERR, "%s", message.c_str());
112  std::cerr << message << std::endl;
113  break;
114  case WARNING:
115  type = "W";
116  syslog(LOG_WARNING, "%s", message.c_str());
117  std::cerr << message << std::endl;
118  break;
119  case INFORMATION:
120  type = "I";
121  syslog(LOG_INFO, "%s", message.c_str());
122  std::cout << message << std::endl;
123  break;
124  case DEBUG:
125  type = "D";
126  syslog(LOG_DEBUG, "%s", message.c_str());
127  std::cout << message << std::endl;
128  return;
129  default:
130  type = " ";
131  syslog(LOG_NOTICE, "%s", message.c_str());
132  std::cout << message << std::endl;
133  break;
134  }
135  if (singleton->logfs.is_open()) {
136  try {
137  singleton->logfs << type << message << std::endl;
138  } catch (class std::ios_base::failure ex) {
139  std::cerr << "Failure to write to logfile." << std::endl;
140  }
141  }
142 }
143 
149 void Messaging::setLevel(const enum Level level) {
150  getSingleton();
151  singleton->messageLevel = level;
152 }
153 
158  if (singleton == NULL) {
159  singleton = new Messaging();
160  }
161  return singleton;
162 }
163 
168  if (singleton != NULL) {
169  delete singleton;
170  singleton = NULL;
171  }
172 }
173 
178  closelog();
179  try {
180  logfs.close();
181  } catch (class std::ios_base::failure ex) {
182  std::cerr << "Failure to close logfile." << std::endl;
183  }
184 }
static void teardown()
Deletes the singleton.
Definition: Messaging.cc:167
static void setLevel(const enum Level)
Sets message level.
Definition: Messaging.cc:149
Error, e.g. malfunction of the code, malware detected.
Definition: Messaging.h:56
Messaging()
Creates the singleton.
Definition: Messaging.cc:46
Send messages.
static void error(const std::string &)
Sends an error message based on errno.
Definition: Messaging.cc:88
std::fstream logfs
Definition: Messaging.h:65
~Messaging()
Deletes singleton.
Definition: Messaging.cc:177
Warning, e.g. file access has been blocked.
Definition: Messaging.h:52
Information, e.g. access scanning has started.
Definition: Messaging.h:48
Outputs messages to system and application log and to the console.
Definition: Messaging.h:35
static void message(const enum Level, const std::string &)
Sends message.
Definition: Messaging.cc:101
enum Level messageLevel
Definition: Messaging.h:66
On access virus scanner.
static Messaging * singleton
Singleton responsible for all messages sent.
Definition: Messaging.h:64
const char * SYSLOG_ID
Definition: skyldav.h:66
Debugging information only to be shown in the console.
Definition: Messaging.h:44
Level
Message levels available.
Definition: Messaging.h:40
static Messaging * getSingleton()
Retrieves the messaging singleton.
Definition: Messaging.cc:157