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)  

time_classes.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 <time_classes.h>
26 #include <eval_context.h>
27 
28 static void RemoveTimeClass(EvalContext *ctx, const char* tags)
29 {
30  Rlist *tags_rlist = RlistFromSplitString(tags, ',');
32  StringSet *global_matches = ClassesMatching(ctx, iter, ".*", tags_rlist, false);
34 
35  StringSetIterator it = StringSetIteratorInit(global_matches);
36  const char *element = NULL;
37  while ((element = StringSetIteratorNext(&it)))
38  {
39  EvalContextClassRemove(ctx, NULL, element);
40  }
41 
42  StringSetDestroy(global_matches);
43 
44  RlistDestroy(tags_rlist);
45 }
46 
47 static void AddTimeClass(EvalContext *ctx, time_t time, const char* tags)
48 {
49  // The first element is the local timezone
50  const char* tz_prefix[2] = { "", "GMT_" };
51  const char* tz_function[2] = { "localtime_r", "gmtime_r" };
52  struct tm tz_parsed_time[2];
53  const struct tm* tz_tm[2] = {
54  localtime_r(&time, &(tz_parsed_time[0])),
55  gmtime_r(&time, &(tz_parsed_time[1]))
56  };
57 
58  for (int tz = 0; tz < 2; tz++)
59  {
60  char buf[CF_BUFSIZE];
61  int day_text_index, quarter, interval_start, interval_end;
62 
63  if (tz_tm[tz] == NULL)
64  {
65  Log(LOG_LEVEL_ERR, "Unable to parse passed time. (%s: %s)", tz_function[tz], GetErrorStr());
66  return;
67  }
68 
69 /* Lifecycle */
70 
71  snprintf(buf, CF_BUFSIZE, "%sLcycle_%d", tz_prefix[tz], ((tz_parsed_time[tz].tm_year + 1900) % 3));
72  EvalContextClassPutHard(ctx, buf, tags);
73 
74 /* Year */
75 
76  snprintf(buf, CF_BUFSIZE, "%sYr%04d", tz_prefix[tz], tz_parsed_time[tz].tm_year + 1900);
77  EvalContextClassPutHard(ctx, buf, tags);
78 
79 /* Month */
80 
81  snprintf(buf, CF_BUFSIZE, "%s%s", tz_prefix[tz], MONTH_TEXT[tz_parsed_time[tz].tm_mon]);
82  EvalContextClassPutHard(ctx, buf, tags);
83 
84 /* Day of week */
85 
86 /* Monday is 1 in tm_wday, 0 in DAY_TEXT
87  Tuesday is 2 in tm_wday, 1 in DAY_TEXT
88  ...
89  Sunday is 0 in tm_wday, 6 in DAY_TEXT */
90  day_text_index = (tz_parsed_time[tz].tm_wday + 6) % 7;
91  snprintf(buf, CF_BUFSIZE, "%s%s", tz_prefix[tz], DAY_TEXT[day_text_index]);
92  EvalContextClassPutHard(ctx, buf, tags);
93 
94 /* Day */
95 
96  snprintf(buf, CF_BUFSIZE, "%sDay%d", tz_prefix[tz], tz_parsed_time[tz].tm_mday);
97  EvalContextClassPutHard(ctx, buf, tags);
98 
99 /* Shift */
100 
101  snprintf(buf, CF_BUFSIZE, "%s%s", tz_prefix[tz], SHIFT_TEXT[tz_parsed_time[tz].tm_hour / 6]);
102  EvalContextClassPutHard(ctx, buf, tags);
103 
104 /* Hour */
105 
106  snprintf(buf, CF_BUFSIZE, "%sHr%02d", tz_prefix[tz], tz_parsed_time[tz].tm_hour);
107  EvalContextClassPutHard(ctx, buf, tags);
108  snprintf(buf, CF_BUFSIZE, "%sHr%d", tz_prefix[tz], tz_parsed_time[tz].tm_hour);
109  EvalContextClassPutHard(ctx, buf, tags);
110 
111 /* Quarter */
112 
113  quarter = tz_parsed_time[tz].tm_min / 15 + 1;
114 
115  snprintf(buf, CF_BUFSIZE, "%sQ%d", tz_prefix[tz], quarter);
116  EvalContextClassPutHard(ctx, buf, tags);
117  snprintf(buf, CF_BUFSIZE, "%sHr%02d_Q%d", tz_prefix[tz], tz_parsed_time[tz].tm_hour, quarter);
118  EvalContextClassPutHard(ctx, buf, tags);
119 
120 /* Minute */
121 
122  snprintf(buf, CF_BUFSIZE, "%sMin%02d", tz_prefix[tz], tz_parsed_time[tz].tm_min);
123  EvalContextClassPutHard(ctx, buf, tags);
124 
125  interval_start = (tz_parsed_time[tz].tm_min / 5) * 5;
126  interval_end = (interval_start + 5) % 60;
127 
128  snprintf(buf, CF_BUFSIZE, "%sMin%02d_%02d", tz_prefix[tz], interval_start, interval_end);
129  EvalContextClassPutHard(ctx, buf, tags);
130  }
131 }
132 
133 void UpdateTimeClasses(EvalContext *ctx, time_t t)
134 {
135  RemoveTimeClass(ctx, "cfengine_internal_time_based_autoremove");
136  AddTimeClass(ctx, t, "time_based,cfengine_internal_time_based_autoremove,source=agent");
137 }
const char *const DAY_TEXT[]
Definition: constants.c:27
const char *const SHIFT_TEXT[]
Definition: constants.c:56
const char *const MONTH_TEXT[]
Definition: constants.c:39
void ClassTableIteratorDestroy(ClassTableIterator *iter)
Definition: class.c:286
#define CF_BUFSIZE
Definition: definitions.h:50
StringSet * ClassesMatching(const EvalContext *ctx, ClassTableIterator *iter, const char *regex, const Rlist *tags, bool first_only)
bool EvalContextClassRemove(EvalContext *ctx, const char *ns, const char *name)
ClassTableIterator * EvalContextClassTableIteratorNewGlobal(const EvalContext *ctx, const char *ns, bool is_hard, bool is_soft)
bool EvalContextClassPutHard(EvalContext *ctx, const char *name, const char *tags)
#define NULL
Definition: getopt1.c:56
struct tm * gmtime_r(const time_t *timep, struct tm *result)
struct tm * localtime_r(const time_t *timep, struct tm *result)
const char * GetErrorStr(void)
Definition: logging.c:275
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
@ LOG_LEVEL_ERR
Definition: logging.h:42
Rlist * RlistFromSplitString(const char *string, char sep)
Definition: rlist.c:1067
void RlistDestroy(Rlist *rl)
Definition: rlist.c:501
StringSetIterator StringSetIteratorInit(StringSet *set)
Definition: set.c:34
void StringSetDestroy(StringSet *set)
Definition: set.c:34
char * StringSetIteratorNext(StringSetIterator *iter)
Definition: set.c:34
Definition: rlist.h:35
Definition: set.h:138
static void AddTimeClass(EvalContext *ctx, time_t time, const char *tags)
Definition: time_classes.c:47
void UpdateTimeClasses(EvalContext *ctx, time_t t)
Definition: time_classes.c:133
static void RemoveTimeClass(EvalContext *ctx, const char *tags)
Definition: time_classes.c:28