Update for port_inspector

This commit is contained in:
Karmaz95
2024-12-17 02:11:37 +01:00
parent 975b88ffcc
commit fe6dcb3b79
+49 -93
View File
@@ -1,107 +1,63 @@
// gcc -o port_inspector port_inspector.c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <mach/mach.h> #include <mach/mach.h>
#include <mach/mach_error.h> #include <unistd.h>
#include <stdlib.h>
void print_port_info(task_t task, mach_port_name_t port_name) {
mach_port_status_t status;
mach_msg_type_number_t status_count = MACH_PORT_RECEIVE_STATUS_COUNT;
kern_return_t kr;
kr = mach_port_get_attributes(task,
port_name,
MACH_PORT_RECEIVE_STATUS,
(mach_port_info_t)&status,
&status_count);
if (kr == KERN_SUCCESS) {
printf(" Queue size: %d\n", status.mps_msgcount);
printf(" Max queue size: %d\n", status.mps_qlimit);
}
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <pid>\n", argv[0]); printf("Usage: %s <pid>\n", argv[0]);
return 1; return 1;
} }
task_t target_task; pid_t target_pid = atoi(argv[1]);
pid_t pid = atoi(argv[1]); kern_return_t kr;
mach_port_t task;
kern_return_t kr = task_for_pid(mach_task_self(), pid, &target_task); kr = task_for_pid(mach_task_self(), target_pid, &task);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
printf("Failed to get task for pid %d: %s\n", pid, mach_error_string(kr)); printf("Failed to get task for pid %d: %s\n", target_pid, mach_error_string(kr));
return 1; return 1;
} }
// Get task basic info ipc_info_name_array_t table_info;
task_basic_info_data_t basic_info; mach_msg_type_number_t table_infoCnt;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; ipc_info_tree_name_array_t tree_info;
kr = task_info(target_task, TASK_BASIC_INFO, (task_info_t)&basic_info, &count); mach_msg_type_number_t tree_infoCnt;
if (kr != KERN_SUCCESS) { ipc_info_space_t space_info;
printf("Failed to get task info: %s\n", mach_error_string(kr));
mach_port_deallocate(mach_task_self(), target_task);
return 1;
}
// Get port info kr = mach_port_space_info(task, &space_info, &table_info, &table_infoCnt, &tree_info, &tree_infoCnt);
ipc_info_space_t space_info; if (kr != KERN_SUCCESS) {
ipc_info_name_array_t table; printf("Failed to get port space info: %s\n", mach_error_string(kr));
mach_msg_type_number_t table_count; return 1;
ipc_info_tree_name_array_t tree; }
mach_msg_type_number_t tree_count;
kr = mach_port_space_info(target_task, &space_info, &table, &table_count, &tree, &tree_count); printf("IPC Space Info:\n");
if (kr != KERN_SUCCESS) { printf("Table size: %d, Next: %d, Active ports: %d\n\n",
printf("Failed to get port space info: %s\n", mach_error_string(kr)); space_info.iis_table_size, space_info.iis_table_next, table_infoCnt);
mach_port_deallocate(mach_task_self(), target_task);
return 1;
}
printf("\nProcess %d Port Space Information:\n", pid); printf("%-6s | %-6s | %-20s | %-4s | %-10s\n",
printf("Total ports: %d\n", table_count); "Index", "Name", "Rights", "Refs", "Generation");
printf("Space generation number mask: 0x%x\n\n", space_info.iis_genno_mask); printf("-------------------------------------------------------------------\n");
// Print information about each port for (int i = 0; i < table_infoCnt; i++) {
for (mach_msg_type_number_t i = 0; i < table_count; i++) { char rights[32] = "";
if (table[i].iin_type != MACH_PORT_TYPE_NONE) { if (table_info[i].iin_type & MACH_PORT_TYPE_RECEIVE) strcat(rights, "RECV ");
printf("Port %d:\n", i); if (table_info[i].iin_type & MACH_PORT_TYPE_SEND) strcat(rights, "SEND ");
printf(" Name: 0x%x\n", table[i].iin_name); if (table_info[i].iin_type & MACH_PORT_TYPE_SEND_ONCE) strcat(rights, "ONCE ");
printf(" Type: 0x%x (", table[i].iin_type); if (table_info[i].iin_type & MACH_PORT_TYPE_PORT_SET) strcat(rights, "SET ");
if (table_info[i].iin_type & MACH_PORT_TYPE_DEAD_NAME) strcat(rights, "DEAD ");
if (rights[0] == '\0') strcpy(rights, "IO_NULL");
// Decode port type printf("%-6d | 0x%-4x | %-20s | %-4d | %-10d\n",
if (table[i].iin_type & MACH_PORT_TYPE_SEND) i,
printf("SEND "); table_info[i].iin_name,
if (table[i].iin_type & MACH_PORT_TYPE_RECEIVE) rights,
printf("RECEIVE "); table_info[i].iin_urefs,
if (table[i].iin_type & MACH_PORT_TYPE_SEND_ONCE) MACH_PORT_INDEX(table_info[i].iin_name) >> 24);
printf("SEND_ONCE "); }
if (table[i].iin_type & MACH_PORT_TYPE_PORT_SET)
printf("PORT_SET ");
if (table[i].iin_type & MACH_PORT_TYPE_DEAD_NAME)
printf("DEAD_NAME ");
printf(")\n");
printf(" User References: %d\n", table[i].iin_urefs); mach_port_deallocate(mach_task_self(), task);
vm_deallocate(mach_task_self(), (vm_address_t)table_info, table_infoCnt * sizeof(*table_info));
// Get additional port info for RECEIVE rights vm_deallocate(mach_task_self(), (vm_address_t)tree_info, tree_infoCnt * sizeof(*tree_info));
if (table[i].iin_type & MACH_PORT_TYPE_RECEIVE) { return 0;
print_port_info(target_task, table[i].iin_name);
}
printf("\n");
}
}
// Cleanup
vm_deallocate(mach_task_self(), (vm_address_t)table,
table_count * sizeof(ipc_info_name_t));
vm_deallocate(mach_task_self(), (vm_address_t)tree,
tree_count * sizeof(ipc_info_tree_name_t));
mach_port_deallocate(mach_task_self(), target_task);
return 0;
} }