3 Answers
3

The Posix standard uname function (implemented in the uname(2) syscall) is dynamically giving you the information about the CPU. You probably want the machine field.

Caution about x86-64 kernels running a 32 bit program (e.g. a 32 bits Debian distribution chroot-ed in a 64 bits Debian, or perhaps a 32 bits ELF binary running on a 64 bits system); I have no idea what they give in that situation; I would imagine some x86_64 in that case, since the kernel does not really know about the binaries and libc of the system.

Right. In fact, those odd situations (x86 ELF on x86-64 kernel) are the only reasons to call uname dynamically at all. Ideally, check a more general macro or heuristic (eg _LP64) to detect 32/64 as appropriate on SPARC, POWER, etc too.
–
Nicholas WilsonJan 2 '13 at 15:19

+1 for personality(2). Never seen that before. See also setarch(8) for more information where it confirms that PER_LINUX32 sneakily causes uname to lie!
–
Nicholas WilsonJan 2 '13 at 16:28