@@ -89,6 +89,8 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
8989 _cleanup_ (oomd_cgroup_context_freep ) OomdCGroupContext * ctx = NULL ;
9090 _cleanup_free_ char * cgroup = NULL ;
9191 OomdCGroupContext * c1 , * c2 ;
92+ bool test_xattrs ;
93+ int r ;
9294
9395 if (geteuid () != 0 )
9496 return (void ) log_tests_skipped ("not root" );
@@ -101,6 +103,16 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
101103
102104 assert_se (cg_pid_get_path (NULL , 0 , & cgroup ) >= 0 );
103105
106+ /* If we don't have permissions to set xattrs we're likely in a userns or missing capabilities
107+ * so skip the xattr portions of the test. */
108+ r = cg_set_xattr (SYSTEMD_CGROUP_CONTROLLER , cgroup , "user.oomd_test" , "1" , 1 , 0 );
109+ test_xattrs = !ERRNO_IS_PRIVILEGE (r ) && !ERRNO_IS_NOT_SUPPORTED (r );
110+
111+ if (test_xattrs ) {
112+ assert_se (cg_set_xattr (SYSTEMD_CGROUP_CONTROLLER , cgroup , "user.oomd_omit" , "1" , 1 , 0 ) >= 0 );
113+ assert_se (cg_set_xattr (SYSTEMD_CGROUP_CONTROLLER , cgroup , "user.oomd_avoid" , "1" , 1 , 0 ) >= 0 );
114+ }
115+
104116 assert_se (oomd_cgroup_context_acquire (cgroup , & ctx ) == 0 );
105117
106118 assert_se (streq (ctx -> path , cgroup ));
@@ -110,12 +122,28 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
110122 assert_se (ctx -> swap_usage == 0 );
111123 assert_se (ctx -> last_pgscan == 0 );
112124 assert_se (ctx -> pgscan == 0 );
125+ /* omit takes precedence over avoid when both are set to true */
126+ if (test_xattrs )
127+ assert_se (ctx -> preference == MANAGED_OOM_PREFERENCE_OMIT );
128+ else
129+ assert_se (ctx -> preference == MANAGED_OOM_PREFERENCE_NONE );
130+ ctx = oomd_cgroup_context_free (ctx );
131+
132+ /* also check when only avoid is set to true */
133+ if (test_xattrs ) {
134+ assert_se (cg_set_xattr (SYSTEMD_CGROUP_CONTROLLER , cgroup , "user.oomd_omit" , "0" , 1 , 0 ) >= 0 );
135+ assert_se (cg_set_xattr (SYSTEMD_CGROUP_CONTROLLER , cgroup , "user.oomd_avoid" , "1" , 1 , 0 ) >= 0 );
136+ }
137+ assert_se (oomd_cgroup_context_acquire (cgroup , & ctx ) == 0 );
138+ if (test_xattrs )
139+ assert_se (ctx -> preference == MANAGED_OOM_PREFERENCE_AVOID );
113140 ctx = oomd_cgroup_context_free (ctx );
114141
115142 /* Test the root cgroup */
116143 assert_se (oomd_cgroup_context_acquire ("" , & ctx ) == 0 );
117144 assert_se (streq (ctx -> path , "/" ));
118145 assert_se (ctx -> current_memory_usage > 0 );
146+ assert_se (ctx -> preference == MANAGED_OOM_PREFERENCE_NONE );
119147
120148 /* Test hashmap inserts */
121149 assert_se (h1 = hashmap_new (& oomd_cgroup_ctx_hash_ops ));
@@ -137,6 +165,14 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
137165 assert_se (c2 -> last_pgscan == 5555 );
138166 assert_se (c2 -> mem_pressure_limit == 6789 );
139167 assert_se (c2 -> last_hit_mem_pressure_limit == 42 );
168+
169+ /* Assert that avoid/omit are not set if the cgroup is not owned by root */
170+ if (test_xattrs ) {
171+ ctx = oomd_cgroup_context_free (ctx );
172+ assert_se (cg_set_access (SYSTEMD_CGROUP_CONTROLLER , cgroup , 65534 , 0 ) >= 0 );
173+ assert_se (oomd_cgroup_context_acquire (cgroup , & ctx ) == 0 );
174+ assert_se (ctx -> preference == MANAGED_OOM_PREFERENCE_NONE );
175+ }
140176}
141177
142178static void test_oomd_system_context_acquire (void ) {
@@ -287,9 +323,11 @@ static void test_oomd_sort_cgroups(void) {
287323 char * * paths = STRV_MAKE ("/herp.slice" ,
288324 "/herp.slice/derp.scope" ,
289325 "/herp.slice/derp.scope/sheep.service" ,
290- "/zupa.slice" );
326+ "/zupa.slice" ,
327+ "/omitted.slice" ,
328+ "/avoid.slice" );
291329
292- OomdCGroupContext ctx [4 ] = {
330+ OomdCGroupContext ctx [6 ] = {
293331 { .path = paths [0 ],
294332 .swap_usage = 20 ,
295333 .pgscan = 60 ,
@@ -306,6 +344,14 @@ static void test_oomd_sort_cgroups(void) {
306344 .swap_usage = 10 ,
307345 .pgscan = 80 ,
308346 .current_memory_usage = 10 },
347+ { .path = paths [4 ],
348+ .swap_usage = 90 ,
349+ .pgscan = 100 ,
350+ .preference = MANAGED_OOM_PREFERENCE_OMIT },
351+ { .path = paths [5 ],
352+ .swap_usage = 99 ,
353+ .pgscan = 200 ,
354+ .preference = MANAGED_OOM_PREFERENCE_AVOID },
309355 };
310356
311357 assert_se (h = hashmap_new (& string_hash_ops ));
@@ -314,26 +360,32 @@ static void test_oomd_sort_cgroups(void) {
314360 assert_se (hashmap_put (h , "/herp.slice/derp.scope" , & ctx [1 ]) >= 0 );
315361 assert_se (hashmap_put (h , "/herp.slice/derp.scope/sheep.service" , & ctx [2 ]) >= 0 );
316362 assert_se (hashmap_put (h , "/zupa.slice" , & ctx [3 ]) >= 0 );
363+ assert_se (hashmap_put (h , "/omitted.slice" , & ctx [4 ]) >= 0 );
364+ assert_se (hashmap_put (h , "/avoid.slice" , & ctx [5 ]) >= 0 );
317365
318- assert_se (oomd_sort_cgroup_contexts (h , compare_swap_usage , NULL , & sorted_cgroups ) == 4 );
366+ assert_se (oomd_sort_cgroup_contexts (h , compare_swap_usage , NULL , & sorted_cgroups ) == 5 );
319367 assert_se (sorted_cgroups [0 ] == & ctx [1 ]);
320368 assert_se (sorted_cgroups [1 ] == & ctx [2 ]);
321369 assert_se (sorted_cgroups [2 ] == & ctx [0 ]);
322370 assert_se (sorted_cgroups [3 ] == & ctx [3 ]);
371+ assert_se (sorted_cgroups [4 ] == & ctx [5 ]);
323372 sorted_cgroups = mfree (sorted_cgroups );
324373
325- assert_se (oomd_sort_cgroup_contexts (h , compare_pgscan_and_memory_usage , NULL , & sorted_cgroups ) == 4 );
374+ assert_se (oomd_sort_cgroup_contexts (h , compare_pgscan_and_memory_usage , NULL , & sorted_cgroups ) == 5 );
326375 assert_se (sorted_cgroups [0 ] == & ctx [3 ]);
327376 assert_se (sorted_cgroups [1 ] == & ctx [0 ]);
328377 assert_se (sorted_cgroups [2 ] == & ctx [2 ]);
329378 assert_se (sorted_cgroups [3 ] == & ctx [1 ]);
379+ assert_se (sorted_cgroups [4 ] == & ctx [5 ]);
330380 sorted_cgroups = mfree (sorted_cgroups );
331381
332382 assert_se (oomd_sort_cgroup_contexts (h , compare_pgscan_and_memory_usage , "/herp.slice/derp.scope" , & sorted_cgroups ) == 2 );
333383 assert_se (sorted_cgroups [0 ] == & ctx [2 ]);
334384 assert_se (sorted_cgroups [1 ] == & ctx [1 ]);
335385 assert_se (sorted_cgroups [2 ] == 0 );
336386 assert_se (sorted_cgroups [3 ] == 0 );
387+ assert_se (sorted_cgroups [4 ] == 0 );
388+ assert_se (sorted_cgroups [5 ] == 0 );
337389 sorted_cgroups = mfree (sorted_cgroups );
338390}
339391
0 commit comments