Some developers may want to determine programmatically whether an application is running using Rosetta. For example, a developer writing device interface code may need to determine whether the user client is using the same endian format as the kernel.
Listing A-3 is a utility routine that can call the sysctlbyname function on a process ID (pid). If you pass a process ID of 0 to the routine, it performs the call on the current process. Otherwise it performs the call on the process specified by the pid value that you pass. (For information on sysctlbyname see Mac OS X Man Pages.)
Listing A-3 A utility routine for calling the sysctlbyname function
static int sysctlbyname_with_pid (const char *name, pid_t pid, |
void *oldp, size_t *oldlenp, |
void *newp, size_t newlen) |
{ |
if (pid == 0) { |
if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1) { |
fprintf(stderr, "sysctlbyname_with_pid(0): sysctlbyname failed:" |
"%s\n", strerror(errno)); |
return -1; |
} |
} else { |
int mib[CTL_MAXNAME]; |
size_t len = CTL_MAXNAME; |
if (sysctlnametomib(name, mib, &len) == -1) { |
fprintf(stderr, "sysctlbyname_with_pid: sysctlnametomib failed:" |
"%s\n", strerror(errno)); |
return -1; |
} |
mib[len] = pid; |
len++; |
if (sysctl(mib, len, oldp, oldlenp, newp, newlen) == -1) { |
fprintf(stderr, "sysctlbyname_with_pid: sysctl failed:" |
"%s\n", strerror(errno)); |
return -1; |
} |
} |
return 0; |
} |
The is_pid_native routine shown in Listing A-4calls the sysctlbyname_with_pid routine, passing the string "sysctl.proc_native". The is_pid_native routine determines whether the specified process is running natively or translated. The routine returns:
0 if the process is running translated using Rosetta
1 if the process is running natively on a PowerPC- or Intel-based Macintosh
–1 if an unexpected error occurs
Listing A-4 A routine that determines whether a process is running natively or translated
int is_pid_native (pid_t pid) |
{ |
int ret = 0; |
size_t sz = sizeof(ret); |
if (sysctlbyname_with_pid("sysctl.proc_native", pid, |
&ret, &sz, NULL, 0) == -1) { |
if (errno == ENOENT) { |
return 1; |
} |
fprintf(stderr, "is_pid_native: sysctlbyname_with_pid failed:" |
"%s\n", strerror(errno)); |
return -1; |
} |
return ret; |
} |
Last updated: 2007-02-26