-
Notifications
You must be signed in to change notification settings - Fork 110
Add utility functions for invoking a subprocess #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/process.c
Outdated
|
||
int exec_ret = execvp(cmd, argv.data); | ||
|
||
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING("Failed to execute process: %d", exec_ret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING("Failed to execute process: %d", exec_ret); | |
if (exec_ret == -1) { | |
int error = errno; | |
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING("Failed to execute process: %d (%s)", error, strerror(error)); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
execvp
will only return if there is an error. The docs say it will return -1, but I'm not really sure what we'd do in this process if it returned anything else anyway - we'd still want to let the user know that something went wrong. I'll take the strerror change though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied the error string portion in 7d97d42.
src/process.c
Outdated
for(size_t i = 0; i < argv.size; i++) { | ||
argv.data[i] = NULL; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do not think we need to do this, because rcutils_string_array_fini
does that with deallocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually "borrowing" the individual strings from the passed argument array, and if they're still referenced in argv
when I deallocate it, they will be deallocated as well.
This really shouldn't be an rcutils_string_array_t
anyway. I think I'll switch to just a calloc/memcpy to simplify the code and make that clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to a standalone zero_allocate in 7d97d42.
&allocator, "allocator is invalid", return ); | ||
|
||
#if defined _WIN32 || defined __CYGWIN__ | ||
CloseHandle(process->handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious here, Linux doesn't require to close the process ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linux doesn't actually open any handles to the process, as far as I can tell. The PID of the child process will remain allocated until the parent process reads the exit code from that process once, at which point the PID is released for reuse elsewhere.
This is why I note in the docs:
Upon successful invocation of this function, subsequent calls will produce undefined behavior. It should typically be followed by a call to rcutils_process_close().
This new function and associated functions provide a cross-platform mechanism for invoking a command as a subprocess of the currently running process. Signed-off-by: Scott K Logan <logans@cottsay.net>
b0b8737
to
7d97d42
Compare
Alright, I rebased to take the string join change from |
Signed-off-by: Scott K Logan <logans@cottsay.net>
13872ec
to
5f893a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with green CI
This new function and associated functions provide a cross-platform mechanism for invoking a command as a subprocess of the currently running process.
There is some nuanced behavior for how process IDs are freed on Linux and Windows that influenced this API:
Requires #490