Mstdlib-1.24.0
m_io_process.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_PROCESS_H__
25#define __M_IO_PROCESS_H__
26
27#include <mstdlib/base/m_defs.h>
28#include <mstdlib/base/m_types.h>
29#include <mstdlib/io/m_io.h>
30#include <mstdlib/io/m_event.h>
31
32__BEGIN_DECLS
33
34/*! \addtogroup m_io_process Start a process and perform I/O using stdin, stdout, and stderr
35 * \ingroup m_eventio_base
36 *
37 * A process starts an executable and opens the processes communication endpoints of
38 * stdin, stdout, and stderr as unidirectional pipes for communicating with the process.
39 *
40 * @{
41 */
42
43
44/*! Create a process and return IO handles for the process itself as well as
45 * unidirectional pipes for stdin, stdout, and stderr.
46 *
47 * Validation will be performed on the command upon calling this function, but
48 * the process will not actually be started until the 'proc' returned value is
49 * associated with an event loop. It is possible that the process may fail to
50 * start due to system resource limitations or missing validations for system
51 * ACLs; an ERROR event will be delivered in such a case. Otherwise if the
52 * process is started successfully, a DISCONNECT will be triggered on exit
53 * regardless of the process exit code (unless the specified timeout is
54 * exceeded, then an ERROR) event will be triggered.
55 *
56 * NOTE: If using stdin to pass data to the command, stdin MUST be closed once
57 * done to notify the remote process there is no more data or the process
58 * may hang indefinitely.
59 *
60 * \param[in] command Required. Command to execute. If an absolute path or relative path is not provided, will
61 * search the PATH environment variable, which can be overwritten in the 'env' parameter and
62 * honored. On Windows, the current working directory is also scanned as is common practice
63 * (but not on any other OS).
64 * \param[in] args Optional. List of arguments to pass to command.
65 * \param[in] env Optional. List of environment variables to pass on to process. Use NULL to pass current environment through.
66 * \param[in] timeout_ms Optional. Maximum execution time of the process before it is forcibly terminated. Use 0 for infinite.
67 * \param[out] proc Required. The io object handle for the process itself. Used to notify when the process has exited, or request termination of process.
68 * \param[out] proc_stdin Optional. The io object handle for the write-only stdin process handle. If NULL is passed, will close handle to process. The
69 * stdin endpoint will not be notified when the process closes so it must be tracked and closed when the process exits by the caller.
70 * \param[out] proc_stdout Optional. The io object handle for the read-only stdout process handle. If NULL is passed, will close handle to process.
71 * \param[out] proc_stderr Optional. The io object handle for the read-only stderr process handle. If NULL is passed, will close handle to process.
72 *
73 * \return M_IO_ERROR_SUCCESS on SUCCESS. Otherwise, M_IO_ERROR_INVALID on misuse, M_IO_ERROR_NOTFOUND if specified executable not found, M_IO_ERROR_NOTPERM if execution not permitted.
74 */
75M_API M_io_error_t M_io_process_create(const char *command, M_list_str_t *args, M_hash_dict_t *env, M_uint64 timeout_ms, M_io_t **proc, M_io_t **proc_stdin, M_io_t **proc_stdout, M_io_t **proc_stderr);
76
77/*! Retrieve the result code of the process.
78 *
79 * \param[in] proc Proc IO object returned from M_io_process_create();
80 * \param[out] result_code Exit code returned from process.
81 *
82 * \return M_TRUE if process exited with a return code. M_FALSE if invalid request due to process state or other error.
83 */
84M_API M_bool M_io_process_get_result_code(M_io_t *proc, int *result_code);
85
86/*! Retrieve the OS Process ID of the process.
87 *
88 * \param[in] proc Proc IO object returned from M_io_process_create();
89 * \return Return the process id of the process
90 */
92
93/*! @} */
94
95__END_DECLS
96
97#endif
98
struct M_hash_dict M_hash_dict_t
Definition: m_hash_dict.h:52
M_bool M_io_process_get_result_code(M_io_t *proc, int *result_code)
M_io_error_t M_io_process_create(const char *command, M_list_str_t *args, M_hash_dict_t *env, M_uint64 timeout_ms, M_io_t **proc, M_io_t **proc_stdin, M_io_t **proc_stdout, M_io_t **proc_stderr)
int M_io_process_get_pid(M_io_t *proc)
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59
struct M_list_str M_list_str_t
Definition: m_list_str.h:80