2021-01-03 14:55:15 +01:00
|
|
|
#ifndef SC_PROCESS_H
|
|
|
|
|
#define SC_PROCESS_H
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2021-01-08 19:18:02 +01:00
|
|
|
#include "common.h"
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2021-01-08 19:24:51 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
#ifdef _WIN32
|
2019-02-10 13:16:55 +01:00
|
|
|
|
2019-03-02 20:09:56 +01:00
|
|
|
// not needed here, but winsock2.h must never be included AFTER windows.h
|
|
|
|
|
# include <winsock2.h>
|
2019-02-10 13:16:55 +01:00
|
|
|
# include <windows.h>
|
2019-06-10 15:14:10 +02:00
|
|
|
# define PATH_SEPARATOR '\\'
|
2018-03-16 08:51:46 +01:00
|
|
|
# define PRIexitcode "lu"
|
2019-02-10 13:16:55 +01:00
|
|
|
// <https://stackoverflow.com/a/44383330/1987178>
|
2020-11-14 22:08:59 +01:00
|
|
|
# define PRIsizet "Iu"
|
2017-12-12 15:12:07 +01:00
|
|
|
# define PROCESS_NONE NULL
|
2019-11-27 13:39:42 +01:00
|
|
|
# define NO_EXIT_CODE -1u // max value as unsigned
|
2017-12-12 15:12:07 +01:00
|
|
|
typedef HANDLE process_t;
|
|
|
|
|
typedef DWORD exit_code_t;
|
2020-11-14 22:12:07 +01:00
|
|
|
typedef HANDLE pipe_t;
|
2019-02-10 13:16:55 +01:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
#else
|
2019-02-10 13:16:55 +01:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
# include <sys/types.h>
|
2019-06-10 15:14:10 +02:00
|
|
|
# define PATH_SEPARATOR '/'
|
2019-02-10 13:16:55 +01:00
|
|
|
# define PRIsizet "zu"
|
|
|
|
|
# define PRIexitcode "d"
|
2017-12-12 15:12:07 +01:00
|
|
|
# define PROCESS_NONE -1
|
2019-11-27 13:39:42 +01:00
|
|
|
# define NO_EXIT_CODE -1
|
2017-12-12 15:12:07 +01:00
|
|
|
typedef pid_t process_t;
|
|
|
|
|
typedef int exit_code_t;
|
2020-11-14 22:12:07 +01:00
|
|
|
typedef int pipe_t;
|
2019-02-10 13:16:55 +01:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
#endif
|
2019-02-10 13:16:55 +01:00
|
|
|
|
2018-09-04 08:42:25 +02:00
|
|
|
enum process_result {
|
|
|
|
|
PROCESS_SUCCESS,
|
|
|
|
|
PROCESS_ERROR_GENERIC,
|
|
|
|
|
PROCESS_ERROR_MISSING_BINARY,
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-03 14:55:15 +01:00
|
|
|
// execute the command and write the result to the output parameter "process"
|
2019-03-02 20:09:56 +01:00
|
|
|
enum process_result
|
2021-01-03 14:55:15 +01:00
|
|
|
process_execute(const char *const argv[], process_t *process);
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2020-11-14 22:12:07 +01:00
|
|
|
enum process_result
|
|
|
|
|
process_execute_redirect(const char *const argv[], process_t *process,
|
|
|
|
|
pipe_t *pipe_stdin, pipe_t *pipe_stdout,
|
|
|
|
|
pipe_t *pipe_stderr);
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
process_terminate(process_t pid);
|
|
|
|
|
|
2021-01-03 14:55:15 +01:00
|
|
|
// kill the process
|
2019-03-02 23:52:22 +01:00
|
|
|
bool
|
2021-01-03 14:55:15 +01:00
|
|
|
process_terminate(process_t pid);
|
2019-03-02 20:09:56 +01:00
|
|
|
|
2021-01-03 17:06:08 +01:00
|
|
|
// wait and close the process (like waitpid())
|
2021-01-22 18:48:17 +01:00
|
|
|
// the "close" flag indicates if the process must be "closed" (reaped)
|
|
|
|
|
// (passing false is equivalent to enable WNOWAIT in waitid())
|
2021-01-22 18:29:21 +01:00
|
|
|
exit_code_t
|
2021-01-22 18:48:17 +01:00
|
|
|
process_wait(process_t pid, bool close);
|
2021-01-03 17:06:08 +01:00
|
|
|
|
|
|
|
|
// close the process
|
|
|
|
|
//
|
2021-01-22 18:48:17 +01:00
|
|
|
// Semantically, process_wait(close) = process_wait(noclose) + process_close
|
2021-01-03 17:06:08 +01:00
|
|
|
void
|
|
|
|
|
process_close(process_t pid);
|
|
|
|
|
|
2018-02-08 15:16:27 +01:00
|
|
|
// convenience function to wait for a successful process execution
|
|
|
|
|
// automatically log process errors with the provided process name
|
2019-03-02 23:52:22 +01:00
|
|
|
bool
|
2021-01-22 19:20:30 +01:00
|
|
|
process_check_success(process_t proc, const char *name, bool close);
|
2018-02-08 15:16:27 +01:00
|
|
|
|
2021-01-03 14:55:15 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
// only used to find package manager, not implemented for Windows
|
|
|
|
|
bool
|
|
|
|
|
search_executable(const char *file);
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-10 15:14:10 +02:00
|
|
|
// return the absolute path of the executable (the scrcpy binary)
|
2021-01-24 15:14:53 +01:00
|
|
|
// may be NULL on error; to be freed by free()
|
2019-06-10 15:14:10 +02:00
|
|
|
char *
|
|
|
|
|
get_executable_path(void);
|
|
|
|
|
|
2021-10-22 18:51:20 +02:00
|
|
|
// Return the absolute path of a file in the same directory as he executable.
|
|
|
|
|
// May be NULL on error. To be freed by free().
|
|
|
|
|
char *
|
|
|
|
|
get_local_file_path(const char *name);
|
|
|
|
|
|
2019-12-05 21:07:11 +01:00
|
|
|
// returns true if the file exists and is not a directory
|
|
|
|
|
bool
|
|
|
|
|
is_regular_file(const char *path);
|
|
|
|
|
|
2020-11-14 22:12:07 +01:00
|
|
|
ssize_t
|
|
|
|
|
read_pipe(pipe_t pipe, char *data, size_t len);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
close_pipe(pipe_t pipe);
|
|
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
#endif
|