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