Skip to content

Conversation

vpodzime
Copy link
Contributor

@vpodzime vpodzime commented Apr 20, 2021

In many places there's a need to keep track of sub-processes
spawned by the main process. Such processes need to be looked up
by various parameters, interacted with, and, eventually,
terminated.

Having a common, reliable and fast implementation with high test
coverage that holds the various pieces of information about
sub-processes together is beneficial.

Ticket: CFE-3572
Changelog: None

Steps:

  • initialization
  • adding subprocesses
  • getter functions
  • pop functions
  • termination functions
  • fix issues on exotics
  • add void *data to the Subprocess struct

@vpodzime vpodzime added the WIP Work in Progress label Apr 20, 2021
@vpodzime vpodzime force-pushed the master-proc_manager branch from d298749 to 7397247 Compare April 20, 2021 12:01
@vpodzime vpodzime force-pushed the master-proc_manager branch from 7397247 to 8d8774b Compare April 21, 2021 09:01
@vpodzime
Copy link
Contributor Author

I replaced the file descriptors by FILE streams. Our code works with them and while it's possible to go from a FILE stream to an FD (fileno()), the other direction is impossible. Lookup functions "by FD" still make sense to me, though.

@vpodzime vpodzime force-pushed the master-proc_manager branch from 8d8774b to 839af1f Compare April 21, 2021 09:10
@vpodzime vpodzime force-pushed the master-proc_manager branch 3 times, most recently from 80179dd to c61c251 Compare April 22, 2021 12:44
@NorthernTechHQ NorthernTechHQ deleted a comment from lgtm-com bot Apr 22, 2021
@NorthernTechHQ NorthernTechHQ deleted a comment from lgtm-com bot Apr 22, 2021
@vpodzime vpodzime force-pushed the master-proc_manager branch from c61c251 to 317bc63 Compare April 22, 2021 12:56
@vpodzime vpodzime changed the title WIP: Add a process manager "class" Add a process manager "class" Apr 22, 2021
@vpodzime vpodzime marked this pull request as ready for review April 22, 2021 13:27
@vpodzime
Copy link
Contributor Author

Actually, we will need the termination functionality for custom promise modules.

@vpodzime vpodzime force-pushed the master-proc_manager branch from 317bc63 to 38ee191 Compare April 22, 2021 20:26
@vpodzime vpodzime force-pushed the master-proc_manager branch 2 times, most recently from 60adb29 to af97d5a Compare April 23, 2021 09:02
@NorthernTechHQ NorthernTechHQ deleted a comment from lgtm-com bot Apr 23, 2021
@vpodzime
Copy link
Contributor Author

@olehermanse please review, if you have a chance. The only missing bit here, AFAICT, are unit tests for the termination functions.

@vpodzime vpodzime force-pushed the master-proc_manager branch from af97d5a to d8a4e45 Compare April 26, 2021 10:44
@vpodzime vpodzime force-pushed the master-proc_manager branch from d8a4e45 to ea96219 Compare April 26, 2021 10:47
@NorthernTechHQ NorthernTechHQ deleted a comment from lgtm-com bot Apr 26, 2021
@NorthernTechHQ NorthernTechHQ deleted a comment from lgtm-com bot Apr 26, 2021
@vpodzime vpodzime force-pushed the master-proc_manager branch from ea96219 to ce3a560 Compare April 26, 2021 16:37
@vpodzime vpodzime removed the WIP Work in Progress label Apr 26, 2021
@vpodzime vpodzime requested review from KotzaBoss and larsewi April 26, 2021 17:04
@vpodzime
Copy link
Contributor Author

@cf-bottom jenkins, please

@cf-bottom
Copy link

olehermanse
olehermanse previously approved these changes Apr 27, 2021
Copy link
Contributor

@olehermanse olehermanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, just some smaller comments.

Comment on lines +99 to +100
memmove(map->values + i, map->values + i + 1,
sizeof(MapKeyValue) * (map->size - i - 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially because of the memmove(), I think this function should have unit tests (which would be running using ASAN).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a copy-pasted code from ArrayMapRemove().

Copy link
Contributor Author

@vpodzime vpodzime Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we don't have any unit tests doing ArrayMapRemove(). I created CFE-3643 instead of trying to save the world in this PR.

Allowing values being removed from the map without being
destroyed (keys are always owned by the map and thus destroyed).

Ticket: CFE-3572
Changelog: None
In many places there's a need to keep track of sub-processes
spawned by the main process. Such processes need to be looked up
by various parameters and interacted with.

Having a common, reliable and fast implementation with high test
coverage that holds the various pieces of information about
sub-processes together is beneficial.

In the future, we should extend this new API with the process
spawning and termination functionality.

Ticket: CFE-3572
Changelog: None
@vpodzime
Copy link
Contributor Author

@cf-bottom jenkins with exotics, please

@cf-bottom
Copy link

Alright, I triggered a build:

Build Status

(with exotics)

Jenkins: https://ci.cfengine.com/job/pr-pipeline/6482/

Packages: http://buildcache.cfengine.com/packages/testing-pr/jenkins-pr-pipeline-6482/

@vpodzime vpodzime requested a review from olehermanse April 27, 2021 08:59
@vpodzime vpodzime added the WIP Work in Progress label Apr 27, 2021
@vpodzime
Copy link
Contributor Author

There are failures on exotics. And we also need to be able to associate some custom data with the sub-processes managed.

Copy link
Contributor

@olehermanse olehermanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks for working on this! I see there are some remaining TODOs, but feel free to address those in separate PRs.

pid_t pid; /** PID of the subprocess [always >= 0] */
FILE *input; /** stream of the input to the subprocess [can be %NULL] */
FILE *output; /** stream of the output from the subprocess [can be %NULL] */
char lookup_io; /** which stream/FD to use for lookup by stream/FD ['i', 'o' or '\0'] */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this seems like a setting that would be in the top level proc manager, not each subprocess entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on the direction of the communication with the particular subprocess.

* @return whether the subprocess was successfully added or not (e.g. in case of
* the process with the same id, PID or lookup stream/FD added before)
*
* @note Ownership of #id, #cmd and #description is transferred to the #manager.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the file streams? Are they closed by Subprocess / ProcManager?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in case of termination.

pid, input, output, 'o');
assert_true(success);

bool graceful_termination_failed = false;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool graceful_termination_failed = false;;
bool graceful_termination_failed = false;

close(parent_write_child_read[1]);
close(parent_read_child_write[0]);

/* Also close the ends the child should normally use, we don't need them. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Also close the ends the child should normally use, we don't need them. */
/* Also close the ends the child should normally use, we don't need them */

/* Ignore SIGTERM */
signal(SIGTERM, SIG_IGN);

/* Wait to be killed. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Wait to be killed. */
/* Wait to be killed */

Comment on lines +13 to +16
/**
* This file contains ProcManager tests that need to fork() a child process
* which totally confuses the unit test framework used by the other tests.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth mentioning that this file does not use said test framework.

Suggested change
/**
* This file contains ProcManager tests that need to fork() a child process
* which totally confuses the unit test framework used by the other tests.
*/
/**
* This file contains ProcManager tests that need to fork() a child process
* which totally confuses the unit test framework used by the other tests.
* This test doesn't use the framework, it instead implements it's own
* assert macros with failure printing and a "normal" main function.
*/

Comment on lines +342 to +346
/* Now try to insert the process again (with a different command and
* description). This shouldn't be allowed. */
ret = ProcManagerAddProcess(manager,
xstrdup("test-process"), xstrdup("/some/command2 arg1 arg2"), xstrdup("better test process"),
(pid_t) 1234, input, output, 'i');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear here whether this should fail because of id or pid or both.

Comment on lines +149 to +153
if (id == NULL)
{
NDEBUG_UNUSED int ret = xasprintf(&id, "%jd", (intmax_t) pid);
assert(ret > 0);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested functionality.

Copy link
Contributor

@olehermanse olehermanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are failures on exotics. And we also need to be able to associate some custom data with the sub-processes managed.

Need to be addressed one way or another, before merging.

@vpodzime vpodzime changed the title Add a process manager "class" WIP: Add a process manager "class" May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP Work in Progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants