1 /*
2   Copyright 2021 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 <retcode.h>
26 
27 #include <printsize.h>
28 #include <actuator.h>
29 #include <rlist.h>
30 #include <string_lib.h>
31 
VerifyCommandRetcode(EvalContext * ctx,int retcode,const Attributes * a,const Promise * pp,PromiseResult * result)32 bool VerifyCommandRetcode(EvalContext *ctx, int retcode, const Attributes *a, const Promise *pp, PromiseResult *result)
33 {
34     assert(a != NULL);
35     assert(pp != NULL);
36     bool result_retcode = true;
37 
38     if (a->classes.retcode_kept ||
39         a->classes.retcode_repaired ||
40         a->classes.retcode_failed)
41     {
42         int matched = false;
43         char retcodeStr[PRINTSIZE(retcode)];
44         xsnprintf(retcodeStr, sizeof(retcodeStr), "%d", retcode);
45 
46         LogLevel info_or_verbose = LOG_LEVEL_INFO;
47 
48         // inform constraint is only for commands promises,
49         // a->inform is actually false for other promise types, so
50         // checking the promise type here is important:
51         if (StringEqual("commands", PromiseGetPromiseType(pp)) && (!a->inform))
52         {
53             // for commands promises which don't make changes to the system,
54             // you can use this to make the log messages verbose:
55             // inform => "false";
56             info_or_verbose = LOG_LEVEL_VERBOSE;
57         }
58 
59         if (RlistKeyIn(a->classes.retcode_kept, retcodeStr))
60         {
61             cfPS(ctx, info_or_verbose, PROMISE_RESULT_NOOP, pp, a,
62                  "Command related to promiser '%s' returned code '%d' defined as promise kept", pp->promiser,
63                  retcode);
64             matched = true;
65         }
66 
67         if (RlistKeyIn(a->classes.retcode_repaired, retcodeStr))
68         {
69 
70             cfPS(ctx, info_or_verbose, PROMISE_RESULT_CHANGE, pp, a,
71                  "Command related to promiser '%s' returned code '%d' defined as promise repaired", pp->promiser,
72                  retcode);
73             *result = PromiseResultUpdate(*result, PROMISE_RESULT_CHANGE);
74             matched = true;
75         }
76 
77         if (RlistKeyIn(a->classes.retcode_failed, retcodeStr))
78         {
79             cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
80                  "Command related to promiser '%s' returned code '%d' defined as promise failed", pp->promiser,
81                  retcode);
82             *result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL);
83             result_retcode = false;
84             matched = true;
85         }
86 
87         if (!matched)
88         {
89             cfPS(ctx, info_or_verbose, PROMISE_RESULT_FAIL, pp, a,
90                  "Command related to promiser '%s' returned code '%d' not defined as promise kept, not kept or repaired; setting to failed",
91                  pp->promiser, retcode);
92             *result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL);
93             result_retcode = false;
94         }
95 
96     }
97     else // default: 0 is success, != 0 is failure
98     {
99         if (retcode == 0)
100         {
101             cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Finished command related to promiser '%s' -- succeeded",
102                  pp->promiser);
103             *result = PromiseResultUpdate(*result, PROMISE_RESULT_CHANGE);
104         }
105         else
106         {
107             cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
108                  "Finished command related to promiser '%s' -- an error occurred, returned %d", pp->promiser,
109                  retcode);
110             *result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL);
111             result_retcode = false;
112         }
113     }
114 
115     return result_retcode;
116 }
117