]> git.ipfire.org Git - thirdparty/libcgroup.git/log
thirdparty/libcgroup.git
2 years agoftests-999: fix random dir name generation logic 355/head
Kamalesh Babulal [Tue, 13 Jun 2023 04:29:21 +0000 (09:59 +0530)] 
ftests-999: fix random dir name generation logic

The current logic of creating a random directory under the /tmp/
the directory is incorrect because the random directory is generated
under '/tmp/999stress', making it a common parent directory and
attempting to create/delete it will fail the test instance, that
overlaps. Fix this by generating '/tmp/<random-999stress>' making
unique per run.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Jun 12 11:39:41
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                          Time (sec)
        ----------------------------------------
        setup                               0.00
        999-stress-cgroup_init.py           1.55
        teardown                            0.00
        ----------------------------------------
        Total Run Time                      1.55

Fixes: b81915a1fbe7 ("tests-999: add a random directory to mount path")
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoconfig.c: fix memory leak in convert_controller_versions()
Kamalesh Babulal [Mon, 12 Jun 2023 10:06:03 +0000 (15:36 +0530)] 
config.c: fix memory leak in convert_controller_versions()

Fix memory leak warning in convert_controller_versions(), reported by
Coverity tool:

CID 313911 (#1 of 1): Resource leak (RESOURCE_LEAK)9. leaked_storage:
Variable cgrp_cpy going out of scope leaks the storage it points to.

struct cgroup cgrp_cpy was not free'd in the success path, fix it.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoworkflows/ci: add build test for samples and systemd disabled 341/head
Kamalesh Babulal [Thu, 25 May 2023 09:07:43 +0000 (14:37 +0530)] 
workflows/ci: add build test for samples and systemd disabled

Fold the build tests under single job "build tests" and also add build
test with  samples=yes and systemd=no.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agosamples/create_systemd_scope: build when systemd is enabled
Kamalesh Babulal [Wed, 24 May 2023 06:56:39 +0000 (06:56 +0000)] 
samples/create_systemd_scope: build when systemd is enabled

create_systemd_scope sample program depends on the systemd support to be
build, and add the dependency in the Makefile.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agosamples: Add a C example that creates a systemd scope
Tom Hromatka [Wed, 17 May 2023 20:53:45 +0000 (14:53 -0600)] 
samples: Add a C example that creates a systemd scope

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agogithub: Add a workflow to build libcgroup with samples enabled
Tom Hromatka [Thu, 18 May 2023 13:13:47 +0000 (07:13 -0600)] 
github: Add a workflow to build libcgroup with samples enabled

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agopython: Rename get_procs() to get_processes()
Tom Hromatka [Tue, 23 May 2023 20:47:10 +0000 (14:47 -0600)] 
python: Rename get_procs() to get_processes()

Rename the python method get_procs() to get_processes() to be more
consistent with other functions in the python cgroup class.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add a test for cgroup_change_cgroup_path()
Tom Hromatka [Wed, 17 May 2023 20:52:01 +0000 (14:52 -0600)] 
ftests: Add a test for cgroup_change_cgroup_path()

Add a functional test that exercises cgroup_change_cgroup_path()
via the python bindings.

-----------------------------------------------------------------
Test Results:
Run Date:                          May 17 21:20:19
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                           Time (sec)
-----------------------------------------
setup                                0.00
087-sudo-move_pid.py                 2.06
teardown                             0.00
-----------------------------------------
Total Run Time

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agopython: Add bindings for cgroup_change_cgroup_path()
Tom Hromatka [Wed, 17 May 2023 20:49:31 +0000 (14:49 -0600)] 
python: Add bindings for cgroup_change_cgroup_path()

Add python bindings for cgroup_change_cgroup_path().  Note
that the bindings currently only support cgroup v2 (unified)
systems.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoapi: Support NULL controllers[] in cgroup_change_cgroup_path()
Tom Hromatka [Wed, 17 May 2023 20:46:58 +0000 (14:46 -0600)] 
api: Support NULL controllers[] in cgroup_change_cgroup_path()

cgroup_change_cgroup_path() allows the user to move a pid to a
different cgroup (via the cgroup name and not a cgroup struct).
In cgroup v1, it's imperative that the controller(s) are
provided, so that the proper path can be built up.  In cgroup v2,
the list of controllers is optional, since they are not a part
of the path.

Add support for the controllers[] list to be NULL iff we are
running in unified (cgroup v2) mode.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoconfig: systemd: Add a return code to cgroup_set_default_systemd_cgroup()
Tom Hromatka [Thu, 18 May 2023 13:14:55 +0000 (07:14 -0600)] 
config: systemd: Add a return code to cgroup_set_default_systemd_cgroup()

Add a return code to cgroup_set_default_systemd_cgroup().  This is
critical for users of the libcgroup C API, as they need to be certain
that a default slice/scope is set.  If they mistakenly think one is
set when it is not, then they could erroneously operate on the root
cgroup.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agosystemd: Set last_errno when ECGOTHER occurs
Tom Hromatka [Wed, 17 May 2023 20:43:36 +0000 (14:43 -0600)] 
systemd: Set last_errno when ECGOTHER occurs

Within libcgroup, ECGOTHER denotes an error when invoking a system
call.  The user can call cgroup_get_last_errno() to receive this
value.

Set last_errno any time a systemd function - cgroup_create_scope()
cgroup_create_scope2(), etc. - returns ECGOTHER.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agotests-999: add a random directory to mount path
Kamalesh Babulal [Mon, 22 May 2023 11:27:54 +0000 (11:27 +0000)] 
tests-999: add a random directory to mount path

Currently, the mount path is static/hard coded and sometimes there might
be corners cases where the mount path directories are not deleted
completely, or there might be two instances of CI running at the same
time. Let's introduce an additional directory to the mount path, that's
generated randomly, so that every run is unique and doesn't interfere
with each other.

-----------------------------------------------------------------
Test Results:
        Run Date:                          May 22 11:27:45
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                          Time (sec)
        ----------------------------------------
        setup                               0.00
        999-stress-cgroup_init.py           1.36
        teardown                            0.00
        ----------------------------------------
        Total Run Time                      1.36

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agosamples: Add systemd command line example
Tom Hromatka [Wed, 10 May 2023 20:37:14 +0000 (14:37 -0600)] 
samples: Add systemd command line example

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add a test for samples/cmdline/systemd-with-idle-process.md
Tom Hromatka [Thu, 11 May 2023 22:56:13 +0000 (16:56 -0600)] 
ftests: Add a test for samples/cmdline/systemd-with-idle-process.md

Add a test that performs the steps outlined in the sample command
line document systemd-with-idle-process.md.

-----------------------------------------------------------------
Test Results:
Run Date:                          May 11 16:55:43
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                    Time (sec)
--------------------------------------------------
setup                                         0.00
086-sudo-systemd_cmdline_example.py           6.19
teardown                                      0.00
--------------------------------------------------
Total Run Time                                6.19

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add support for None controller to Cgroup.classify()
Tom Hromatka [Thu, 11 May 2023 22:54:08 +0000 (16:54 -0600)] 
ftests: Add support for None controller to Cgroup.classify()

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoapi: Delete the cgroup if cgroup_create_cgroup() fails
Tom Hromatka [Thu, 11 May 2023 19:53:18 +0000 (13:53 -0600)] 
api: Delete the cgroup if cgroup_create_cgroup() fails

cgroup_create_cgroup() loops through the controllers in struct cgroup
and creates the requested cgroups.  Some controllers could succeed
before a controller fails.  This results in a case where the cgroup
directory has been created and remains in /sys/fs/cgroup even after a
failure.

Invoke cgroup_delete_cgroup() after we fail to create a cgroup under a
controller.  This will delete the cgroup directory if it had been
created and put the system (nearly) back to its original state.  (It's
possible that the parent cgroup's subtree_control file was modified and
not restored, but that's best fixed in another patch.)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agogithub: Add a workflow to build libcgroup with SYSTEMD undefined
Tom Hromatka [Tue, 9 May 2023 20:51:44 +0000 (14:51 -0600)] 
github: Add a workflow to build libcgroup with SYSTEMD undefined

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agotests/ftests: cgroup: rename blacklist, whitelist terms
Kamalesh Babulal [Mon, 8 May 2023 05:18:17 +0000 (10:48 +0530)] 
tests/ftests: cgroup: rename blacklist, whitelist terms

Rename 'cgsnapshot_blacklist.conf -> cgsnapshot_denylist.conf'.  Also,
update the error messages, check to search for replaced terms.

Suggested-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agosamples/config: cgsnapshot: rename blacklist, whitelist config
Kamalesh Babulal [Mon, 8 May 2023 05:17:42 +0000 (10:47 +0530)] 
samples/config: cgsnapshot: rename blacklist, whitelist config

Rename 'cgsnapshot_whitelist.conf' -> 'cgsnapshot_allowlist.conf' and
'cgsnapshot_blacklist.conf -> cgsnapshot_denylist.conf'.  Also, update
Makefile with renames.

Suggested-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoman: cgsnapshot: replace blacklist, whitelist terms
Kamalesh Babulal [Mon, 8 May 2023 05:17:36 +0000 (10:47 +0530)] 
man: cgsnapshot: replace blacklist, whitelist terms

Replace terms 'blacklist' -> 'denylist' and
'whitelist' -> 'allowlist'.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgsnapshot: replace blacklist, whitelist terms
Kamalesh Babulal [Mon, 8 May 2023 05:16:24 +0000 (10:46 +0530)] 
tools/cgsnapshot: replace blacklist, whitelist terms

Replace terms 'blacklist' -> 'denylist' and
'whitelist' -> 'allowlist'. No functional changes.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agofix: include paths for out-of-source builds
Adriaan Schmidt [Tue, 9 May 2023 12:51:56 +0000 (14:51 +0200)] 
fix: include paths for out-of-source builds

This is so the generated libcgroup/init.h is found when not
building in the source tree.

Signed-off-by: Adriaan Schmidt <adriaan.schmidt@siemens.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agofix: repair build for --disable-systemd
Adriaan Schmidt [Tue, 9 May 2023 12:54:26 +0000 (14:54 +0200)] 
fix: repair build for --disable-systemd

Ensure that the prototypes of `create_systemd_scope` match for both
`WITH_SYSTEMD` and `!WITH_SYSTEMD`.

Signed-off-by: Adriaan Schmidt <adriaan.schmidt@siemens.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoapi: cgroup_get_current_controller_path: fix a segfault
Kamalesh Babulal [Wed, 3 May 2023 10:47:06 +0000 (16:17 +0530)] 
api: cgroup_get_current_controller_path: fix a segfault

cgroup_get_current_controller_path(pid, controller, current_path), the
third argument isn't currently validated and will cause segfault if the
user passes NULL, in place of expect char **.  Introduce a check to
validate current_path argument too.

Reproducer:
___________
 #include <stdio.h>
 #include <stdlib.h>
 #include <libcgroup.h>

 int main(int argc, char **argv)
 {
  pid_t pid;
  int ret;

  ret = cgroup_init();
  if (ret) {
  printf("cgroup initialization failed:%s\n", cgroup_strerror(ret));
  return ret;
  }

ret = cgroup_get_current_controller_path(atoi(argv[1]), NULL, NULL);
/* should not reach here */
return 0;
 }

 # gcc -o rep rep.c
 # ./rep <valid pid>
 Segmentation fault (core dumped)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoman: cgcreate: document -p option
Tom Hromatka [Thu, 27 Apr 2023 13:02:40 +0000 (13:02 +0000)] 
man: cgcreate: document -p option

Add documentation about the -p option. It helps users to specify the
task they want to use in place of the default idle process created by
libcgroup, during systemd scope.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: add test to pass pid to systemd scope create via cgcreate
Kamalesh Babulal [Fri, 28 Apr 2023 04:57:57 +0000 (10:27 +0530)] 
ftests: add test to pass pid to systemd scope create via cgcreate

Add a test to pass task pid to systemd scope during creation using the
cgcreate tool.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 27 12:54:10
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                       Time (sec)
        -----------------------------------------------------
        setup                                            0.00
        084-sudo-cgcreate_systemd_scope_pid.py           2.16
        teardown                                         0.00
        -----------------------------------------------------
        Total Run Time                                   2.16

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Added one more call to Cgroup.get_pids_in_cgroup()

2 years agoftests: cgroup: add support to pass pid for systemd scope
Kamalesh Babulal [Tue, 25 Apr 2023 13:01:55 +0000 (18:31 +0530)] 
ftests: cgroup: add support to pass pid for systemd scope

Add support to pass a task pid, while creating systemd slice/scope, this
helps the users to specify the task they wish to run instead of the
default idle_thread in the transient scope.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgcreate: add -p <pid> support for systemd scope
Kamalesh Babulal [Thu, 27 Apr 2023 12:38:42 +0000 (12:38 +0000)] 
tools/cgcreate: add -p <pid> support for systemd scope

Currently, an idle_thread is created for the transient systemd scope by
default, it works fine, when the slice/scope creation is treated in par
to create of traditional cgroup, but the user may wish to create a
slice/scope, with a process of their choice, instead of a default idle
thread.  Add support to pass task pid via '-p/--pid' option, while
creating a systemd slice/scope.

 # ./src/tools/cgcreate -c -gcpu:foo.slice/bar.scope --pid=4253

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add cgroup_get_current_controller_path() (v2) test
Kamalesh Babulal [Thu, 27 Apr 2023 11:22:04 +0000 (11:22 +0000)] 
ftests: Add cgroup_get_current_controller_path() (v2) test

Add a test cases to stress pybindings of get_current_controller_path(),
that calls cgroup_get_current_controller_path() on unified mode.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 27 11:21:33
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                             Time (sec)
        -----------------------------------------------------------
        setup                                                  0.00
        082-pybindings-cgrp_get_curr_ctrl_path-v2.py           2.18
        teardown                                               0.00
        -----------------------------------------------------------
        Total Run Time                                         2.18

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add cgroup_get_current_controller_path() (v1) test
Kamalesh Babulal [Thu, 27 Apr 2023 11:11:16 +0000 (11:11 +0000)] 
ftests: Add cgroup_get_current_controller_path() (v1) test

Add a test cases to stress pybindings of get_current_controller_path(),
that calls cgroup_get_current_controller_path() on legacy/hybrid mode.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 27 11:10:22
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                             Time (sec)
        -----------------------------------------------------------
        setup                                                  0.00
        081-pybindings-cgrp_get_curr_ctrl_path-v1.py           2.34
        teardown                                               0.00
        -----------------------------------------------------------
        Total Run Time                                         2.34

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agopython: Add python bindings for get_current_controller_path()
Kamalesh Babulal [Thu, 27 Apr 2023 11:09:05 +0000 (11:09 +0000)] 
python: Add python bindings for get_current_controller_path()

Add python bindings for get_current_controller_path()

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoapi.c: add cgroup v2 support to cgroup_get_current_controller_path()
Kamalesh Babulal [Sun, 23 Apr 2023 11:16:48 +0000 (16:46 +0530)] 
api.c: add cgroup v2 support to cgroup_get_current_controller_path()

Add support to read/parse cgroup v2 format /proc/<pid>/cgroup to
cgroup_get_current_controller_path(). The format of /proc/<pid>/cgroup
remains the same:
hierarchy-ID:controller-list:cgroup-path

the difference is that in cgroup v2 mode:
- hierarchy-ID is always 0 (one hierarchy allowed)
- controller-list is empty

there can also be a mix of both legacy and unified controllers for a
given task if the cgroup mode is hybrid mode.  This patch, add support
for the mixed cgroup setup mode too.

Reported-by: Adriaan Schmidt <adriaan.schmidt@siemens.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: add test case to excerise cgroup_setup_mode helpers
Kamalesh Babulal [Tue, 25 Apr 2023 12:36:28 +0000 (12:36 +0000)] 
ftests: add test case to excerise cgroup_setup_mode helpers

Add tests to excerise the cgroup_setup_mode() helpers pybindings
is_cgroup_mode_{legacy,hybrid,unified}

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 25 12:46:29
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                      Time (sec)
        ----------------------------------------------------
        setup                                           0.00
        083-pybindings-helpers_cgroup_mode.py           0.00
        teardown                                        0.00
        ----------------------------------------------------
        Total Run Time                                  0.00

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agopython: add pybindings for cgroup_setup_mode() helpers
Kamalesh Babulal [Mon, 24 Apr 2023 09:02:44 +0000 (09:02 +0000)] 
python: add pybindings for cgroup_setup_mode() helpers

Add pybindings for the cgroup_setup_mode() helper functions:
- is_cgroup_mode_legacy()
- is_cgroup_mode_hybrid()
- is_cgroup_mode_unified()

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Fixed a few comment typos

2 years agoapi: add helpers functions to check cgroup setup mode
Kamalesh Babulal [Tue, 25 Apr 2023 12:32:08 +0000 (12:32 +0000)] 
api: add helpers functions to check cgroup setup mode

Add helpers functions is_cgroup_mode_{legacy,hybrid,unified}() to check
for a particular cgroup setup mode. Also, export them.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agosamples/c: add sample program for cgroup_setup_mode()
Kamalesh Babulal [Tue, 25 Apr 2023 12:27:02 +0000 (12:27 +0000)] 
samples/c: add sample program for cgroup_setup_mode()

Add a sample program to demonstrate the usage of the cgroup_setup_mode()
to the developers.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoapi: export cgroup_setup_mode()
Kamalesh Babulal [Mon, 24 Apr 2023 07:00:51 +0000 (07:00 +0000)] 
api: export cgroup_setup_mode()

export cgroup_setup_mode(), the function will be helpful for the
developers to take decisions/actions based on the cgroup setup mode the
system is booted with.

Suggested-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: cgroup_add_all_controllers(): fix variable typo
Kamalesh Babulal [Sun, 23 Apr 2023 11:35:05 +0000 (17:05 +0530)] 
wrapper: cgroup_add_all_controllers(): fix variable typo

Coverity reported uninitialized value:

CID 313909 (#1 of 1): Uninitialized scalar variable (UNINIT)8.
uninit_use_in_call: Using uninitialized value *info.name as argument to
%s when calling fprintf.

In cgroup_add_all_controllers(), fix the wrong variable name in the
error message, while parsing the controller name.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add a domain invalid test
Tom Hromatka [Mon, 17 Apr 2023 14:58:15 +0000 (14:58 +0000)] 
ftests: Add a domain invalid test

-----------------------------------------------------------------
Test Results:
Run Date:                          Apr 13 19:23:11
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                             Time (sec)
-------------------------------------------
setup                                  0.00
080-kernel-domain_invalid.py           4.22
teardown                               0.00
-------------------------------------------
Total Run Time                         4.22

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add a test to create a default systemd scope via cgcreate
Tom Hromatka [Mon, 17 Apr 2023 14:57:15 +0000 (14:57 +0000)] 
ftests: Add a test to create a default systemd scope via cgcreate

Add a test to create a systemd scope via the cgcreate command
line tool.  The scope is set as the default and used to create
a child cgroup.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 10 21:39:10
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
        Test                                          Time (sec)
        --------------------------------------------------------
        setup                                               0.00
        079-sudo-cgcreate_default_systemd_scope.py          5.06
        teardown                                            0.00
        --------------------------------------------------------
        Total Run Time                                      5.06

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add a test to create a systemd scope via cgcreate
Tom Hromatka [Tue, 4 Apr 2023 22:32:12 +0000 (22:32 +0000)] 
ftests: Add a test to create a systemd scope via cgcreate

Add a test to create a systemd scope via the cgcreate command
line tool.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 05 01:35:43
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                  Time (sec)
        ------------------------------------------------
        setup                                       0.00
        078-sudo-cgcreate_systemd_scope.py          5.03
        teardown                                    0.00
        ------------------------------------------------
        Total Run Time                              5.03

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoman: document cgcreate's -c and -S options
Tom Hromatka [Thu, 6 Apr 2023 19:36:57 +0000 (13:36 -0600)] 
man: document cgcreate's -c and -S options

Add documentation about the -c (create scope) option and the -S
(set as default) option in the cgcreate man page. -c instructs
cgcreate to invoke cgroup_create_scope2() and create a systemd
scope.  -S (used in conjunction with -c) will set the provided
scope as the default for subsequent libcgroup operations.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoapi: Add support for domain invalid cgroups
Tom Hromatka [Mon, 10 Apr 2023 22:01:23 +0000 (22:01 +0000)] 
api: Add support for domain invalid cgroups

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agosrc: ftests: Better management of libcgroup idle thread
Tom Hromatka [Wed, 5 Apr 2023 15:38:55 +0000 (15:38 +0000)] 
src: ftests: Better management of libcgroup idle thread

Add the libcgroup_systemd_idle_thread to the EXTRA_DIST list. This
makes it available during `make distcheck`.

Modifying the PATH environment variable when running sudo is not
allowed on some systems.  Copy the libcgroup_systemd_idle_thread
to /bin to overcome this.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add support for lists in Cgroup.exists()
Tom Hromatka [Fri, 7 Apr 2023 22:18:30 +0000 (16:18 -0600)] 
ftests: Add support for lists in Cgroup.exists()

Add support for checking if a cgroup exists when a list of
controllers is provided.  For example, given:

controllers = ['cpu', 'pids']
cgroup_name = 'test_cgroup'

Cgroup.exists(config, controllers, cgroup_name)

will now work.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add cgcreate systemd scope support
Tom Hromatka [Tue, 4 Apr 2023 22:31:51 +0000 (22:31 +0000)] 
ftests: Add cgcreate systemd scope support

Add support for creating systemd scopes via the cgcreate command
line tool.

Python's subprocess.Popen.communicate() will not return until the
spawned process and all of its children finish running.  This will
cause a cgcreate of a scope to hang because libcgroup creates an
idle process in the newly-created scope.  Add a timeout to the run()
method so that it can complete even when a child process remains.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agocgcreate: Add support to create a systemd scope
Tom Hromatka [Mon, 3 Apr 2023 15:39:31 +0000 (09:39 -0600)] 
cgcreate: Add support to create a systemd scope

Add support to create a systemd scope.  A user can create a delegated
systemd scope using the following format:
$ cgcreate -c -g <controllers>:<slicename>/<scopename>

e.g.
$ cgcreate -c -g cpu,memory:libcgroup.slice/database.scope

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agosystemd: Cleanup return codes in cgroup_create_scope()
Tom Hromatka [Tue, 4 Apr 2023 21:31:12 +0000 (21:31 +0000)] 
systemd: Cleanup return codes in cgroup_create_scope()

cgroup_create_scope() used a single variable, ret, to handle return
codes from systemd/dbus, system calls, and internal libcgroup functions.
Use a separate return variable for each of these three cases to clearly
delineate errors.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoman/cgget: add '-c' option description
Kamalesh Babulal [Tue, 18 Apr 2023 08:20:54 +0000 (13:50 +0530)] 
man/cgget: add '-c' option description

Add a brief explanation about '-c' option. It displays the controllers
and their versions.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgget: add '-c' option
Kamalesh Babulal [Tue, 18 Apr 2023 08:20:48 +0000 (13:50 +0530)] 
tools/cgget: add '-c' option

Add '-c' option to cgget, that prints the controllers and their
versions,
this is useful for the user, who wants to list the available controllers
and/or their version too.

 $ cgget -c
 #Controller     Version
 cpu             2
 memory          2
 cpuset          1
 misc            1
 hugetlb         1
 blkio           1
 net_cls         1
 net_prio        1
 freezer         1
 devices         1
 pids            1
 perf_event      1
 rdma            1

this option works with legacy, unified, and hybrid cgroup setup modes.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Changed comment formatting

2 years agotools/cgxget: fix segfault in free path
Kamalesh Babulal [Fri, 14 Apr 2023 15:30:01 +0000 (15:30 +0000)] 
tools/cgxget: fix segfault in free path

cgxget tool crashes, when passing basic operations such as multiple
cgroups as arguments or invalid parameters, it turns out the address of
struct cgroup passed for cgroup_free() has been invalid.

$ sudo ./src/tools/cgxget -1 -r cpu.shares a b
Segmentation fault (core dumped)

$ sudo ./src/tools/cgxget -1 cpu.shares a b
Segmentation fault (core dumped)

this patch fixes it by passing the right address of the struct cgroup[]
to cgroup_free().

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgget: fix '-m' option usage
Kamalesh Babulal [Sun, 16 Apr 2023 04:40:13 +0000 (10:10 +0530)] 
tools/cgget: fix '-m' option usage

'-m' is logically an independent option and should not be mixed and
matched with other options and also when used with '-h', help always
takes precedence over other options. Fix both cases of usage with
a bool flag, that gets set when parsing the command line option but
executes only it meets the rules of not mixing with other options.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add test for cgroup_get_procs()
Tom Hromatka [Fri, 17 Mar 2023 15:25:49 +0000 (09:25 -0600)] 
ftests: Add test for cgroup_get_procs()

Add a functional test for cgroup_get_procs()

-----------------------------------------------------------------
Test Results:
Run Date:                          Mar 17 16:11:34
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                   Time (sec)
-------------------------------------------------
setup                                        0.00
077-pybindings-cgroup_get_procs.py          40.31
teardown                                     0.00
-------------------------------------------------
Total Run Time                              40.31

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add controller list() support to Cgroup.classify()
Tom Hromatka [Fri, 17 Mar 2023 15:24:32 +0000 (09:24 -0600)] 
ftests: Add controller list() support to Cgroup.classify()

Add support for passing in a list of controllers to Cgroup.classify().

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agopython: Add python bindings for cgroup_get_procs()
Tom Hromatka [Fri, 17 Mar 2023 15:23:59 +0000 (09:23 -0600)] 
python: Add python bindings for cgroup_get_procs()

Add python bindings for cgroup_get_procs().

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests-070: avoid enabling controller on leaf cgroup node
Kamalesh Babulal [Wed, 12 Apr 2023 14:31:01 +0000 (14:31 +0000)] 
ftests-070: avoid enabling controller on leaf cgroup node

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too. Let's not enable cpu controller
on the leaf cgroup node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-068: avoid enabling controller on leaf cgroup node
Kamalesh Babulal [Wed, 12 Apr 2023 14:30:34 +0000 (14:30 +0000)] 
ftests-068: avoid enabling controller on leaf cgroup node

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too. Let's not enable cpu controller
on the leaf cgroup node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-066: avoid enabling controller on leaf cgroup node
Kamalesh Babulal [Wed, 12 Apr 2023 14:28:37 +0000 (14:28 +0000)] 
ftests-066: avoid enabling controller on leaf cgroup node

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too. Let's not enable cpu controller
on the leaf cgroup node. Also, fix the expected stderr, accordingly
while migrating the tasks using cgclassify.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-064: avoid enabling controller on leaf cgroup node
Kamalesh Babulal [Wed, 12 Apr 2023 14:27:58 +0000 (14:27 +0000)] 
ftests-064: avoid enabling controller on leaf cgroup node

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too. Let's not enable cpu controller
on the leaf cgroup node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-062: avoid enabling controller on leaf cgroup node
Kamalesh Babulal [Wed, 12 Apr 2023 14:27:06 +0000 (14:27 +0000)] 
ftests-062: avoid enabling controller on leaf cgroup node

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too. Let's not enable cpu controller
on the leaf cgroup node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoRevert "gunit/test-015: Populate subtree_control for leaf cgroup"
Kamalesh Babulal [Wed, 12 Apr 2023 09:34:19 +0000 (09:34 +0000)] 
Revert "gunit/test-015: Populate subtree_control for leaf cgroup"

This reverts commit 328ec5e8e4f00790cb91f81ff53f2cdd42d0106f.

Revert the logic to rely on the leaf cgroup node's subtree_control file
to examine the enabled controllers, this breaks the cgroup v2's no
internal process constraint and fallback to the original idea of relying
upon on leaf cgroup nodes, parent subtree_control to examine the enabled
controllers.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoRevert "gunit/test-012: Create subtree_control for leaf cgroup"
Kamalesh Babulal [Wed, 12 Apr 2023 09:34:30 +0000 (09:34 +0000)] 
Revert "gunit/test-012: Create subtree_control for leaf cgroup"

This reverts commit c567528cdb9c8394c9c61c906e68daa0d91a80d8.

Fallback to the original approach of no internal process constraint,
revert the logic to enable the controller on the cgroup leaf node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoRevert "api: cgroup v2 – read subtree_control of the leaf node"
Kamalesh Babulal [Wed, 12 Apr 2023 14:51:17 +0000 (20:21 +0530)] 
Revert "api: cgroup v2 â€“ read subtree_control of the leaf node"

This reverts commit 39012eeefa454e09fc4192cacddb508ee00abee6.

As per the no internal process constraint, of cgroup v2 no controller
should be enabled in the leaf cgroup node. This patch breaks this
constraint by assuming the controllers are enabled until the leaf node
of the hierarchy. Let's revert to the original approach of reading
until the parent of the leaf cgroup node.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoRevert "api: cgroup v2 – enable controller at the leaf node"
Kamalesh Babulal [Wed, 12 Apr 2023 14:49:45 +0000 (20:19 +0530)] 
Revert "api: cgroup v2 â€“ enable controller at the leaf node"

This reverts commit bb291add5db87fe36a82a18c6eb6aaf64dc07694.

cgroup v2, has no internal process constraint, where the process runs
only on the leaf node of the cgroup hierarchy and no controllers should
be enabled on the leaf cgroup node too, so that they don't compete with
the parent's internal process. This patch broke this rule by enabling
the controller on the leaf node by default. Let's revert to the original
approach of not enabling the controller on the leaf cgroup node.

Signed-by-off: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add cgconfigparser auto convert test
Kamalesh Babulal [Fri, 7 Apr 2023 14:15:19 +0000 (19:45 +0530)] 
ftests: Add cgconfigparser auto convert test

Add a test to exercise cgconfigparser auto convert functionality.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 07 14:14:57
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                             Time (sec)
        -------------------------------------------
        setup                                  0.00
        076-cgconfig-auto_convert.py           0.11
        teardown                               0.00
        -------------------------------------------
        Total Run Time                         0.11

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Changed test number to 76 and added it to Makefile.am

2 years agocgconfig: auto convert cgroup controller versions
Kamalesh Babulal [Wed, 12 Apr 2023 09:15:01 +0000 (14:45 +0530)] 
cgconfig: auto convert cgroup controller versions

The user might be using cgroup v1 controller settings in the
cgconfig.conf and when migrating to the cgroup v2 setup, they are
required to manually edit the cgconfig.conf file to map v1 controllers
settings to that of v2 settings, this might be cumbersome activity.
This patch tries to address the mapping issue, by silently mapping
the possible controller settings to the current cgroup setup.

Consider the scenario:
$ cgget -m
Unified Mode (Cgroup v2 only).

$ cat cgconfig.conf
group foo {
        cpu {
                cpu.shares = 512;
                cpu.cfs_quota_us = 5000;
        }
        cpuset {
                cpuset.cpus = 0-1;
        }
}

the cpu controller settings are of cgroup v1 but the system is booted
into cgroup v2 setup. Running cgconfigparser without the patch, the user
will be warned about invalid settings and the parser will exit.

$ sudo cgconfigparser -l ./cgconfig.conf
cgconfigparser; error loading cgconfig.conf: Cgroup, requested group parameter does not exist

with this patch, cgconfigparser will attempt to convert the settings.
$ sudo cgconfigparser -l ./cgconfig.conf
$ cat /sys/fs/cgroup/foo/cpu.weight
50
$ cat /sys/fs/cgroup/foo/cpu.max
5000 100000

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: cleanup return statements
Kamalesh Babulal [Mon, 10 Apr 2023 12:40:02 +0000 (18:10 +0530)] 
ftests: cleanup return statements

This patch cleans up a few oddities around return statements:
1. Removes redundant return statements across the ftests/*, mostly in
   the function prereqs and a few other places too.
2. Follows the same function template for prereqs, across the ftests/*.,
   and removes the checks for success on return from prereqs function,
   those will be true always.
3. Also, replace consts.TEST_PASSED, None with pass for functions, that
   serves as stub and remove the return value checks for stubs.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-072: fix flake8 warning
Kamalesh Babulal [Mon, 3 Apr 2023 10:05:08 +0000 (15:35 +0530)] 
ftests-072: fix flake8 warning

Fix "E302 expected 2 blank lines, found 1" flake warnings.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests-059: fix flake8 warnings
Kamalesh Babulal [Mon, 3 Apr 2023 10:05:04 +0000 (15:35 +0530)] 
tests-059: fix flake8 warnings

Fix "F401 'utils' imported but unused" and
"E303 too many blank lines (2)" warnings.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests-050: fix flake8 unused import warning
Kamalesh Babulal [Mon, 3 Apr 2023 10:05:00 +0000 (15:35 +0530)] 
ftests-050: fix flake8 unused import warning

Fix "F401 'run.Run' imported but unused" flake8 warning.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add cgroup_compare_cgroup() test
Tom Hromatka [Tue, 7 Mar 2023 19:49:23 +0000 (12:49 -0700)] 
ftests: Add cgroup_compare_cgroup() test

Add a test for the cgroup_compare_cgroup() python bindings

-----------------------------------------------------------------
Test Results:
Run Date:                          Mar 07 12:49:07
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                        Time (sec)
------------------------------------------------------
setup                                             0.00
075-pybindings-cgroup_compare_cgroup.py           0.09
teardown                                          0.00
------------------------------------------------------
Total Run Time                                    0.09

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agopython: Add python bindings for cgroup_compare_cgroup()
Tom Hromatka [Tue, 7 Mar 2023 19:45:35 +0000 (12:45 -0700)] 
python: Add python bindings for cgroup_compare_cgroup()

Add python bindings for comparing two Cgroup instances.  Perform
the usual python comparisons and also invoke cgroup_compare_cgroup()
to ensure the cgroups completely match.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: Make cgroup_compare_cgroup() order agnostic
Tom Hromatka [Tue, 7 Mar 2023 22:34:34 +0000 (15:34 -0700)] 
wrapper: Make cgroup_compare_cgroup() order agnostic

cgroup_compare_cgroup() can be used to compare two struct cgroup
instances.  Improve its comparison algorithm so that it can successfully
identify cgroups as the same if the only difference is the order of the
controllers stored within them.

With this change, the following two cgroups would be identified as
equal.

cgroup_a
name = foo
controller[0] = memory
controller[1] = cpu

cgroup_b
name = foo
controller[0] = cpu
controller[1] = memory

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Add a test for cgroup_add_all_controllers()
Tom Hromatka [Sat, 4 Mar 2023 03:14:47 +0000 (03:14 +0000)] 
ftests: Add a test for cgroup_add_all_controllers()

Add a test for cgroup_add_all_controllers()

-----------------------------------------------------------------
Test Results:
Run Date:                          Mar 04 04:00:53
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                                Time (sec)
--------------------------------------------------------------
setup                                                     0.00
074-pybindings-cgroup_add_all_controllers-v1.py           0.13
teardown                                                  0.00
--------------------------------------------------------------
Total Run Time                                            0.13

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add a test for cgroup_add_all_controllers()
Tom Hromatka [Fri, 3 Mar 2023 18:16:00 +0000 (11:16 -0700)] 
ftests: Add a test for cgroup_add_all_controllers()

Add a test for cgroup_add_all_controllers()

-----------------------------------------------------------------
Test Results:
        Run Date:                          Mar 03 19:24:58
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                             Time (sec)
        -----------------------------------------------------------
        setup                                                  0.00
        073-pybindings-cgroup_add_all_controllers-v2.py        0.08
        teardown                                               0.00
        -----------------------------------------------------------
        Total Run Time                                         0.08

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agopython: Add python bindings for cgroup_add_all_controllers()
Tom Hromatka [Fri, 3 Mar 2023 17:58:09 +0000 (10:58 -0700)] 
python: Add python bindings for cgroup_add_all_controllers()

Add python bindings for adding all controllers to a cgroup
instance via cgroup_add_all_controllers().

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoapi: Read cgroup.controllers in cgroup_get_cgroup()
Tom Hromatka [Fri, 3 Mar 2023 17:02:50 +0000 (10:02 -0700)] 
api: Read cgroup.controllers in cgroup_get_cgroup()

In cgroup_get_cgroup(), use the cgroup.controllers file
(rather than cgroup.subtree_control) to determine which
controllers are enabled.

To reduce code duplication, refactor cgroupv2_get_subtree_control()
to support reading both files.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agoftests: Add ability to enable/disable controllers in the subtree control file
Tom Hromatka [Wed, 8 Mar 2023 19:44:56 +0000 (12:44 -0700)] 
ftests: Add ability to enable/disable controllers in the subtree control file

Add a method to the functional tests that can enable/disable controllers
in the cgroup.subtree_control file.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agowrapper: Add cgroup v2 support to cgroup_add_all_controllers()
Tom Hromatka [Thu, 2 Mar 2023 23:25:12 +0000 (16:25 -0700)] 
wrapper: Add cgroup v2 support to cgroup_add_all_controllers()

Add cgroup v2 support to cgroup_add_all_controllers().  For cgroup v1
(both legacy and hybrid), cgroup_add_all_controllers() reads
/proc/cgroups.  For cgroup v2, cgroup_add_all_controllers() reads the
cgroup's cgroup.controllers file.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
2 years agogunit/009: adopt the cgroup_set_values_recursive() changes
Kamalesh Babulal [Tue, 21 Mar 2023 09:12:30 +0000 (09:12 +0000)] 
gunit/009: adopt the cgroup_set_values_recursive() changes

cgroup_set_values_recursive(), third argument ignore_non_dirty_values no
more suppress the errors but skips writing of the controller setting
that isn't marked dirty, propagate the changes to the test cases too.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoapi: cgroup_copy_controller_values() mark settings dirty
Kamalesh Babulal [Tue, 21 Mar 2023 09:06:16 +0000 (09:06 +0000)] 
api: cgroup_copy_controller_values() mark settings dirty

Copying controllers settings from source to destination cgroups,
overwrites the value of the destination cgroup controller and hence
the dirty flag for all the controller settings should be
unconditionally set.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoapi.c: write dirty settings only in cgroup_set_values_recursive()
Kamalesh Babulal [Tue, 21 Mar 2023 09:04:46 +0000 (09:04 +0000)] 
api.c: write dirty settings only in cgroup_set_values_recursive()

cgroup_set_values_recursive() is called by cgroup_modify_cgroup() to
modify controller values, where all the settings modified or not are
written to the disk.  This breaks when writing a new value of a setting
that is linked to another setting of the controller, followed by writing
an unmodified value to the linked setting. This effectively undoes the
modification. For example, consider two linked settings of the cpu
controller: cpu.weight and cpu.weight.nice, where writing the new value
of cpu.weight is followed by unmodified cpu.weight.nice value. Writing
of the latter will undo the new value of the former.

Reproducer:
-----------

int print_cpu_weight()
{
        FILE *fp;
        char value[10];

        fp = fopen(PROC_CPU_WEIGHT, "r");
        if (!fp) {
                fprintf(stderr, "Failed to open %s\n", PROC_CPU_WEIGHT);
                return 1;
        }

        fgets(value, 10, fp);
        fprintf(stderr, "cpu.weight %s", value);
        fclose(fp);

        return 0;
}

int main(void)
{
        struct cgroup_controller *cgc;
        struct cgroup *cgroup;
        int ret;

        ret = cgroup_init();
        if (ret) {
                fprintf(stderr, "cgroup initialization failed\n");
                exit (1);
        }

         /* Create */
        cgroup = cgroup_new_cgroup(CGRP_NAME);
        if (!cgroup) {
                fprintf(stderr, "Failed to allocate cgroup %s\n", CGRP_NAME);
                exit(1);
        }

        cgc = cgroup_add_controller(cgroup, CTRL_NAME);
        if (!cgc) {
                fprintf(stderr, "Failed to add controller %s\n", CTRL_NAME);
                exit (1);
        }

        ret = cgroup_create_cgroup(cgroup, 0);
        if (ret) {
                fprintf(stderr, "Failed to create cgroup %s\n", CGRP_NAME);
                goto out;
        }

        ret = print_cpu_weight();
        if (ret)
                goto out;

        cgroup_free(&cgroup);

        /* Load and modify */
        cgroup = cgroup_new_cgroup(CGRP_NAME);
        if (!cgroup) {
                fprintf(stderr, "Failed to allocate cgroup %s\n", CGRP_NAME);
                exit(1);
        }

        ret = cgroup_get_cgroup(cgroup);
        if (ret) {
                fprintf(stderr, "Failed to get cgroup %s\n", CGRP_NAME);
                goto out;
        }

        cgc = NULL;
        cgc = cgroup_get_controller(cgroup, CTRL_NAME);
        if (!cgc) {
                fprintf(stderr, "Failed to get controller %s\n", CTRL_NAME);
                exit (1);
        }

        ret = cgroup_set_value_string(cgc, CTRL_SETTING, "8");
        if (ret) {
                fprintf(stderr, "Failed to set the %s value\n", CTRL_SETTING);
                goto out;
        }

        ret = cgroup_modify_cgroup(cgroup);
        if (ret) {
                fprintf(stderr, "Failed to modify cgroup\n");\
                goto out;
        }

        ret = print_cpu_weight();
        if (ret)
                goto out;

out:
        cgroup_free(&cgroup);
        return 0;
}

This patch additionally cleans up cgroup_set_values_recursive(), by
renaming the third argument ignore_non_dirty_failure to
ignore_non_dirty_values.  This rename also changes the purpose of the
flag, where the calling functions, set it to ignore the writing of the
controller setting, which is not modified/dirty and introduces extensive
checks for writing the controller setting.

Fixes: https://github.com/libcgroup/libcgroup/issues/323
Reported-by: Justin Israel <justinisrael@gmail.com>
[Justin contributed to the reproducer]
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_get_value_bool()
Kamalesh Babulal [Wed, 8 Mar 2023 15:26:38 +0000 (20:56 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_get_value_bool()

Add fuzzing to the cgroup_get_value_bool() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 13 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_uint64
[       OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_uint64
[       OK ] APIArgsTest.API_cgroup_get_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_bool
[       OK ] APIArgsTest.API_cgroup_set_value_bool (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_bool
[       OK ] APIArgsTest.API_cgroup_get_value_bool (0 ms)
[----------] 13 tests from APIArgsTest (1 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Fix comment typo

2 years agowrapper: fix segfault in cgroup_get_value_bool()
Kamalesh Babulal [Wed, 8 Mar 2023 15:26:24 +0000 (20:56 +0530)] 
wrapper: fix segfault in cgroup_get_value_bool()

The second and third arguments passed to cgroup_get_value_bool() are of
type pointers and the user might pass NULL in place of one or both of
the arguments, causing a segfault. The reason is, argument values are
used without checks, fix it by checking for NULL pointers before
proceeding.

Reproducer:
-----------

 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
  struct cgroup_controller *cgc;
  struct cgroup *cgrp;
  int ret;

  ret = cgroup_init();
  if (ret)
  exit(1);

  cgrp = cgroup_new_cgroup("fuzzer");
  if (!cgrp)
  exit(1);

  cgc = cgroup_add_controller(cgrp, "cpuset");
  if (!cgc)
  exit(1);

  ret = cgroup_add_value_string(cgc, "cpuset.cpu_exclusive", "0");
  if (ret)
  exit (1);

  cgroup_get_value_bool(cgc, "cpuset.cpu_exclusive", NULL);
//should not reach here
  return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: renumber the test case 098 to 998
Kamalesh Babulal [Wed, 8 Mar 2023 14:32:36 +0000 (07:32 -0700)] 
ftests: renumber the test case 098 to 998

The test cases, that cover the unlikely conditions, are numbered in the
reverse starting from 999, similarly, test case 098 tries to delete a
non-existent shared point.  Re-number it from '099' -> '998' to hint
at the nature of the test case.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_set_value_bool()
Kamalesh Babulal [Tue, 7 Mar 2023 05:32:23 +0000 (11:02 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_set_value_bool()

Add fuzzing to the cgroup_set_value_bool() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 12 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (1 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_uint64
[       OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_uint64
[       OK ] APIArgsTest.API_cgroup_get_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_bool
[       OK ] APIArgsTest.API_cgroup_set_value_bool (0 ms)
[----------] 12 tests from APIArgsTest (1 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: fix segfault in cgroup_set_value_bool()
Kamalesh Babulal [Tue, 7 Mar 2023 05:32:09 +0000 (11:02 +0530)] 
wrapper: fix segfault in cgroup_set_value_bool()

The second argument passed to cgroup_set_value_bool() is of type
char * and the user might pass NULL in the place of the argument,
causing a segfault.  The reason is, argument values are used without
checks, fix it by checking for NULL pointers before proceeding.

Reproducer:
----------

 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
  struct cgroup_controller *cgc;
  struct cgroup *cgrp;
  int ret;

  ret = cgroup_init();
  if (ret)
  exit(1);

  cgrp = cgroup_new_cgroup("fuzzer");
  if (!cgrp)
  exit(1);

  cgc = cgroup_add_controller(cgrp, "cpuset");
  if (!cgc)
  exit(1);

  ret = cgroup_add_value_string(cgc, "cpuset.cpu_exclusive", "0");
  if (ret)
  exit (1);

  cgroup_set_value_bool(cgc, NULL, 0);
  //should not reach here
  return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgxset: check for delimiter in name_value string
Kamalesh Babulal [Sat, 4 Mar 2023 06:34:10 +0000 (06:34 +0000)] 
tools/cgxset: check for delimiter in name_value string

While parsing -r option for name, and value pairs, we rely on strtok()
to return NULL, when there is no delimiter and lhf/rhf can't be mapped
into the name, and value tokens. This assumption is not true, strtok()
returns the whole string when it doesn't find the delimiter. Operating
under this assumption also segfaults later in the code.  Fix it, by
checking for the presence of a delimiter in the passed name_value_str
in parse_r_flag().  This also initializes the pointer to NULL, to avoid
reading them before assignment in the error path.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotools/cgset: check for delimiter in name_value string
Kamalesh Babulal [Sat, 4 Mar 2023 06:20:53 +0000 (06:20 +0000)] 
tools/cgset: check for delimiter in name_value string

While parsing -r option for name, and value pairs, we rely on strtok()
to return NULL, when there is no delimiter and lhf/rhf can't be mapped
into the name, and value tokens. This assumption is not true, strtok()
returns the whole string when it doesn't find the delimiter. Operating
under this assumption also segfaults later in the code.  Fix it, by
checking for the presence of a delimiter in the passed name_value_str
in parse_r_flag().  This also initializes the pointer to NULL, to
avoid reading them before assignment in the error path.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_get_value_uint64()
Kamalesh Babulal [Sat, 4 Mar 2023 06:50:43 +0000 (12:20 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_get_value_uint64()

Add fuzzing to the cgroup_get_value_uint64() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 11 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_uint64
[       OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_uint64
[       OK ] APIArgsTest.API_cgroup_get_value_uint64 (0 ms)
[----------] 11 tests from APIArgsTest (0 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Fix comment typo

2 years agowrapper: fix segfault in cgroup_get_value_uint64()
Kamalesh Babulal [Sat, 4 Mar 2023 06:50:03 +0000 (12:20 +0530)] 
wrapper: fix segfault in cgroup_get_value_uint64()

The second and third arguments passed to cgroup_get_value_uint64() are
of type char * and the user might pass NULL in place of one or both of
the arguments, causing a segfault. The reason is, argument values are
used without checks, fix it by checking for NULL pointers before
proceeding.

Reproducer:
-----------
 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
  struct cgroup_controller *cgc;
  struct cgroup *cgrp;
  int ret;

  ret = cgroup_init();
  if (ret)
  exit(1);

  cgrp = cgroup_new_cgroup("fuzzer");
  if (!cgrp)
  exit(1);

  cgc = cgroup_add_controller(cgrp, "cpu");
  if (!cgc)
  exit(1);

  ret = cgroup_add_value_string(cgc, "cpu.shares", "512");
  if (ret)
  exit (1);

  cgroup_get_value_uint64(cgc, NULL, NULL);
  // should not reach here.
  return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_set_value_uint64()
Kamalesh Babulal [Sat, 4 Mar 2023 04:19:23 +0000 (09:49 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_set_value_uint64()

Add fuzzing to the cgroup_set_value_uint64() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 10 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_uint64
[       OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms)
[----------] 10 tests from APIArgsTest (0 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: fix segfault in cgroup_set_value_uint64()
Kamalesh Babulal [Sat, 4 Mar 2023 04:18:53 +0000 (09:48 +0530)] 
wrapper: fix segfault in cgroup_set_value_uint64()

The second argument passed to cgroup_set_value_uint64() is of type
char * and the user might pass NULL in the place of the argument,
causing a segfault.  The reason is, argument values are used without
checks, fix it by checking for NULL pointers before proceeding.

Reproducer:
-----------
 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
         struct cgroup_controller *cgc;
         struct cgroup *cgrp;
         int ret;

         ret = cgroup_init();
         if (ret)
                 exit(1);

         cgrp = cgroup_new_cgroup("fuzzer");
         if (!cgrp)
                 exit(1);

         cgc = cgroup_add_controller(cgrp, "cpu");
         if (!cgc)
                 exit(1);

         ret = cgroup_add_value_string(cgc, "cpu.shares", "512");
         if (ret)
                 exit (1);

         cgroup_set_value_uint64(cgc, NULL, 512);
         // should not reach here.
         return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_get_value_int64()
Kamalesh Babulal [Fri, 3 Mar 2023 04:31:57 +0000 (10:01 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_get_value_int64()

Add fuzzing to the cgroup_get_value_int64() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 9 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[----------] 9 tests from APIArgsTest (0 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: fix segfault in cgroup_get_value_int64()
Kamalesh Babulal [Fri, 3 Mar 2023 04:31:41 +0000 (10:01 +0530)] 
wrapper: fix segfault in cgroup_get_value_int64()

The second and third arguments passed to cgroup_get_value_int64() are of
type char * and the user might pass NULL in place of one or both of the
arguments, causing a segfault. The reason is, argument values are used
without checks, fix it by checking for NULL pointers before proceeding.

Reproducer:
-----------
 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
  struct cgroup_controller *cgc;
  struct cgroup *cgrp;
  int ret;

  ret = cgroup_init();
  if (ret)
  exit(1);

  cgrp = cgroup_new_cgroup("fuzzer");
  if (!cgrp)
  exit(1);

  cgc = cgroup_add_controller(cgrp, "cpu");
  if (!cgc)
  exit(1);

  ret = cgroup_add_value_string(cgc, "cpu.shares", "512");
  if (ret)
  exit (1);

  cgroup_get_value_int64(cgc, NULL, NULL);
  // should not reach here.
  return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_set_value_int64()
Kamalesh Babulal [Thu, 2 Mar 2023 05:05:04 +0000 (10:35 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_set_value_int64()

Add fuzzing to the cgroup_set_value_int64() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 8 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (1 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[----------] 8 tests from APIArgsTest (1 ms total)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agowrapper: fix segfault in cgroup_set_value_int64()
Kamalesh Babulal [Thu, 2 Mar 2023 05:04:49 +0000 (10:34 +0530)] 
wrapper: fix segfault in cgroup_set_value_int64()

The second argument passed to cgroup_set_value_int64() is of type char *
and the user might pass NULL in the place of the argument, causing a
segfault.  The reason is, argument values are used without checks, fix
it by checking for NULL pointers before proceeding.

Reproducer:
-----------
 #include <stdlib.h>
 #include <libcgroup.h>

 int main(void)
 {
         struct cgroup_controller *cgc;
         struct cgroup *cgrp;
         int ret;

         ret = cgroup_init();
         if (ret)
                 exit(1);

         cgrp = cgroup_new_cgroup("fuzzer");
         if (!cgrp)
                 exit(1);

         cgc = cgroup_add_controller(cgrp, "cpu");
         if (!cgc)
                 exit(1);

         ret = cgroup_add_value_string(cgc, "cpu.shares", "512");
         if (ret)
                 exit (1);

         cgroup_set_value_int64(cgc, NULL, 512);
         // should not reach here.
         return 0;
 }

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agoftests: Cleanup pid logic in functional tests
Tom Hromatka [Fri, 17 Feb 2023 22:16:34 +0000 (15:16 -0700)] 
ftests: Cleanup pid logic in functional tests

PIDs were inconsistently being managed in the functional tests.  Some
functions treated them as `int` and in others they were `str`.  Modify
all functions that deal with PIDs to treat them as `int` or a list of
`int`s.

Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
2 years agotests/gunit: Extend the fuzzer to test cgroup_get_uid_gid()
Kamalesh Babulal [Tue, 28 Feb 2023 09:15:11 +0000 (14:45 +0530)] 
tests/gunit: Extend the fuzzer to test cgroup_get_uid_gid()

Add fuzzing to the cgroup_get_uid_gid() API, by passing combination of
of valid/NULL as arguments to the API.

[----------] 7 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>