add: getpid

This commit is contained in:
2023-10-22 19:48:55 +02:00
parent dd12830288
commit 851675f46a
3 changed files with 34 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ int main() {
if (!open_device())
return -1;
int pid = 55803;
int pid = get_pid("nsnake");
if (!open_process(pid))
return -1;

View File

@@ -24,6 +24,38 @@ void close_device(void)
close(file_desc);
}
int get_pid(const char *program_name) {
FILE *fp;
char command[128];
char buffer[128];
int pid = -1;
// Create a command to run 'pidof' for the specified program
snprintf(command, sizeof(command), "pidof %s", program_name);
// Open a pipe to execute the command and read the output
fp = popen(command, "r");
if (fp == NULL) {
perror("popen");
return -1;
}
// Read the output (should be a space-separated list of PIDs)
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
// Extract the first PID from the list
if (sscanf(buffer, "%d", &pid) == 1) {
}
}
// Close the pipe and check for errors
if (pclose(fp) == -1) {
perror("pclose");
return -1;
}
return pid;
}
void *RPM(uintptr_t address, ssize_t size)
{
struct s_RPM args;

View File

@@ -4,6 +4,7 @@
int open_device(void);
void close_device(void);
int get_pid(const char *program_name);
void *RPM(uintptr_t address, ssize_t size);
void WPM(uintptr_t addr, uintptr_t value, ssize_t size);
int open_process(int pid);