Skip to content

Commit cb8f68c

Browse files
Merge pull request #36 from drakenclimber/multiple_rules
Fix an adaptived bug where multiple rules weren't properly loaded
2 parents 4825289 + 26e7c43 commit cb8f68c

File tree

5 files changed

+217
-5
lines changed

5 files changed

+217
-5
lines changed

adaptived/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ API int adaptived_loop(struct adaptived_ctx * const ctx, bool parse)
443443
}
444444

445445
pthread_mutex_lock(&ctx->ctx_mutex);
446+
rule = ctx->rules;
447+
while(rule) {
448+
adaptived_dbg("Rule \"%s\" loaded\n", rule->name);
449+
rule = rule->next;
450+
}
451+
446452
if (ctx->daemon_mode) {
447453
adaptived_dbg("adaptived_loop: Try to run as daemon, nochdir = %d, noclose = %d\n",
448454
ctx->daemon_nochdir, ctx->daemon_noclose);
@@ -456,9 +462,9 @@ API int adaptived_loop(struct adaptived_ctx * const ctx, bool parse)
456462
} else {
457463
adaptived_dbg("adaptived_loop: Debug mode. Skip running as daemon.\n");
458464
}
459-
pthread_mutex_unlock(&ctx->ctx_mutex);
460465

461466
ctx->loop_cnt = 0;
467+
pthread_mutex_unlock(&ctx->ctx_mutex);
462468

463469
while (1) {
464470
pthread_mutex_lock(&ctx->ctx_mutex);

adaptived/src/parse.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,15 @@ int parse_rule(struct adaptived_ctx * const ctx, struct json_object * const rule
600600
* do not goto error after this point. we have added the rule
601601
* to the rules linked list
602602
*/
603-
if (!ctx->rules)
603+
if (!ctx->rules) {
604604
ctx->rules = rule;
605-
else
606-
ctx->rules->next = rule;
605+
} else {
606+
tmp_rule = ctx->rules;
607+
608+
while (tmp_rule->next)
609+
tmp_rule = tmp_rule->next;
610+
tmp_rule->next = rule;
611+
}
607612

608613
return ret;
609614

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/**
24+
* Test to verify all rules are properly loaded
25+
*
26+
*/
27+
28+
#include <pthread.h>
29+
#include <syslog.h>
30+
#include <unistd.h>
31+
#include <errno.h>
32+
33+
#include <adaptived.h>
34+
35+
#include "ftests.h"
36+
37+
static void *adaptived_wrapper(void *arg)
38+
{
39+
struct adaptived_ctx *ctx = arg;
40+
uintptr_t ret;
41+
42+
ret = adaptived_loop(ctx, true);
43+
44+
return (void *)ret;
45+
}
46+
47+
int main(int argc, char *argv[])
48+
{
49+
char config_path[FILENAME_MAX];
50+
pthread_t adaptived_thread;
51+
struct adaptived_ctx *ctx;
52+
uint32_t rule_cnt = 0;
53+
void *tret;
54+
int ret;
55+
56+
snprintf(config_path, FILENAME_MAX - 1, "%s/070-rule-multiple_rules.json", argv[1]);
57+
config_path[FILENAME_MAX - 1] = '\0';
58+
59+
ctx = adaptived_init(config_path);
60+
if (!ctx)
61+
return AUTOMAKE_HARD_ERROR;
62+
63+
ret = adaptived_set_attr(ctx, ADAPTIVED_ATTR_LOG_LEVEL, LOG_DEBUG);
64+
if (ret)
65+
goto err;
66+
ret = adaptived_set_attr(ctx, ADAPTIVED_ATTR_INTERVAL, 1500);
67+
if (ret)
68+
goto err;
69+
ret = adaptived_set_attr(ctx, ADAPTIVED_ATTR_MAX_LOOPS, 2);
70+
if (ret)
71+
goto err;
72+
73+
ret = pthread_create(&adaptived_thread, NULL, &adaptived_wrapper, ctx);
74+
if (ret)
75+
goto err;
76+
77+
/* wait for the adaptived loop to get up and running */
78+
sleep(1);
79+
80+
ret = adaptived_get_attr(ctx, ADAPTIVED_ATTR_RULE_CNT, &rule_cnt);
81+
if (ret)
82+
goto err;
83+
if (rule_cnt != 5)
84+
goto err;
85+
86+
pthread_join(adaptived_thread, &tret);
87+
88+
if (tret != (void *)-ETIME)
89+
goto err;
90+
91+
adaptived_release(&ctx);
92+
93+
return AUTOMAKE_PASSED;
94+
95+
err:
96+
adaptived_release(&ctx);
97+
98+
return AUTOMAKE_HARD_ERROR;
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"rules": [
3+
{
4+
"name": "1",
5+
"causes": [
6+
{
7+
"name": "always",
8+
"args": {
9+
}
10+
}
11+
],
12+
"effects": [
13+
{
14+
"name": "print",
15+
"args": {
16+
"file": "stdout",
17+
"message": "Rule 1\n"
18+
}
19+
}
20+
]
21+
},
22+
{
23+
"name": "2",
24+
"causes": [
25+
{
26+
"name": "always",
27+
"args": {
28+
}
29+
}
30+
],
31+
"effects": [
32+
{
33+
"name": "print",
34+
"args": {
35+
"file": "stdout",
36+
"message": "Rule 2\n"
37+
}
38+
}
39+
]
40+
},
41+
{
42+
"name": "3",
43+
"causes": [
44+
{
45+
"name": "always",
46+
"args": {
47+
}
48+
}
49+
],
50+
"effects": [
51+
{
52+
"name": "print",
53+
"args": {
54+
"file": "stdout",
55+
"message": "Rule 3\n"
56+
}
57+
}
58+
]
59+
},
60+
{
61+
"name": "4",
62+
"causes": [
63+
{
64+
"name": "always",
65+
"args": {
66+
}
67+
}
68+
],
69+
"effects": [
70+
{
71+
"name": "print",
72+
"args": {
73+
"file": "stdout",
74+
"message": "Rule 4\n"
75+
}
76+
}
77+
]
78+
},
79+
{
80+
"name": "5",
81+
"causes": [
82+
{
83+
"name": "always",
84+
"args": {
85+
}
86+
}
87+
],
88+
"effects": [
89+
{
90+
"name": "print",
91+
"args": {
92+
"file": "stdout",
93+
"message": "Rule 5\n"
94+
}
95+
}
96+
]
97+
}
98+
]
99+
}

adaptived/tests/ftests/Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ test066_SOURCES = 066-cause-cgroup_memory_setting_ll_lt.c ftests.c
109109
test067_SOURCES = 067-effect-kill_processes.c ftests.c
110110
test068_SOURCES = 068-effect-kill_processes_rss.c ftests.c
111111
test069_SOURCES = 069-effect-signal.c ftests.c
112+
test070_SOURCES = 070-rule-multiple_rules.c ftests.c
112113

113114
sudo1000_SOURCES = 1000-sudo-effect-sd_bus_setting_set_int.c ftests.c
114115
sudo1001_SOURCES = 1001-sudo-effect-sd_bus_setting_add_int.c ftests.c
@@ -185,6 +186,7 @@ check_PROGRAMS = \
185186
test067 \
186187
test068 \
187188
test069 \
189+
test070 \
188190
sudo1000 \
189191
sudo1001 \
190192
sudo1002 \
@@ -279,7 +281,8 @@ EXTRA_DIST_CFGS = \
279281
066-cause-cgroup_memory_setting_ll_lt.json \
280282
067-effect-kill_processes.json \
281283
068-effect-kill_processes_rss.json \
282-
069-effect-signal.json
284+
069-effect-signal.json \
285+
070-rule-multiple_rules.json
283286

284287
EXTRA_DIST_H_FILES = \
285288
ftests.h

0 commit comments

Comments
 (0)