Annotation of coherent/b/bin/ipcs/getipc.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Module read all information about ipc.
        !             3:  */
        !             4: #include <errno.h>
        !             5: #include <coff.h>
        !             6: #include <fcntl.h>
        !             7: #include <stdio.h>
        !             8: #include <errno.h>
        !             9: #include <sys/shm.h>
        !            10: #include <sys/msg.h>
        !            11: #include "ipcs.h"
        !            12: 
        !            13: extern char    *calloc();
        !            14: 
        !            15: struct msqid_ds        *msqbuf;
        !            16: 
        !            17: /*
        !            18:  * Get ipc and format ipc data
        !            19:  */
        !            20: int get_data(fname, corefile)
        !            21: char   *fname; /* namelist */
        !            22: char   *corefile;
        !            23: {
        !            24:        if (mflag) {
        !            25:                get_shmid_stats();
        !            26:        }
        !            27: 
        !            28:        if (sflag) {
        !            29:                get_sem_stats();
        !            30:        }
        !            31: 
        !            32:        if (qflag) {
        !            33:                get_msg_stats(fname);
        !            34:        }
        !            35: 
        !            36:        return 0;
        !            37: }
        !            38: 
        !            39: 
        !            40: /* 
        !            41:  * get shared memory information.
        !            42:  */
        !            43: 
        !            44: int get_shmid_stats()
        !            45: {
        !            46: 
        !            47: int id = SHMMNI - 1;   /* shared memory identifier number */
        !            48: 
        !            49:        total_shmids = 0;
        !            50:        
        !            51:        /* Allocate space for shared memory data struct */
        !            52:        if (NULL == (shmid = (struct shmid_ds *)
        !            53:            calloc(SHMMNI, sizeof(struct shmid_ds)))) {
        !            54:                perror("ipcs");
        !            55:                exit(1);
        !            56:        }
        !            57:        /* Alocate space to keep track about shm segments */
        !            58:        if (NULL == (valid_shmid = (int *) calloc(SHMMNI, sizeof(int)))) {
        !            59:                perror("ipcs");
        !            60:                exit(1);
        !            61:        }
        !            62: 
        !            63:        /* loop through all shared memory segments available to system.
        !            64:         * increment total_shmid counter when we find one.
        !            65:         */
        !            66:        for (id = SHMMNI - 1; id >= 0 ; id --){
        !            67:                if ( -1 != (shmctl(id, IPC_STAT, &shmid[id]))){
        !            68:                        total_shmids++;
        !            69:                        valid_shmid[id] = 1;
        !            70:                } else {
        !            71:                        valid_shmid[id] = 0;
        !            72:                }                       
        !            73:        }
        !            74: }
        !            75: 
        !            76: /*
        !            77:  * get_sem_stats() contains a loop that performs semctls to IPC_STAT
        !            78:  * allocated semaphores.
        !            79:  *
        !            80:  */
        !            81: get_sem_stats()
        !            82: {
        !            83: 
        !            84: int id;
        !            85:        total_sems = 0;
        !            86: 
        !            87:        for (id = SEMMNI - 1; id >= 0 ; id--) {
        !            88:                if( -1 != (semctl(id, 0 , IPC_STAT, &semid[id]))){
        !            89:                        valid_semid[id] = 1;
        !            90:                        total_sems++;
        !            91:                }else{
        !            92:                        valid_semid[id] = 0;
        !            93:                }
        !            94:        }
        !            95: }
        !            96: 
        !            97: /* 
        !            98:  * get_msg_stats() read msgq data from the corefile to msqbuf
        !            99:  */
        !           100: get_msg_stats(fname)
        !           101: char   *fname;
        !           102: {
        !           103:        SYMENT  sym;            /* The table of names to find */
        !           104:        int     fd;             /* kernel file descriptor */
        !           105:        long    msqoffset;      /* offset to message queue headers */
        !           106: 
        !           107:        /* Allocate space for message queues headers */
        !           108:        if (NULL == (msqbuf = (struct msqbuf *)
        !           109:            calloc((unsigned) NMSQID, (unsigned) sizeof(struct msqid_ds)))) {
        !           110:                perror("ipcs");
        !           111:                exit(1);
        !           112:        }
        !           113:        sym._n._n_n._n_zeroes = 0;      /* stuff for coffnlist */
        !           114:        sym.n_type = -1;
        !           115: 
        !           116:        /* "msqs" is internal kernel variable that points to message
        !           117:         * queues headers.
        !           118:         */
        !           119:        strcpy(sym.n_name, "msqs");
        !           120:        /* do lookups. coffnlist returns 0 on error. */
        !           121:        if (!coffnlist(fname, &sym, NULL, 1)) {
        !           122:                fprintf(stderr, "ipcs: error obtaining values from %s\n", 
        !           123:                                                                        fname);
        !           124:                exit(1);
        !           125:        }
        !           126: 
        !           127:        /* Now we got addresses of the queues headers. So, we can go to corefile
        !           128:         * and read proper values. 
        !           129:         */
        !           130:        /* Get address of message queue data */
        !           131:        if ((fd = iMemSeek(sym.n_value, 0)) < 0) {
        !           132:                perror("ipcs");
        !           133:                exit(1);
        !           134:        }
        !           135:        if (read(fd, &msqoffset, sizeof(int)) != sizeof(int)) {
        !           136:                fprintf(stderr, "ipcs: error getting message queue offset\n");
        !           137:                exit(1);
        !           138:        }
        !           139:        close(fd);
        !           140:        /* Before first request to msgget, msqs points to NULL.
        !           141:         * So we cannot and should not read. 
        !           142:         * After the first request to msgget, all queue headers are alloced.
        !           143:         * At this point we will have to use IPC_ALLOC bit.
        !           144:         */
        !           145:        usemsqs = msqoffset ? 1 : 0;
        !           146:        if (!usemsqs) { /* There was no msq in use */
        !           147:                free(msqbuf);
        !           148:                return;
        !           149:        }
        !           150:        if ((fd = iMemSeek(msqoffset, 0)) < 0) {
        !           151:                perror("ipcs");
        !           152:                exit(1);
        !           153:        }
        !           154:        if (read(fd, msqbuf, sizeof(struct msqid_ds) * NMSQID) !=
        !           155:                                sizeof(struct msqid_ds) * NMSQID) {
        !           156:                fprintf(stderr, "ipcs: error getting message queue status\n");
        !           157:                exit(1);
        !           158:        }
        !           159:        close(fd);
        !           160: }
        !           161: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.