cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

audit.c
Go to the documentation of this file.
1 /*
2  Copyright 2019 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #include <audit.h>
26 #include <misc_lib.h>
27 #include <conversion.h>
28 #include <logging.h>
29 #include <string_lib.h>
30 #include <cleanup.h>
31 
32 int PR_KEPT = 0; /* GLOBAL_X */
33 int PR_REPAIRED = 0; /* GLOBAL_X */
34 int PR_NOTKEPT = 0; /* GLOBAL_X */
35 
36 static bool END_AUDIT_REQUIRED = false; /* GLOBAL_X */
37 
38 void BeginAudit()
39 {
40  END_AUDIT_REQUIRED = true;
41 }
42 
44 {
45  switch (status)
46  {
48  PR_REPAIRED++;
49  break;
50 
52  PR_KEPT++;
53  break;
54 
60  PR_NOTKEPT++;
61  break;
62 
63  default:
64  ProgrammingError("Unexpected status '%c' has been passed to UpdatePromiseCounters", status);
65  }
66 }
67 
68 void EndAudit(const EvalContext *ctx, int background_tasks)
69 {
70  if (!END_AUDIT_REQUIRED)
71  {
72  return;
73  }
74 
75  double total = (double) (PR_KEPT + PR_NOTKEPT + PR_REPAIRED) / 100.0;
76 
78  if (!version)
79  {
80  version = "(not specified)";
81  }
82 
83  if (total == 0)
84  {
85  Log(LOG_LEVEL_VERBOSE, "Outcome of version '%s', no checks were scheduled", version);
86  return;
87  }
88  else
89  {
90  LogTotalCompliance(version, background_tasks);
91  }
92 }
93 
94 void FatalError(const EvalContext *ctx, char *s, ...)
95 {
96  if (s)
97  {
98  va_list ap;
99  char buf[CF_BUFSIZE] = "";
100 
101  va_start(ap, s);
102  vsnprintf(buf, CF_BUFSIZE - 1, s, ap);
103  va_end(ap);
104  Log(LOG_LEVEL_ERR, "Fatal CFEngine error: %s", buf);
105  }
106 
107  EndAudit(ctx, 0);
108  /* calling abort would bypass cleanup handlers and trigger subtle bugs */
109  DoCleanupAndExit(EXIT_FAILURE);
110 }
void FatalError(const EvalContext *ctx, char *s,...)
Definition: audit.c:94
static bool END_AUDIT_REQUIRED
Definition: audit.c:36
void EndAudit(const EvalContext *ctx, int background_tasks)
Definition: audit.c:68
void BeginAudit()
Definition: audit.c:38
int PR_NOTKEPT
Definition: audit.c:34
int PR_REPAIRED
Definition: audit.c:33
int PR_KEPT
Definition: audit.c:32
void UpdatePromiseCounters(PromiseResult status)
Definition: audit.c:43
@ COMMON_CONTROL_VERSION
Definition: cf3.defs.h:421
PromiseResult
Definition: cf3.defs.h:122
@ PROMISE_RESULT_CHANGE
Definition: cf3.defs.h:125
@ PROMISE_RESULT_INTERRUPTED
Definition: cf3.defs.h:130
@ PROMISE_RESULT_NOOP
Definition: cf3.defs.h:124
@ PROMISE_RESULT_WARN
Definition: cf3.defs.h:126
@ PROMISE_RESULT_DENIED
Definition: cf3.defs.h:128
@ PROMISE_RESULT_FAIL
Definition: cf3.defs.h:127
@ PROMISE_RESULT_TIMEOUT
Definition: cf3.defs.h:129
void DoCleanupAndExit(int ret)
Definition: cleanup.c:57
#define CF_BUFSIZE
Definition: definitions.h:50
void LogTotalCompliance(const char *version, int background_tasks)
const void * EvalContextVariableControlCommonGet(const EvalContext *ctx, CommonControl lval)
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
@ LOG_LEVEL_ERR
Definition: logging.h:42
@ LOG_LEVEL_VERBOSE
Definition: logging.h:46
#define ProgrammingError(...)
Definition: misc_lib.h:33