phys. mem. size

Thanks to all who responded.

The most efficient methods involve having read access to /dev/kmem.

The following program is available from sun-spots archives. Once

compiled it should have group set to kmem and setgid on execution

if universal access is required. (I hope this isn't a security problem!)

*******************************************************************

/*

 * mem.c - print physical memory installed in a machine

 *

 * $Author: kensmith $

 *

 * $Date: 90/03/12 14:18:18 $

 *

 * $Log: mem.c,v $

 * Revision 1.2 90/03/12 14:18:18 kensmith

 * misc. cleanup

 *

 * Revision 1.1 90/02/09 21:43:41 kensmith

 * Initial revision

 *

 */

static char rcsid[] = "$Id: mem.c,v 1.2 90/03/12 14:18:18 kensmith Exp Locker: kensmith $";

#include <stdio.h>

#include <sys/param.h>

#include <sys/types.h>

#include <sys/file.h>

#include <nlist.h>

struct nlist nls[] = {

        { "_physmem" },

#define PHYSMEM 0

        { "" },

};

int fd, physmem;

char *name; /* program name */

char *rindex();

main(argc, argv)

        int argc;

        char *argv[];

{

        if ((name = rindex(argv[0], '/')) == NULL)

                name = argv[0];

        else

                name++;

        if (nlist("/vmunix", nls) < 0) {

                perror("nlist");

                exit(1);

        }

        if ((fd = open("/dev/kmem", O_RDONLY)) <= 0) {

                perror("/dev/kmem");

                exit(1);

        }

        if (nls[PHYSMEM].n_type == 0) {

                fprintf(stderr, "%s: physmem symbol not found\n", name);

                exit(1);

        }

        lseek(fd, nls[PHYSMEM].n_value, L_SET);

        read(fd, &physmem, sizeof(physmem));

        printf("%dK bytes of memory\n", ctob(physmem) / 1024);

}

[3281 byte] By [CodeProf.com] at [2007-12-25 7:23:00]