1#include <elf.h>
2#include <link.h>
3#include <stdio.h>
4#include <stddef.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <stdlib.h>
11
12int
13main (int argc, char **argv, char **envp)
14{
15  ElfW(auxv_t) auxv;
16  ElfW(auxv_t) *auxv_p;
17
18  void *entry0 = NULL;
19  void *entry1 = NULL;
20
21  char *platform0 = NULL;
22  char *platform1 = NULL;
23
24  // First try the "traditional" way.
25  while (*envp++ != NULL)
26    ; /* Skip, skip, skip... and after finding a NULL we have the auxv. */
27
28  for (auxv_p = (ElfW(auxv_t) *) envp;
29       auxv_p->a_type != AT_NULL;
30       auxv_p++)
31    {
32      if (auxv_p->a_type == AT_ENTRY)
33	entry0 = (void *) auxv_p->a_un.a_val;
34      if (auxv_p->a_type == AT_PLATFORM)
35	platform0 = strdup((char *) auxv_p->a_un.a_val);
36    }
37
38  // Now the /proc way as often used in libraries.
39  int fd = open("/proc/self/auxv", O_RDONLY);
40  if (fd == -1)
41    return -1;
42
43  while (read(fd, &auxv, sizeof(auxv)) == sizeof(auxv))
44    {
45      if (auxv.a_type == AT_ENTRY)
46	entry1 = (void *) auxv.a_un.a_val;
47      if (auxv.a_type == AT_PLATFORM)
48	platform1 = strdup((char *) auxv.a_un.a_val);
49    }
50  close(fd);
51
52  if (entry0 == entry1 && entry0 != NULL)
53    fprintf(stderr, "entries OK\n");
54
55  if (strcmp (platform0, platform1) == 0)
56    fprintf(stderr, "platform OK\n");
57
58  free (platform0);
59  free (platform1);
60
61  return 0;
62}
63