Injector
injector.h
Go to the documentation of this file.
1 /* -*- indent-tabs-mode: nil -*-
2  *
3  * injector - Library for injecting a shared library into a Linux process
4  *
5  * URL: https://github.com/kubo/injector
6  *
7  * ------------------------------------------------------
8  *
9  * Copyright (C) 2018-2023 Kubo Takehiro <kubo@jiubao.org>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
30 #ifndef INJECTOR_H
31 #define INJECTOR_H
32 
33 #if defined(_WIN32)
34 #include <windows.h>
35 typedef DWORD injector_pid_t;
36 #else
37 #include <sys/types.h>
38 
42 typedef pid_t injector_pid_t;
43 #endif
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 #if 0
49 }
50 #endif
51 
52 #define INJERR_SUCCESS 0 /* linux, windows, macos */
53 #define INJERR_OTHER -1 /* linux, windows, macos */
54 #define INJERR_NO_MEMORY -2 /* linux, windows, macos */
55 #define INJERR_NO_PROCESS -3 /* linux, windows, macos */
56 #define INJERR_NO_LIBRARY -4 /* linux */
57 #define INJERR_NO_FUNCTION -4 /* linux */
58 #define INJERR_ERROR_IN_TARGET -5 /* linux, windows, macos */
59 #define INJERR_FILE_NOT_FOUND -6 /* linux, windows, macos */
60 #define INJERR_INVALID_MEMORY_AREA -7 /* linux, macos */
61 #define INJERR_PERMISSION -8 /* linux, windows, macos */
62 #define INJERR_UNSUPPORTED_TARGET -9 /* linux, windows, macos */
63 #define INJERR_INVALID_ELF_FORMAT -10 /* linux */
64 #define INJERR_WAIT_TRACEE -11 /* linux */
65 #define INJERR_FUNCTION_MISSING -12 /* linux, windows, macos */
66 
67 typedef struct injector injector_t;
68 
75 int injector_attach(injector_t **injector, injector_pid_t pid);
76 
82 int injector_detach(injector_t *injector);
83 
99 int injector_inject(injector_t *injector, const char *path, void **handle);
100 
109 int injector_uninject(injector_t *injector, void *handle);
110 
111 #if defined(INJECTOR_DOC) || defined(__linux__) || defined(__APPLE__)
128 int injector_call(injector_t *injector, void *handle, const char* name);
129 #endif
130 
135 const char *injector_error(void);
136 
137 #if defined(INJECTOR_DOC) || defined(__linux__) || defined(_WIN32)
138 #define INJECTOR_HAS_REMOTE_CALL_FUNCS 1
139 #include <stdarg.h>
140 #include <stdint.h>
141 
172 int injector_remote_func_addr(injector_t *injector, void *handle, const char* name, size_t *func_addr_out);
173 
190 int injector_remote_call(injector_t *injector, intptr_t *retval, size_t func_addr, ...);
191 
208 int injector_remote_vcall(injector_t *injector, intptr_t *retval, size_t func_addr, va_list ap);
209 #endif
210 
211 #if defined(INJECTOR_DOC) || defined(_WIN32)
219 int injector_inject_w(injector_t *injector, const wchar_t *path, void **handle);
220 #endif
221 
222 #if defined(INJECTOR_DOC) || (defined(__linux__) && defined(__x86_64__))
223 #define INJECTOR_HAS_INJECT_IN_CLONED_THREAD 1 // feature test macro
235 int injector_inject_in_cloned_thread(injector_t *injector, const char *path, void **handle);
236 #endif
237 
238 #if 0
239 {
240 #endif
241 #ifdef __cplusplus
242 }; /* extern "C" */
243 #endif
244 
245 #endif
pid_t injector_pid_t
Platform-dependent process id type (pid_t on Unix. DWORD on Windows)
Definition: injector.h:42
int injector_remote_vcall(injector_t *injector, intptr_t *retval, size_t func_addr, va_list ap)
Call the function in the target process (Linux and Windows only)
int injector_remote_call(injector_t *injector, intptr_t *retval, size_t func_addr,...)
Call the function in the target process (Linux and Windows only)
int injector_call(injector_t *injector, void *handle, const char *name)
Call the specified function taking no arguments in the target process (Linux and macOS only)
int injector_remote_func_addr(injector_t *injector, void *handle, const char *name, size_t *func_addr_out)
Get the function address in the target process (Linux and Windows only)
const char * injector_error(void)
Get the message of the last error.
int injector_inject(injector_t *injector, const char *path, void **handle)
Inject the specified shared library into the target process.
int injector_attach(injector_t **injector, injector_pid_t pid)
Attach to the specified process.
int injector_inject_w(injector_t *injector, const wchar_t *path, void **handle)
Same with injector_inject except the type of the path argument. (Windows only)
int injector_uninject(injector_t *injector, void *handle)
Uninject the shared library specified by handle.
int injector_detach(injector_t *injector)
Detach from the attached process and destroy the specified handle.