| 1 | #ifndef AWS_COMMON_PROCESS_H |
| 2 | #define AWS_COMMON_PROCESS_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <aws/common/common.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | struct aws_run_command_result { |
| 13 | /* return code from running the command. */ |
| 14 | int ret_code; |
| 15 | |
| 16 | /** |
| 17 | * captured stdout message from running the command, |
| 18 | * caller is responsible for releasing the memory. |
| 19 | */ |
| 20 | struct aws_string *std_out; |
| 21 | |
| 22 | /** |
| 23 | * captured stderr message from running the command, |
| 24 | * caller is responsible for releasing the memory. |
| 25 | * It's currently not implemented and the value will be set to NULL. |
| 26 | */ |
| 27 | struct aws_string *std_err; |
| 28 | }; |
| 29 | |
| 30 | struct aws_run_command_options { |
| 31 | /** |
| 32 | * command path and commandline options of running that command. |
| 33 | */ |
| 34 | const char *command; |
| 35 | }; |
| 36 | |
| 37 | AWS_EXTERN_C_BEGIN |
| 38 | |
| 39 | /** |
| 40 | * Returns the current process's PID (process id). |
| 41 | * @return PID as int |
| 42 | */ |
| 43 | AWS_COMMON_API int aws_get_pid(void); |
| 44 | |
| 45 | /** |
| 46 | * Returns the soft limit for max io handles (max fds in unix terminology). This limit is one more than the actual |
| 47 | * limit. The soft limit can be changed up to the hard limit by any process regardless of permissions. |
| 48 | */ |
| 49 | AWS_COMMON_API size_t aws_get_soft_limit_io_handles(void); |
| 50 | |
| 51 | /** |
| 52 | * Returns the hard limit for max io handles (max fds in unix terminology). This limit is one more than the actual |
| 53 | * limit. This limit cannot be increased without sudo permissions. |
| 54 | */ |
| 55 | AWS_COMMON_API size_t aws_get_hard_limit_io_handles(void); |
| 56 | |
| 57 | /** |
| 58 | * Sets the new soft limit for io_handles (max fds). This can be up to the hard limit but may not exceed it. |
| 59 | * |
| 60 | * This operation will always fail with AWS_ERROR_UNIMPLEMENTED error code on Windows. |
| 61 | */ |
| 62 | AWS_COMMON_API int aws_set_soft_limit_io_handles(size_t max_handles); |
| 63 | |
| 64 | AWS_COMMON_API int aws_run_command_result_init(struct aws_allocator *allocator, struct aws_run_command_result *result); |
| 65 | |
| 66 | AWS_COMMON_API void aws_run_command_result_cleanup(struct aws_run_command_result *result); |
| 67 | |
| 68 | /** |
| 69 | * Currently this API is implemented using popen on Posix system and |
| 70 | * _popen on Windows to capture output from running a command. Note |
| 71 | * that popen only captures stdout, and doesn't provide an option to |
| 72 | * capture stderr. We will add more options, such as acquire stderr |
| 73 | * in the future so probably will alter the underlying implementation |
| 74 | * as well. |
| 75 | */ |
| 76 | AWS_COMMON_API int aws_run_command( |
| 77 | struct aws_allocator *allocator, |
| 78 | struct aws_run_command_options *options, |
| 79 | struct aws_run_command_result *result); |
| 80 | |
| 81 | AWS_EXTERN_C_END |
| 82 | AWS_POP_SANE_WARNING_LEVEL |
| 83 | |
| 84 | #endif /* AWS_COMMON_PROCESS_H */ |