summaryrefslogtreecommitdiff
path: root/lib/tru-knlist-demo.c
blob: 1fa65aa8db5df8fd1ccc247ddb185c39c9f9158e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Derived from the example in the OSF1 knlist manpage.
   OSF1 spe154.testdrive.compaq.com V5.0 1094 alpha
   aka (w/my private hostname compaq-tru64-50a)  */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <nlist.h>

#ifndef FD_CLOEXEC
# define FD_CLOEXEC 1
#endif

#ifndef LDAV_SYMBOL
# define LDAV_SYMBOL "_avenrun"
#endif

int
main ()
{
  struct nlist nl[2];
  int retval;
  long offset;

  nl[0].n_name = LDAV_SYMBOL;
  nl[1].n_name = "";

  /*******************************************************/
  /* Call the knlist routine                             */
  retval = knlist (nl);

  /******************************************************/
  /* Display addresses if returned.  Otherwise, display */
  /* the appropriate error message.                     */
  if (retval < 0)
    {
      printf ("No kernel symbol addresses returned.\n");
      exit (1);
    }

  if (nl[0].n_type == 0)
    {
      printf ("Unable to return address of symbol %s\n", nl[0].n_name);
      exit (1);
    }

  offset = nl[0].n_value;
  printf ("The address of symbol %s is %lx\n", nl[0].n_name, offset);

  {
    double load_ave[3];
    int channel = open ("/dev/kmem", 0);
    if (channel < 0)
      {
	printf ("open failed\n");
	exit (1);
      }
#ifdef FD_SETFD
    (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
#endif

    if (lseek (channel, offset, 0) == -1L
	|| read (channel, (char *) load_ave, sizeof (load_ave))
	!= sizeof (load_ave))
      {
	close (channel);
      }
  }

  exit (0);
}