I have did a self signed code codesign -- bu开发者_StackOverflow中文版t not sure why vm_write is failing.(I am using 10.6.6)
trying to figure out what is missing to make it work -- I appreciate some help of this issues.
#include <mach/vm_map.h>
#include <mach/mach_traps.h>
#include <mach/mach_error.h>
int main (int argc, const char * argv[])
{
//get the task for PID
kern_return_t err;
int pid = 73002; // PID of process in the system
mach_port_name_t t;
err = task_for_pid(mach_task_self(), pid, &t);
if(0 != err)
{
printf("task_for_pid : %s",mach_error_string(err));
}
vm_address_t address;
vm_size_t size = 108;
err = vm_allocate(t,&address,size,TRUE);
if(0 != err)
{
printf("vm_allocate : %s",mach_error_string(err));
}
vm_offset_t data = pid;
mach_msg_type_number_t dataCnt = sizeof(pid);
err = vm_write(t,address,data,dataCnt);
if(0 != err)
{
printf("vm_write : %s",mach_error_string(err));
}
return 0;
}
Update:
self signed code codesign --- I have generated the certificate in the system and used for codesign.
vm_write returns (os/kern) invalid address (return value is "1")
You should be using the more-up-to-date mach_vm_address_t, mach_vm_size_t, mach_vm_allocate, mach_vm_offset_t, mach_vm_write(), etc. As far as I remember from experience, the older API's don't particularly work well on 64-bit processes (which can be quite relevant on 10.6). Also, use pid_t instead of int for the process ID.
The other poster is right that you're incorrectly passing the third argument to vm_write, and perhaps you want to pass &data, but you should be using mach_vm_write by the way. I'd expect your program to crash or try to write garbage with the way you were using vm_write, but the error returned seems to be KERN_INVALID_ADDRESS, which indicates that it failed writing to the memory address in the process.
I reckon you signed your code correctly if task_for_pid returns without errors. You should also use the constant KERN_SUCCESS, not 0 where appropriate.
The third argument of vm_write
takes a pointer to a memory location (pointer_t
), yet you pass the PID which simply isn't a valid memory location. You need to pass a correct memory location, like:
char *text = "123";
err = vm_write(t, address, text, strlen(text));
... which would pass the pointer to a static string.
精彩评论