141ad7e73914ee13bd1467ce48a1532570218e327tom#include <elf.h>
241ad7e73914ee13bd1467ce48a1532570218e327tom#include <link.h>
341ad7e73914ee13bd1467ce48a1532570218e327tom#include <stdio.h>
441ad7e73914ee13bd1467ce48a1532570218e327tom#include <stddef.h>
541ad7e73914ee13bd1467ce48a1532570218e327tom#include <string.h>
641ad7e73914ee13bd1467ce48a1532570218e327tom#include <sys/types.h>
741ad7e73914ee13bd1467ce48a1532570218e327tom#include <sys/stat.h>
841ad7e73914ee13bd1467ce48a1532570218e327tom#include <fcntl.h>
941ad7e73914ee13bd1467ce48a1532570218e327tom#include <unistd.h>
1041ad7e73914ee13bd1467ce48a1532570218e327tom#include <stdlib.h>
1141ad7e73914ee13bd1467ce48a1532570218e327tom
1241ad7e73914ee13bd1467ce48a1532570218e327tomint
1341ad7e73914ee13bd1467ce48a1532570218e327tommain (int argc, char **argv, char **envp)
1441ad7e73914ee13bd1467ce48a1532570218e327tom{
1541ad7e73914ee13bd1467ce48a1532570218e327tom  ElfW(auxv_t) auxv;
1641ad7e73914ee13bd1467ce48a1532570218e327tom  ElfW(auxv_t) *auxv_p;
1741ad7e73914ee13bd1467ce48a1532570218e327tom
1841ad7e73914ee13bd1467ce48a1532570218e327tom  void *entry0 = NULL;
1941ad7e73914ee13bd1467ce48a1532570218e327tom  void *entry1 = NULL;
2041ad7e73914ee13bd1467ce48a1532570218e327tom
2141ad7e73914ee13bd1467ce48a1532570218e327tom  char *platform0 = NULL;
2241ad7e73914ee13bd1467ce48a1532570218e327tom  char *platform1 = NULL;
2341ad7e73914ee13bd1467ce48a1532570218e327tom
2441ad7e73914ee13bd1467ce48a1532570218e327tom  // First try the "traditional" way.
2541ad7e73914ee13bd1467ce48a1532570218e327tom  while (*envp++ != NULL)
2641ad7e73914ee13bd1467ce48a1532570218e327tom    ; /* Skip, skip, skip... and after finding a NULL we have the auxv. */
2741ad7e73914ee13bd1467ce48a1532570218e327tom
2841ad7e73914ee13bd1467ce48a1532570218e327tom  for (auxv_p = (ElfW(auxv_t) *) envp;
2941ad7e73914ee13bd1467ce48a1532570218e327tom       auxv_p->a_type != AT_NULL;
3041ad7e73914ee13bd1467ce48a1532570218e327tom       auxv_p++)
3141ad7e73914ee13bd1467ce48a1532570218e327tom    {
3241ad7e73914ee13bd1467ce48a1532570218e327tom      if (auxv_p->a_type == AT_ENTRY)
3341ad7e73914ee13bd1467ce48a1532570218e327tom	entry0 = (void *) auxv_p->a_un.a_val;
3441ad7e73914ee13bd1467ce48a1532570218e327tom      if (auxv_p->a_type == AT_PLATFORM)
3541ad7e73914ee13bd1467ce48a1532570218e327tom	platform0 = strdup((char *) auxv_p->a_un.a_val);
3641ad7e73914ee13bd1467ce48a1532570218e327tom    }
3741ad7e73914ee13bd1467ce48a1532570218e327tom
3841ad7e73914ee13bd1467ce48a1532570218e327tom  // Now the /proc way as often used in libraries.
3941ad7e73914ee13bd1467ce48a1532570218e327tom  int fd = open("/proc/self/auxv", O_RDONLY);
4041ad7e73914ee13bd1467ce48a1532570218e327tom  if (fd == -1)
4141ad7e73914ee13bd1467ce48a1532570218e327tom    return -1;
4241ad7e73914ee13bd1467ce48a1532570218e327tom
4341ad7e73914ee13bd1467ce48a1532570218e327tom  while (read(fd, &auxv, sizeof(auxv)) == sizeof(auxv))
4441ad7e73914ee13bd1467ce48a1532570218e327tom    {
4541ad7e73914ee13bd1467ce48a1532570218e327tom      if (auxv.a_type == AT_ENTRY)
4641ad7e73914ee13bd1467ce48a1532570218e327tom	entry1 = (void *) auxv.a_un.a_val;
4741ad7e73914ee13bd1467ce48a1532570218e327tom      if (auxv.a_type == AT_PLATFORM)
4841ad7e73914ee13bd1467ce48a1532570218e327tom	platform1 = strdup((char *) auxv.a_un.a_val);
4941ad7e73914ee13bd1467ce48a1532570218e327tom    }
5041ad7e73914ee13bd1467ce48a1532570218e327tom  close(fd);
5141ad7e73914ee13bd1467ce48a1532570218e327tom
5241ad7e73914ee13bd1467ce48a1532570218e327tom  if (entry0 == entry1 && entry0 != NULL)
5341ad7e73914ee13bd1467ce48a1532570218e327tom    fprintf(stderr, "entries OK\n");
5441ad7e73914ee13bd1467ce48a1532570218e327tom
5541ad7e73914ee13bd1467ce48a1532570218e327tom  if (strcmp (platform0, platform1) == 0)
5641ad7e73914ee13bd1467ce48a1532570218e327tom    fprintf(stderr, "platform OK\n");
5741ad7e73914ee13bd1467ce48a1532570218e327tom
5841ad7e73914ee13bd1467ce48a1532570218e327tom  free (platform0);
5941ad7e73914ee13bd1467ce48a1532570218e327tom  free (platform1);
6041ad7e73914ee13bd1467ce48a1532570218e327tom
6141ad7e73914ee13bd1467ce48a1532570218e327tom  return 0;
6241ad7e73914ee13bd1467ce48a1532570218e327tom}
63