File: execd-config.c

package info (click to toggle)
cfengine3 3.15.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 34,456 kB
  • sloc: ansic: 145,932; sh: 8,550; makefile: 1,558; yacc: 1,192; python: 1,056; lex: 758; perl: 211; pascal: 149; awk: 58; xml: 21; sed: 13
file content (120 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download
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
/*
  Copyright 2019 Northern.tech AS

  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; version 3.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#include <execd-config.h>

#include <generic_agent.h>
#include <eval_context.h>
#include <conversion.h>
#include <string_lib.h>

/* Get a number in the interval [0, 1) for a calculation of a pseudo-random delay */
static double GetSplay(void)
{
    char splay[CF_BUFSIZE];
    snprintf(splay, CF_BUFSIZE, "%s+%s+%ju", VFQNAME, VIPADDRESS, (uintmax_t)getuid());
    return ((double) MIN(StringHash(splay, 0), UINT_MAX - 1)) / UINT_MAX;
}

ExecdConfig *ExecdConfigNew(const EvalContext *ctx, const Policy *policy)
{
    ExecdConfig *execd_config = xcalloc(1, sizeof(ExecdConfig));

    execd_config->schedule = StringSetNew();
    StringSetAdd(execd_config->schedule, xstrdup("Min00"));
    StringSetAdd(execd_config->schedule, xstrdup("Min05"));
    StringSetAdd(execd_config->schedule, xstrdup("Min10"));
    StringSetAdd(execd_config->schedule, xstrdup("Min15"));
    StringSetAdd(execd_config->schedule, xstrdup("Min20"));
    StringSetAdd(execd_config->schedule, xstrdup("Min25"));
    StringSetAdd(execd_config->schedule, xstrdup("Min30"));
    StringSetAdd(execd_config->schedule, xstrdup("Min35"));
    StringSetAdd(execd_config->schedule, xstrdup("Min40"));
    StringSetAdd(execd_config->schedule, xstrdup("Min45"));
    StringSetAdd(execd_config->schedule, xstrdup("Min50"));
    StringSetAdd(execd_config->schedule, xstrdup("Min55"));
    execd_config->splay_time = 0;
    execd_config->log_facility = xstrdup("LOG_USER");

    Seq *constraints = ControlBodyConstraints(policy, AGENT_TYPE_EXECUTOR);
    if (constraints)
    {
        for (size_t i = 0; i < SeqLength(constraints); i++)
        {
            Constraint *cp = SeqAt(constraints, i);

            if (!IsDefinedClass(ctx, cp->classes))
            {
                continue;
            }

            VarRef *ref = VarRefParseFromScope(cp->lval, "control_executor");
            DataType t;
            const void *value = EvalContextVariableGet(ctx, ref, &t);
            VarRefDestroy(ref);

            if (t == CF_DATA_TYPE_NONE)
            {
                ProgrammingError("Unknown attribute '%s' in control body,"
                                 " should have already been stopped by the parser",
                                 cp->lval);
            }

            if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_EXECUTORFACILITY].lval) == 0)
            {
                free(execd_config->log_facility);
                execd_config->log_facility = xstrdup(value);
                Log(LOG_LEVEL_DEBUG, "executorfacility '%s'", execd_config->log_facility);
            }
            else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SPLAYTIME].lval) == 0)
            {
                int time = IntFromString(value);
                execd_config->splay_time = (int) (time * SECONDS_PER_MINUTE * GetSplay());
            }
            else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SCHEDULE].lval) == 0)
            {
                Log(LOG_LEVEL_DEBUG, "Loading user-defined schedule...");
                StringSetClear(execd_config->schedule);

                for (const Rlist *rp = value; rp; rp = rp->next)
                {
                    StringSetAdd(execd_config->schedule, xstrdup(RlistScalarValue(rp)));
                    Log(LOG_LEVEL_DEBUG, "Adding '%s'", RlistScalarValue(rp));
                }
            }
        }
    }

    return execd_config;
}

void ExecdConfigDestroy(ExecdConfig *execd_config)
{
    if (execd_config)
    {
        free(execd_config->log_facility);
        StringSetDestroy(execd_config->schedule);
    }
    free(execd_config);
}