Update for port_inspector

This commit is contained in:
Karmaz95 committed 2024-12-17 02:11:37 +01:00
1 parent 975b88ffcc
commit fe6dcb3b79
1 file changed
+35 -79
+35 -79
View File
@@ -1,27 +1,7 @@
// 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) {
@@ -29,79 +9,55 @@ int main(int argc, char *argv[]) {
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) {
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
ipc_info_space_t space_info; ipc_info_space_t space_info;
ipc_info_name_array_t table;
mach_msg_type_number_t table_count;
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); kr = mach_port_space_info(task, &space_info, &table_info, &table_infoCnt, &tree_info, &tree_infoCnt);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
printf("Failed to get port space info: %s\n", mach_error_string(kr)); printf("Failed to get port space info: %s\n", mach_error_string(kr));
mach_port_deallocate(mach_task_self(), target_task);
return 1; return 1;
} }
printf("\nProcess %d Port Space Information:\n", pid); printf("IPC Space Info:\n");
printf("Total ports: %d\n", table_count); printf("Table size: %d, Next: %d, Active ports: %d\n\n",
printf("Space generation number mask: 0x%x\n\n", space_info.iis_genno_mask); space_info.iis_table_size, space_info.iis_table_next, table_infoCnt);
// Print information about each port printf("%-6s | %-6s | %-20s | %-4s | %-10s\n",
for (mach_msg_type_number_t i = 0; i < table_count; i++) { "Index", "Name", "Rights", "Refs", "Generation");
if (table[i].iin_type != MACH_PORT_TYPE_NONE) { printf("-------------------------------------------------------------------\n");
printf("Port %d:\n", i);
printf(" Name: 0x%x\n", table[i].iin_name);
printf(" Type: 0x%x (", table[i].iin_type);
// Decode port type for (int i = 0; i < table_infoCnt; i++) {
if (table[i].iin_type & MACH_PORT_TYPE_SEND) char rights[32] = "";
printf("SEND "); if (table_info[i].iin_type & MACH_PORT_TYPE_RECEIVE) strcat(rights, "RECV ");
if (table[i].iin_type & MACH_PORT_TYPE_RECEIVE) if (table_info[i].iin_type & MACH_PORT_TYPE_SEND) strcat(rights, "SEND ");
printf("RECEIVE "); if (table_info[i].iin_type & MACH_PORT_TYPE_SEND_ONCE) strcat(rights, "ONCE ");
if (table[i].iin_type & MACH_PORT_TYPE_SEND_ONCE) if (table_info[i].iin_type & MACH_PORT_TYPE_PORT_SET) strcat(rights, "SET ");
printf("SEND_ONCE "); if (table_info[i].iin_type & MACH_PORT_TYPE_DEAD_NAME) strcat(rights, "DEAD ");
if (table[i].iin_type & MACH_PORT_TYPE_PORT_SET) if (rights[0] == '\0') strcpy(rights, "IO_NULL");
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); printf("%-6d | 0x%-4x | %-20s | %-4d | %-10d\n",
i,
// Get additional port info for RECEIVE rights table_info[i].iin_name,
if (table[i].iin_type & MACH_PORT_TYPE_RECEIVE) { rights,
print_port_info(target_task, table[i].iin_name); table_info[i].iin_urefs,
} MACH_PORT_INDEX(table_info[i].iin_name) >> 24);
printf("\n");
}
} }
// Cleanup mach_port_deallocate(mach_task_self(), task);
vm_deallocate(mach_task_self(), (vm_address_t)table, vm_deallocate(mach_task_self(), (vm_address_t)table_info, table_infoCnt * sizeof(*table_info));
table_count * sizeof(ipc_info_name_t)); vm_deallocate(mach_task_self(), (vm_address_t)tree_info, tree_infoCnt * sizeof(*tree_info));
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; return 0;
} }