]> git.ipfire.org Git - thirdparty/libcgroup.git/log
thirdparty/libcgroup.git
4 years agoftests: Add cgroup v1 cgrules test
Tom Hromatka [Mon, 1 Mar 2021 16:38:03 +0000 (16:38 +0000)] 
ftests: Add cgroup v1 cgrules test

Add a simple cgrulesengd tests.  Note that it cannot run within
a container.

-----------------------------------------------------------------
Test Results:
Run Date:                          Mar 01 16:43:17
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                    Time (sec)
---------------------------------------------------------
setup                                         0.00
006-cgrules-basic_cgrules_v1.py               5.21
teardown                                      0.00
---------------------------------------------------------
Total Run Time                                5.21

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit eeb176b289b5ac704d27ca52996423b1887c6c3e)

4 years agoftests: Clean up a few exceptions
Tom Hromatka [Mon, 1 Mar 2021 16:37:38 +0000 (16:37 +0000)] 
ftests: Clean up a few exceptions

In the ftests and process files, two exceptions were
erroneously throwing ValueException rather than
ValueError.  Fix this.

In CgroupVersion.get_version(), it returned CGROUP_UNK
if it couldn't determine the version.  Make this method
more pythonic by raising a ValueError exception.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add support for getting a process' cgroups
Tom Hromatka [Mon, 1 Mar 2021 16:37:23 +0000 (16:37 +0000)] 
ftests: Add support for getting a process' cgroups

Add support to get the current cgroup(s) of a running process.
Works with both cgroup v1 and cgroup v2.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add cgrulesengd support to the Cgroup class
Tom Hromatka [Mon, 1 Mar 2021 16:37:06 +0000 (16:37 +0000)] 
ftests: Add cgrulesengd support to the Cgroup class

Add cgrulesengd support to the Cgroup class.  Add methods
to insert a rule, initialize the daemon, and start the
daemon in a separate process.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #8 from drakenclimber/issues/nocontainer
Tom Hromatka [Mon, 1 Mar 2021 15:53:22 +0000 (08:53 -0700)] 
Merge pull request #8 from drakenclimber/issues/nocontainer

Add support to run the functional tests outside of a container

4 years agoMerge pull request #7 from drakenclimber/issues/21
Tom Hromatka [Thu, 25 Feb 2021 15:13:44 +0000 (08:13 -0700)] 
Merge pull request #7 from drakenclimber/issues/21

gunit: Update test 009 to better test dirty flag

4 years agogunit: Update test 009 to better test dirty flag
Tom Hromatka [Tue, 23 Feb 2021 20:55:40 +0000 (13:55 -0700)] 
gunit: Update test 009 to better test dirty flag

When the dirty flag is set, cgroup_set_values_recursive()
was erroneously failing the recursive setting of values.
There were two problems in the unit test that caused this to
be missed:
    1. The dirty flag wasn't being set
    2. If fgets() returned a NULL ptr, the contents of the
       settings file weren't being checked

Reported-by: Bharani viswas <bharani.viswas@gmail.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add a script to run the non-containerized tests
Tom Hromatka [Wed, 3 Feb 2021 17:49:45 +0000 (10:49 -0700)] 
ftests: Add a script to run the non-containerized tests

Add a script to run the non-containerized tests and
invoke it from `make check`.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add permissions support to cgsnapshot parsing
Tom Hromatka [Wed, 3 Feb 2021 17:46:23 +0000 (10:46 -0700)] 
ftests: Add permissions support to cgsnapshot parsing

In a container cgsnapshot doesn't report owner/group
permissions, but on a bare-metal setup, it does.  Add
support to parse the permissions fields.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Make the Process() class instantiable
Tom Hromatka [Wed, 3 Feb 2021 17:32:14 +0000 (10:32 -0700)] 
ftests: Make the Process() class instantiable

Make the process class instantiable so that it can
better manage its list of children processes.  This
helps ensure that individual tests don't collide with
each other.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Ensure the deny list file exists
Tom Hromatka [Wed, 3 Feb 2021 17:07:36 +0000 (10:07 -0700)] 
ftests: Ensure the deny list file exists

It's possible that the libcgroup tests are being run
on a system that doesn't have libcgroup installed.
Ensure that the deny list file (currently named
cgsnapshot_blacklist.conf but slated to be renamed)
exists.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Use sudo for many cgroup commands when not in a container
Tom Hromatka [Wed, 3 Feb 2021 17:00:52 +0000 (10:00 -0700)] 
ftests: Use sudo for many cgroup commands when not in a container

When not running in a container, prepend 'sudo' to the
front of cgcreate, cgdelete, cgset, and cgclassify.

It would be advantageous to properly utilize permissions
within Linux so that we don't need to 'sudo' these
commands, but that's more of a kernel/sysfs/cgroups
issue than a libcgroup issue.  (Yes, libcgroup does allow
for setting owners and permissions, so someday we should
test this, but that's not a high priority at present.)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Test 004 must be run within a container
Tom Hromatka [Wed, 3 Feb 2021 16:54:55 +0000 (09:54 -0700)] 
ftests: Test 004 must be run within a container

Test 004-cgsnapshot-basic_snapshot_v1.py must be
run within a container because the cgsnapshot
data will vary slightly when inside/outside of a
container.  Add a check to its prereqs function
to enforce this.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add --container and --no-container flags
Tom Hromatka [Wed, 3 Feb 2021 16:42:44 +0000 (09:42 -0700)] 
ftests: Add --container and --no-container flags

In preparation of running the tests outside of a
container, add two flags to ftests.py: --container
and --no-container.  As the names imply, these flags
are used to specify whether the tests should be
containerized or not.

Note1: Some tests (cgrulesengd) will only work
       in a non-containerized environment.

Note2: Running the tests in a non-containerized
       environment could be destructive to a
       system as cgroup changes could be made that
       adversely affect the entire system.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Fix failure cause when multiple tests don't pass
Tom Hromatka [Thu, 24 Dec 2020 21:13:40 +0000 (21:13 +0000)] 
ftests: Fix failure cause when multiple tests don't pass

When multiple tests don't pass, the last failure_cause
recorded was the one being displayed.  Append the
failure_cause to the failed_tests[] and skipped_tests[]
arrays, so that each failure is properly saved and
reported.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #6 from drakenclimber/issues/cgsnapshot2
Tom Hromatka [Wed, 23 Dec 2020 15:17:37 +0000 (08:17 -0700)] 
Merge pull request #6 from drakenclimber/issues/cgsnapshot2

Add tests for cgroup v1 and cgroup v2 cgsnapshot

4 years agoftests: Add cgroup v2 cgsnapshot test
Tom Hromatka [Thu, 17 Dec 2020 22:14:54 +0000 (15:14 -0700)] 
ftests: Add cgroup v2 cgsnapshot test

Add a cgroup v2 cgsnapshot test.

-----------------------------------------------------------------
Test Results:
Run Date:                          Dec 17 22:18:33
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                    Time (sec)
---------------------------------------------------------
setup                                        16.83
005-cgsnapshot-basic_snapshot_v2.py           1.13
teardown                                      0.00
---------------------------------------------------------
Total Run Time                               17.96

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add cgroup v1 cgsnapshot test
Tom Hromatka [Thu, 17 Dec 2020 20:48:35 +0000 (13:48 -0700)] 
ftests: Add cgroup v1 cgsnapshot test

Add a cgroup v1 cgsnapshot test.

-----------------------------------------------------------------
Test Results:
Run Date:                          Dec 17 22:13:22
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                    Time (sec)
---------------------------------------------------------
setup                                        18.66
004-cgsnapshot-basic_snapshot_v1.py           0.47
teardown                                      0.00
---------------------------------------------------------
Total Run Time                               19.14

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add support for cgsnapshot
Tom Hromatka [Thu, 17 Dec 2020 20:38:43 +0000 (13:38 -0700)] 
ftests: Add support for cgsnapshot

Add functional test support for cgsnapshot to the
Cgroup class.  The method, snapshot(), invokes libcgroup's
cgsnapshot and returns a dictionary of Cgroup class
instantiations (which are analogous to struct cgroup
within libcgroup.)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Make the Cgroup class instantiable
Tom Hromatka [Thu, 17 Dec 2020 21:54:13 +0000 (14:54 -0700)] 
ftests: Make the Cgroup class instantiable

Add logic to the functional test's Cgroup class to
make it analogous to libcgroup's struct cgroup.
By adding  __init__(), __str__(), and __eq__() methods,
the cgroup class can very closely mirror struct cgroup.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add a cgroup controller class
Tom Hromatka [Thu, 17 Dec 2020 19:47:48 +0000 (12:47 -0700)] 
ftests: Add a cgroup controller class

Add a controller class that mirrors libcgroup's
struct cgroup_controller.  The controller class
contains a dictionary that represents the array
of struct control_values.  To simplify the usability
within the functional tests, the __str__() and
__eq__() methods have been overridden.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add a utilities file and an indent() function
Tom Hromatka [Thu, 17 Dec 2020 19:43:26 +0000 (12:43 -0700)] 
ftests: Add a utilities file and an indent() function

Add a new file to hold generic utilities for the
functional tests.  As part of this change, a function
that can indent a block of text has been added.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Move cgroup version enums to their own class
Tom Hromatka [Thu, 17 Dec 2020 19:02:06 +0000 (12:02 -0700)] 
ftests: Move cgroup version enums to their own class

In preparation for adding non-static functionality to
the Cgroup class, move the cgroup version enumerations
to their own class.  (Enums and __init__() can collide
in strange ways.)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #5 from drakenclimber/issues/betterlogs
Tom Hromatka [Thu, 17 Dec 2020 15:45:15 +0000 (08:45 -0700)] 
Merge pull request #5 from drakenclimber/issues/betterlogs

ftests: Invoke ftests.sh in Makefile.am

4 years agoftests: Invoke ftests.sh in Makefile.am
Tom Hromatka [Thu, 17 Dec 2020 14:59:52 +0000 (07:59 -0700)] 
ftests: Invoke ftests.sh in Makefile.am

Invoke a wrapper script, ftests.sh, in the TESTS target
within Makefile.am.  This provides two advantages:
1. When new tests are added, Makefile.am does not need
   to be updated
2. This adds verbose logging to the continuous integration
   suite

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #4 from drakenclimber/issues/ftests_cgv2
Tom Hromatka [Mon, 23 Nov 2020 16:29:40 +0000 (09:29 -0700)] 
Merge pull request #4 from drakenclimber/issues/ftests_cgv2

Add cgroup v2 support to the functional tests

4 years agoftests: Add a cgroup v2 basic cgget test
Tom Hromatka [Tue, 3 Nov 2020 15:04:20 +0000 (08:04 -0700)] 
ftests: Add a cgroup v2 basic cgget test

Add a test - 003-cgget-basic_cgget_v2.py - that tests
cgget on a cgroup v2 system.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Nov 03 17:49:35
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                    Time (sec)
        ---------------------------------------------------------
        setup                                         3.76
        003-cgget-basic_cgget_v2.py                   0.70
        teardown                                      0.00
        ---------------------------------------------------------
        Total Run Time                                4.46

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Rename Test 001 to denote it is cgroup v1 only
Tom Hromatka [Tue, 3 Nov 2020 14:55:05 +0000 (07:55 -0700)] 
ftests: Rename Test 001 to denote it is cgroup v1 only

Rename test 1 from 001-cgget-basic_cgget.py to
001-cgget-basic_cgget_v1.py.  Also add logic to the test
to skip it on a v2 system.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Fix config typo in test 001
Tom Hromatka [Wed, 28 Oct 2020 15:34:13 +0000 (15:34 +0000)] 
ftests: Fix config typo in test 001

Test 001 erroneously set the test number in the wrong place -
config.test_num.  This caused all the tests to run when only
test 001 was invoked.  The correct setting is config.args.num

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Run a cgv1 process in the recursive cgdelete test
Tom Hromatka [Wed, 28 Oct 2020 14:43:19 +0000 (14:43 +0000)] 
ftests: Run a cgv1 process in the recursive cgdelete test

For cgroup v1, run a process in the child cgroup during
the recursive cgdelete tests - 002-cgdelete-recursive_delete.py.
In cgroup v1, cgdelete should migrate the process to its
parent cgroup.

If the test is being run on a cgroup v2 system, don't
create the process.  Cgroup v2 doesn't support processes
running in a non-leaf cgroup, so cgdelete's recursive
command doesn't really make sense.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add class to create and manage child processes
Tom Hromatka [Tue, 27 Oct 2020 19:12:07 +0000 (19:12 +0000)] 
ftests: Add class to create and manage child processes

Add a class that can create and delete child processes.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add support for shell commands in containers
Tom Hromatka [Wed, 28 Oct 2020 14:08:59 +0000 (14:08 +0000)] 
ftests: Add support for shell commands in containers

Add support for shell commands to Container.run() method.
The parameter, shell_bool, is passed directly to Run.run()
and is ultimately sent on to subprocess.Popen().  As the
name implies, when shell_bool is set to true, the requested
command will be executed through the shell.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add support for cgclassify to Cgroup class
Tom Hromatka [Wed, 28 Oct 2020 15:30:55 +0000 (15:30 +0000)] 
ftests: Add support for cgclassify to Cgroup class

Add support for cgclassify to Cgroup class.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add a method to get a controller's cgroup version
Tom Hromatka [Mon, 26 Oct 2020 17:53:06 +0000 (11:53 -0600)] 
ftests: Add a method to get a controller's cgroup version

Add a static method to the Cgroup class to get a
controller's cgroup version.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Michal Koutný <mkoutny@suse.com>
4 years agoftests: Cleanup printing of test timing
Tom Hromatka [Mon, 26 Oct 2020 22:06:14 +0000 (16:06 -0600)] 
ftests: Cleanup printing of test timing

Combine the passed, skipped, and failed tests into a
single list when displaying the timing results of
each test.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Increase the display width for printing test results
Tom Hromatka [Mon, 26 Oct 2020 20:11:12 +0000 (14:11 -0600)] 
ftests: Increase the display width for printing test results

In the functional tests, increase the column width for
displaying the test name from 30 to 35 characters.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Add symlink to sed
Tom Hromatka [Tue, 27 Oct 2020 18:18:57 +0000 (18:18 +0000)] 
ftests: Add symlink to sed

LXC on Ubuntu 20.04 placed at /bin/sed rather than
/usr/bin/sed like the cgtools scripts expect.  Create
a symlink at /usr/bin/sed so that the cgroup tools can
run like usual.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoftests: Remove unused config files
Tom Hromatka [Wed, 28 Oct 2020 18:39:38 +0000 (12:39 -0600)] 
ftests: Remove unused config files

Remove references to default.conf and tmp.conf.  When the ftests
switched from LXC to LXD, these files became no longer needed.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #3 from drakenclimber/issues/cgdelete
Tom Hromatka [Thu, 22 Oct 2020 18:13:13 +0000 (12:13 -0600)] 
Merge pull request #3 from drakenclimber/issues/cgdelete

ftests: Add recursive cgdelete functional test

4 years agoftests: Add recursive cgdelete functional test
Tom Hromatka [Fri, 16 Oct 2020 14:58:12 +0000 (14:58 +0000)] 
ftests: Add recursive cgdelete functional test

Add a test that creates a cgroup hierarchy and then invokes
cgdelete on the parent cgroup with the recursive flag set.

-----------------------------------------------------------------
Test Results:
        Run Date:                     Oct 16 14:54:20
        Passed:                             2 test(s)
        Skipped:                            0 test(s)
        Failed:                             0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                               Time (sec)
        ---------------------------------------------------------
        setup                                   15.11
        001-cgget-basic_cgget.py                 0.53
        002-cgdelete-recursive_delete.py         0.26
        teardown                                 0.00
        ---------------------------------------------------------
        Total Run Time                          15.90

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #2 from drakenclimber/issues/cgclassify
Tom Hromatka [Thu, 15 Oct 2020 20:00:05 +0000 (14:00 -0600)] 
Merge pull request #2 from drakenclimber/issues/cgclassify

Add unit tests for cgclassify and cgexec functions

4 years agogunit: Add unit tests for cgroupv2_controller_enabled()
Tom Hromatka [Wed, 23 Sep 2020 16:49:12 +0000 (10:49 -0600)] 
gunit: Add unit tests for cgroupv2_controller_enabled()

This commit adds unit tests for cgroupv2_controller_enabled()

[----------] 4 tests from CgroupV2ControllerEnabled
[ RUN      ] CgroupV2ControllerEnabled.CgroupV1Controller
[       OK ] CgroupV2ControllerEnabled.CgroupV1Controller (0 ms)
[ RUN      ] CgroupV2ControllerEnabled.RootCgroup
[       OK ] CgroupV2ControllerEnabled.RootCgroup (1 ms)
[ RUN      ] CgroupV2ControllerEnabled.ControllerEnabled
[       OK ] CgroupV2ControllerEnabled.ControllerEnabled (0 ms)
[ RUN      ] CgroupV2ControllerEnabled.ControllerDisabled
[       OK ] CgroupV2ControllerEnabled.ControllerDisabled (0 ms)
[----------] 4 tests from CgroupV2ControllerEnabled (1 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agogunit: Add unit tests for cgroupv2_get_subtree_control()
Tom Hromatka [Wed, 23 Sep 2020 16:48:34 +0000 (10:48 -0600)] 
gunit: Add unit tests for cgroupv2_get_subtree_control()

This commit adds unit tests for cgroupv2_get_subtree_control()

[----------] 6 tests from GetSubtreeControlTest
[ RUN      ] GetSubtreeControlTest.SingleControllerEnabled
[       OK ] GetSubtreeControlTest.SingleControllerEnabled (0 ms)
[ RUN      ] GetSubtreeControlTest.SingleControllerNoMatch
[       OK ] GetSubtreeControlTest.SingleControllerNoMatch (0 ms)
[ RUN      ] GetSubtreeControlTest.SingleControllerNoMatch2
[       OK ] GetSubtreeControlTest.SingleControllerNoMatch2 (0 ms)
[ RUN      ] GetSubtreeControlTest.MultipleControllersEnabled
[       OK ] GetSubtreeControlTest.MultipleControllersEnabled (0 ms)
[ RUN      ] GetSubtreeControlTest.MultipleControllersEnabled2
[       OK ] GetSubtreeControlTest.MultipleControllersEnabled2 (0 ms)
[ RUN      ] GetSubtreeControlTest.MultipleControllersNoMatch
[       OK ] GetSubtreeControlTest.MultipleControllersNoMatch (0 ms)
[----------] 6 tests from GetSubtreeControlTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agogunit: Add unit tests for cgroup_build_tasks_procs_path()
Tom Hromatka [Wed, 23 Sep 2020 16:47:32 +0000 (10:47 -0600)] 
gunit: Add unit tests for cgroup_build_tasks_procs_path()

This commit adds unit tests for cgroup_build_tasks_procs_path()

[----------] 6 tests from BuildTasksProcPathTest
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_ControllerNotFound
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_ControllerNotFound (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_UnknownCgVersion
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_UnknownCgVersion (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1 (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2 (1 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1WithNs
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1WithNs (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2WithNs
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2WithNs (0 ms)
[----------] 6 tests from BuildTasksProcPathTest (1 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
4 years agoMerge pull request #1 from drakenclimber/issues/cgcreate
Tom Hromatka [Wed, 9 Sep 2020 16:02:05 +0000 (10:02 -0600)] 
Merge pull request #1 from drakenclimber/issues/cgcreate

Add tests for cgcreate cgroup v2 support

5 years agogunit: Add unit tests for cgroup_create_cgroup()
Tom Hromatka [Thu, 16 Jul 2020 20:55:33 +0000 (14:55 -0600)] 
gunit: Add unit tests for cgroup_create_cgroup()

This commit adds unit tests for cgroup_create_cgroup().

[----------] 3 tests from CgroupCreateCgroupTest
[ RUN      ] CgroupCreateCgroupTest.CgroupCreateCgroupV1
[       OK ] CgroupCreateCgroupTest.CgroupCreateCgroupV1 (1 ms)
[ RUN      ] CgroupCreateCgroupTest.CgroupCreateCgroupV2
[       OK ] CgroupCreateCgroupTest.CgroupCreateCgroupV2 (0 ms)
[ RUN      ] CgroupCreateCgroupTest.CgroupCreateCgroupV1AndV2
[       OK ] CgroupCreateCgroupTest.CgroupCreateCgroupV1AndV2 (1 ms)
[----------] 3 tests from CgroupCreateCgroupTest (2 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogunit: Add unit tests for cgroupv2_subtree_control()
Tom Hromatka [Thu, 16 Jul 2020 20:34:55 +0000 (14:34 -0600)] 
gunit: Add unit tests for cgroupv2_subtree_control()

This commit adds unit tests for cgroupv2_subtree_control().

[----------] 2 tests from SubtreeControlTest
[ RUN      ] SubtreeControlTest.AddController
[       OK ] SubtreeControlTest.AddController (0 ms)
[ RUN      ] SubtreeControlTest.RemoveController
[       OK ] SubtreeControlTest.RemoveController (0 ms)
[----------] 2 tests from SubtreeControlTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Set cgroup version in test 006
Tom Hromatka [Thu, 16 Jul 2020 19:57:49 +0000 (13:57 -0600)] 
tests: Set cgroup version in test 006

This commit explicitly sets the cgroup version in the
fictitious mount table in test 006.  With the introduction
of the CGROUP_UNK enumeration value, this test fails without
explicitly setting the version to CGROUP_V1.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogunit: Add unit tests for cgroup_chown_chmod_tasks()
Tom Hromatka [Wed, 15 Jul 2020 18:36:07 +0000 (12:36 -0600)] 
gunit: Add unit tests for cgroup_chown_chmod_tasks()

This commit adds unit tests for cgroup_chown_chmod_tasks().

[----------] 1 test from ChownChmodTasksTest
[ RUN      ] ChownChmodTasksTest.SuccessfulChownChmod
[       OK ] ChownChmodTasksTest.SuccessfulChownChmod (0 ms)
[----------] 1 test from ChownChmodTasksTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogunit: Add unit tests for cgroup_set_values_recursive()
Tom Hromatka [Wed, 15 Jul 2020 18:15:14 +0000 (12:15 -0600)] 
gunit: Add unit tests for cgroup_set_values_recursive()

This commit adds unit tests for cgroup_set_values_recursive().

[----------] 1 test from SetValuesRecursiveTest
[ RUN      ] SetValuesRecursiveTest.SuccessfulSetValues
[       OK ] SetValuesRecursiveTest.SuccessfulSetValues (1 ms)
[----------] 1 test from SetValuesRecursiveTest (1 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogunit: Add unit tests for cgroup_process_v2_mnt()
Tom Hromatka [Thu, 7 May 2020 20:19:01 +0000 (20:19 +0000)] 
gunit: Add unit tests for cgroup_process_v2_mnt()

This commit adds unit tests for cgroup_process_v2_mnt().

[----------] 3 tests from CgroupProcessV2MntTest
[ RUN      ] CgroupProcessV2MntTest.AddV2Mount
[       OK ] CgroupProcessV2MntTest.AddV2Mount (1 ms)
[ RUN      ] CgroupProcessV2MntTest.AddV2Mount_Duplicate
[       OK ] CgroupProcessV2MntTest.AddV2Mount_Duplicate (0 ms)
[ RUN      ] CgroupProcessV2MntTest.EmptyControllersFile
[       OK ] CgroupProcessV2MntTest.EmptyControllersFile (1 ms)
[----------] 3 tests from CgroupProcessV2MntTest (2 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogunit: Add unit tests for cgroup_process_v1_mnt()
Tom Hromatka [Thu, 7 May 2020 20:15:24 +0000 (20:15 +0000)] 
gunit: Add unit tests for cgroup_process_v1_mnt()

This commit adds unit tests for cgroup_process_v1_mnt().  Note
that the compiler flag -Wno-write-strings was also added because
C++ throws a warning due to how the controllers[] array is
utilized.

[----------] 4 tests from CgroupProcessV1MntTest
[ RUN      ] CgroupProcessV1MntTest.AddV1Mount
[       OK ] CgroupProcessV1MntTest.AddV1Mount (0 ms)
[ RUN      ] CgroupProcessV1MntTest.AddV1Mount_Duplicate
[       OK ] CgroupProcessV1MntTest.AddV1Mount_Duplicate (0 ms)
[ RUN      ] CgroupProcessV1MntTest.AddV1NamedMount
[       OK ] CgroupProcessV1MntTest.AddV1NamedMount (0 ms)
[ RUN      ] CgroupProcessV1MntTest.AddV1NamedMount_Duplicate
[       OK ] CgroupProcessV1MntTest.AddV1NamedMount_Duplicate (0 ms)
[----------] 4 tests from CgroupProcessV1MntTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Add unit tests for cgroup_get_cgroup()
Tom Hromatka [Mon, 20 Apr 2020 17:37:52 +0000 (11:37 -0600)] 
tests: Add unit tests for cgroup_get_cgroup()

This commit adds a unit test for cgroup_get_cgroup().  To
facilitate this, it creates a pseudo-cgroup sysfs in the
working directory and updates the test's cg_mount_table[]
to point at this temporary directory.

[----------] 2 tests from CgroupGetCgroupTest
[ RUN      ] CgroupGetCgroupTest.CgroupGetCgroup1
[       OK ] CgroupGetCgroupTest.CgroupGetCgroup1 (3 ms)
[ RUN      ] CgroupGetCgroupTest.CgroupGetCgroup_NoTasksFile
[       OK ] CgroupGetCgroupTest.CgroupGetCgroup_NoTasksFile (2 ms)
[----------] 2 tests from CgroupGetCgroupTest (6 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogit: Moved the tests to the root directory
Tom Hromatka [Tue, 7 Apr 2020 13:59:34 +0000 (07:59 -0600)] 
git: Moved the tests to the root directory

This commit moves all of the files from tests/* to the
root of the git repository.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agogit: folder re-organization
Tom Hromatka [Tue, 7 Apr 2020 13:58:19 +0000 (07:58 -0600)] 
git: folder re-organization

In preparation of moving the tests to their own git repo, this
commit deletes every file/folder other than the test folder.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoMerge pull request #5 from drakenclimber/issues/fix_dist_check v0.42.1 v0.42.2
Tom Hromatka [Fri, 31 Jan 2020 21:20:05 +0000 (14:20 -0700)] 
Merge pull request #5 from drakenclimber/issues/fix_dist_check

Fix small issues with distribution zip files

5 years agoMerge pull request #6 from drakenclimber/codereviews/nforro-old-fixes2
Tom Hromatka [Fri, 31 Jan 2020 21:19:37 +0000 (14:19 -0700)] 
Merge pull request #6 from drakenclimber/codereviews/nforro-old-fixes2

Series of old fixes from Nikola Forró

5 years agoIncrease maximal size of controller values 6/head
Nikola Forró [Thu, 16 Jan 2020 17:43:54 +0000 (18:43 +0100)] 
Increase maximal size of controller values

Maximal length of a controller value is determined by CG_VALUE_MAX,
which is equal to 100. That is not sufficient in some cases.

Add new constant CG_CONTROL_VALUE_MAX (to prevent breaking current API)
and set it to 4096, which is usually equal to the amount of bytes that
can be written to a sysctl file directly.

Add warning message about exceeding the limit while parsing
configuration file.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix level of failed user/group lookup warnings
Nikola Forró [Thu, 16 Jan 2020 17:43:53 +0000 (18:43 +0100)] 
api.c: Fix level of failed user/group lookup warnings

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Show warning when tasks file can not be opened
Nikola Forró [Thu, 16 Jan 2020 17:43:52 +0000 (18:43 +0100)] 
api.c: Show warning when tasks file can not be opened

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Preserve dirty flag when copying controller values
Nikola Forró [Thu, 16 Jan 2020 17:43:51 +0000 (18:43 +0100)] 
api.c: Preserve dirty flag when copying controller values

When setting cgroup parameters with cgset fails, no error is reported.
This is caused by the fact that cgroup_copy_controller_values is not
preserving dirty flags of the values, so it's making all errors
considered non-fatal.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix potential buffer overflow
Nikola Forró [Thu, 16 Jan 2020 17:43:50 +0000 (18:43 +0100)] 
api.c: Fix potential buffer overflow

It is assumed that arguments read from /proc/<pid>/cmdline don't exceed
buf_pname buffer size, which is FILENAME_MAX - 1 characters, but that's
not always the case.

Add check to prevent buffer overflow and discard the excessive part of
an argument.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix order of memory subsystem parameters generated by cgsnapshot
Nikola Forró [Thu, 16 Jan 2020 17:43:49 +0000 (18:43 +0100)] 
api.c: Fix order of memory subsystem parameters generated by cgsnapshot

Order of parameters usually doesn't matter, but that's not the case with
memory.limit_in_bytes and memory.memsw.limit_in_bytes. When the latter
is first in the list of parameters, the resulting configuration is not
loadable with cgconfigparser.

This happens because when a cgroup is created, both memory.limit_in_bytes
and memory.memsw.limit_in_bytes parameters are initialized to highest
value possible (RESOURCE_MAX). And because memory.memsw.limit_in_bytes
must be always higher or equal to memory.limit_in_bytes, it's impossible
to change its value first.

Make sure that after constructing parameter list of memory subsystem,
the mentioned parameters are in correct order.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Fix test failure on Fedora 30 5/head
Tom Hromatka [Mon, 13 Jan 2020 23:34:56 +0000 (23:34 +0000)] 
tests: Fix test failure on Fedora 30

Unit test 004-cgroup_compare_ignore_rule.cpp segfaults on
test CgroupCompareIgnoreRuleTest.CombinedControllers().  This
is due to rule.procname being uninitialized.  This commit
initializes the entire rule structure to zero.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoautotools: gtest: Distribute the googletest *.so files
Tom Hromatka [Mon, 13 Jan 2020 23:31:20 +0000 (23:31 +0000)] 
autotools: gtest: Distribute the googletest *.so files

googletest strongly recommends that their library is
rebuilt on each platform, but this runs counter to a
tagged release of code that utilizes gtest.  This commit
enables `make dist` to bundle the googletest *.so files
into the resultant zip file.

Note that these googletest *.so files are dependent upon
a certain version of glibc (and other libraries) and may
not work on distros with very old versions of those
tools.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoRelease v0.42 v0.42
Dhaval Giani [Fri, 10 Jan 2020 23:20:39 +0000 (15:20 -0800)] 
Release v0.42

5 years agoMerge pull request #4 from drakenclimber/issues/fixdistzip
Dhaval Giani [Fri, 10 Jan 2020 23:16:53 +0000 (15:16 -0800)] 
Merge pull request #4 from drakenclimber/issues/fixdistzip

Fix a few issues with the test Makefile.ams

5 years agotests: Ship the functional test files 4/head
Tom Hromatka [Fri, 10 Jan 2020 22:48:03 +0000 (22:48 +0000)] 
tests: Ship the functional test files

This commit adds the *.py files to the dist zip so that
the functional tests can be run in the dist zip.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Dhaval Giani <dhaval.giani@oracle.com>
5 years agogtest: Cleanup gtest make targets
Tom Hromatka [Fri, 10 Jan 2020 22:46:26 +0000 (22:46 +0000)] 
gtest: Cleanup gtest make targets

Prior to this commit the unit test map file was being
included in the source directory.  It has now been moved
to the tests/gunit directory.  This commit also properly
connects the tests/gunit Makefile to the googletest *.la
so that a recompile of googletest is not required.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Dhaval Giani <dhaval.giani@oracle.com>
5 years agobootstrap: Actually build gtest
Tom Hromatka [Fri, 10 Jan 2020 22:44:04 +0000 (22:44 +0000)] 
bootstrap: Actually build gtest

googletest should be entirely compiled into an *.la
prior to ./configure.  The *.la should be added as
an extra distribution item to the dist tarball.  This
will make it so that the distribution vendors do not
need to rebuild googletest.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Dhaval Giani <dhaval.giani@oracle.com>
5 years agoMerge pull request #3 from drakenclimber/issues/releaseprocess
Dhaval Giani [Fri, 10 Jan 2020 21:13:07 +0000 (13:13 -0800)] 
Merge pull request #3 from drakenclimber/issues/releaseprocess

doc: Add release process for github

5 years agodoc: Add release process for github 3/head
Tom Hromatka [Fri, 10 Jan 2020 21:13:10 +0000 (14:13 -0700)] 
doc: Add release process for github

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoMerge branch 'issues/fix_warnings2' of git://github.com/drakenclimber/libcgroup
Dhaval Giani [Fri, 10 Jan 2020 20:27:24 +0000 (12:27 -0800)] 
Merge branch 'issues/fix_warnings2' of git://github.com/drakenclimber/libcgroup

Signed-off-by: Dhaval Giani <dhaval.giani@oracle.com>
5 years agowrapper.c: Fix potentially unterminated strings 2/head
Tom Hromatka [Tue, 7 Jan 2020 19:31:45 +0000 (12:31 -0700)] 
wrapper.c: Fix potentially unterminated strings

This commit fixes two adjacent strncpys that could result
in unterminated strings:

CID 1412144 (#2 of 2): Buffer not null terminated
(BUFFER_SIZE_WARNING)13. buffer_size_warning: Calling strncpy with a
maximum size argument of 100 bytes on destination array
cntl_value->value of size 100 bytes might leave the destination string
unterminated.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agowrapper.c: Fix buffer not null terminated Coverity warning
Tom Hromatka [Tue, 7 Jan 2020 18:53:23 +0000 (11:53 -0700)] 
wrapper.c: Fix buffer not null terminated Coverity warning

This patch fixes the following Coverity warning:

CID 1412155 (#1 of 1): Buffer not null terminated
(BUFFER_SIZE_WARNING)4. buffer_size_warning: Calling strncpy with a
maximum size argument of 100 bytes on destination array val->value of
size 100 bytes might leave the destination string unterminated.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix double free in cgroup_create_template_group()
Tom Hromatka [Tue, 7 Jan 2020 18:47:22 +0000 (11:47 -0700)] 
api.c: Fix double free in cgroup_create_template_group()

Coverity identified a double free situation:

CID 1412117 (#1 of 1): Double free (USE_AFTER_FREE)7. double_free:
Calling free frees pointer template_name which has already been freed.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoconfig.c: Silence Coverity warning about unterminated string
Tom Hromatka [Tue, 7 Jan 2020 15:03:49 +0000 (08:03 -0700)] 
config.c: Silence Coverity warning about unterminated string

This commit silences the following Coverity warning:

CID 1412118 (#1 of 1): Buffer not null terminated
(BUFFER_SIZE_WARNING)6. buffer_size_warning: Calling strncpy with a
maximum size argument of 4096 bytes on destination array mount->name of
size 4096 bytes might leave the destination string unterminated.

Note that there are several areas (including this piece of code)
in libcgroup that do not gracefully handle string truncation.  That
will be addressed in future patches.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agocgrulesengd: Fix Coverity warnings about a socket leak
Tom Hromatka [Tue, 7 Jan 2020 14:50:03 +0000 (07:50 -0700)] 
cgrulesengd: Fix Coverity warnings about a socket leak

This commit resolves the following Coverity warnings:

CID 1412123 (#1 of 1): Resource leak (RESOURCE_LEAK)12. leaked_handle:
Handle variable sk_nl going out of scope leaks the handle.

CID 1412129 (#1 of 1): Resource leak (RESOURCE_LEAK)13. leaked_handle:
Handle variable sk_unix going out of scope leaks the handle.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoconfig.c: Fix Coverity warning about uninitialized pointer read
Tom Hromatka [Tue, 7 Jan 2020 14:22:14 +0000 (07:22 -0700)] 
config.c: Fix Coverity warning about uninitialized pointer read

This commit resolves the following warning from Coverity:

CID 1412139 (#1 of 1): Uninitialized pointer read (UNINIT)5.
uninit_use_in_call: Using uninitialized value ctrl_handle when calling
cgroup_get_controller_end.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix string truncation warning
Tom Hromatka [Sun, 5 Jan 2020 22:26:47 +0000 (15:26 -0700)] 
api.c: Fix string truncation warning

This commit fixes this warning in api.c:

api.c: In function ‘cgroup_delete_cgroup_ext’:
api.c:2285:51: warning: ‘snprintf’ output may be truncated before the
    last format character [-Wformat-truncation=]
    2285 |    snprintf(child_name, sizeof(child_name), "%s/%s",
         |                                                   ^
api.c:2285:4: note: ‘snprintf’ output 2 or more bytes (assuming 4097)
    into a destination of size 4096
    2285 |    snprintf(child_name, sizeof(child_name), "%s/%s",
         |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    2286 |      cgroup_name,
         |      ~~~~~~~~~~~~
    2287 |      info.full_path + group_len);
         |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix string truncation warning
Tom Hromatka [Sun, 5 Jan 2020 22:17:09 +0000 (15:17 -0700)] 
api.c: Fix string truncation warning

This commit fixes this warning in api.c:

api.c: In function ‘cgroup_read_value_begin’:
api.c:4114:47: warning: ‘snprintf’ output may be truncated before the
    last format character [-Wformat-truncation=]
    4114 |  snprintf(stat_file, sizeof(stat_file), "%s/%s", stat_path,
         |                                               ^
api.c:4114:2: note: ‘snprintf’ output 2 or more bytes (assuming 4097)
    into a destination of size 4096
    4114 |  snprintf(stat_file, sizeof(stat_file), "%s/%s", stat_path,
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    4115 |   name);
         |   ~~~~~

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix string truncation warning
Tom Hromatka [Sun, 5 Jan 2020 22:13:51 +0000 (15:13 -0700)] 
api.c: Fix string truncation warning

This commit fixes this warning in api.c:

api.c: In function ‘cgroup_read_stats_begin’:
api.c:4189:47: warning: ‘.stat’ directive output may be truncated
    writing 5 bytes into a region of size between 0 and 4095
    [-Wformat-truncation=]
    4189 |  snprintf(stat_file, sizeof(stat_file), "%s/%s.stat", stat_path,
         |                                               ^~~~~
api.c:4189:2: note: ‘snprintf’ output 7 or more bytes (assuming 4102)
    into a destination of size 4096
    4189 |  snprintf(stat_file, sizeof(stat_file), "%s/%s.stat", stat_path,
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    4190 |    controller);
         |    ~~~~~~~~~~~

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix warning caused by misplaced const keyword
Tom Hromatka [Sun, 5 Jan 2020 21:50:54 +0000 (14:50 -0700)] 
api.c: Fix warning caused by misplaced const keyword

This commit fixes this warning in api.c:

api.c:267:34: warning: duplicate ‘const’ declaration specifier
    [-Wduplicate-decl-specifier]
    267 |   int owner_is_umask, const char const **ignore_list)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix warning caused by misplaced const keyword
Tom Hromatka [Sun, 5 Jan 2020 21:49:14 +0000 (14:49 -0700)] 
api.c: Fix warning caused by misplaced const keyword

This commit fixes this warning in api.c:

api.c:117:19: warning: duplicate ‘const’ declaration specifier
    [-Wduplicate-decl-specifier]
    117 | static const char const *cgroup_ignored_tasks_files[] = {
    "tasks", NULL };

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Fix warning caused by misplaced const keyword
Tom Hromatka [Sun, 5 Jan 2020 21:47:27 +0000 (14:47 -0700)] 
api.c: Fix warning caused by misplaced const keyword

This commit fixes this warning in api.c:

api.c:84:12: warning: duplicate ‘const’ declaration specifier
    [-Wduplicate-decl-specifier]
    84 | const char const *cgroup_strerror_codes[] = {

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agocgrulesengd: Fix improper indentation warning
Tom Hromatka [Sun, 5 Jan 2020 22:46:02 +0000 (15:46 -0700)] 
cgrulesengd: Fix improper indentation warning

This commit fixes the following warning:

cgrulesengd.c: In function ‘cgre_parse_syslog_facility’:
cgrulesengd.c:1015:5: warning: this ‘if’ clause does not guard...
    [-Wmisleading-indentation]
    1015 |     if (strlen(arg) > 1)
         |     ^~
cgrulesengd.c:1018:2: note: ...this statement, but the latter is
    misleadingly indented as if it were guarded by the ‘if’
    1018 |  switch (arg[0]) {
         |  ^~~~~~

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoparse.y: Fix type declaration for group_name
Nikola Forró [Mon, 6 Jan 2020 16:09:30 +0000 (09:09 -0700)] 
parse.y: Fix type declaration for group_name

The group_name grammar rule was incorrectly defined as
returning a type<val> which is an int.  It actually returns
a type<name> which is a char *.

Having group_name declared as val (int) and assigning a char * value
to it can lead to crashes on platforms where pointer size exceeds
size of int.

On newer versions of Bison, this bug led to the following
warnings during compilation:

parse.y: In function ‘yyparse’:
parse.y:106:56: warning: passing argument 1 of
    ‘cgroup_config_insert_cgroup’ makes pointer from integer
    without a cast [-Wint-conversion]
    106 |    $$ = cgroup_config_insert_cgroup($2);
        |                                                        ^
        |                                                        |
        |                                                        int

In file included from parse.y:21:
./libcgroup-internal.h:231:39: note: expected ‘char *’ but argument is
    of type ‘int’
    231 | int cgroup_config_insert_cgroup(char *cg_name);
        |                                 ~~~~~~^~~~~~~

parse.y:125:15: warning: assignment to ‘int’ from ‘char *’ makes integer
    from pointer without a cast [-Wint-conversion]
    125 |   $$ = $1;
        |               ^

parse.y:129:15: warning: assignment to ‘int’ from ‘char *’ makes integer
    from pointer without a cast [-Wint-conversion]
    129 |   $$ = $1;
        |               ^

parse.y: In function ‘yyparse’:
parse.y:106:56: warning: passing argument 1 of
    ‘cgroup_config_insert_cgroup’ makes pointer from integer without a cast
    [-Wint-conversion]
    106 |    $$ = cgroup_config_insert_cgroup($2);
        |                                                        ^
        |                                                        |
        |                                                        int

In file included from parse.y:21:
./libcgroup-internal.h:231:39: note: expected ‘char *’ but argument is
    of type ‘int’
    231 | int cgroup_config_insert_cgroup(char *cg_name);
        |                                 ~~~~~~^~~~~~~
parse.y:125:15: warning: assignment to ‘int’ from ‘char *’ makes integer
    from pointer without a cast [-Wint-conversion]
    125 |   $$ = $1;
        |               ^

parse.y:129:15: warning: assignment to ‘int’ from ‘char *’ makes integer
    from pointer without a cast [-Wint-conversion]
    129 |   $$ = $1;
        |               ^

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Nikola Forró <nforro@redhat.com>
Acked-by: Michal Koutný <mkoutny@suse.com>
5 years agoTag new release 0.42.rc1 v0.42.rc1
Dhaval [Fri, 3 Jan 2020 21:29:25 +0000 (13:29 -0800)] 
Tag new release 0.42.rc1

Signed-off-by: Dhaval <dhaval@localhost.localdomain>
5 years ago tests: Add unit tests for cgroup_compare_wildcard_procname()
Tom Hromatka [Wed, 20 Nov 2019 17:09:33 +0000 (17:09 +0000)] 
 tests: Add unit tests for cgroup_compare_wildcard_procname()

This commit adds unit tests for cgroup_compare_wildcard_procname().

The following tests are included in this commit:

WildcardProcnameSimpleMatch - Compares an ignore rule with a
    wildcarded procname with a matching procname.  Expects true.

WildcardProcnameNoMatch - Compares an ignore rule with a wildcarded
    procname with a process that has a non-matching name.  Expects
    false.

ProcnameWildcard_AsteriskNoMatch - Compares a wildcard rule with
    a process name that doesn't match.  Expects false.

ProcnameWildcard_AsteriskMatch - Compares a wildcard rule with
    a process name that does match the rule.  Expects true.

ProcnameWildcard_AsteriskNoMatch2 - Compares a wildcard rule
    with a process name that is shorter than the rule and thus
    does not match.  Expects false.

ProcnameWildcard_AsteriskMatchExactly - Compares a wildcard
    rule with a process name that matches the rule exactly.
    Expects true.

ProcnameWildcard_NoAsteriskMatchExactly - Compares a rule
    with no wildcards with a process name that exactly matches
    the rule.  Expects false as the function exits early due
    to no asterisk in the rule.

The results from googletest are reported below:

[----------] 13 tests from CgroupCompareIgnoreRuleTest
<snip>
[ RUN      ] CgroupCompareIgnoreRuleTest.WildcardProcnameSimpleMatch
[       OK ] CgroupCompareIgnoreRuleTest.WildcardProcnameSimpleMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.WildcardProcnameNoMatch
[       OK ] CgroupCompareIgnoreRuleTest.WildcardProcnameNoMatch (0 ms)
[----------] 13 tests from CgroupCompareIgnoreRuleTest (1 ms total)

[----------] 7 tests from ProcnameWildcardTest
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_NoAsterisk
[       OK ] ProcnameWildcardTest.ProcnameWildcard_NoAsterisk (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch
[       OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatch
[       OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatch (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch2
[       OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch2 (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatchExactly
[       OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatchExactly (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_NoAsteriskMatchExactly
[       OK ] ProcnameWildcardTest.ProcnameWildcard_NoAsteriskMatchExactly (0 ms)
[ RUN      ] ProcnameWildcardTest.ProcnameWildcard_AsteriskFirstChar
[       OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskFirstChar (0 ms)
[----------] 7 tests from ProcnameWildcardTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agocgrulesengd: Add wildcard matching for process names
Tom Hromatka [Mon, 4 Nov 2019 23:23:22 +0000 (23:23 +0000)] 
cgrulesengd: Add wildcard matching for process names

This commit adds wildcard matching to process name
parsing in cgrulesengd.  Note that wildcard matching
works for standard rules and ignore rules.

For example, given the following rule in cgrules.conf

\#<user> <controllers> <destination>
tom:foo* memory FooCG/

Processes named 'foo', foo1', 'foo2', etc. would be moved
to the FooCG/ cgroup.  Processes named 'bar', 'fo', etc.
would not match this rule.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Add unit tests for cgroup_compare_ignore_rule()
Tom Hromatka [Mon, 4 Nov 2019 23:20:01 +0000 (23:20 +0000)] 
tests: Add unit tests for cgroup_compare_ignore_rule()

The following tests are in this commit:

NotAnIgnore() - Test that non-ignore rules are not processed

SimpleMatch() - Test that an ignore rule with a matching cgroup
    and controller match

CgroupsDontMatch() - Test that cgroups with similar but not matching
    names do not match

ControllersDontMatch() - Test that controllers with non-matching
    names do not match

CombinedControllers() - Test that controllers that have been mounted
    on the same path, e.g. cpu,cpuacct, can be matched by a rule

MatchChildFolder() - Compares a rule with a single-level hierarchy
    with a rule that matches that hierarchy

MatchGrandchildFolder() - Compares a rule with a multi-level hierarchy
    with a process that matches that hierachy

MatchSimilarChildFolder() - Compares a child folder that is similar
    to the rule.  This mimics a misconfiguration that a user could
    make

RealWorldMatch() - Test that a realistic cgroup proc file matches
    a new process

RealWorldNoMatch() - Test that a realistic cgroup proc file will
    not match a new process

SimilarFolderNoMatch() - Compares a rule with a folder name that
   is similar to the process' cgroup, but not a match

RootDestinationMatch() - Compares a rule with the root as the
   destination and a process also running in the root cgroup

RootDestinationNoMatch() - Compares a rule with the root as the
   destination and a process in another cgroup folder

The results from googletest are reported below:

[----------] 13 tests from CgroupCompareIgnoreRuleTest
[ RUN      ] CgroupCompareIgnoreRuleTest.NotAnIgnore
[       OK ] CgroupCompareIgnoreRuleTest.NotAnIgnore (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.SimpleMatch
[       OK ] CgroupCompareIgnoreRuleTest.SimpleMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.CgroupsDontMatch
[       OK ] CgroupCompareIgnoreRuleTest.CgroupsDontMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.ControllersDontMatch
[       OK ] CgroupCompareIgnoreRuleTest.ControllersDontMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.CombinedControllers
[       OK ] CgroupCompareIgnoreRuleTest.CombinedControllers (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.MatchChildFolder
[       OK ] CgroupCompareIgnoreRuleTest.MatchChildFolder (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.MatchGrandchildFolder
[       OK ] CgroupCompareIgnoreRuleTest.MatchGrandchildFolder (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.MatchSimilarChildFolder
[       OK ] CgroupCompareIgnoreRuleTest.MatchSimilarChildFolder (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.RealWorldMatch
[       OK ] CgroupCompareIgnoreRuleTest.RealWorldMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.RealWorldNoMatch
[       OK ] CgroupCompareIgnoreRuleTest.RealWorldNoMatch (1 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.SimilarFolderNoMatch
[       OK ] CgroupCompareIgnoreRuleTest.SimilarFolderNoMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.RootDestinationMatch
[       OK ] CgroupCompareIgnoreRuleTest.RootDestinationMatch (0 ms)
[ RUN      ] CgroupCompareIgnoreRuleTest.RootDestinationNoMatch
[       OK ] CgroupCompareIgnoreRuleTest.RootDestinationNoMatch (0 ms)
[----------] 13 tests from CgroupCompareIgnoreRuleTest (3 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Add logic to process ignore rules
Tom Hromatka [Mon, 4 Nov 2019 23:18:48 +0000 (23:18 +0000)] 
api.c: Add logic to process ignore rules

This commit updates the cgrulesengd logic that matches the new pid
to its corresponding rule by adding support for ignore rules.  If
a user has created an ignore rule and the pid matches that rule
completely, then cgrulesengd will perform no further processing
(e.g. moving the pid to a different cgroup) on that pid.  It is
assumed that a separate process - outside of libcgroup - will
manage that process and its cgroups.

A few examples:

<user>      <controller>        <destination>        <options>
*           cpuset              IgnoreCgroup         ignore
*           cpuset              DefaultCgroup

For the above example:
    * A new process named "foo" spawned in the IgnoreCgroup will be
      ignored by cgrulesengd and will not be moved to DefaultCgroup
    * A new process named "bar" spawned in the root cgroup will not
      match the ignore rule and will be moved to the DefaultCgroup
      by the second rule
    * Any processes not in IgnoreCgroup will be automatically moved
      to the default cgroup

<user>      <controller>        <destination>        <options>
*           memory              IgnoreCgroup2/       ignore
*           memory              DefaultCgroup

For the above example:
    * A new process named "bar" spawned in IgnoreCgroup2/childcg
      will match the first rule and thus be ignored by cgrulesengd
      and will not be moved
    * In fact, any process in IgnoreCgroup2 or its children cgroup(s)
      will be ignored by cgrulesengd
    * cgrulesengd will attempt to move all other processes that don't
      match the ignore rule to the default cgroup

<user>      <controller>        <destination>        <options>
jdoe:foo    cpu                 IgnoreCgroup         ignore
*           cpu                 DefaultCgroup

For the above example:
    * If a process named "foo" is spawned by user jdoe and is in the
      IgnoreCgroup cgroup, then cgrulesengd will ignore it and it
      will not be moved
    * If a process named "bar" is spawned in IgnoreCgroup, it will
      not match the first rule and will fall into the default
      rule.  Thus it will be moved to the DefaultCgroup

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Add unit tests for cg_get_cgroups_from_proc_cgroups()
Tom Hromatka [Mon, 4 Nov 2019 23:16:15 +0000 (23:16 +0000)] 
tests: Add unit tests for cg_get_cgroups_from_proc_cgroups()

The following tests are in this commit:

ReadSingleLine() - Read a single valid line with a non-root controller

ReadSingleLine2() - Read a single valid line with a root controller

ReadEmptyController() - Some systems list a controller with no name.
    This test simulates that scenario

ReadExampleFile() - Read several lines that are similar to a typical
    /proc/{pid}/cgroups file

The results from googletest are reported below:

[----------] 4 tests from GetCgroupsFromProcCgroupsTest
[ RUN      ] GetCgroupsFromProcCgroupsTest.ReadSingleLine
[       OK ] GetCgroupsFromProcCgroupsTest.ReadSingleLine (0 ms)
[ RUN      ] GetCgroupsFromProcCgroupsTest.ReadSingleLine2
[       OK ] GetCgroupsFromProcCgroupsTest.ReadSingleLine2 (1 ms)
[ RUN      ] GetCgroupsFromProcCgroupsTest.ReadEmptyController
[       OK ] GetCgroupsFromProcCgroupsTest.ReadEmptyController (0 ms)
[ RUN      ] GetCgroupsFromProcCgroupsTest.ReadExampleFile
[       OK ] GetCgroupsFromProcCgroupsTest.ReadExampleFile (0 ms)
[----------] 4 tests from GetCgroupsFromProcCgroupsTest (1 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Add function to read /proc/{pid}/cgroup
Tom Hromatka [Mon, 4 Nov 2019 23:16:00 +0000 (23:16 +0000)] 
api.c: Add function to read /proc/{pid}/cgroup

This commit adds a function that, given a pid, can read in
the controllers and cgroups listed in /proc/{pid}/cgroup.
The caller is expected to allocate the controller_list[]
and cgroup_list[] arrays as well as null each entry in the
arrays.  cg_get_cgroups_from_proc_cgroups() will allocate
the necessary memory for each string within the arrays.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Add unit tests for cgroup_parse_rules_options()
Tom Hromatka [Mon, 4 Nov 2019 23:15:32 +0000 (23:15 +0000)] 
tests: Add unit tests for cgroup_parse_rules_options()

The following tests are in this commit:

RulesOptions_Ignore() - The valid option "ignore" is tested

RulesOptions_IgnoreWithComma() - The valid (but syntactically
    strange) option "ignore," is tested

RulesOptions_InvalidOption() - An invalid option is tested

RulesOptions_InvalidOption2() - An invalid option along with
    a valid option is tested

RulesOptions_EmptyOptions() - An empty string is tested

RulesOptions_NullOptions() - A null-pointer option string is
    tested

The results from googletest are reported below:

[----------] 6 tests from ParseRulesOptionsTest
[ RUN      ] ParseRulesOptionsTest.RulesOptions_Ignore
[       OK ] ParseRulesOptionsTest.RulesOptions_Ignore (0 ms)
[ RUN      ] ParseRulesOptionsTest.RulesOptions_IgnoreWithComma
[       OK ] ParseRulesOptionsTest.RulesOptions_IgnoreWithComma (0 ms)
[ RUN      ] ParseRulesOptionsTest.RulesOptions_InvalidOption
[       OK ] ParseRulesOptionsTest.RulesOptions_InvalidOption (0 ms)
[ RUN      ] ParseRulesOptionsTest.RulesOptions_InvalidOption2
[       OK ] ParseRulesOptionsTest.RulesOptions_InvalidOption2 (0 ms)
[ RUN      ] ParseRulesOptionsTest.RulesOptions_EmptyOptions
[       OK ] ParseRulesOptionsTest.RulesOptions_EmptyOptions (0 ms)
[ RUN      ] ParseRulesOptionsTest.RulesOptions_NullOptions
[       OK ] ParseRulesOptionsTest.RulesOptions_NullOptions (0 ms)
[----------] 6 tests from ParseRulesOptionsTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi.c: Add options field for rules defined in cgrules conf file
Tom Hromatka [Mon, 4 Nov 2019 23:15:11 +0000 (23:15 +0000)] 
api.c: Add options field for rules defined in cgrules conf file

This commit adds a fourth field called options to the rules
entry in cgrules configuration files.  Note that the field is
optional and existing rules will be parsed exactly as before.

Also, this commit only adds the parsing of the options field.
It doesn't change the rule behavior logic; that will come in
a subsequent commit.

An example cgrules.conf using this feature:

<user>    <controller>    <destination>           <options>
*         cpu             MyCgroup                ignore
*         cpu             DefaultCgroup

In the above example (and once the subsequent processing is
added), any process currently in the cpu controller and
MyCgroup will be ignored by cgrulesengd.  In other words,
cgrules will not try to move these processes to another cgroup
or manage them in any fashion.  It is anticipated that a
separate user process (outside of the scope of libcgroup) will
manage pids in this scenario.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoapi: Increase MAX_MNT_ELEMENTS to 16
Tom Hromatka [Mon, 18 Nov 2019 16:32:08 +0000 (16:32 +0000)] 
api: Increase MAX_MNT_ELEMENTS to 16

Newer kernels now support many cgroup controllers; thus we
need to increase the number of elements we can safely read
from /proc/{pid}/cgroup.  This commit increases the number
of elements from 8 to 16.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agotests: Add STATIC and UNIT_TEST definitions to automake files
Tom Hromatka [Mon, 4 Nov 2019 23:14:16 +0000 (23:14 +0000)] 
tests: Add STATIC and UNIT_TEST definitions to automake files

This commit adds two new definitions to the automake Makefile.am
files.  This change has no effect on the deliverable binaries.

STATIC - For a standard libcgroup library build, STATIC is equal
         to the "static" keyword and behaves exactly the same.
         For a unit test build, STATIC is mapped to an empty
         string.  This allows the unit tests to invoke static
         functions that would otherwise be unavailable to the
         test suite.

UNIT_TEST - This define is used in libcgroup-internal.h to
         wrap the function prototypes of STATIC functions.
         In a standard libcgroup library build, UNIT_TEST
         is _not_ defined, and thus the prototypes are not
         available.  In a unit test build, UNIT_TEST is
         defined and the function prototypes are available
         for the unit tests to use.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
5 years agoREADME: Add README.md
Tom Hromatka [Thu, 10 Oct 2019 15:52:03 +0000 (09:52 -0600)] 
README: Add README.md

This commit adds README.md.  It contains graphics that show the
status of the continuous integration and code coverage results.
It also contains a link to the original README.  Note that github
doesn't allow the inlining of files in markdown due to security
concerns, so a link is the best we can do.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Dhaval Giani <dhaval.giani@oracle.com>
5 years agotests: Gather code coverage data after a successful CI run
Tom Hromatka [Mon, 23 Sep 2019 22:14:50 +0000 (22:14 +0000)] 
tests: Gather code coverage data after a successful CI run

Upon successful completion of a continuous integration (CI) build
through Travis CI, this commit enables coveralls.io to generate
code coverage results by utilizing the gcov data generated
during both the unit and functional test runs.

Note that the m4/ax_code_coverage.m4 file is provided by the
autoconf team, and it has been used verbatim.  The original
file is available here:
https://git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_code_coverage.m4

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Dhaval Giani <dhaval.giani@oracle.com>