[comp.sys.hp] System-configurable parameters

luke@modus.sublink.ORG (Luciano Mannucci) (11/01/90)

Does anyone know how a program (i.e. a C program) can test a system
configurable parameter (such as shmseg or msgmax) in order to complain
propperly whithout a need of generating a system error?

Thanks in advance.

luke.
-- 
  _ _           __             Via Aleardo Aleardi, 12 - 20154 Milano (Italy)
 | | | _  _|   (__             PHONE : +39 2 3315328 FAX: +39 2 3315778
 | | |(_)(_||_|___) Srl        E-MAIL: luke@modus.sublink.ORG
______________________________ Software & Services for Advertising & Marketing

paul@prcrs.UUCP (Paul Hite) (11/09/90)

In article <675@modus.sublink.ORG>, luke@modus.sublink.ORG (Luciano Mannucci) writes:
> Does anyone know how a program (i.e. a C program) can test a system
> configurable parameter (such as shmseg or msgmax) in order to complain
> propperly whithout a need of generating a system error?
> 
> luke.

Well there's a problem with this approach.  You're trying to do 
something like:
	if(verify_that_a_resource_is_available())
		use_resource();
	else
		complain_nicely();


But between the "verify" and the "use", some other process could
consume the resource.  The standard approach is to simply check
the return code from shmget.  errno will tell you what the problem
is and your program can then issue whatever error message you like.
Am I missing something?  What's wrong with this approach?

Anyway, I've seen another request about obtaining system configurable
parameters.  There is no truly portable way to do this.  Basicly, you
just poke around /dev/kmem looking for them.  Sometimes you can find
what you're looking for.  In the case of shmseg, I first tried
"nm /hp-ux | grep shmseg".  No luck.  So then I tried 
"nm /hp-ux | grep shm"  and I found a bunch of stuff.  I ignored the
code and looked at the data.  I knew what my shm parameters were so
I could recognize them when I found them.  They were stored in a neat
little table called shminfo.  A program to do this would be:

	#include<stdio.h>
	#include<nlist.h>
	#include<fcntl.h>
	main()
	{
	int fdkern;
	static struct nlist nl[2] = { { "shminfo" } , { NULL } };
	int shmmax;
	int shmmin;
	int shmmni;
	int shmseg;
	char *address;

		nlist("/hp-ux", nl);
		address = nl[0].n_value;
		printf("address = %d\n", (int) address);

		fdkern = open("/dev/kmem", O_RDONLY);
		printf("fdkern = %d\n", fdkern);
		lseek(fdkern,(long) address,0);
		read(fdkern, (char *) &shmmax, sizeof(shmmax));
		printf("max shared memory segment size = %x\n", (int)shmmax);
		read(fdkern, (char *) &shmmin, sizeof(shmmin));
		printf("min shared memory segment size = %x\n", (int)shmmin);
		read(fdkern, (char *) &shmmni, sizeof(shmmni));
		printf("max shared memory ids = %d\n", (int)shmmni);
		read(fdkern, (char *) &shmseg, sizeof(shmseg));
		printf("max shared memory segments = %d\n", (int)shmseg);

		exit(0);
	}

But the rub is that this program must be able to read /dev/kmem.  Like ps,
it should be sgid'd to sys.  

Paul Hite   PRC Realty Systems  McLean,Va   uunet!prcrs!paul    (703) 556-2243
        You can't tell which way the train went by studying its tracks.

blewis@hpcuhc.cup.hp.com (Bob Lewis) (11/17/90)

First, list the .h files that describe the parameters of interest.

$ cd /usr/include/sys
$ sed -n -e '/info/,/}/p' msg.h sem.h shm.h
**      Message information structure.
*/

struct msginfo {
        int     msgmap, /* # of entries in msg map */
                msgmax, /* max message size */
                msgmnb, /* max # bytes on queue */
                msgmni, /* # of message queue identifiers */
                msgssz, /* msg segment size (should be word size multiple) */
                msgtql; /* # of system message headers */
        ushort  msgseg; /* # of msg segments (MUST BE < 32768) */
};
** semaphore information structure
*/
struct  seminfo {
        int     semmap,         /* # of entries in semaphore map */
                semmni,         /* # of semaphore identifiers */
                semmns,         /* # of semaphores in system */
                semmnu,         /* # of undo structures in system */
                semmsl,         /* max # of semaphores per id */
                semopm,         /* max # of operations per semop call */
                semume,         /* max # of undo entries per process */
                semusz,         /* size in bytes of undo structure */
                semvmx,         /* semaphore maximum value */
                semaem;         /* adjust on exit max value */
};
struct  shminfo {
        int     shmmax, /* max shared memory segment size */
                shmmin, /* min shared memory segment size */
                shmmni, /* # of shared memory identifiers */
                shmseg, /* max attached shared memory segments per process */
                shmbrk, /* gap (in clicks) used between data and shared memory *
                shmall, /* max total shared memory system wide (in clicks) */
                nshm_hash,      /* size of segment address hash table */
                nshm_free_segs; /* number of free segment discriptors */
};


Then, use adb to display those parameters.  Note that one "gotcha" exists.
See the above ushort.  That is displayed using the "/d" to display short.
The other values are just listed nicely in a table and you can pick
out the values you want by referring to the .h file descriptions.

The specific key strokes are:

echo msginfo+18\/d\\nmsginfo,19\/D|adb -k /hp-ux /dev/kmem

The results:
$ adb -k /hp-ux /dev/kmem
sbr 121280 slr 22C2800
msginfo+18:     1024
msginfo:
msginfo:        100             8192            16384           50
                8               40              67108864
seminfo:        10              10              60              30
                500             500             10              96
                32767           16384
shminfo:        67108864        1               100             12
                16              1536            128             200
$q